Skip to content

Commit fdf786a

Browse files
committed
Better Py2 compatibility in v2 parser
1 parent a5e9be2 commit fdf786a

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

javaobj/v2/beans.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
limitations under the License.
2525
"""
2626

27+
from __future__ import absolute_import
28+
2729
from enum import Enum, IntEnum
2830
from typing import Any, Dict, List, Optional, Set
2931
import logging
3032

3133
from .stream import DataStreamReader
3234
from ..constants import ClassDescFlags, TypeCode
33-
from ..modifiedutf8 import decode_modified_utf8
35+
from ..modifiedutf8 import decode_modified_utf8, byte_to_int
3436
from ..utils import UNICODE_TYPE
3537

3638

@@ -75,7 +77,7 @@ class FieldType(IntEnum):
7577
OBJECT = TypeCode.TYPE_OBJECT.value
7678

7779

78-
class ParsedJavaContent:
80+
class ParsedJavaContent(object):
7981
"""
8082
Generic representation of data parsed from the stream
8183
"""
@@ -112,7 +114,7 @@ class ExceptionState(ParsedJavaContent):
112114

113115
def __init__(self, exception_object, data):
114116
# type: (ParsedJavaContent, bytes) -> None
115-
super().__init__(ContentType.EXCEPTIONSTATE)
117+
super(ExceptionState, self).__init__(ContentType.EXCEPTIONSTATE)
116118
self.exception_object = exception_object
117119
self.stream_data = data
118120
self.handle = exception_object.handle
@@ -142,7 +144,7 @@ class JavaString(ParsedJavaContent):
142144

143145
def __init__(self, handle, data):
144146
# type: (int, bytes) -> None
145-
super().__init__(ContentType.STRING)
147+
super(JavaString, self).__init__(ContentType.STRING)
146148
self.handle = handle
147149
value, length = decode_modified_utf8(data)
148150
self.value = value # type: str
@@ -207,7 +209,7 @@ class JavaClassDesc(ParsedJavaContent):
207209

208210
def __init__(self, class_desc_type):
209211
# type: (ClassDescType) -> None
210-
super().__init__(ContentType.CLASSDESC)
212+
super(JavaClassDesc, self).__init__(ContentType.CLASSDESC)
211213

212214
# Type of class description
213215
self.class_type = class_desc_type # type: ClassDescType
@@ -350,10 +352,12 @@ class JavaInstance(ParsedJavaContent):
350352
"""
351353

352354
def __init__(self):
353-
super().__init__(ContentType.INSTANCE)
355+
super(JavaInstance, self).__init__(ContentType.INSTANCE)
354356
self.classdesc = None # type: JavaClassDesc
355357
self.field_data = {} # type: Dict[JavaClassDesc, Dict[JavaField, Any]]
356-
self.annotations = {} # type: Dict[JavaClassDesc, List[ParsedJavaContent]]
358+
self.annotations = (
359+
{}
360+
) # type: Dict[JavaClassDesc, List[ParsedJavaContent]]
357361

358362
def __str__(self):
359363
return "[instance 0x{0:x}: type {1}]".format(
@@ -445,7 +449,7 @@ class JavaClass(ParsedJavaContent):
445449

446450
def __init__(self, handle, class_desc):
447451
# type: (int, JavaClassDesc) -> None
448-
super().__init__(ContentType.CLASS)
452+
super(JavaClass, self).__init__(ContentType.CLASS)
449453
self.handle = handle
450454
self.classdesc = class_desc
451455

@@ -469,7 +473,7 @@ class JavaEnum(ParsedJavaContent):
469473

470474
def __init__(self, handle, class_desc, value):
471475
# type: (int, JavaClassDesc, JavaString) -> None
472-
super().__init__(ContentType.ENUM)
476+
super(JavaEnum, self).__init__(ContentType.ENUM)
473477
self.handle = handle
474478
self.classdesc = class_desc
475479
self.value = value
@@ -542,7 +546,7 @@ class BlockData(ParsedJavaContent):
542546

543547
def __init__(self, data):
544548
# type: (bytes) -> None
545-
super().__init__(ContentType.BLOCKDATA)
549+
super(BlockData, self).__init__(ContentType.BLOCKDATA)
546550
self.data = data
547551

548552
def __str__(self):
@@ -555,11 +559,11 @@ def __repr__(self):
555559

556560
def __eq__(self, other):
557561
if isinstance(other, (str, UNICODE_TYPE)):
558-
other_data = other.encode("latin1")
562+
other_data = tuple(byte_to_int(x) for x in other)
559563
elif isinstance(other, bytes):
560-
other_data = other
564+
other_data = tuple(byte_to_int(x) for x in other)
561565
else:
562566
# Can't compare
563567
return False
564568

565-
return other_data == self.data
569+
return other_data == tuple(byte_to_int(x) for x in self.data)

javaobj/v2/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
limitations under the License.
2626
"""
2727

28+
from __future__ import absolute_import
29+
2830
from enum import Enum
2931
from typing import Any, Callable, Dict, IO, List, Optional
3032
import logging

javaobj/v2/stream.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import struct
2929

3030
from ..modifiedutf8 import decode_modified_utf8
31+
from ..utils import unicode_char
3132

3233

3334
class DataStreamReader:
@@ -85,7 +86,7 @@ def read_char(self):
8586
"""
8687
Shortcut to read a single `char` (2 bytes)
8788
"""
88-
return chr(self.read(">H")[0])
89+
return unicode_char(self.read(">H")[0])
8990

9091
def read_short(self):
9192
# type: () -> int

0 commit comments

Comments
 (0)