Skip to content

Commit

Permalink
gh-107659: Add docstrings for ctypes.pointer and ctypes.POINTER (#107660
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tomasr8 committed Aug 8, 2023
1 parent 7a250fd commit de72677
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
@@ -0,0 +1 @@
Add docstrings for :func:`ctypes.pointer` and :func:`ctypes.POINTER`.
44 changes: 39 additions & 5 deletions Modules/_ctypes/callproc.c
Expand Up @@ -54,6 +54,11 @@
*/

/*[clinic input]
module _ctypes
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=476a19c49b31a75c]*/

#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
Expand Down Expand Up @@ -98,6 +103,7 @@

#include "pycore_runtime.h" // _PyRuntime
#include "pycore_global_objects.h" // _Py_ID()
#include "clinic/callproc.c.h"

#define CTYPES_CAPSULE_NAME_PYMEM "_ctypes pymem"

Expand Down Expand Up @@ -1893,8 +1899,22 @@ unpickle(PyObject *self, PyObject *args)
return NULL;
}

/*[clinic input]
_ctypes.POINTER as create_pointer_type
type as cls: object
A ctypes type.
/
Create and return a new ctypes pointer type.
Pointer types are cached and reused internally,
so calling this function repeatedly is cheap.
[clinic start generated code]*/

static PyObject *
POINTER(PyObject *self, PyObject *cls)
create_pointer_type(PyObject *module, PyObject *cls)
/*[clinic end generated code: output=98c3547ab6f4f40b input=3b81cff5ff9b9d5b]*/
{
PyObject *result;
PyTypeObject *typ;
Expand Down Expand Up @@ -1944,8 +1964,22 @@ POINTER(PyObject *self, PyObject *cls)
return result;
}

/*[clinic input]
_ctypes.pointer as create_pointer_inst
obj as arg: object
/
Create a new pointer instance, pointing to 'obj'.
The returned object is of the type POINTER(type(obj)). Note that if you
just want to pass a pointer to an object to a foreign function call, you
should use byref(obj) which is much faster.
[clinic start generated code]*/

static PyObject *
pointer(PyObject *self, PyObject *arg)
create_pointer_inst(PyObject *module, PyObject *arg)
/*[clinic end generated code: output=3b543bc9f0de2180 input=713685fdb4d9bc27]*/
{
PyObject *result;
PyObject *typ;
Expand All @@ -1957,7 +1991,7 @@ pointer(PyObject *self, PyObject *arg)
else if (PyErr_Occurred()) {
return NULL;
}
typ = POINTER(NULL, (PyObject *)Py_TYPE(arg));
typ = create_pointer_type(NULL, (PyObject *)Py_TYPE(arg));
if (typ == NULL)
return NULL;
result = PyObject_CallOneArg(typ, arg);
Expand Down Expand Up @@ -1997,8 +2031,8 @@ buffer_info(PyObject *self, PyObject *arg)
PyMethodDef _ctypes_module_methods[] = {
{"get_errno", get_errno, METH_NOARGS},
{"set_errno", set_errno, METH_VARARGS},
{"POINTER", POINTER, METH_O },
{"pointer", pointer, METH_O },
CREATE_POINTER_TYPE_METHODDEF
CREATE_POINTER_INST_METHODDEF
{"_unpickle", unpickle, METH_VARARGS },
{"buffer_info", buffer_info, METH_O, "Return buffer interface information"},
{"resize", resize, METH_VARARGS, "Resize the memory buffer of a ctypes instance"},
Expand Down
38 changes: 38 additions & 0 deletions Modules/_ctypes/clinic/callproc.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de72677

Please sign in to comment.