Skip to content

Commit a13d475

Browse files
committed
merge r66932 and add a few py3k only checks
1 parent 6019208 commit a13d475

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

Lib/json/decoder.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818

1919

2020
def linecol(doc, pos):
21-
lineno = doc.count('\n', 0, pos) + 1
21+
if isinstance(doc, bytes):
22+
newline = b'\n'
23+
else:
24+
newline = '\n'
25+
lineno = doc.count(newline, 0, pos) + 1
2226
if lineno == 1:
2327
colno = pos
2428
else:
25-
colno = pos - doc.rindex('\n', 0, pos)
29+
colno = pos - doc.rindex(newline, 0, pos)
2630
return lineno, colno
2731

2832

Lib/json/tests/test_scanstring.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import decimal
33
from unittest import TestCase
44

5+
import json
56
import json.decoder
67

78
class TestScanString(TestCase):
@@ -101,3 +102,9 @@ def _test_scanstring(self, scanstring):
101102
self.assertEquals(
102103
scanstring('["Bad value", truth]', 2, None, True),
103104
('Bad value', 12))
105+
106+
def test_issue3623(self):
107+
self.assertRaises(ValueError, json.decoder.scanstring, b"xxx", 1,
108+
"xxx")
109+
self.assertRaises(UnicodeDecodeError,
110+
json.encoder.encode_basestring_ascii, b"xx\xff")

Modules/_json.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,13 @@ raise_errmsg(char *msg, PyObject *s, Py_ssize_t end)
179179
errmsg_fn = PyObject_GetAttrString(decoder, "errmsg");
180180
if (errmsg_fn == NULL)
181181
return;
182-
Py_XDECREF(decoder);
182+
Py_DECREF(decoder);
183183
}
184184
pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end);
185-
PyErr_SetObject(PyExc_ValueError, pymsg);
186-
Py_DECREF(pymsg);
185+
if (pymsg) {
186+
PyErr_SetObject(PyExc_ValueError, pymsg);
187+
Py_DECREF(pymsg);
188+
}
187189
/*
188190
189191
def linecol(doc, pos):
@@ -602,7 +604,7 @@ py_encode_basestring_ascii(PyObject* self, PyObject *pystr)
602604
Py_TYPE(pystr)->tp_name);
603605
return NULL;
604606
}
605-
if (PyBytes_Check(rval)) {
607+
if (rval != NULL && PyBytes_Check(rval)) {
606608
PyObject *urval = PyUnicode_DecodeASCII(PyBytes_AS_STRING(rval), PyBytes_GET_SIZE(rval), NULL);
607609
Py_DECREF(rval);
608610
return urval;

0 commit comments

Comments
 (0)