Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e723d2

Browse files
committed
Issue #25659: Change assert to TypeError in from_buffer/_copy()
Based on suggestion by Eryk Sun.
1 parent 395733d commit 6e723d2

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

Lib/ctypes/test/test_frombuffer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,13 @@ def test_from_buffer_copy_with_offset(self):
120120
with self.assertRaises(ValueError):
121121
(c_int * 1).from_buffer_copy(a, 16 * sizeof(c_int))
122122

123+
def test_abstract(self):
124+
self.assertRaises(TypeError, Array.from_buffer, bytearray(10))
125+
self.assertRaises(TypeError, Structure.from_buffer, bytearray(10))
126+
self.assertRaises(TypeError, Union.from_buffer, bytearray(10))
127+
self.assertRaises(TypeError, Array.from_buffer_copy, b"123")
128+
self.assertRaises(TypeError, Structure.from_buffer_copy, b"123")
129+
self.assertRaises(TypeError, Union.from_buffer_copy, b"123")
130+
123131
if __name__ == '__main__':
124132
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ Core and Builtins
121121
Library
122122
-------
123123

124+
- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and
125+
from_buffer_copy() methods on abstract classes like Array.
126+
124127
- Issue #28732: Fix crash in os.spawnv() with no elements in args
125128

126129
- Issue #28485: Always raise ValueError for negative

Modules/_ctypes/_ctypes.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,10 @@ CDataType_from_buffer(PyObject *type, PyObject *args)
469469
Py_ssize_t offset = 0;
470470

471471
StgDictObject *dict = PyType_stgdict(type);
472-
assert (dict);
472+
if (!dict) {
473+
PyErr_SetString(PyExc_TypeError, "abstract class");
474+
return NULL;
475+
}
473476

474477
if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset))
475478
return NULL;
@@ -537,9 +540,12 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args)
537540
Py_ssize_t offset = 0;
538541
PyObject *result;
539542
StgDictObject *dict = PyType_stgdict(type);
540-
assert (dict);
543+
if (!dict) {
544+
PyErr_SetString(PyExc_TypeError, "abstract class");
545+
return NULL;
546+
}
541547

542-
if (!PyArg_ParseTuple(args, "y*|n:from_buffer", &buffer, &offset))
548+
if (!PyArg_ParseTuple(args, "y*|n:from_buffer_copy", &buffer, &offset))
543549
return NULL;
544550

545551
if (offset < 0) {

0 commit comments

Comments
 (0)