Issue #25659: Change assert to TypeError in from_buffer/_copy()

Based on suggestion by Eryk Sun.
This commit is contained in:
Martin Panter 2016-11-20 07:58:35 +00:00
parent 395733d46b
commit 6e723d2d11
3 changed files with 20 additions and 3 deletions

View File

@ -120,5 +120,13 @@ class Test(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
(c_int * 1).from_buffer_copy(a, 16 * sizeof(c_int)) (c_int * 1).from_buffer_copy(a, 16 * sizeof(c_int))
def test_abstract(self):
self.assertRaises(TypeError, Array.from_buffer, bytearray(10))
self.assertRaises(TypeError, Structure.from_buffer, bytearray(10))
self.assertRaises(TypeError, Union.from_buffer, bytearray(10))
self.assertRaises(TypeError, Array.from_buffer_copy, b"123")
self.assertRaises(TypeError, Structure.from_buffer_copy, b"123")
self.assertRaises(TypeError, Union.from_buffer_copy, b"123")
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -121,6 +121,9 @@ Core and Builtins
Library Library
------- -------
- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and
from_buffer_copy() methods on abstract classes like Array.
- Issue #28732: Fix crash in os.spawnv() with no elements in args - Issue #28732: Fix crash in os.spawnv() with no elements in args
- Issue #28485: Always raise ValueError for negative - Issue #28485: Always raise ValueError for negative

View File

@ -469,7 +469,10 @@ CDataType_from_buffer(PyObject *type, PyObject *args)
Py_ssize_t offset = 0; Py_ssize_t offset = 0;
StgDictObject *dict = PyType_stgdict(type); StgDictObject *dict = PyType_stgdict(type);
assert (dict); if (!dict) {
PyErr_SetString(PyExc_TypeError, "abstract class");
return NULL;
}
if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset)) if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset))
return NULL; return NULL;
@ -537,9 +540,12 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args)
Py_ssize_t offset = 0; Py_ssize_t offset = 0;
PyObject *result; PyObject *result;
StgDictObject *dict = PyType_stgdict(type); StgDictObject *dict = PyType_stgdict(type);
assert (dict); if (!dict) {
PyErr_SetString(PyExc_TypeError, "abstract class");
return NULL;
}
if (!PyArg_ParseTuple(args, "y*|n:from_buffer", &buffer, &offset)) if (!PyArg_ParseTuple(args, "y*|n:from_buffer_copy", &buffer, &offset))
return NULL; return NULL;
if (offset < 0) { if (offset < 0) {