# HG changeset patch # Parent fcf3ae3e7db49dc19279fd5ae39d0aa5fc737f19 Issue #25659: Change assert to TypeError in from_buffer/_copy() Based on suggestion by Eryk Sun. diff -r fcf3ae3e7db4 Lib/ctypes/test/test_frombuffer.py --- a/Lib/ctypes/test/test_frombuffer.py Mon Nov 14 05:04:36 2016 +0000 +++ b/Lib/ctypes/test/test_frombuffer.py Sat Nov 19 00:58:04 2016 +0000 @@ -120,5 +120,13 @@ with self.assertRaises(ValueError): (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__': unittest.main() diff -r fcf3ae3e7db4 Misc/NEWS --- a/Misc/NEWS Mon Nov 14 05:04:36 2016 +0000 +++ b/Misc/NEWS Sat Nov 19 00:58:04 2016 +0000 @@ -122,6 +122,9 @@ Library ------- +- Issue #25659: Prevent crash calling ctypes from_buffer() and + from_buffer_copy() methods on abstract classes like Array. + - Issue #19717: Makes Path.resolve() succeed on paths that do not exist. Patch by Vajrasky Kok diff -r fcf3ae3e7db4 Modules/_ctypes/_ctypes.c --- a/Modules/_ctypes/_ctypes.c Mon Nov 14 05:04:36 2016 +0000 +++ b/Modules/_ctypes/_ctypes.c Sat Nov 19 00:58:04 2016 +0000 @@ -463,7 +463,10 @@ Py_ssize_t offset = 0; 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)) return NULL; @@ -531,9 +534,12 @@ Py_ssize_t offset = 0; PyObject *result; StgDictObject *dict = PyType_stgdict(type); - assert (dict); - - if (!PyArg_ParseTuple(args, "y*|n:from_buffer", &buffer, &offset)) + if (!dict) { + PyErr_SetString(PyExc_TypeError, "abstract class"); + return NULL; + } + + if (!PyArg_ParseTuple(args, "y*|n:from_buffer_copy", &buffer, &offset)) return NULL; if (offset < 0) {