Tidy up Bytes and String to require all the appropriate parameters.

This commit is contained in:
Mike Auty
2016-02-14 02:49:28 +00:00
parent 86526fa9ad
commit 0ef389dec9
2 changed files with 31 additions and 12 deletions
+29 -10
View File
@@ -92,16 +92,16 @@ class Bytes(PrimitiveObject, bytes):
struct_format = str(length) + "s")
self._vol['length'] = length
def __new__(cls, context, structure_name, object_info, **kwargs):
def __new__(cls, context, structure_name, object_info, length = 1, **kwargs):
"""Creates the appropriate class and returns it so that the native type is inherritted
The only reason the **kwargs is added, is so that the inherriting types can override __init__
without needing to override __new__"""
return bytes.__new__(cls,
cls._struct_value(context,
struct_format = str(kwargs["length"]) + "s",
layer_name = object_info.layer_name,
offset = object_info.offset))
return cls._struct_type.__new__(cls,
cls._struct_value(context,
struct_format = str(length) + "s",
layer_name = object_info.layer_name,
offset = object_info.offset))
# TODO: Fix up strings unpacking to include an encoding
@@ -112,14 +112,33 @@ class String(PrimitiveObject, str):
"""
_struct_type = str
def __init__(self, context, structure_name, object_info, struct_format, length = 1, encoding = 'ascii'):
self._struct_format = str(length) + 's'
self._vol['length'] = length
def __init__(self, context, structure_name, object_info, max_length = 1, encoding = "utf-8", errors = None):
PrimitiveObject.__init__(self,
context = context,
structure_name = structure_name,
object_info = object_info,
struct_format = struct_format)
struct_format = str(max_length) + 's')
self._vol["max_length"] = max_length
self._vol['encoding'] = encoding
self._vol['errors'] = errors
def __new__(cls, context, structure_name, object_info, max_length = 1, encoding = "utf-8", errors = None, **kwargs):
"""Creates the appropriate class and returns it so that the native type is inherited
The only reason the **kwargs is added, is so that the inherriting types can override __init__
without needing to override __new__"""
params = {}
if encoding:
params['encoding'] = encoding
if errors:
params['errors'] = errors
value = cls._struct_type.__new__(cls,
cls._struct_value(context,
struct_format = str(max_length) + "s",
layer_name = object_info.layer_name,
offset = object_info.offset),
**params)
return value
class Pointer(Integer):
+2 -2
View File
@@ -36,7 +36,7 @@ class NativeTable(interfaces.symbols.NativeTableInterface):
symbol_space is used to resolve any target symbols if they don't exist in this list
"""
# TODO: Add strings and bytes to this set
# NOTE: These need updating whenever the object init signatures change
additional = {}
obj = None
if structure_name == 'void':
@@ -52,7 +52,7 @@ class NativeTable(interfaces.symbols.NativeTableInterface):
additional = {"start_bit": 0, "end_bit": 0}
elif structure_name == 'String':
obj = objects.String
additional = {"length": 0}
additional = {"max_length": 0}
elif structure_name == 'Bytes':
obj = objects.Bytes
additional = {"length": 0}