Skip to content

Commit 8a7c6df

Browse files
committed
Add tests exercising the string-decode scan paths
Cover long runs that cross the scan windows with a terminator, backslash escape and \uXXXX escape at every offset in 1-byte and wider strings, plus strict and non-strict control-character handling at the window boundaries.
1 parent c17ac21 commit 8a7c6df

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Lib/test/test_json/test_decode.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ def test_limit_int(self):
155155
with self.assertRaises(ValueError):
156156
self.loads('1' * (maxdigits + 1))
157157

158+
def test_long_string_scan_paths(self):
159+
# Exercise the string scan over long runs that cross the 8-byte scan
160+
# windows: a terminator, a backslash escape and a \uXXXX escape at every
161+
# offset, in 1-byte and wider (BMP, astral) strings.
162+
loads = self.loads
163+
for n in range(40):
164+
run = "a" * n
165+
self.assertEqual(loads('"' + run + '"'), run)
166+
self.assertEqual(loads('"' + run + '\\nz"'), run + "\nz")
167+
self.assertEqual(loads('"' + run + '\\u00e9z"'), run + "\xe9z")
168+
self.assertEqual(loads('"' + "中" * n + '\\n"'), "中" * n + "\n")
169+
self.assertEqual(loads('"' + "\U0001f600" * n + '"'), "\U0001f600" * n)
170+
# Strict control-character detection at the window boundaries, and the
171+
# non-strict path that keeps them.
172+
for n in (7, 8, 15, 16, 17, 23, 24):
173+
self.assertRaises(self.JSONDecodeError, loads, '"' + "a" * n + '\x01"')
174+
self.assertEqual(loads('"' + "a" * n + '\x01"', strict=False),
175+
"a" * n + "\x01")
176+
158177

159178
class TestPyDecode(TestDecode, PyTest): pass
160179
class TestCDecode(TestDecode, CTest): pass

0 commit comments

Comments
 (0)