diff --git a/minijson.pyx b/minijson.pyx index 6dc239e46432ded169057fb1bc066f34b83f6887..0ad344f6427463129598b5714faa9e5d331a044e 100644 --- a/minijson.pyx +++ b/minijson.pyx @@ -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 @@ -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') @@ -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) diff --git a/tests/test_minijson.py b/tests/test_minijson.py index 0a312c3466ef98719b7f09005f7066911061203a..4fb97321aedd0516ccb85d688c3250e39d9e8315 100644 --- a/tests/test_minijson.py +++ b/tests/test_minijson.py @@ -50,13 +50,6 @@ class TestMiniJSON(unittest.TestCase): 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)