Skip to content

Commit abf7925

Browse files
committed
gh-78773: Document CDLL(None)
1 parent 9b22261 commit abf7925

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

Doc/library/ctypes.rst

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,12 +1536,35 @@ function exported by these libraries, and reacquired afterwards.
15361536
The *name* parameter can now be a :term:`path-like object`.
15371537

15381538
All these classes can be instantiated by calling them with at least one
1539-
argument, the pathname of the shared library. If you have an existing handle to
1540-
an already loaded shared library, it can be passed as the ``handle`` named
1541-
parameter, otherwise the underlying platform's :c:func:`!dlopen` or
1539+
argument, the pathname of the shared library.
1540+
1541+
If you have an existing :py:attr:`handle <ctypes.PyDLL._handle>` to an already
1542+
loaded shared library, it can be passed as the *handle* argument.
1543+
1544+
If *handle* is ``None``, the underlying platform's :c:func:`!dlopen` or
15421545
:c:func:`!LoadLibrary` function is used to load the library into
15431546
the process, and to get a handle to it.
15441547

1548+
On systems that use :c:func:`!dlopen` (that is, not Windows), if *name* is
1549+
``None``, :c:func:`!dlopen` is called with ``NULL``, which opens the "library"
1550+
corresponding to the main program.
1551+
(Some systems do the same is *name* is empty; ``None``/``NULL`` is more
1552+
portable.)
1553+
1554+
.. admonition:: CPython implementation detail
1555+
1556+
Since CPython is linked to ``libc``, a *name* of ``None`` is often used
1557+
to access the C standard library::
1558+
1559+
>>> printf = ctypes.CDLL(None).printf
1560+
>>> printf.argtypes = [ctypes.c_char_p]
1561+
>>> printf(b"hello\n")
1562+
hello
1563+
6
1564+
1565+
To access the Python C API, see :py:data:`~ctypes.pythonapi` which works
1566+
across platforms.
1567+
15451568
The *mode* parameter can be used to specify how the library is loaded. For
15461569
details, consult the :manpage:`dlopen(3)` manpage. On Windows, *mode* is
15471570
ignored. On posix systems, RTLD_NOW is always added, and is not

0 commit comments

Comments
 (0)