Skip to content

Commit 5501a94

Browse files
committed
Code review, disabled some pylint warnings, format
1 parent 0b5436b commit 5501a94

File tree

12 files changed

+132
-77
lines changed

12 files changed

+132
-77
lines changed

javaobj/modifiedutf8.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# ------------------------------------------------------------------------------
3434

3535
if sys.version_info[0] >= 3:
36-
unicode_char = chr
36+
unicode_char = chr # pylint:disable=C0103
3737

3838
def byte_to_int(data):
3939
# type: (bytes) -> int
@@ -42,12 +42,17 @@ def byte_to_int(data):
4242
"""
4343
if isinstance(data, int):
4444
return data
45-
elif isinstance(data, bytes):
45+
46+
if isinstance(data, bytes):
4647
return data[0]
4748

49+
raise ValueError("Didn't get a byte as input")
50+
4851

4952
else:
50-
unicode_char = unichr # pylint:disable=undefined-variable # noqa: F821
53+
unicode_char = (
54+
unichr # pylint:disable=C0103,undefined-variable # noqa: F821
55+
)
5156

5257
def byte_to_int(data):
5358
# type: (bytes) -> int
@@ -56,14 +61,17 @@ def byte_to_int(data):
5661
"""
5762
if isinstance(data, int):
5863
return data
59-
elif isinstance(data, str):
64+
65+
if isinstance(data, str):
6066
return ord(data[0])
6167

68+
raise ValueError("Didn't get a byte as input")
69+
6270

6371
# ------------------------------------------------------------------------------
6472

6573

66-
class DecodeMap(object):
74+
class DecodeMap(object): # pylint:disable=R0205
6775
"""
6876
A utility class which manages masking, comparing and mapping in bits.
6977
If the mask and compare fails, this will raise UnicodeDecodeError so
@@ -167,14 +175,16 @@ def next_byte(_it, start, count):
167175
raise UnicodeDecodeError(
168176
NAME, data, i, i + 1, "embedded zero-byte not allowed"
169177
)
170-
elif d & 0x80: # 1xxxxxxx
178+
179+
if d & 0x80: # 1xxxxxxx
171180
if d & 0x40: # 11xxxxxx
172181
if d & 0x20: # 111xxxxx
173182
if d & 0x10: # 1111xxxx
174183
raise UnicodeDecodeError(
175184
NAME, data, i, i + 1, "invalid encoding character"
176185
)
177-
elif d == 0xED:
186+
187+
if d == 0xED:
178188
value = 0
179189
for i1, dm in enumerate(DECODE_MAP[6]):
180190
d1 = next_byte(it, i, i1 + 1)
@@ -221,7 +231,8 @@ def decode_modified_utf8(data, errors="strict"):
221231
except UnicodeDecodeError as e:
222232
if errors == "strict":
223233
raise e
224-
elif errors == "ignore":
234+
235+
if errors == "ignore":
225236
pass
226237
elif errors == "replace":
227238
value += "\uFFFD"
@@ -230,4 +241,7 @@ def decode_modified_utf8(data, errors="strict"):
230241

231242

232243
def mutf8_unichr(value):
244+
"""
245+
Mimics Python 2 unichr() and Python 3 chr()
246+
"""
233247
return unicode_char(value)

javaobj/utils.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from __future__ import absolute_import
3131

3232
# Standard library
33-
from typing import Any, Tuple # noqa: F401
33+
from typing import Tuple # noqa: F401
3434
import logging
3535
import struct
3636
import sys
@@ -117,7 +117,7 @@ def hexdump(src, start_offset=0, length=16):
117117
:param length: Length of a dump line
118118
:return: A dump string
119119
"""
120-
FILTER = "".join(
120+
hex_filter = "".join(
121121
(len(repr(chr(x))) == 3) and chr(x) or "." for x in range(256)
122122
)
123123
pattern = "{{0:04X}} {{1:<{0}}} {{2}}\n".format(length * 3)
@@ -129,7 +129,7 @@ def hexdump(src, start_offset=0, length=16):
129129
for i in range(0, len(src), length):
130130
s = src[i : i + length]
131131
hexa = " ".join("{0:02X}".format(ord(x)) for x in s)
132-
printable = s.translate(FILTER)
132+
printable = s.translate(hex_filter)
133133
result.append(pattern.format(i + start_offset, hexa, printable))
134134

135135
return "".join(result)
@@ -139,11 +139,14 @@ def hexdump(src, start_offset=0, length=16):
139139

140140

141141
if sys.version_info[0] >= 3:
142-
BYTES_TYPE = bytes
143-
UNICODE_TYPE = str
144-
unicode_char = chr
142+
BYTES_TYPE = bytes # pylint:disable=C0103
143+
UNICODE_TYPE = str # pylint:disable=C0103
144+
unicode_char = chr # pylint:disable=C0103
145145

146146
def bytes_char(c):
147+
"""
148+
Converts the given character to a bytes string
149+
"""
147150
return bytes((c,))
148151

149152
# Python 3 interpreter : bytes & str
@@ -156,7 +159,7 @@ def to_bytes(data, encoding="UTF-8"):
156159
:param encoding: The encoding of data
157160
:return: The corresponding array of bytes
158161
"""
159-
if type(data) is bytes:
162+
if type(data) is bytes: # pylint:disable=C0123
160163
# Nothing to do
161164
return data
162165
return data.encode(encoding)
@@ -170,7 +173,7 @@ def to_str(data, encoding="UTF-8"):
170173
:param encoding: The encoding of data
171174
:return: The corresponding string
172175
"""
173-
if type(data) is str:
176+
if type(data) is str: # pylint:disable=C0123
174177
# Nothing to do
175178
return data
176179
try:
@@ -179,7 +182,7 @@ def to_str(data, encoding="UTF-8"):
179182
return decode_modified_utf8(data)[0]
180183

181184
# Same operation
182-
to_unicode = to_str
185+
to_unicode = to_str # pylint:disable=C0103
183186

184187
def read_to_str(data):
185188
"""
@@ -189,10 +192,14 @@ def read_to_str(data):
189192

190193

191194
else:
192-
BYTES_TYPE = str
193-
UNICODE_TYPE = unicode # pylint:disable=undefined-variable # noqa: F821
194-
unicode_char = unichr # pylint:disable=undefined-variable # noqa: F821
195-
bytes_char = chr
195+
BYTES_TYPE = str # pylint:disable=C0103
196+
UNICODE_TYPE = (
197+
unicode # pylint:disable=C0103,undefined-variable # noqa: F821
198+
)
199+
unicode_char = (
200+
unichr # pylint:disable=C0103,undefined-variable # noqa: F821
201+
)
202+
bytes_char = chr # pylint:disable=C0103
196203

197204
# Python 2 interpreter : str & unicode
198205
def to_str(data, encoding="UTF-8"):
@@ -204,13 +211,13 @@ def to_str(data, encoding="UTF-8"):
204211
:param encoding: The encoding of data
205212
:return: The corresponding string
206213
"""
207-
if type(data) is str:
214+
if type(data) is str: # pylint:disable=C0123
208215
# Nothing to do
209216
return data
210217
return data.encode(encoding)
211218

212219
# Same operation
213-
to_bytes = to_str
220+
to_bytes = to_str # pylint:disable=C0103
214221

215222
# Python 2 interpreter : str & unicode
216223
def to_unicode(data, encoding="UTF-8"):
@@ -222,7 +229,7 @@ def to_unicode(data, encoding="UTF-8"):
222229
:param encoding: The encoding of data
223230
:return: The corresponding string
224231
"""
225-
if type(data) is UNICODE_TYPE:
232+
if type(data) is UNICODE_TYPE: # pylint:disable=C0123
226233
# Nothing to do
227234
return data
228235
try:

javaobj/v1/beans.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
# ------------------------------------------------------------------------------
5454

5555

56-
class JavaClass(object):
56+
class JavaClass(object): # pylint:disable=R0205
5757
"""
5858
Represents a class in the Java world
5959
"""
@@ -63,7 +63,7 @@ def __init__(self):
6363
Sets up members
6464
"""
6565
self.name = None # type: str
66-
self.serialVersionUID = None # type: int
66+
self.serialVersionUID = None # type: int # pylint:disable=C0103
6767
self.flags = None # type: int
6868
self.fields_names = [] # type: List[str]
6969
self.fields_types = [] # type: List[JavaString]
@@ -101,7 +101,7 @@ def __eq__(self, other):
101101
)
102102

103103

104-
class JavaObject(object):
104+
class JavaObject(object): # pylint:disable=R0205
105105
"""
106106
Represents a deserialized non-primitive Java object
107107
"""

javaobj/v1/marshaller.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def dump(self, obj):
120120
self.writeObject(obj)
121121
return self.object_stream.getvalue()
122122

123-
def _writeStreamHeader(self):
123+
def _writeStreamHeader(self): # pylint:disable=C0103
124124
"""
125125
Writes the Java serialization magic header in the serialization stream
126126
"""
@@ -130,7 +130,7 @@ def _writeStreamHeader(self):
130130
(StreamConstants.STREAM_MAGIC, StreamConstants.STREAM_VERSION),
131131
)
132132

133-
def writeObject(self, obj):
133+
def writeObject(self, obj): # pylint:disable=C0103
134134
"""
135135
Appends an object to the serialization stream
136136
@@ -156,7 +156,7 @@ def writeObject(self, obj):
156156
elif obj is None:
157157
# Null
158158
self.write_null()
159-
elif type(obj) is str:
159+
elif type(obj) is str: # pylint:disable=C0123
160160
# String value
161161
self.write_blockdata(obj)
162162
else:
@@ -166,7 +166,7 @@ def writeObject(self, obj):
166166
"supported.".format(type(obj))
167167
)
168168

169-
def _writeStruct(self, unpack, length, args):
169+
def _writeStruct(self, unpack, length, args): # pylint:disable=C0103
170170
"""
171171
Appends data to the serialization stream
172172
@@ -177,7 +177,7 @@ def _writeStruct(self, unpack, length, args):
177177
ba = struct.pack(unpack, *args)
178178
self.object_stream.write(ba)
179179

180-
def _writeString(self, obj, use_reference=True):
180+
def _writeString(self, obj, use_reference=True): # pylint:disable=C0103
181181
"""
182182
Appends a string to the serialization stream
183183
@@ -270,7 +270,7 @@ def write_enum(self, obj):
270270

271271
self.write_string(obj.constant)
272272

273-
def write_blockdata(self, obj, parent=None):
273+
def write_blockdata(self, obj, parent=None): # pylint:disable=W0613
274274
"""
275275
Appends a block of data to the serialization stream
276276
@@ -374,7 +374,7 @@ def write_object(self, obj, parent=None):
374374
self.writeObject(annotation)
375375
self._writeStruct(">B", 1, (TerminalCode.TC_ENDBLOCKDATA,))
376376

377-
def write_class(self, obj, parent=None):
377+
def write_class(self, obj, parent=None): # pylint:disable=W0613
378378
"""
379379
Writes a class to the stream
380380
@@ -384,7 +384,7 @@ def write_class(self, obj, parent=None):
384384
self._writeStruct(">B", 1, (TerminalCode.TC_CLASS,))
385385
self.write_classdesc(obj)
386386

387-
def write_classdesc(self, obj, parent=None):
387+
def write_classdesc(self, obj, parent=None): # pylint:disable=W0613
388388
"""
389389
Writes a class description
390390
@@ -553,9 +553,11 @@ def _convert_type_to_char(type_char):
553553
"""
554554
if isinstance(type_char, TypeCode):
555555
return type_char.value
556-
elif isinstance(type_char, int):
556+
557+
if isinstance(type_char, int):
557558
return type_char
558-
elif isinstance(type_char, (BYTES_TYPE, UNICODE_TYPE)):
559+
560+
if isinstance(type_char, (BYTES_TYPE, UNICODE_TYPE)):
559561
# Conversion to TypeCode will raise an error if the type
560562
# is invalid
561563
return TypeCode(ord(type_char[0])).value

javaobj/v1/transformers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
__all__ = ("DefaultObjectTransformer",)
4646

4747

48-
class DefaultObjectTransformer(object):
48+
class DefaultObjectTransformer(object): # pylint:disable=R0205
4949
"""
5050
Default transformer for the deserialized objects.
5151
Converts JavaObject objects to Python types (maps, lists, ...)

javaobj/v1/unmarshaller.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def do_classdesc(self, parent=None, ident=0):
329329
expect=(TerminalCode.TC_STRING, TerminalCode.TC_REFERENCE),
330330
)
331331

332-
if type(field_type) is not JavaString:
332+
if type(field_type) is not JavaString: # pylint:disable=C0123
333333
raise AssertionError(
334334
"Field type must be a JavaString, "
335335
"not {0}".format(type(field_type))
@@ -345,7 +345,7 @@ def do_classdesc(self, parent=None, ident=0):
345345
# FIXME: ugly trick
346346
field_type = JavaString(field_type.name)
347347

348-
if type(field_type) is not JavaString:
348+
if type(field_type) is not JavaString: # pylint:disable=C0123
349349
raise AssertionError(
350350
"Field type must be a JavaString, "
351351
"not {0}".format(type(field_type))
@@ -367,8 +367,8 @@ def do_classdesc(self, parent=None, ident=0):
367367
clazz.fields_types.append(field_type)
368368

369369
if parent:
370-
parent.__fields = clazz.fields_names
371-
parent.__types = clazz.fields_types
370+
parent.__fields = clazz.fields_names # pylint:disable=W0212
371+
parent.__types = clazz.fields_types # pylint:disable=W0212
372372

373373
# classAnnotation
374374
(opid,) = self._readStruct(">B")
@@ -714,7 +714,10 @@ def do_enum(self, parent=None, ident=0):
714714
)
715715
enum.classdesc = classdesc
716716
self._add_reference(enum, ident)
717-
_, enumConstantName = self._read_and_exec_opcode(
717+
(
718+
_,
719+
enumConstantName,
720+
) = self._read_and_exec_opcode( # pylint:disable=C0103
718721
ident=ident + 1,
719722
expect=(TerminalCode.TC_STRING, TerminalCode.TC_REFERENCE),
720723
)

0 commit comments

Comments
 (0)