]> granicus.if.org Git - python/commitdiff
merge r66932 and add a few py3k only checks
authorBenjamin Peterson <benjamin@python.org>
Thu, 16 Oct 2008 21:17:24 +0000 (21:17 +0000)
committerBenjamin Peterson <benjamin@python.org>
Thu, 16 Oct 2008 21:17:24 +0000 (21:17 +0000)
Lib/json/decoder.py
Lib/json/tests/test_scanstring.py
Modules/_json.c

index f0bc245645f8feee0c982a1251edec2d6b482883..4e88ba6cc9f2ce5f3f83f456c1e7db08191e0e27 100644 (file)
@@ -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
 
 
index cd205a4986d037cf09fb23182055dafa88bc2443..025d15da25fc96f555de3ba7a013ec4d81245dee 100644 (file)
@@ -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")
index 47c4a567ed53e2c610587f5bc8fdaeaf62e3d712..0068bda9d2dfb73af65b91c0fd585ef0b12d3e28 100644 (file)
@@ -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;