Skip to content

Commit 1c3594e

Browse files
committed
Various documentation updates per reviews
1 parent cb22479 commit 1c3594e

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

Doc/library/ctypes.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,15 @@ The :mod:`!ctypes.util` module provides the :func:`~ctypes.util.dllist` function
14191419
which calls the different APIs provided by the various platforms to help determine
14201420
which shared libraries have already been loaded into the current process.
14211421

1422+
The exact output of this function will be system dependent. On most platforms,
1423+
the first entry of this list represents the current process itself, which may
1424+
be an empty string.
1425+
For example, on glibc-based Linux, the return may look like::
1426+
1427+
>>> from ctypes.util import dllist
1428+
>>> dllist()
1429+
['', 'linux-vdso.so.1', '/lib/x86_64-linux-gnu/libm.so.6', '/lib/x86_64-linux-gnu/libc.so.6', ... ]
1430+
14221431
.. _ctypes-loading-shared-libraries:
14231432

14241433
Loading shared libraries
@@ -2108,7 +2117,7 @@ Utility functions
21082117
executable file. It may be an empty string.
21092118

21102119
.. availability:: Windows, macOS, iOS, glibc, BSD libc, musl
2111-
.. versionadded:: 3.14
2120+
.. versionadded:: next
21122121

21132122
.. function:: FormatError([code])
21142123

Doc/whatsnew/3.14.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ ctypes
357357
complex C types.
358358
(Contributed by Sergey B Kirpichev in :gh:`61103`).
359359

360-
* Added :func:`ctypes.util.dllist` for listing the shared libraries
360+
* Add :func:`ctypes.util.dllist` for listing the shared libraries
361361
loaded by the current process.
362362
(Contributed by Brian Ward in :gh:`119349`.)
363363

Lib/ctypes/util.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ def find_library(name):
6767
return fname
6868
return None
6969

70-
import ctypes
71-
from ctypes import wintypes
72-
70+
# Listing loaded DLLs on Windows relies on the following APIs:
7371
# https://learn.microsoft.com/windows/win32/api/psapi/nf-psapi-enumprocessmodules
7472
# https://learn.microsoft.com/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew
73+
import ctypes
74+
from ctypes import wintypes
7575

7676
_kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
7777
_get_current_process = _kernel32["GetCurrentProcess"]
@@ -120,6 +120,7 @@ def _get_module_handles():
120120
return modules[:n]
121121

122122
def dllist():
123+
"""Return a list of loaded shared libraries in the current process."""
123124
modules = _get_module_handles()
124125
libraries = [name for h in modules
125126
if (name := _get_module_filename(h)) is not None]
@@ -138,14 +139,16 @@ def find_library(name):
138139
continue
139140
return None
140141

142+
# Listing loaded libraries on Apple systems relies on the following API:
143+
# https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html
141144
import ctypes
142145

143-
# https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html
144146
_libc = ctypes.CDLL(find_library("c"))
145147
_dyld_get_image_name = _libc["_dyld_get_image_name"]
146148
_dyld_get_image_name.restype = ctypes.c_char_p
147149

148150
def dllist():
151+
"""Return a list of loaded shared libraries in the current process."""
149152
num_images = _libc._dyld_image_count()
150153
libraries = [os.fsdecode(name) for i in range(num_images)
151154
if (name := _dyld_get_image_name(i)) is not None]
@@ -414,7 +417,9 @@ def find_library(name):
414417
_get_soname(_findLib_gcc(name)) or _get_soname(_findLib_ld(name))
415418

416419

417-
# other systems use functions common to Linux and a few other Unix-like systems
420+
# Listing loaded libraries on other systems will try to use
421+
# functions common to Linux and a few other Unix-like systems.
422+
# See the following for several platforms' documentation of the same API:
418423
# https://man7.org/linux/man-pages/man3/dl_iterate_phdr.3.html
419424
# https://man.freebsd.org/cgi/man.cgi?query=dl_iterate_phdr
420425
# https://man.openbsd.org/dl_iterate_phdr
@@ -454,6 +459,7 @@ def _info_callback(info, _size, data):
454459
_dl_iterate_phdr.restype = ctypes.c_int
455460

456461
def dllist():
462+
"""Return a list of loaded shared libraries in the current process."""
457463
libraries = []
458464
_dl_iterate_phdr(_info_callback,
459465
ctypes.byref(ctypes.py_object(libraries)))
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Added the :func:`ctypes.util.dllist` function to list the loaded shared
1+
Add the :func:`ctypes.util.dllist` function to list the loaded shared
22
libraries for the current process.

0 commit comments

Comments
 (0)