Use buffer() in Python2 and use memoryview in Python3 #2242
|
@ -30,6 +30,7 @@ from bmconfigparser import config
|
||||||
from helper_sql import sqlExecute, sqlQuery
|
from helper_sql import sqlExecute, sqlQuery
|
||||||
from network import knownnodes, StoppableThread
|
from network import knownnodes, StoppableThread
|
||||||
from six.moves import configparser, queue
|
from six.moves import configparser, queue
|
||||||
|
import six
|
||||||
|
|
||||||
|
|
||||||
def sizeof_fmt(num, suffix='h/s'):
|
def sizeof_fmt(num, suffix='h/s'):
|
||||||
|
@ -515,9 +516,15 @@ class singleWorker(StoppableThread):
|
||||||
payload, TTL, log_prefix='(For onionpeer object)')
|
payload, TTL, log_prefix='(For onionpeer object)')
|
||||||
|
|
||||||
inventoryHash = highlevelcrypto.calculateInventoryHash(payload)
|
inventoryHash = highlevelcrypto.calculateInventoryHash(payload)
|
||||||
|
if six.PY2:
|
||||||
|
payload_buffer = buffer(payload)
|
||||||
|
tag_buffer = buffer(tag)
|
||||||
|
else: # assume six.PY3
|
||||||
|
payload_buffer = memoryview(payload)
|
||||||
|
tag_buffer = memoryview(tag)
|
||||||
state.Inventory[inventoryHash] = (
|
state.Inventory[inventoryHash] = (
|
||||||
objectType, streamNumber, buffer(payload), # noqa: F821
|
objectType, streamNumber, payload_buffer, # noqa: F821
|
||||||
embeddedTime, buffer(tag) # noqa: F821
|
embeddedTime, tag_buffer # noqa: F821
|
||||||
)
|
)
|
||||||
self.logger.info(
|
self.logger.info(
|
||||||
'sending inv (within sendOnionPeerObj function) for object: %s',
|
'sending inv (within sendOnionPeerObj function) for object: %s',
|
||||||
|
|
|
@ -9,6 +9,7 @@ import re
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import time
|
import time
|
||||||
|
import six
|
||||||
|
|
||||||
# magic imports!
|
# magic imports!
|
||||||
import addresses
|
import addresses
|
||||||
|
@ -409,8 +410,12 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.object.checkObjectByType()
|
self.object.checkObjectByType()
|
||||||
|
if six.PY2:
|
||||||
|
data_buffer = buffer(self.object.data)
|
||||||
|
else: # assume six.PY3
|
||||||
|
data_buffer = memoryview(self.object.data)
|
||||||
objectProcessorQueue.put((
|
objectProcessorQueue.put((
|
||||||
self.object.objectType, buffer(self.object.data))) # noqa: F821
|
self.object.objectType, data_buffer)) # noqa: F821
|
||||||
except BMObjectInvalidError:
|
except BMObjectInvalidError:
|
||||||
BMProto.stopDownloadingObject(self.object.inventoryHash, True)
|
BMProto.stopDownloadingObject(self.object.inventoryHash, True)
|
||||||
else:
|
else:
|
||||||
|
@ -424,10 +429,16 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
|
||||||
state.Dandelion.removeHash(
|
state.Dandelion.removeHash(
|
||||||
self.object.inventoryHash, "cycle detection")
|
self.object.inventoryHash, "cycle detection")
|
||||||
|
|
||||||
|
if six.PY2:
|
||||||
|
object_buffer = buffer(self.payload[objectOffset:])
|
||||||
|
tag_buffer = buffer(self.object.tag)
|
||||||
|
else: # assume six.PY3
|
||||||
|
object_buffer = memoryview(self.payload[objectOffset:])
|
||||||
|
tag_buffer = memoryview(self.object.tag)
|
||||||
state.Inventory[self.object.inventoryHash] = (
|
state.Inventory[self.object.inventoryHash] = (
|
||||||
self.object.objectType, self.object.streamNumber,
|
self.object.objectType, self.object.streamNumber,
|
||||||
buffer(self.payload[objectOffset:]), self.object.expiresTime, # noqa: F821
|
object_buffer, self.object.expiresTime, # noqa: F821
|
||||||
buffer(self.object.tag) # noqa: F821
|
tag_buffer # noqa: F821
|
||||||
)
|
)
|
||||||
self.handleReceivedObject(
|
self.handleReceivedObject(
|
||||||
self.object.streamNumber, self.object.inventoryHash)
|
self.object.streamNumber, self.object.inventoryHash)
|
||||||
|
|
Reference in New Issue
Block a user