Blob Blame Raw

# HG changeset patch
# User Benjamin Peterson <benjamin@python.org>
# Date 1397442496 14400
# Node ID 4f15bd1ab28fe25c2e381ab05b11b60ce42fe613
# Parent  b49d990aaa9d708a8c3174f6d51b8e069040ffe4# Parent  8130b8c066062bc589d337aebd3da4b156ee7f45
merge 3.2

diff --git a/Lib/test/test_json/test_decode.py b/Lib/test/test_json/test_decode.py
--- a/Lib/test/json_tests/test_decode.py
+++ b/Lib/test/json_tests/test_decode.py
@@ -70,5 +70,9 @@ class TestDecode:
         msg = 'escape'
         self.assertRaisesRegex(ValueError, msg, self.loads, s)
 
+    def test_negative_index(self):
+        d = self.json.JSONDecoder()
+        self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
+
 class TestPyDecode(TestDecode, PyTest): pass
 class TestCDecode(TestDecode, CTest): pass
diff --git a/Modules/_json.c b/Modules/_json.c
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -975,7 +975,10 @@ scan_once_unicode(PyScannerObject *s, Py
     kind = PyUnicode_KIND(pystr);
     length = PyUnicode_GET_LENGTH(pystr);
 
-    if (idx >= length) {
+    if (idx < 0)
+        /* Compatibility with Python version. */
+        idx += length;
+    if (idx < 0 || idx >= length) {
         PyErr_SetNone(PyExc_StopIteration);
         return NULL;
     }