]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-154936: Fix JSON control character error position (GH-154941)
authoryangbaechu <45089264+yangbaechu@users.noreply.github.com>
Fri, 31 Jul 2026 08:57:00 +0000 (17:57 +0900)
committerGitHub <noreply@github.com>
Fri, 31 Jul 2026 08:57:00 +0000 (11:57 +0300)
Report the index of the invalid literal control character instead of
the following index in the pure Python decoder.

Lib/json/decoder.py
Lib/test/test_json/test_unicode.py
Misc/NEWS.d/next/Library/2026-07-30-23-29-56.gh-issue-154936.a90443.rst [new file with mode: 0644]

index 364e44d40cc30732e0b9561f4ce3162b254d0899..3150a29405f75c3a99e096dd2ef346d0fae23ddb 100644 (file)
@@ -97,7 +97,7 @@ def py_scanstring(s, end, strict=True,
             if strict:
                 #msg = "Invalid control character %r at" % (terminator,)
                 msg = "Invalid control character {0!r} at".format(terminator)
-                raise JSONDecodeError(msg, s, end)
+                raise JSONDecodeError(msg, s, end - 1)
             else:
                 _append(terminator)
                 continue
index 1aa9546dc4630618b23f0a91414959a6d4bb3b9f..291580cb047be466d6ecfd15bbb31dab580ed729 100644 (file)
@@ -44,7 +44,13 @@ class TestUnicode:
                          '\b\t\n\f\r')
         s = ''.join(map(chr, range(32)))
         for c in s:
-            self.assertRaises(self.JSONDecodeError, self.loads, f'"{c}"')
+            with self.subTest(control_character=ord(c)):
+                with self.assertRaises(self.JSONDecodeError) as cm:
+                    self.loads(f'"a{c}b"')
+                error = cm.exception
+                self.assertEqual(error.pos, 2)
+                self.assertEqual(error.lineno, 1)
+                self.assertEqual(error.colno, 3)
         self.assertEqual(self.loads(f'"{s}"', strict=False), s)
         self.assertEqual(self.loads('"\x7f"'), '\x7f')
 
diff --git a/Misc/NEWS.d/next/Library/2026-07-30-23-29-56.gh-issue-154936.a90443.rst b/Misc/NEWS.d/next/Library/2026-07-30-23-29-56.gh-issue-154936.a90443.rst
new file mode 100644 (file)
index 0000000..19660c4
--- /dev/null
@@ -0,0 +1,2 @@
+Fix the pure Python :mod:`json` decoder to report the correct position for
+invalid literal control characters in JSON strings.