Skip to content

Commit

Permalink
better support on memory view
Browse files Browse the repository at this point in the history
  • Loading branch information
hubo1016 committed Jul 18, 2018
1 parent b1cd153 commit e8eb63b
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions namedstruct/namedstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _subclass(self, parser):
:param parser: parser of subclass
'''
_set(self, '_sub', parser._create(getattr(self, '_extra', b''), self._target))
_set(self, '_sub', parser._create(memoryview(getattr(self, '_extra', b'')), self._target))
try:
object.__delattr__(self, '_extra')
except:
Expand Down Expand Up @@ -274,12 +274,12 @@ def __copy__(self):
'''
Create a copy of the struct. It is always a deepcopy, in fact it packs the struct and unpack it again.
'''
return self._parser.create(self._tobytes(), None)
return self._parser.create(memoryview(self._tobytes()), None)
def __deepcopy__(self, memo):
'''
Create a copy of the struct.
'''
return self._parser.create(self._tobytes(), None)
return self._parser.create(memoryview(self._tobytes()), None)
def __repr__(self, *args, **kwargs):
'''
Return the representation of the struct.
Expand Down Expand Up @@ -1308,9 +1308,9 @@ def create(self, data, inlineparent = None):
Compatible to Parser.create()
'''
if self.cstr:
return data.rstrip(b'\x00')
return _copy(data).rstrip(b'\x00')
else:
return data
return _copy(data)
def sizeof(self, prim):
'''
Compatible to Parser.sizeof()
Expand Down Expand Up @@ -1340,18 +1340,18 @@ def __init__(self):
pass
def parse(self, buffer, inlineparent = None):
for i in range(0, len(buffer)):
if buffer[i:i+1] == b'\x00':
return (buffer[0:i], i + 1)
if buffer[i] in (0, b'\x00'):
return (_copy(buffer[:i]), i + 1)
return None
def new(self, inlineparent = None):
return b''
def create(self, data, inlineparent = None):
if data[-1:] != b'\x00':
raise BadFormatError(b'Cstr is not zero-terminated')
if data[-1] not in (0, b'\x00'):
raise BadFormatError('Cstr is not zero-terminated')
for i in range(0, len(data) - 1):
if data[i:i+1] == b'\x00':
raise BadFormatError(b'Cstr has zero inside the string')
return data
if data[i] in (0, b'\x00'):
raise BadFormatError('Cstr has zero inside the string')
return _copy(data)
def sizeof(self, prim):
return len(prim) + 1
def paddingsize(self, prim):
Expand Down

0 comments on commit e8eb63b

Please sign in to comment.