@@ -1360,104 +1360,6 @@ is already known, on a case by case basis.
13601360ctypes reference
13611361----------------
13621362
1363-
1364- .. _ctypes-finding-shared-libraries :
1365-
1366- Finding shared libraries
1367- ^^^^^^^^^^^^^^^^^^^^^^^^
1368-
1369- When programming in a compiled language, shared libraries are accessed when
1370- compiling/linking a program, and when the program is run.
1371-
1372- The purpose of the :func: `~ctypes.util.find_library ` function is to locate a library in a way
1373- similar to what the compiler or runtime loader does (on platforms with several
1374- versions of a shared library the most recent should be loaded), while the ctypes
1375- library loaders act like when a program is run, and call the runtime loader
1376- directly.
1377-
1378- The :mod: `!ctypes.util ` module provides a function which can help to determine
1379- the library to load.
1380-
1381-
1382- .. data :: find_library(name)
1383- :module: ctypes.util
1384- :noindex:
1385-
1386- Try to find a library and return a pathname. *name * is the library name without
1387- any prefix like *lib *, suffix like ``.so ``, ``.dylib `` or version number (this
1388- is the form used for the posix linker option :option: `!-l `). If no library can
1389- be found, returns ``None ``.
1390-
1391- The exact functionality is system dependent.
1392-
1393- On Linux, :func: `~ctypes.util.find_library ` tries to run external programs
1394- (``/sbin/ldconfig ``, ``gcc ``, ``objdump `` and ``ld ``) to find the library file.
1395- It returns the filename of the library file.
1396-
1397- Note that if the output of these programs does not correspond to the dynamic
1398- linker used by Python, the result of this function may be misleading.
1399-
1400- .. versionchanged :: 3.6
1401- On Linux, the value of the environment variable ``LD_LIBRARY_PATH `` is used
1402- when searching for libraries, if a library cannot be found by any other means.
1403-
1404- Here are some examples::
1405-
1406- >>> from ctypes.util import find_library
1407- >>> find_library("m")
1408- 'libm.so.6'
1409- >>> find_library("c")
1410- 'libc.so.6'
1411- >>> find_library("bz2")
1412- 'libbz2.so.1.0'
1413- >>>
1414-
1415- On macOS and Android, :func: `~ctypes.util.find_library ` uses the system's
1416- standard naming schemes and paths to locate the library, and returns a full
1417- pathname if successful::
1418-
1419- >>> from ctypes.util import find_library
1420- >>> find_library("c")
1421- '/usr/lib/libc.dylib'
1422- >>> find_library("m")
1423- '/usr/lib/libm.dylib'
1424- >>> find_library("bz2")
1425- '/usr/lib/libbz2.dylib'
1426- >>> find_library("AGL")
1427- '/System/Library/Frameworks/AGL.framework/AGL'
1428- >>>
1429-
1430- On Windows, :func: `~ctypes.util.find_library ` searches along the system search path, and
1431- returns the full pathname, but since there is no predefined naming scheme a call
1432- like ``find_library("c") `` will fail and return ``None ``.
1433-
1434- If wrapping a shared library with :mod: `!ctypes `, it *may * be better to determine
1435- the shared library name at development time, and hardcode that into the wrapper
1436- module instead of using :func: `~ctypes.util.find_library ` to locate the library at runtime.
1437-
1438-
1439- .. _ctypes-listing-loaded-shared-libraries :
1440-
1441- Listing loaded shared libraries
1442- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1443-
1444- When writing code that relies on code loaded from shared libraries, it can be
1445- useful to know which shared libraries have already been loaded into the current
1446- process.
1447-
1448- The :mod: `!ctypes.util ` module provides the :func: `~ctypes.util.dllist ` function,
1449- which calls the different APIs provided by the various platforms to help determine
1450- which shared libraries have already been loaded into the current process.
1451-
1452- The exact output of this function will be system dependent. On most platforms,
1453- the first entry of this list represents the current process itself, which may
1454- be an empty string.
1455- For example, on glibc-based Linux, the return may look like::
1456-
1457- >>> from ctypes.util import dllist
1458- >>> dllist()
1459- ['', 'linux-vdso.so.1', '/lib/x86_64-linux-gnu/libm.so.6', '/lib/x86_64-linux-gnu/libc.so.6', ... ]
1460-
14611363.. _ctypes-loading-shared-libraries :
14621364
14631365Loading shared libraries
@@ -1485,13 +1387,20 @@ way is to instantiate :py:class:`CDLL` or one of its subclasses:
14851387 attribute, but it may be adjusted and/or validated.
14861388
14871389 If *handle * is ``None ``, the underlying platform's :manpage: `dlopen(3)` or
1488- :c:func: ` !LoadLibrary ` function is used to load the library into
1390+ ` LoadLibraryExW `_ function is used to load the library into
14891391 the process, and to get a handle to it.
14901392
14911393 *name * is the pathname of the shared library to open.
14921394 If *name * does not contain a path separator, the library is found
14931395 in a platform-specific way.
14941396
1397+ On Windows, the ``.DLL `` suffix may be missing. (For details, see
1398+ `LoadLibraryExW `_ documentation.)
1399+ Other platform-specific prefixes and suffixes (for example, ``lib ``,
1400+ ``.so ``, ``.dylib ``, or version numbers) must be present in *name *;
1401+ they are not added automatically.
1402+ See :ref: `ctypes-finding-shared-libraries ` for more information.
1403+
14951404 On non-Windows systems, *name * can be ``None ``. In this case,
14961405 :c:func: `!dlopen ` is called with ``NULL ``, which opens the main program
14971406 as a "library".
@@ -1536,7 +1445,7 @@ way is to instantiate :py:class:`CDLL` or one of its subclasses:
15361445
15371446 The *winmode * parameter is used on Windows to specify how the library is loaded
15381447 (since *mode * is ignored). It takes any value that is valid for the Win32 API
1539- `` LoadLibraryEx `` flags parameter. When omitted, the default is to use the
1448+ `LoadLibraryExW `_ flags parameter. When omitted, the default is to use the
15401449 flags that result in the most secure DLL load, which avoids issues such as DLL
15411450 hijacking. Passing the full path to the DLL is the safest way to ensure the
15421451 correct library and dependencies are loaded.
@@ -1587,6 +1496,8 @@ way is to instantiate :py:class:`CDLL` or one of its subclasses:
15871496
15881497 The name of the library passed in the constructor.
15891498
1499+ .. _LoadLibraryExW : https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw
1500+
15901501.. class :: OleDLL
15911502
15921503 See :py:class: `~ctypes.CDLL `, the superclass, for common information.
@@ -1665,39 +1576,36 @@ attribute of the loader instance.
16651576
16661577These prefabricated library loaders are available:
16671578
1668- .. data :: cdll
1669-
1670- Creates :class: `CDLL ` instances.
1579+ .. data :: cdll
16711580
1581+ Creates :class: `CDLL ` instances.
16721582
1673- .. data :: windll
16741583
1675- Creates :class: ` WinDLL ` instances.
1584+ .. data :: windll
16761585
1677- .. availability :: Windows
1586+ Creates :class: ` WinDLL ` instances.
16781587
1588+ .. availability :: Windows
16791589
1680- .. data :: oledll
16811590
1682- Creates :class: ` OleDLL ` instances.
1591+ .. data :: oledll
16831592
1684- .. availability :: Windows
1593+ Creates :class: ` OleDLL ` instances.
16851594
1595+ .. availability :: Windows
16861596
1687- .. data :: pydll
16881597
1689- Creates :class: ` PyDLL ` instances.
1598+ .. data :: pydll
16901599
1600+ Creates :class: `PyDLL ` instances.
16911601
1692- For accessing the C Python api directly, a ready-to-use Python shared library
1693- object is available:
16941602
1695- .. data :: pythonapi
1603+ .. data :: pythonapi
16961604
1697- An instance of :class: `PyDLL ` that exposes Python C API functions as
1698- attributes. Note that all these functions are assumed to return C
1699- :c:expr: `int `, which is of course not always the truth, so you have to assign
1700- the correct :attr: `!restype ` attribute to use these functions.
1605+ An instance of :class: `PyDLL ` that exposes Python C API functions as
1606+ attributes. Note that all these functions are assumed to return C
1607+ :c:expr: `int `, which is of course not always the truth, so you have to assign
1608+ the correct :attr: `!restype ` attribute to use these functions.
17011609
17021610.. audit-event :: ctypes.dlopen name ctypes.LibraryLoader
17031611
@@ -1717,6 +1625,135 @@ object is available:
17171625 accessing a function raises an auditing event ``ctypes.dlsym/handle `` with
17181626 arguments ``handle `` (the raw library handle) and ``name ``.
17191627
1628+
1629+ .. _ctypes-finding-shared-libraries :
1630+
1631+ Finding shared libraries
1632+ ^^^^^^^^^^^^^^^^^^^^^^^^
1633+
1634+ When programming in a compiled language, shared libraries are accessed when
1635+ compiling/linking a program, and when the program is run.
1636+ The programmer specifies a short name; the C compiler, linker, and
1637+ runtime dynamic library loader then interact in system-specific ways to find
1638+ the filename of the library to load.
1639+
1640+ While the mapping from short names to filenames is not consistently exposed
1641+ by platforms, the :mod: `!ctypes.util ` module provides a function,
1642+ :func: `!find_library `, that attempts to match it.
1643+ However, as backwards compatibility concerns make it difficult to adjust
1644+ its behavior for new platforms and configurations, the function
1645+ is :term: `soft deprecated `.
1646+
1647+ If wrapping a shared library with :mod: `!ctypes `, consider determining the
1648+ shared library name at development time, and hardcoding it into the wrapper
1649+ module instead of using :func: `!find_library ` to locate the library
1650+ at runtime.
1651+ Also consider addding a configuration option or environment variable to let
1652+ users select a library to use, and then perhaps use :func: `!find_library `
1653+ as a default or fallback.
1654+
1655+ .. function :: find_library(name)
1656+ :module: ctypes.util
1657+
1658+ Try to find a library and return a pathname.
1659+
1660+ *name * is the "short" library name without any prefix like ``lib ``,
1661+ suffix like ``.so ``, ``.dylib `` or version number.
1662+ (This is the form used for the posix linker option :option: `!-l `.)
1663+ The result is in a format suitable for passing to :py:class: `~ctypes.CDLL `.
1664+
1665+ If no library can be found, return ``None ``.
1666+
1667+ The exact functionality is system dependent, and is *not guaranteed *
1668+ to match the behavior of the compiler, linker, and loader used for
1669+ (or by) Python.
1670+ It is recommended to only use this function as a default or fallback,
1671+
1672+ .. deprecated :: next
1673+
1674+ This function is :term: `soft deprecated `.
1675+ It is kept for use in cases where it works, but not expected to be
1676+ updated for additional platforms and configurations.
1677+
1678+ On Linux, :func: `!find_library ` tries to run external
1679+ programs (``/sbin/ldconfig ``, ``gcc ``, ``objdump `` and ``ld ``) to find the
1680+ library file.
1681+ If the output of these programs does not correspond to the dynamic
1682+ linker used by Python, the result of this function may be misleading.
1683+
1684+ .. versionchanged :: 3.6
1685+ On Linux, the value of the environment variable ``LD_LIBRARY_PATH `` is used
1686+ when searching for libraries, if a library cannot be found by any other means.
1687+
1688+ Here are some examples::
1689+
1690+ >>> from ctypes.util import find_library
1691+ >>> find_library("m")
1692+ 'libm.so.6'
1693+ >>> find_library("c")
1694+ 'libc.so.6'
1695+ >>> find_library("bz2")
1696+ 'libbz2.so.1.0'
1697+ >>>
1698+
1699+ On macOS and Android, :func: `!find_library ` uses the system's
1700+ standard naming schemes and paths to locate the library, and returns a full
1701+ pathname if successful::
1702+
1703+ >>> from ctypes.util import find_library
1704+ >>> find_library("c")
1705+ '/usr/lib/libc.dylib'
1706+ >>> find_library("m")
1707+ '/usr/lib/libm.dylib'
1708+ >>> find_library("bz2")
1709+ '/usr/lib/libbz2.dylib'
1710+ >>> find_library("AGL")
1711+ '/System/Library/Frameworks/AGL.framework/AGL'
1712+ >>>
1713+
1714+ On Windows, :func: `!find_library ` searches along the system search path, and
1715+ returns the full pathname, but since there is no predefined naming scheme a call
1716+ like ``find_library("c") `` will fail and return ``None ``.
1717+
1718+ .. function :: find_msvcrt()
1719+ :module: ctypes.util
1720+
1721+ Returns the filename of the VC runtime library used by Python,
1722+ and by the extension modules.
1723+
1724+ If the name of the library cannot be determined, ``None `` is returned.
1725+ Notably, this will happen for recent versions of the VC runtime library,
1726+ which are not directly loadable.
1727+
1728+ If you need to free memory, for example, allocated by an extension module
1729+ with a call to the ``free(void *) ``, it is important that you use the
1730+ function in the same library that allocated the memory.
1731+
1732+ .. availability :: Windows
1733+
1734+
1735+ .. _ctypes-listing-loaded-shared-libraries :
1736+
1737+ Listing loaded shared libraries
1738+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1739+
1740+ When writing code that relies on code loaded from shared libraries, it can be
1741+ useful to know which shared libraries have already been loaded into the current
1742+ process.
1743+
1744+ The :mod: `!ctypes.util ` module provides the :func: `~ctypes.util.dllist ` function,
1745+ which calls the different APIs provided by the various platforms to help determine
1746+ which shared libraries have already been loaded into the current process.
1747+
1748+ The exact output of this function will be system dependent. On most platforms,
1749+ the first entry of this list represents the current process itself, which may
1750+ be an empty string.
1751+ For example, on glibc-based Linux, the return may look like::
1752+
1753+ >>> from ctypes.util import dllist
1754+ >>> dllist()
1755+ ['', 'linux-vdso.so.1', '/lib/x86_64-linux-gnu/libm.so.6', '/lib/x86_64-linux-gnu/libc.so.6', ... ]
1756+
17201757.. _ctypes-foreign-functions :
17211758
17221759Foreign functions
@@ -2139,33 +2176,6 @@ Utility functions
21392176 .. availability :: Windows
21402177
21412178
2142- .. function :: find_library(name)
2143- :module: ctypes.util
2144-
2145- Try to find a library and return a pathname. *name * is the library name
2146- without any prefix like ``lib ``, suffix like ``.so ``, ``.dylib `` or version
2147- number (this is the form used for the posix linker option :option: `!-l `). If
2148- no library can be found, returns ``None ``.
2149-
2150- The exact functionality is system dependent.
2151-
2152- See :ref: `ctypes-finding-shared-libraries ` for complete documentation.
2153-
2154-
2155- .. function :: find_msvcrt()
2156- :module: ctypes.util
2157-
2158- Returns the filename of the VC runtime library used by Python,
2159- and by the extension modules. If the name of the library cannot be
2160- determined, ``None `` is returned.
2161-
2162- If you need to free memory, for example, allocated by an extension module
2163- with a call to the ``free(void *) ``, it is important that you use the
2164- function in the same library that allocated the memory.
2165-
2166- .. availability :: Windows
2167-
2168-
21692179.. function :: dllist()
21702180 :module: ctypes.util
21712181
0 commit comments