diff options
author | Bob Ippolito <bob@redivi.com> | 2009-12-27 22:54:11 +0000 |
---|---|---|
committer | Bob Ippolito <bob@redivi.com> | 2009-12-27 22:54:11 +0000 |
commit | be27d6a3df096b90668a9d3c7c0370ee27fb6ab3 (patch) | |
tree | c05cfd9e4912f07cb735cfdda84b40c405c9b5f1 | |
parent | 1d834705e21003683dd18a914f90f294b9196947 (diff) | |
download | simplejson-be27d6a3df096b90668a9d3c7c0370ee27fb6ab3.tar.gz |
fix Py_ssize_t converter
git-svn-id: http://simplejson.googlecode.com/svn/trunk@215 a4795897-2c25-0410-b006-0d3caba88fa1
-rw-r--r-- | simplejson/_speedups.c | 7 | ||||
-rw-r--r-- | simplejson/tests/__init__.py | 1 | ||||
-rw-r--r-- | simplejson/tests/test_decode.py | 6 | ||||
-rw-r--r-- | simplejson/tests/test_indent.py | 8 | ||||
-rw-r--r-- | simplejson/tests/test_scanstring.py | 6 | ||||
-rw-r--r-- | simplejson/tests/test_speedups.py | 14 |
6 files changed, 33 insertions, 9 deletions
diff --git a/simplejson/_speedups.c b/simplejson/_speedups.c index 297675f..14efa70 100644 --- a/simplejson/_speedups.c +++ b/simplejson/_speedups.c @@ -161,9 +161,9 @@ _convertPyInt_AsSsize_t(PyObject *o, Py_ssize_t *size_ptr) { /* PyObject to Py_ssize_t converter */ *size_ptr = PyInt_AsSsize_t(o); - if (*size_ptr == -1 && PyErr_Occurred()); - return 1; - return 0; + if (*size_ptr == -1 && PyErr_Occurred()) + return 0; + return 1; } static PyObject * @@ -476,6 +476,7 @@ scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict, Py_s char *buf = PyString_AS_STRING(pystr); PyObject *chunks = NULL; PyObject *chunk = NULL; + if (end < 0 || len <= end) { PyErr_SetString(PyExc_ValueError, "end is out of bounds"); goto bail; diff --git a/simplejson/tests/__init__.py b/simplejson/tests/__init__.py index 12a1005..b4ce3b3 100644 --- a/simplejson/tests/__init__.py +++ b/simplejson/tests/__init__.py @@ -42,6 +42,7 @@ def all_tests_suite(): 'simplejson.tests.test_recursion', 'simplejson.tests.test_scanstring', 'simplejson.tests.test_separators', + 'simplejson.tests.test_speedups', 'simplejson.tests.test_unicode', ]) suite = additional_tests(suite) diff --git a/simplejson/tests/test_decode.py b/simplejson/tests/test_decode.py index 6ebd071..a5a1c48 100644 --- a/simplejson/tests/test_decode.py +++ b/simplejson/tests/test_decode.py @@ -8,16 +8,16 @@ from simplejson import OrderedDict class TestDecode(TestCase): if not hasattr(TestCase, 'assertIs'): def assertIs(self, a, b): - self.assert_(a is b, '%r is %r' % (a, b)) + self.assertTrue(a is b, '%r is %r' % (a, b)) def test_decimal(self): rval = json.loads('1.1', parse_float=decimal.Decimal) - self.assert_(isinstance(rval, decimal.Decimal)) + self.assertTrue(isinstance(rval, decimal.Decimal)) self.assertEquals(rval, decimal.Decimal('1.1')) def test_float(self): rval = json.loads('1', parse_int=float) - self.assert_(isinstance(rval, float)) + self.assertTrue(isinstance(rval, float)) self.assertEquals(rval, 1.0) def test_decoder_optimizations(self): diff --git a/simplejson/tests/test_indent.py b/simplejson/tests/test_indent.py index fa07075..985831b 100644 --- a/simplejson/tests/test_indent.py +++ b/simplejson/tests/test_indent.py @@ -5,7 +5,8 @@ import textwrap class TestIndent(TestCase): def test_indent(self): - h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', + h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', + 'i-vhbjkhnth', {'nifty': 87}, {'field': 'yes', 'morefield': False} ] expect = textwrap.dedent("""\ @@ -44,6 +45,9 @@ class TestIndent(TestCase): self.assertEquals(h2, h) self.assertEquals(h3, h) self.assertEquals(h4, h) - self.assertEquals(d2, expect) self.assertEquals(d3, expect.replace('\t', ' ')) self.assertEquals(d4, expect.replace('\t', ' ')) + # NOTE: Python 2.4 textwrap.dedent converts tabs to spaces, + # so the following is expected to fail. Python 2.4 is not a + # supported platform in simplejson 2.1.0+. + self.assertEquals(d2, expect) diff --git a/simplejson/tests/test_scanstring.py b/simplejson/tests/test_scanstring.py index b08dec7..a44a558 100644 --- a/simplejson/tests/test_scanstring.py +++ b/simplejson/tests/test_scanstring.py @@ -1,5 +1,4 @@ import sys -import decimal from unittest import TestCase import simplejson as json @@ -109,3 +108,8 @@ class TestScanString(TestCase): "xxx") self.assertRaises(UnicodeDecodeError, json.encoder.encode_basestring_ascii, "xx\xff") + + def test_overflow(self): + self.assertRaises(OverflowError, json.decoder.scanstring, "xxx", + sys.maxsize + 1) + diff --git a/simplejson/tests/test_speedups.py b/simplejson/tests/test_speedups.py new file mode 100644 index 0000000..d0c3c9b --- /dev/null +++ b/simplejson/tests/test_speedups.py @@ -0,0 +1,14 @@ +import decimal +from unittest import TestCase + +from simplejson import decoder, encoder, scanner + +class TestDecode(TestCase): + def test_make_scanner(self): + self.assertRaises(AttributeError, scanner.c_make_scanner, 1) + + def test_make_encoder(self): + self.assertRaises(TypeError, encoder.c_make_encoder, + None, + "\xCD\x7D\x3D\x4E\x12\x4C\xF9\x79\xD7\x52\xBA\x82\xF2\x27\x4A\x7D\xA0\xCA\x75", + None) |