[Python-checkins] cpython (merge 3.1 -> 3.2): #12051: merge with 3.1.

ezio.melotti python-checkins at python.org
Wed May 11 00:24:23 CEST 2011


http://hg.python.org/cpython/rev/3847b279d665
changeset: 70022:3847b279d665
branch: 3.2
parent: 70016:695a7acae392
parent: 70021:9557e4eeb291
user: Ezio Melotti <ezio.melotti at gmail.com>
date: Wed May 11 01:10:27 2011 +0300
summary:
 #12051: merge with 3.1.
files:
 Lib/test/json_tests/test_recursion.py | 21 ++++++++++++++-
 Misc/NEWS | 3 ++
 Modules/_json.c | 17 ++++++++++-
 3 files changed, 38 insertions(+), 3 deletions(-)
diff --git a/Lib/test/json_tests/test_recursion.py b/Lib/test/json_tests/test_recursion.py
--- a/Lib/test/json_tests/test_recursion.py
+++ b/Lib/test/json_tests/test_recursion.py
@@ -16,6 +16,11 @@
 return 'JSONTestObject'
 return json.JSONEncoder.default(o)
 
+class EndlessJSONEncoder(json.JSONEncoder):
+ def default(self, o):
+ """If check_circular is False, this will keep adding another list."""
+ return [o]
+
 
 class TestRecursion(TestCase):
 def test_listrecursion(self):
@@ -67,7 +72,7 @@
 self.fail("didn't raise ValueError on default recursion")
 
 
- def test_highly_nested_objects(self):
+ def test_highly_nested_objects_decoding(self):
 # test that loading highly-nested objects doesn't segfault when C
 # accelerations are used. See #12017
 with self.assertRaises(RuntimeError):
@@ -77,3 +82,17 @@
 with self.assertRaises(RuntimeError):
 json.loads('[' * 100000 + '1' + ']' * 100000)
 
+ def test_highly_nested_objects_encoding(self):
+ # See #12051
+ l, d = [], {}
+ for x in range(100000):
+ l, d = [l], {'k':d}
+ with self.assertRaises(RuntimeError):
+ json.dumps(l)
+ with self.assertRaises(RuntimeError):
+ json.dumps(d)
+
+ def test_endless_recursion(self):
+ # See #12051
+ with self.assertRaises(RuntimeError):
+ EndlessJSONEncoder(check_circular=False).encode(5j)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -342,6 +342,9 @@
 Extension Modules
 -----------------
 
+- Issue #12051: Fix segfault in json.dumps() while encoding highly-nested
+ objects using the C accelerations.
+
 - Issue #12017: Fix segfault in json.loads() while decoding highly-nested
 objects using the C accelerations.
 
diff --git a/Modules/_json.c b/Modules/_json.c
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -1338,10 +1338,18 @@
 return _steal_list_append(rval, encoded);
 }
 else if (PyList_Check(obj) || PyTuple_Check(obj)) {
- return encoder_listencode_list(s, rval, obj, indent_level);
+ if (Py_EnterRecursiveCall(" while encoding a JSON object"))
+ return -1;
+ rv = encoder_listencode_list(s, rval, obj, indent_level);
+ Py_LeaveRecursiveCall();
+ return rv;
 }
 else if (PyDict_Check(obj)) {
- return encoder_listencode_dict(s, rval, obj, indent_level);
+ if (Py_EnterRecursiveCall(" while encoding a JSON object"))
+ return -1;
+ rv = encoder_listencode_dict(s, rval, obj, indent_level);
+ Py_LeaveRecursiveCall();
+ return rv;
 }
 else {
 PyObject *ident = NULL;
@@ -1367,7 +1375,12 @@
 Py_XDECREF(ident);
 return -1;
 }
+
+ if (Py_EnterRecursiveCall(" while encoding a JSON object"))
+ return -1;
 rv = encoder_listencode_obj(s, rval, newobj, indent_level);
+ Py_LeaveRecursiveCall();
+
 Py_DECREF(newobj);
 if (rv) {
 Py_XDECREF(ident);
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /