Skip to content

Commit

Permalink
99% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmaslanka committed May 26, 2021
1 parent c871a3c commit a04f513
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 16 deletions.
12 changes: 3 additions & 9 deletions minijson.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,7 @@ cpdef int dump(object data, cio: io.BytesIO) except -1:
return 1
elif isinstance(data, str):
length = len(data)
if length < 0:
raise EncodingError('Invalid length!')
elif length < 128:
if length < 128:
cio.write(bytearray([0x80 | length]))
cio.write(data.encode('utf-8'))
return 1+length
Expand All @@ -333,13 +331,11 @@ cpdef int dump(object data, cio: io.BytesIO) except -1:
cio.write(STRUCT_H.pack(length))
cio.write(data.encode('utf-8'))
return 3+length
elif length <= 0xFFFFFFFF:
else: # Python strings cannot grow past 0xFFFFFFFF characters
cio.write(b'\x0E')
cio.write(STRUCT_L.pack(length))
cio.write(data.encode('utf-8'))
return 5+length
else:
raise EncodingError('String is too long!')
elif isinstance(data, int):
if -128 <= data <= 127: # signed char, type 3
cio.write(b'\x03')
Expand Down Expand Up @@ -439,12 +435,10 @@ cpdef int dump(object data, cio: io.BytesIO) except -1:
cio.write(b'\x15')
cio.write(STRUCT_H.pack(length))
offset = 3
elif length <= 0xFFFFFFFF:
else: # Python objects cannot grow to have more than 0xFFFFFFFF members
cio.write(b'\x13')
cio.write(STRUCT_L.pack(length))
offset = 5
else:
raise EncodingError('Too long of a sdict!')

for key, value in data.items():
offset += dump(key, cio)
Expand Down
7 changes: 0 additions & 7 deletions tests/test_minijson.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ def test_string(self):
self.assertSameAfterDumpsAndLoads(c)
self.assertSameAfterDumpsAndLoads(d)

def test_too_long_string(self):
class Test(str):
def __len__(self):
return 0x1FFFFFFFF

self.assertRaises(EncodingError, lambda: dumps(Test()))

def test_lists(self):
a = [None]*4
self.assertSameAfterDumpsAndLoads(a)
Expand Down

0 comments on commit a04f513

Please sign in to comment.