From: Serhiy Storchaka Date: Thu, 21 Feb 2013 18:26:52 +0000 (+0200) Subject: Issue #17225: JSON decoder now counts columns in the first line starting X-Git-Tag: v3.4.0a1~1344 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=920007ad76f1a880e6784ddb97a5c6c99a19b9a9;p=python Issue #17225: JSON decoder now counts columns in the first line starting with 1, as in other lines. --- 920007ad76f1a880e6784ddb97a5c6c99a19b9a9 diff --cc Lib/test/json_tests/test_fail.py index a2dc29a29d,7809056a73..5b652e864a --- a/Lib/test/json_tests/test_fail.py +++ b/Lib/test/json_tests/test_fail.py @@@ -101,82 -100,6 +101,94 @@@ class TestFail #This is for python encoder self.assertRaises(TypeError, self.dumps, data, indent=True) + 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'^{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): + 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 {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 diff --cc Misc/NEWS index 8d935c66e5,36cd3ccb21..541b1f7d6b --- a/Misc/NEWS +++ 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.