Skip to content

Commit 6498f26

Browse files
committed
Fix test to use infinite_recursion pattern and catch RecursionError
- Use support.infinite_recursion() context manager - Catch RecursionError explicitly instead of all exceptions - Fix import order with two blank lines after from imports - Remove extra blank lines in _json.c - Use Py_DECREF instead of Py_XDECREF
1 parent 5359ecd commit 6498f26

2 files changed

Lines changed: 6 additions & 10 deletions

File tree

Lib/test/test_json/test_recursion.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
from test import support
44
from test.test_json import PyTest, CTest
55

6+
67
class JSONTestObject:
78
pass
8-
9-
109
class TestRecursion:
1110
def test_listrecursion(self):
1211
x = []
@@ -137,10 +136,9 @@ def default(obj):
137136
for _ in range(depth):
138137
obj = [obj]
139138

140-
try:
141-
self.dumps(obj, default=default)
142-
except Exception:
143-
pass
139+
with support.infinite_recursion():
140+
with self.assertRaises(RecursionError):
141+
self.dumps(obj, default=default)
144142

145143
support.gc_collect()
146144
self.assertTrue(weak_refs, "No objects were created to track")

Modules/_json.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,21 +1646,19 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
16461646
rv = encoder_listencode_obj(s, writer, newobj, indent_level, indent_cache);
16471647
_Py_LeaveRecursiveCall();
16481648
Py_DECREF(newobj);
1649-
16501649
if (rv) {
16511650
if (ident != NULL) {
16521651
int del_rv = PyDict_DelItem(s->markers, ident);
1653-
Py_XDECREF(ident);
1652+
Py_DECREF(ident);
16541653
if (del_rv < 0) {
16551654
return -1;
16561655
}
16571656
}
16581657
return -1;
16591658
}
1660-
16611659
if (ident != NULL) {
16621660
int del_rv = PyDict_DelItem(s->markers, ident);
1663-
Py_XDECREF(ident);
1661+
Py_DECREF(ident);
16641662
if (del_rv < 0) {
16651663
return -1;
16661664
}

0 commit comments

Comments
 (0)