]> granicus.if.org Git - python/commitdiff
Issue #17225: JSON decoder now counts columns in the first line starting
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 21 Feb 2013 18:26:52 +0000 (20:26 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 21 Feb 2013 18:26:52 +0000 (20:26 +0200)
with 1, as in other lines.

1  2 
Doc/library/json.rst
Lib/json/__init__.py
Lib/json/decoder.py
Lib/json/tool.py
Lib/test/json_tests/test_fail.py
Misc/NEWS

Simple merge
Simple merge
Simple merge
Simple merge
index a2dc29a29d42c73c883d086f7ba39b58d18af7f4,7809056a731e5d363aa2fa2f5f027e6f3f545ade..5b652e864a7301b30f4148d34ff2dc335d7da5a8
@@@ -101,82 -100,6 +101,94 @@@ class TestFail
          #This is for python encoder
          self.assertRaises(TypeError, self.dumps, data, indent=True)
  
-                 r'^{0}: line 1 column {1} \(char {1}\)'.format(
-                     re.escape(msg), idx),
 +    def test_truncated_input(self):
 +        test_cases = [
 +            ('', 'Expecting value', 0),
 +            ('[', 'Expecting value', 1),
 +            ('[42', "Expecting ',' delimiter", 3),
 +            ('[42,', 'Expecting value', 4),
 +            ('["', 'Unterminated string starting at', 1),
 +            ('["spam', 'Unterminated string starting at', 1),
 +            ('["spam"', "Expecting ',' delimiter", 7),
 +            ('["spam",', 'Expecting value', 8),
 +            ('{', 'Expecting property name enclosed in double quotes', 1),
 +            ('{"', 'Unterminated string starting at', 1),
 +            ('{"spam', 'Unterminated string starting at', 1),
 +            ('{"spam"', "Expecting ':' delimiter", 7),
 +            ('{"spam":', 'Expecting value', 8),
 +            ('{"spam":42', "Expecting ',' delimiter", 10),
 +            ('{"spam":42,', 'Expecting property name enclosed in double quotes', 11),
 +        ]
 +        test_cases += [
 +            ('"', 'Unterminated string starting at', 0),
 +            ('"spam', 'Unterminated string starting at', 0),
 +        ]
 +        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):
 +        test_cases = [
 +            ('[,', 'Expecting value', 1),
 +            ('{"spam":[}', 'Expecting value', 9),
 +            ('[42:', "Expecting ',' delimiter", 3),
 +            ('[42 "spam"', "Expecting ',' delimiter", 4),
 +            ('[42,]', 'Expecting value', 4),
 +            ('{"spam":[42}', "Expecting ',' delimiter", 11),
 +            ('["]', 'Unterminated string starting at', 1),
 +            ('["spam":', "Expecting ',' delimiter", 7),
 +            ('["spam",]', 'Expecting value', 8),
 +            ('{:', 'Expecting property name enclosed in double quotes', 1),
 +            ('{,', 'Expecting property name enclosed in double quotes', 1),
 +            ('{42', 'Expecting property name enclosed in double quotes', 1),
 +            ('[{]', 'Expecting property name enclosed in double quotes', 2),
 +            ('{"spam",', "Expecting ':' delimiter", 7),
 +            ('{"spam"}', "Expecting ':' delimiter", 7),
 +            ('[{"spam"]', "Expecting ':' delimiter", 8),
 +            ('{"spam":}', 'Expecting value', 8),
 +            ('[{"spam":]', 'Expecting value', 9),
 +            ('{"spam":42 "ham"', "Expecting ',' delimiter", 11),
 +            ('[{"spam":42]', "Expecting ',' delimiter", 11),
 +            ('{"spam":42,}', 'Expecting property name enclosed in double quotes', 11),
 +        ]
 +        for data, msg, idx in test_cases:
 +            self.assertRaisesRegex(ValueError,
-                 r' \(char {1} - {2}\)'.format(
-                     re.escape(msg), idx, len(data)),
++                r'^{0}: line 1 column {1} \(char {2}\)'.format(
++                    re.escape(msg), idx + 1, idx),
 +                self.loads, data)
 +
 +    def test_extra_data(self):
 +        test_cases = [
 +            ('[]]', 'Extra data', 2),
 +            ('{}}', 'Extra data', 2),
 +            ('[],[]', 'Extra data', 2),
 +            ('{},{}', 'Extra data', 2),
 +        ]
 +        test_cases += [
 +            ('42,"spam"', 'Extra data', 2),
 +            ('"spam",42', 'Extra data', 6),
 +        ]
 +        for data, msg, idx in test_cases:
 +            self.assertRaisesRegex(ValueError,
 +                r'^{0}: line 1 column {1} - line 1 column {2}'
++                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
diff --cc Misc/NEWS
index 8d935c66e5ca02bdb108d1ab039edf86c6304e44,36cd3ccb21ba8ed8517f3993e7468aefbdbe857b..541b1f7d6b458230b937521a2c415d4aec82fe8e
+++ b/Misc/NEWS
@@@ -260,9 -181,9 +260,12 @@@ Core and Builtin
  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.
 +
  - Issue #13700: Fix byte/string handling in imaplib authentication when an
    authobject is specified.