Commits

Serhiy Storchaka  committed 69f793c Merge

Issue #17225: JSON decoder now counts columns in the first line starting
with 1, as in other lines.

  • Participants
  • Parent commits d496855, 361ba6d

Comments (0)

Files changed (6)

File Doc/library/json.rst

         "json": "obj"
     }
     $ echo '{1.2:3.4}' | python -mjson.tool
-    Expecting property name enclosed in double quotes: line 1 column 1 (char 1)
+    Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
 
 .. highlight:: python3
 

File Lib/json/__init__.py

         "json": "obj"
     }
     $ echo '{ 1.2:3.4}' | python -m json.tool
-    Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
+    Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
 """
 __version__ = '2.0.9'
 __all__ = [

File Lib/json/decoder.py

         newline = '\n'
     lineno = doc.count(newline, 0, pos) + 1
     if lineno == 1:
-        colno = pos
+        colno = pos + 1
     else:
         colno = pos - doc.rindex(newline, 0, pos)
     return lineno, colno

File Lib/json/tool.py

         "json": "obj"
     }
     $ echo '{ 1.2:3.4}' | python -m json.tool
-    Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
+    Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
 
 """
 import sys

File Lib/test/json_tests/test_fail.py

         ]
         for data, msg, idx in test_cases:
             self.assertRaisesRegex(ValueError,
-                r'^{0}: line 1 column {1} \(char {1}\)'.format(
-                    re.escape(msg), idx),
+                r'^{0}: line 1 column {1} \(char {2}\)'.format(
+                    re.escape(msg), idx + 1, idx),
                 self.loads, data)
 
     def test_unexpected_data(self):
         ]
         for data, msg, idx in test_cases:
             self.assertRaisesRegex(ValueError,
-                r'^{0}: line 1 column {1} \(char {1}\)'.format(
-                    re.escape(msg), idx),
+                r'^{0}: line 1 column {1} \(char {2}\)'.format(
+                    re.escape(msg), idx + 1, idx),
                 self.loads, data)
 
     def test_extra_data(self):
         for data, msg, idx in test_cases:
             self.assertRaisesRegex(ValueError,
                 r'^{0}: line 1 column {1} - line 1 column {2}'
-                r' \(char {1} - {2}\)'.format(
-                    re.escape(msg), idx, len(data)),
+                r' \(char {3} - {4}\)'.format(
+                    re.escape(msg), idx + 1, len(data) + 1, idx, len(data)),
                 self.loads, data)
 
+    def test_linecol(self):
+        test_cases = [
+            ('!', 1, 1, 0),
+            (' !', 1, 2, 1),
+            ('\n!', 2, 1, 1),
+            ('\n  \n\n     !', 4, 6, 10),
+        ]
+        for data, line, col, idx in test_cases:
+            self.assertRaisesRegex(ValueError,
+                r'^Expecting value: line {0} column {1}'
+                r' \(char {2}\)$'.format(line, col, idx),
+                self.loads, data)
 
 class TestPyFail(TestFail, PyTest): pass
 class TestCFail(TestFail, CTest): pass
 Library
 -------
 
+- Issue #17225: JSON decoder now counts columns in the first line starting
+  with 1, as in other lines.
+
 - Issue #6623: Added explicit DeprecationWarning for ftplib.netrc, which has
   been deprecated and undocumented for a long time.