Skip to content

Commit 30796ad

Browse files
committed
Error when adding convert
1 parent f0c6fcc commit 30796ad

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

Doc/library/ctypes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,14 @@ invalid non-\ ``NULL`` pointers would crash Python)::
870870
ValueError: NULL pointer access
871871
>>>
872872

873+
Converting pointer to iterator causes Python to crash::
874+
875+
>>> x = c_int32(54321)
876+
>>> list(pointer(x))
877+
Traceback (most recent call last):
878+
...
879+
NotImplementedError: Pointer object does not support iterator
880+
873881

874882
.. _ctypes-type-conversions:
875883

Lib/test/test_ctypes/test_pointers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class A(POINTER(c_ulong)):
4848
# Pointer can't set contents: has no _type_
4949
self.assertRaises(TypeError, A, c_ulong(33))
5050

51+
def test_disable_iter(self):
52+
self.assertRaises(NotImplementedError, list, pointer(c_int(123)))
53+
self.assertRaises(NotImplementedError, set, pointer(c_int(123)))
54+
self.assertRaises(NotImplementedError, tuple, pointer(c_int(123)))
55+
5156
def test_pass_pointers(self):
5257
dll = CDLL(_ctypes_test.__file__)
5358
func = dll._testfunc_p_p
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:exc:`NotImplementedError` error thrown when converting :func:`ctypes.pointer` to iterator.

Modules/_ctypes/_ctypes.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5477,11 +5477,20 @@ Pointer_bool(CDataObject *self)
54775477
return (*(void **)self->b_ptr != NULL);
54785478
}
54795479

5480+
static PyObject *
5481+
Pointer_iter(PyObject *myself)
5482+
{
5483+
PyErr_SetString(PyExc_NotImplementedError,
5484+
"Pointer object does not support iterator");
5485+
return NULL;
5486+
}
5487+
54805488
static PyType_Slot pycpointer_slots[] = {
54815489
{Py_tp_doc, PyDoc_STR("XXX to be provided")},
54825490
{Py_tp_getset, Pointer_getsets},
54835491
{Py_tp_init, Pointer_init},
54845492
{Py_tp_new, Pointer_new},
5493+
{Py_tp_iter, Pointer_iter},
54855494
{Py_bf_getbuffer, PyCData_NewGetBuffer},
54865495
{Py_nb_bool, Pointer_bool},
54875496
{Py_mp_subscript, Pointer_subscript},

0 commit comments

Comments
 (0)