benjamin.peterson
2008-10-16 21:17:25 UTC
Author: benjamin.peterson
Date: Thu Oct 16 23:17:24 2008
New Revision: 66934
Log:
merge r66932 and add a few py3k only checks
Modified:
python/branches/py3k/ (props changed)
python/branches/py3k/Lib/json/decoder.py
python/branches/py3k/Lib/json/tests/test_scanstring.py
python/branches/py3k/Modules/_json.c
Modified: python/branches/py3k/Lib/json/decoder.py
==============================================================================
--- python/branches/py3k/Lib/json/decoder.py (original)
+++ python/branches/py3k/Lib/json/decoder.py Thu Oct 16 23:17:24 2008
@@ -18,11 +18,15 @@
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
Modified: python/branches/py3k/Lib/json/tests/test_scanstring.py
==============================================================================
--- python/branches/py3k/Lib/json/tests/test_scanstring.py (original)
+++ python/branches/py3k/Lib/json/tests/test_scanstring.py Thu Oct 16 23:17:24 2008
@@ -2,6 +2,7 @@
import decimal
from unittest import TestCase
+import json
import json.decoder
class TestScanString(TestCase):
@@ -101,3 +102,9 @@
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")
Modified: python/branches/py3k/Modules/_json.c
==============================================================================
--- python/branches/py3k/Modules/_json.c (original)
+++ python/branches/py3k/Modules/_json.c Thu Oct 16 23:17:24 2008
@@ -179,11 +179,13 @@
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_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;
Date: Thu Oct 16 23:17:24 2008
New Revision: 66934
Log:
merge r66932 and add a few py3k only checks
Modified:
python/branches/py3k/ (props changed)
python/branches/py3k/Lib/json/decoder.py
python/branches/py3k/Lib/json/tests/test_scanstring.py
python/branches/py3k/Modules/_json.c
Modified: python/branches/py3k/Lib/json/decoder.py
==============================================================================
--- python/branches/py3k/Lib/json/decoder.py (original)
+++ python/branches/py3k/Lib/json/decoder.py Thu Oct 16 23:17:24 2008
@@ -18,11 +18,15 @@
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
Modified: python/branches/py3k/Lib/json/tests/test_scanstring.py
==============================================================================
--- python/branches/py3k/Lib/json/tests/test_scanstring.py (original)
+++ python/branches/py3k/Lib/json/tests/test_scanstring.py Thu Oct 16 23:17:24 2008
@@ -2,6 +2,7 @@
import decimal
from unittest import TestCase
+import json
import json.decoder
class TestScanString(TestCase):
@@ -101,3 +102,9 @@
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")
Modified: python/branches/py3k/Modules/_json.c
==============================================================================
--- python/branches/py3k/Modules/_json.c (original)
+++ python/branches/py3k/Modules/_json.c Thu Oct 16 23:17:24 2008
@@ -179,11 +179,13 @@
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_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;