Skip to content

Commit b187df5

Browse files
committed
Fixed the TypeCode conversion issue
Input field type can be unicode in Python 2
1 parent 7186d52 commit b187df5

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

javaobj/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def hexdump(src, start_offset=0, length=16):
138138

139139

140140
if sys.version_info[0] >= 3:
141+
BYTES_TYPE = bytes
141142
UNICODE_TYPE = str
142143
unicode_char = chr
143144

@@ -187,6 +188,7 @@ def read_to_str(data):
187188

188189

189190
else:
191+
BYTES_TYPE = str
190192
UNICODE_TYPE = unicode # pylint:disable=undefined-variable
191193
unicode_char = unichr # pylint:disable=undefined-variable
192194
bytes_char = chr

javaobj/v1/core.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
to_bytes,
7575
to_str,
7676
to_unicode,
77+
BYTES_TYPE,
7778
UNICODE_TYPE,
7879
unicode_char,
7980
hexdump,
@@ -817,11 +818,11 @@ def _read_value(self, raw_field_type, ident, name=""):
817818
:return: The read value
818819
:raise RuntimeError: Unknown field type
819820
"""
820-
if isinstance(raw_field_type, (bytes, str)):
821+
if isinstance(raw_field_type, (TypeCode, int)):
822+
field_type = raw_field_type
823+
else:
821824
# We don't need details for arrays and objects
822825
field_type = TypeCode(ord(raw_field_type[0]))
823-
else:
824-
field_type = raw_field_type
825826

826827
if field_type == TypeCode.TYPE_BOOLEAN:
827828
(val,) = self._readStruct(">B")
@@ -1343,11 +1344,11 @@ def _write_value(self, raw_field_type, value):
13431344
:param raw_field_type: Value type
13441345
:param value: The value itself
13451346
"""
1346-
if isinstance(raw_field_type, (bytes, str)):
1347+
if isinstance(raw_field_type, (TypeCode, int)):
1348+
field_type = raw_field_type
1349+
else:
13471350
# We don't need details for arrays and objects
13481351
field_type = TypeCode(ord(raw_field_type[0]))
1349-
else:
1350-
field_type = raw_field_type
13511352

13521353
if field_type == TypeCode.TYPE_BOOLEAN:
13531354
self._writeStruct(">B", 1, (1 if value else 0,))
@@ -1376,7 +1377,7 @@ def _write_value(self, raw_field_type, value):
13761377
self.write_object(value)
13771378
elif isinstance(value, JavaString):
13781379
self.write_string(value)
1379-
elif isinstance(value, (bytes, str)):
1380+
elif isinstance(value, (BYTES_TYPE, UNICODE_TYPE)):
13801381
self.write_blockdata(value)
13811382
else:
13821383
raise RuntimeError("Unknown typecode: {0}".format(field_type))

0 commit comments

Comments
 (0)