diff options
-rw-r--r-- | Lib/test/test_structmembers.py | 28 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Modules/_testcapimodule.c | 11 | ||||
-rw-r--r-- | Objects/longobject.c | 25 | ||||
-rw-r--r-- | Python/structmember.c | 7 |
5 files changed, 61 insertions, 13 deletions
diff --git a/Lib/test/test_structmembers.py b/Lib/test/test_structmembers.py index 17ca2ac..9d82f38 100644 --- a/Lib/test/test_structmembers.py +++ b/Lib/test/test_structmembers.py @@ -3,7 +3,8 @@ from _testcapi import _test_structmembersType, \ SHRT_MAX, SHRT_MIN, USHRT_MAX, \ INT_MAX, INT_MIN, UINT_MAX, \ LONG_MAX, LONG_MIN, ULONG_MAX, \ - LLONG_MAX, LLONG_MIN, ULLONG_MAX + LLONG_MAX, LLONG_MIN, ULLONG_MAX, \ + PY_SSIZE_T_MAX, PY_SSIZE_T_MIN import warnings, unittest, sys from test import support @@ -17,6 +18,7 @@ ts=_test_structmembersType(False, # T_BOOL 6, # T_UINT 7, # T_LONG 8, # T_ULONG + 23, # T_PYSSIZET 9.99999,# T_FLOAT 10.1010101010, # T_DOUBLE "hi" # T_STRING_INPLACE @@ -63,6 +65,12 @@ class ReadWriteTests(unittest.TestCase): ts.T_ULONG = ULONG_MAX self.assertEquals(ts.T_ULONG, ULONG_MAX) + def test_py_ssize_t(self): + ts.T_PYSSIZET = PY_SSIZE_T_MAX + self.assertEquals(ts.T_PYSSIZET, PY_SSIZE_T_MAX) + ts.T_PYSSIZET = PY_SSIZE_T_MIN + self.assertEquals(ts.T_PYSSIZET, PY_SSIZE_T_MIN) + @unittest.skipUnless(hasattr(ts, "T_LONGLONG"), "long long not present") def test_longlong(self): ts.T_LONGLONG = LLONG_MAX @@ -79,6 +87,24 @@ class ReadWriteTests(unittest.TestCase): ts.T_ULONGLONG = 4 self.assertEquals(ts.T_ULONGLONG, 4) + def test_bad_assignments(self): + integer_attributes = [ + 'T_BOOL', + 'T_BYTE', 'T_UBYTE', + 'T_SHORT', 'T_USHORT', + 'T_INT', 'T_UINT', + 'T_LONG', 'T_ULONG', + 'T_PYSSIZET' + ] + if hasattr(ts, 'T_LONGLONG'): + integer_attributes.extend(['T_LONGLONG', 'T_ULONGLONG']) + + # issue8014: this produced 'bad argument to internal function' + # internal error + for nonint in None, 3.2j, "full of eels", {}, []: + for attr in integer_attributes: + self.assertRaises(TypeError, setattr, ts, attr, nonint) + def test_inplace_string(self): self.assertEquals(ts.T_STRING_INPLACE, "hi") self.assertRaises(TypeError, setattr, ts, "T_STRING_INPLACE", "s") @@ -12,6 +12,9 @@ What's New in Python 3.1.3? Core and Builtins ----------------- +- Issue #8014: Setting a T_UINT or T_PYSSIZET attribute of an object with + PyMemberDefs could produce an internal error; raise TypeError instead. + - Raise a TypeError when trying to delete a T_STRING_INPLACE struct member. - Issue #8226: sys.setfilesystemencoding() raises a LookupError if the encoding diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 54e596a..ab28e9d 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -1670,6 +1670,7 @@ typedef struct { unsigned int uint_member; long long_member; unsigned long ulong_member; + Py_ssize_t pyssizet_member; float float_member; double double_member; char inplace_member[6]; @@ -1694,6 +1695,7 @@ static struct PyMemberDef test_members[] = { {"T_UINT", T_UINT, offsetof(test_structmembers, structmembers.uint_member), 0, NULL}, {"T_LONG", T_LONG, offsetof(test_structmembers, structmembers.long_member), 0, NULL}, {"T_ULONG", T_ULONG, offsetof(test_structmembers, structmembers.ulong_member), 0, NULL}, + {"T_PYSSIZET", T_PYSSIZET, offsetof(test_structmembers, structmembers.pyssizet_member), 0, NULL}, {"T_FLOAT", T_FLOAT, offsetof(test_structmembers, structmembers.float_member), 0, NULL}, {"T_DOUBLE", T_DOUBLE, offsetof(test_structmembers, structmembers.double_member), 0, NULL}, {"T_STRING_INPLACE", T_STRING_INPLACE, offsetof(test_structmembers, structmembers.inplace_member), 0, NULL}, @@ -1710,13 +1712,13 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) { static char *keywords[] = { "T_BOOL", "T_BYTE", "T_UBYTE", "T_SHORT", "T_USHORT", - "T_INT", "T_UINT", "T_LONG", "T_ULONG", + "T_INT", "T_UINT", "T_LONG", "T_ULONG", "T_PYSSIZET", "T_FLOAT", "T_DOUBLE", "T_STRING_INPLACE", -#ifdef HAVE_LONG_LONG +#ifdef HAVE_LONG_LONG "T_LONGLONG", "T_ULONGLONG", #endif NULL}; - static char *fmt = "|bbBhHiIlkfds#" + static char *fmt = "|bbBhHiIlknfds#" #ifdef HAVE_LONG_LONG "LK" #endif @@ -1735,9 +1737,10 @@ test_structmembers_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) &ob->structmembers.short_member, &ob->structmembers.ushort_member, &ob->structmembers.int_member, - &ob->structmembers.uint_member, + &ob->structmembers.uint_member, &ob->structmembers.long_member, &ob->structmembers.ulong_member, + &ob->structmembers.pyssizet_member, &ob->structmembers.float_member, &ob->structmembers.double_member, &s, &string_len diff --git a/Objects/longobject.c b/Objects/longobject.c index f84b54e..cfd6eba 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -440,10 +440,15 @@ PyLong_AsSsize_t(PyObject *vv) { Py_ssize_t i; int sign; - if (vv == NULL || !PyLong_Check(vv)) { + if (vv == NULL) { PyErr_BadInternalCall(); return -1; } + if (!PyLong_Check(vv)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return -1; + } + v = (PyLongObject *)vv; i = Py_SIZE(v); switch (i) { @@ -490,10 +495,15 @@ PyLong_AsUnsignedLong(PyObject *vv) unsigned long x, prev; Py_ssize_t i; - if (vv == NULL || !PyLong_Check(vv)) { + if (vv == NULL) { PyErr_BadInternalCall(); - return (unsigned long) -1; + return (unsigned long)-1; + } + if (!PyLong_Check(vv)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return (unsigned long)-1; } + v = (PyLongObject *)vv; i = Py_SIZE(v); x = 0; @@ -528,10 +538,15 @@ PyLong_AsSize_t(PyObject *vv) size_t x, prev; Py_ssize_t i; - if (vv == NULL || !PyLong_Check(vv)) { + if (vv == NULL) { PyErr_BadInternalCall(); - return (unsigned long) -1; + return (size_t) -1; + } + if (!PyLong_Check(vv)) { + PyErr_SetString(PyExc_TypeError, "an integer is required"); + return (size_t)-1; } + v = (PyLongObject *)vv; i = Py_SIZE(v); x = 0; diff --git a/Python/structmember.c b/Python/structmember.c index 8e37184..9109f23 100644 --- a/Python/structmember.c +++ b/Python/structmember.c @@ -187,12 +187,13 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) } case T_UINT:{ unsigned long ulong_val = PyLong_AsUnsignedLong(v); - if ((ulong_val == (unsigned int)-1) && PyErr_Occurred()) { + if ((ulong_val == (unsigned long)-1) && PyErr_Occurred()) { /* XXX: For compatibility, accept negative int values as well. */ PyErr_Clear(); ulong_val = PyLong_AsLong(v); - if ((ulong_val == (unsigned int)-1) && PyErr_Occurred()) + if ((ulong_val == (unsigned long)-1) && + PyErr_Occurred()) return -1; *(unsigned int *)addr = (unsigned int)ulong_val; WARN("Writing negative value into unsigned field"); @@ -216,7 +217,7 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v) as well. */ PyErr_Clear(); *(unsigned long*)addr = PyLong_AsLong(v); - if ((*(unsigned long*)addr == (unsigned int)-1) + if ((*(unsigned long*)addr == (unsigned long)-1) && PyErr_Occurred()) return -1; WARN("Writing negative value into unsigned field"); |