diff options
author | Benjamin Peterson <benjamin@python.org> | 2008-10-16 21:17:24 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2008-10-16 21:17:24 (GMT) |
commit | a13d475901553b4a0ec7ed679ce2eac4598b557f (patch) | |
tree | dc0745a76b64e954cded961b6ec99bc08bdeac78 | |
parent | 60192084c405292f874d886eed05ed83614d20c4 (diff) | |
download | cpython-a13d475901553b4a0ec7ed679ce2eac4598b557f.zip cpython-a13d475901553b4a0ec7ed679ce2eac4598b557f.tar.gz cpython-a13d475901553b4a0ec7ed679ce2eac4598b557f.tar.bz2 |
merge r66932 and add a few py3k only checks
-rw-r--r-- | Lib/json/decoder.py | 8 | ||||
-rw-r--r-- | Lib/json/tests/test_scanstring.py | 7 | ||||
-rw-r--r-- | Modules/_json.c | 10 |
3 files changed, 19 insertions, 6 deletions
diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py index f0bc245..4e88ba6 100644 --- a/Lib/json/decoder.py +++ b/Lib/json/decoder.py @@ -18,11 +18,15 @@ NaN, PosInf, NegInf = float('nan'), float('inf'), float('-inf') def linecol(doc, pos): - lineno = doc.count('\n', 0, pos) + 1 + if isinstance(doc, bytes): + newline = b'\n' + else: + newline = '\n' + lineno = doc.count(newline, 0, pos) + 1 if lineno == 1: colno = pos else: - colno = pos - doc.rindex('\n', 0, pos) + colno = pos - doc.rindex(newline, 0, pos) return lineno, colno diff --git a/Lib/json/tests/test_scanstring.py b/Lib/json/tests/test_scanstring.py index cd205a4..025d15d 100644 --- a/Lib/json/tests/test_scanstring.py +++ b/Lib/json/tests/test_scanstring.py @@ -2,6 +2,7 @@ import sys import decimal from unittest import TestCase +import json import json.decoder class TestScanString(TestCase): @@ -101,3 +102,9 @@ class TestScanString(TestCase): self.assertEquals( scanstring('["Bad value", truth]', 2, None, True), ('Bad value', 12)) + + def test_issue3623(self): + self.assertRaises(ValueError, json.decoder.scanstring, b"xxx", 1, + "xxx") + self.assertRaises(UnicodeDecodeError, + json.encoder.encode_basestring_ascii, b"xx\xff") diff --git a/Modules/_json.c b/Modules/_json.c index 47c4a56..0068bda 100644 --- a/Modules/_json.c +++ b/Modules/_json.c @@ -179,11 +179,13 @@ raise_errmsg(char *msg, PyObject *s, Py_ssize_t end) errmsg_fn = PyObject_GetAttrString(decoder, "errmsg"); if (errmsg_fn == NULL) return; - Py_XDECREF(decoder); + Py_DECREF(decoder); } pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end); - PyErr_SetObject(PyExc_ValueError, pymsg); - Py_DECREF(pymsg); + if (pymsg) { + PyErr_SetObject(PyExc_ValueError, pymsg); + Py_DECREF(pymsg); + } /* def linecol(doc, pos): @@ -602,7 +604,7 @@ py_encode_basestring_ascii(PyObject* self, PyObject *pystr) Py_TYPE(pystr)->tp_name); return NULL; } - if (PyBytes_Check(rval)) { + if (rval != NULL && PyBytes_Check(rval)) { PyObject *urval = PyUnicode_DecodeASCII(PyBytes_AS_STRING(rval), PyBytes_GET_SIZE(rval), NULL); Py_DECREF(rval); return urval; |