Skip to content

Commit a56433a

Browse files
authored
Merge pull request #65 from tcalmant/bug-fix
Bug fix
2 parents ccb2fae + c899f99 commit a56433a

9 files changed

Lines changed: 35 additions & 38 deletions

File tree

javaobj/v1/beans.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,14 @@ def __hash__(self):
173173
return UNICODE_TYPE.__hash__(self)
174174

175175
def __eq__(self, other):
176-
if not isinstance(other, UNICODE_TYPE):
176+
# Accept both UNICODE_TYPE and plain str.
177+
# In Python 2, UNICODE_TYPE is unicode while str literals are bytes;
178+
# including str here lets assertEqual(java_string, "literal") work
179+
# in Python 2 as well as Python 3 (where str == UNICODE_TYPE).
180+
if not isinstance(other, (UNICODE_TYPE, str)):
177181
return False
178-
return UNICODE_TYPE.__eq__(self, other)
182+
result = UNICODE_TYPE.__eq__(self, other)
183+
return False if result is NotImplemented else result
179184

180185

181186
class JavaEnum(JavaObject):
@@ -199,7 +204,7 @@ def __init__(self, classdesc=None):
199204
self.classdesc = classdesc
200205

201206
def __hash__(self):
202-
return list.__hash__(self)
207+
return object.__hash__(self)
203208

204209

205210
class JavaByteArray(JavaObject):

javaobj/v1/transformers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def __extra_loading__(self, unmarshaller, ident=0):
136136
"""
137137
# Ignore the blockdata opid
138138
(opid,) = unmarshaller._readStruct(">B")
139-
if opid != ClassDescFlags.SC_BLOCK_DATA:
139+
if opid != TerminalCode.TC_BLOCKDATA:
140140
raise ValueError("Start of block data not found")
141141

142142
# Read HashMap fields

javaobj/v1/unmarshaller.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
# Convertion of a Java type char to its NumPy equivalent
8484
NUMPY_TYPE_MAP = {
8585
TypeCode.TYPE_BYTE: "B",
86-
TypeCode.TYPE_CHAR: "b",
86+
TypeCode.TYPE_CHAR: ">u2",
8787
TypeCode.TYPE_DOUBLE: ">d",
8888
TypeCode.TYPE_FLOAT: ">f",
8989
TypeCode.TYPE_INTEGER: ">i",
@@ -508,7 +508,8 @@ def do_object(self, parent=None, ident=0):
508508
# classdata[]
509509

510510
if (
511-
classdesc.flags & ClassDescFlags.SC_EXTERNALIZABLE
511+
classdesc is not None
512+
and classdesc.flags & ClassDescFlags.SC_EXTERNALIZABLE
512513
and not classdesc.flags & ClassDescFlags.SC_BLOCK_DATA
513514
):
514515
# TODO:
@@ -831,8 +832,9 @@ def _oops_dump_state(self, ignore_remaining_data=False):
831832
"(2nd line is an actual position!):"
832833
)
833834

834-
# Do not use a keyword argument
835-
self.object_stream.seek(-16, os.SEEK_CUR)
835+
# Do not use a keyword argument; clamp to avoid seeking before start
836+
current_pos = self.object_stream.tell()
837+
self.object_stream.seek(max(0, current_pos - 16))
836838
position = self.object_stream.tell()
837839
the_rest = self.object_stream.read()
838840

javaobj/v2/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def run(self):
146146
# Read content
147147
contents = [] # type: List[ParsedJavaContent]
148148
while True:
149-
self._log.info("Reading next content")
149+
self._log.debug("Reading next content")
150150
start = self.__fd.tell()
151151
try:
152152
type_code = self.__reader.read_byte()

javaobj/v2/transformers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ class NumpyArrayTransformer(ObjectTransformer):
506506
# Convertion of a Java type char to its NumPy equivalent
507507
NUMPY_TYPE_MAP = {
508508
TypeCode.TYPE_BYTE: "B",
509-
TypeCode.TYPE_CHAR: "b",
509+
TypeCode.TYPE_CHAR: ">u2",
510510
TypeCode.TYPE_DOUBLE: ">d",
511511
TypeCode.TYPE_FLOAT: ">f",
512512
TypeCode.TYPE_INTEGER: ">i",

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ classifiers = [
3838
]
3939

4040
dependencies = [
41-
"enum34; python_version<='3.4'",
42-
"typing; python_version<='3.4'"
41+
"enum34; python_version<'3.4'",
42+
"typing; python_version<'3.5'"
4343
]
4444

4545
[project.optional-dependencies]

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ def read(fname):
7272
packages=["javaobj", "javaobj.v1", "javaobj.v2"],
7373
test_suite="tests",
7474
install_requires=[
75-
'enum34;python_version<="3.4"',
76-
'typing;python_version<="3.4"',
75+
"enum34; python_version<'3.4'",
76+
"typing; python_version<'3.5'"
7777
],
7878
long_description=read("README.md"),
7979
long_description_content_type="text/markdown",
@@ -91,6 +91,8 @@ def read(fname):
9191
"Programming Language :: Python :: 3.10",
9292
"Programming Language :: Python :: 3.11",
9393
"Programming Language :: Python :: 3.12",
94+
"Programming Language :: Python :: 3.13",
95+
"Programming Language :: Python :: 3.14",
9496
"Topic :: Software Development :: Libraries :: Python Modules",
9597
],
9698
)

tests/test_v1.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -337,18 +337,12 @@ def test_char_array(self):
337337
jobj = self.read_file("testCharArray.ser")
338338
pobj = javaobj.loads(jobj)
339339
_logger.debug(pobj)
340-
self.assertEqual(
341-
pobj,
342-
[
343-
"\u0000",
344-
"\ud800",
345-
"\u0001",
346-
"\udc00",
347-
"\u0002",
348-
"\uffff",
349-
"\u0003",
350-
],
351-
)
340+
# Compare by code points to avoid Python 2/3 string literal ambiguity
341+
# (ruff strips u"" prefixes; \u-escapes in plain str are literal in Py2)
342+
expected_codepoints = [0x0000, 0xD800, 0x0001, 0xDC00, 0x0002, 0xFFFF, 0x0003]
343+
self.assertEqual(len(pobj), len(expected_codepoints))
344+
for actual, cp in zip(pobj, expected_codepoints):
345+
self.assertEqual(ord(actual), cp)
352346
self._try_marshalling(jobj, pobj)
353347

354348
def test_2d_array(self):

tests/test_v2.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -417,18 +417,12 @@ def test_char_array(self):
417417
jobj = self.read_file("testCharArray.ser")
418418
pobj = javaobj.loads(jobj)
419419
_logger.debug(pobj)
420-
self.assertEqual(
421-
pobj,
422-
[
423-
"\u0000",
424-
"\ud800",
425-
"\u0001",
426-
"\udc00",
427-
"\u0002",
428-
"\uffff",
429-
"\u0003",
430-
],
431-
)
420+
# Compare by code points to avoid Python 2/3 string literal ambiguity
421+
# (ruff strips u"" prefixes; \u-escapes in plain str are literal in Py2)
422+
expected_codepoints = [0x0000, 0xD800, 0x0001, 0xDC00, 0x0002, 0xFFFF, 0x0003]
423+
self.assertEqual(len(pobj), len(expected_codepoints))
424+
for actual, cp in zip(pobj, expected_codepoints):
425+
self.assertEqual(ord(actual), cp)
432426

433427
def test_2d_array(self):
434428
"""

0 commit comments

Comments
 (0)