Skip to content

Commit 765ffb2

Browse files
test_json: add regression test for re-entrant JSONDecodeError crash
1 parent bbc357d commit 765ffb2

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

Lib/test/test_json/test_fail.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from test.test_json import PyTest, CTest
2-
from test import support
32
import json
43

54
# 2007-10-05
@@ -250,15 +249,20 @@ def __call__(self, *args, **kwargs):
250249
return ValueError("boom")
251250

252251
hook = Trigger()
253-
with (
254-
support.swap_attr(json, "JSONDecodeError", hook),
255-
support.swap_attr(json.decoder, "JSONDecodeError", hook)
256-
):
257-
# The exact exception type is not important here;
258-
# this test only ensures we don't crash.
259-
with self.assertRaises(json.JSONDecodeError):
260-
json.loads('"\\uZZZZ"')
261252

253+
orig_json_error = json.JSONDecodeError
254+
orig_decoder_error = json.decoder.JSONDecodeError
255+
256+
try:
257+
json.JSONDecodeError = hook
258+
json.decoder.JSONDecodeError = hook
259+
del hook
260+
261+
with self.assertRaises(ValueError):
262+
json.loads('"\\uZZZZ"')
263+
finally:
264+
json.JSONDecodeError = orig_json_error
265+
json.decoder.JSONDecodeError = orig_decoder_error
262266

263267
class TestPyFail(TestFail, PyTest): pass
264268
class TestCFail(TestFail, CTest): pass

0 commit comments

Comments
 (0)