Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 920007a

Browse files
Issue #17225: JSON decoder now counts columns in the first line starting
with 1, as in other lines.
2 parents fc9bf11 + ed891c1 commit 920007a

6 files changed

Lines changed: 25 additions & 10 deletions

File tree

Doc/library/json.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ Using json.tool from the shell to validate and pretty-print::
101101
"json": "obj"
102102
}
103103
$ echo '{1.2:3.4}' | python -mjson.tool
104-
Expecting property name enclosed in double quotes: line 1 column 1 (char 1)
104+
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
105105

106106
.. highlight:: python3
107107

Lib/json/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"json": "obj"
9797
}
9898
$ echo '{ 1.2:3.4}' | python -m json.tool
99-
Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
99+
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
100100
"""
101101
__version__ = '2.0.9'
102102
__all__ = [

Lib/json/decoder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def linecol(doc, pos):
2424
newline = '\n'
2525
lineno = doc.count(newline, 0, pos) + 1
2626
if lineno == 1:
27-
colno = pos
27+
colno = pos + 1
2828
else:
2929
colno = pos - doc.rindex(newline, 0, pos)
3030
return lineno, colno

Lib/json/tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"json": "obj"
88
}
99
$ echo '{ 1.2:3.4}' | python -m json.tool
10-
Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
10+
Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
1111
1212
"""
1313
import sys

Lib/test/json_tests/test_fail.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ def test_truncated_input(self):
125125
]
126126
for data, msg, idx in test_cases:
127127
self.assertRaisesRegex(ValueError,
128-
r'^{0}: line 1 column {1} \(char {1}\)'.format(
129-
re.escape(msg), idx),
128+
r'^{0}: line 1 column {1} \(char {2}\)'.format(
129+
re.escape(msg), idx + 1, idx),
130130
self.loads, data)
131131

132132
def test_unexpected_data(self):
@@ -155,8 +155,8 @@ def test_unexpected_data(self):
155155
]
156156
for data, msg, idx in test_cases:
157157
self.assertRaisesRegex(ValueError,
158-
r'^{0}: line 1 column {1} \(char {1}\)'.format(
159-
re.escape(msg), idx),
158+
r'^{0}: line 1 column {1} \(char {2}\)'.format(
159+
re.escape(msg), idx + 1, idx),
160160
self.loads, data)
161161

162162
def test_extra_data(self):
@@ -173,10 +173,22 @@ def test_extra_data(self):
173173
for data, msg, idx in test_cases:
174174
self.assertRaisesRegex(ValueError,
175175
r'^{0}: line 1 column {1} - line 1 column {2}'
176-
r' \(char {1} - {2}\)'.format(
177-
re.escape(msg), idx, len(data)),
176+
r' \(char {3} - {4}\)'.format(
177+
re.escape(msg), idx + 1, len(data) + 1, idx, len(data)),
178178
self.loads, data)
179179

180+
def test_linecol(self):
181+
test_cases = [
182+
('!', 1, 1, 0),
183+
(' !', 1, 2, 1),
184+
('\n!', 2, 1, 1),
185+
('\n \n\n !', 4, 6, 10),
186+
]
187+
for data, line, col, idx in test_cases:
188+
self.assertRaisesRegex(ValueError,
189+
r'^Expecting value: line {0} column {1}'
190+
r' \(char {2}\)$'.format(line, col, idx),
191+
self.loads, data)
180192

181193
class TestPyFail(TestFail, PyTest): pass
182194
class TestCFail(TestFail, CTest): pass

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ Core and Builtins
260260
Library
261261
-------
262262

263+
- Issue #17225: JSON decoder now counts columns in the first line starting
264+
with 1, as in other lines.
265+
263266
- Issue #6623: Added explicit DeprecationWarning for ftplib.netrc, which has
264267
been deprecated and undocumented for a long time.
265268

0 commit comments

Comments
 (0)