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

Skip to content

Commit de72677

Browse files
authored
gh-107659: Add docstrings for ctypes.pointer and ctypes.POINTER (#107660)
1 parent 7a250fd commit de72677

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add docstrings for :func:`ctypes.pointer` and :func:`ctypes.POINTER`.

Modules/_ctypes/callproc.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
5555
*/
5656

57+
/*[clinic input]
58+
module _ctypes
59+
[clinic start generated code]*/
60+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=476a19c49b31a75c]*/
61+
5762
#ifndef Py_BUILD_CORE_BUILTIN
5863
# define Py_BUILD_CORE_MODULE 1
5964
#endif
@@ -98,6 +103,7 @@
98103

99104
#include "pycore_runtime.h" // _PyRuntime
100105
#include "pycore_global_objects.h" // _Py_ID()
106+
#include "clinic/callproc.c.h"
101107

102108
#define CTYPES_CAPSULE_NAME_PYMEM "_ctypes pymem"
103109

@@ -1893,8 +1899,22 @@ unpickle(PyObject *self, PyObject *args)
18931899
return NULL;
18941900
}
18951901

1902+
/*[clinic input]
1903+
_ctypes.POINTER as create_pointer_type
1904+
1905+
type as cls: object
1906+
A ctypes type.
1907+
/
1908+
1909+
Create and return a new ctypes pointer type.
1910+
1911+
Pointer types are cached and reused internally,
1912+
so calling this function repeatedly is cheap.
1913+
[clinic start generated code]*/
1914+
18961915
static PyObject *
1897-
POINTER(PyObject *self, PyObject *cls)
1916+
create_pointer_type(PyObject *module, PyObject *cls)
1917+
/*[clinic end generated code: output=98c3547ab6f4f40b input=3b81cff5ff9b9d5b]*/
18981918
{
18991919
PyObject *result;
19001920
PyTypeObject *typ;
@@ -1944,8 +1964,22 @@ POINTER(PyObject *self, PyObject *cls)
19441964
return result;
19451965
}
19461966

1967+
/*[clinic input]
1968+
_ctypes.pointer as create_pointer_inst
1969+
1970+
obj as arg: object
1971+
/
1972+
1973+
Create a new pointer instance, pointing to 'obj'.
1974+
1975+
The returned object is of the type POINTER(type(obj)). Note that if you
1976+
just want to pass a pointer to an object to a foreign function call, you
1977+
should use byref(obj) which is much faster.
1978+
[clinic start generated code]*/
1979+
19471980
static PyObject *
1948-
pointer(PyObject *self, PyObject *arg)
1981+
create_pointer_inst(PyObject *module, PyObject *arg)
1982+
/*[clinic end generated code: output=3b543bc9f0de2180 input=713685fdb4d9bc27]*/
19491983
{
19501984
PyObject *result;
19511985
PyObject *typ;
@@ -1957,7 +1991,7 @@ pointer(PyObject *self, PyObject *arg)
19571991
else if (PyErr_Occurred()) {
19581992
return NULL;
19591993
}
1960-
typ = POINTER(NULL, (PyObject *)Py_TYPE(arg));
1994+
typ = create_pointer_type(NULL, (PyObject *)Py_TYPE(arg));
19611995
if (typ == NULL)
19621996
return NULL;
19631997
result = PyObject_CallOneArg(typ, arg);
@@ -1997,8 +2031,8 @@ buffer_info(PyObject *self, PyObject *arg)
19972031
PyMethodDef _ctypes_module_methods[] = {
19982032
{"get_errno", get_errno, METH_NOARGS},
19992033
{"set_errno", set_errno, METH_VARARGS},
2000-
{"POINTER", POINTER, METH_O },
2001-
{"pointer", pointer, METH_O },
2034+
CREATE_POINTER_TYPE_METHODDEF
2035+
CREATE_POINTER_INST_METHODDEF
20022036
{"_unpickle", unpickle, METH_VARARGS },
20032037
{"buffer_info", buffer_info, METH_O, "Return buffer interface information"},
20042038
{"resize", resize, METH_VARARGS, "Resize the memory buffer of a ctypes instance"},

Modules/_ctypes/clinic/callproc.c.h

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)