diff options
Diffstat (limited to 'Doc/library/ctypes.rst')
-rw-r--r-- | Doc/library/ctypes.rst | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 52950b5..4757371 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -148,15 +148,14 @@ Calling functions ^^^^^^^^^^^^^^^^^ You can call these functions like any other Python callable. This example uses -the ``time()`` function, which returns system time in seconds since the Unix -epoch, and the ``GetModuleHandleA()`` function, which returns a win32 module -handle. +the ``rand()`` function, which takes no arguments and returns a pseudo-random integer:: -This example calls both functions with a ``NULL`` pointer (``None`` should be used -as the ``NULL`` pointer):: + >>> print(libc.rand()) # doctest: +SKIP + 1804289383 + +On Windows, you can call the ``GetModuleHandleA()`` function, which returns a win32 module +handle (passing ``None`` as single argument to call it with a ``NULL`` pointer):: - >>> print(libc.time(None)) # doctest: +SKIP - 1150640792 >>> print(hex(windll.kernel32.GetModuleHandleA(None))) # doctest: +WINDOWS 0x1d000000 >>> @@ -247,6 +246,8 @@ Fundamental data types | :class:`c_ssize_t` | :c:type:`ssize_t` or | int | | | :c:type:`Py_ssize_t` | | +----------------------+------------------------------------------+----------------------------+ +| :class:`c_time_t` | :c:type:`time_t` | int | ++----------------------+------------------------------------------+----------------------------+ | :class:`c_float` | :c:type:`float` | float | +----------------------+------------------------------------------+----------------------------+ | :class:`c_double` | :c:type:`double` | float | @@ -447,6 +448,21 @@ By default functions are assumed to return the C :c:type:`int` type. Other return types can be specified by setting the :attr:`restype` attribute of the function object. +The C prototype of ``time()`` is ``time_t time(time_t *)``. Because ``time_t`` +might be of a different type than the default return type ``int``, you should +specify the ``restype``:: + + >>> libc.time.restype = c_time_t + +The argument types can be specified using ``argtypes``:: + + >>> libc.time.argtypes = (POINTER(c_time_t),) + +To call the function with a ``NULL`` pointer as first argument, use ``None``:: + + >>> print(libc.time(None)) # doctest: +SKIP + 1150640792 + Here is a more advanced example, it uses the ``strchr`` function, which expects a string pointer and a char, and returns a pointer to a string:: @@ -2275,6 +2291,13 @@ These are the fundamental ctypes data types: .. versionadded:: 3.2 +.. class:: c_time_t + + Represents the C :c:type:`time_t` datatype. + + .. versionadded:: 3.12 + + .. class:: c_ubyte Represents the C :c:type:`unsigned char` datatype, it interprets the value as |