Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_ctypes/test_pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,20 @@ class BadType(ctypes._Pointer):
with self.assertRaises(ctypes.ArgumentError):
func(object())

def test_incomplete_pointer_subscript(self):
class BadType(ctypes._Pointer):
pass

buf = bytearray(8)
buf[0] = 1
obj = BadType.from_buffer(buf)
with self.assertRaisesRegex(TypeError, "has no _type_"):
obj[0:1]
with self.assertRaisesRegex(TypeError, "has no _type_"):
obj[0]
with self.assertRaisesRegex(TypeError, "has no _type_"):
obj[0] = 0

class PointerTypeCacheTestCase(unittest.TestCase):
# dummy tests to check warnings and base behavior
def tearDown(self):
Expand Down
38 changes: 30 additions & 8 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -5665,14 +5665,21 @@ Pointer_item_impl(PyObject *myself, Py_ssize_t index)
assert(stginfo); /* Cannot be NULL for pointer object instances */

proto = stginfo->proto;
assert(proto);
if (proto == NULL) {
PyErr_SetString(PyExc_TypeError,
"Cannot access items: has no _type_");
return NULL;
}

StgInfo *iteminfo;
if (PyStgInfo_FromType(st, proto, &iteminfo) < 0) {
return NULL;
}
assert(iteminfo); /* proto is the item type of the pointer, a ctypes
type, so this cannot be NULL */
if (iteminfo == NULL) {
PyErr_SetString(PyExc_SystemError,
"pointer _type_ has no storage info");
return NULL;
}

size = iteminfo->size;
offset = index * iteminfo->size;
Expand Down Expand Up @@ -5722,14 +5729,21 @@ Pointer_ass_item_impl(PyObject *myself, Py_ssize_t index, PyObject *value)
assert(stginfo); /* Cannot be NULL for pointer instances */

proto = stginfo->proto;
assert(proto);
if (proto == NULL) {
PyErr_SetString(PyExc_TypeError,
"Cannot access items: has no _type_");
return -1;
}

StgInfo *iteminfo;
if (PyStgInfo_FromType(st, proto, &iteminfo) < 0) {
return -1;
}
assert(iteminfo); /* Cannot be NULL because the itemtype of a pointer
is always a ctypes type */
if (iteminfo == NULL) {
PyErr_SetString(PyExc_SystemError,
"pointer _type_ has no storage info");
return -1;
}

size = iteminfo->size;
offset = index * iteminfo->size;
Expand Down Expand Up @@ -5957,12 +5971,20 @@ Pointer_subscript(PyObject *myself, PyObject *item)
}
assert(stginfo); /* Cannot be NULL for pointer instances */
proto = stginfo->proto;
assert(proto);
if (proto == NULL) {
PyErr_SetString(PyExc_TypeError,
"Cannot access items: has no _type_");
return NULL;
}
StgInfo *iteminfo;
if (PyStgInfo_FromType(st, proto, &iteminfo) < 0) {
return NULL;
}
assert(iteminfo);
if (iteminfo == NULL) {
PyErr_SetString(PyExc_SystemError,
"pointer _type_ has no storage info");
return NULL;
}
if (iteminfo->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
char *dest;

Expand Down
Loading