[3.13] gh-154936: Fix JSON control character error position (GH-154941) (#154976)

hugovk <[email protected]> Fri, 31 Jul 2026 05:31:38 -0400 (EDT)
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/fc5a406d0824f6d5dc0dd06bf3fa48d0c1ea3ffe
commit: fc5a406d0824f6d5dc0dd06bf3fa48d0c1ea3ffe
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: hugovk <[email protected]>
date: 2026-07-31T12:31:21+03:00
summary:

[3.13] gh-154936: Fix JSON control character error position (GH-154941) (#154976)

Co-authored-by: yangbaechu <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-07-30-23-29-56.gh-issue-154936.a90443.rst
M Lib/json/decoder.py
M Lib/test/test_json/test_unicode.py

diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py
index 92ad63525576409..44a13fd3965b1a3 100644
--- a/Lib/json/decoder.py
+++ b/Lib/json/decoder.py
@@ -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
diff --git a/Lib/test/test_json/test_unicode.py b/Lib/test/test_json/test_unicode.py
index 1aa9546dc463061..291580cb047be46 100644
--- a/Lib/test/test_json/test_unicode.py
+++ b/Lib/test/test_json/test_unicode.py
@@ -44,7 +44,13 @@ def test_ascii_non_printable_decode(self):
                          '\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
index 000000000000000..19660c406eb2d0f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-30-23-29-56.gh-issue-154936.a90443.rst
@@ -0,0 +1,2 @@
+Fix the pure Python :mod:`json` decoder to report the correct position for
+invalid literal control characters in JSON strings.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]