Skip to content

Commit 47f0473

Browse files
ZeroIntensitymiss-islington
authored andcommitted
gh-81241: Fix importing ctypes from statically linked interpreters (GH-153890)
See astral-sh/python-build-standalone#873. (cherry picked from commit 5637f4e) Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent 5985fcb commit 47f0473

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎Doc/library/ctypes.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,9 @@ These prefabricated library loaders are available:
17041704
:c:expr:`int`, which is of course not always the truth, so you have to assign
17051705
the correct :attr:`!restype` attribute to use these functions.
17061706

1707+
Note that if the Python interpreter is statically linked, this will be
1708+
``None``, as ``dlopen`` is not possible in this case.
1709+
17071710
.. audit-event:: ctypes.dlopen name ctypes.LibraryLoader
17081711

17091712
Loading a library through any of these objects raises an

‎Lib/ctypes/__init__.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,10 @@ def LoadLibrary(self, name):
553553
# These are Unix-like platforms which use a dynamically-linked libpython.
554554
pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
555555
else:
556-
pythonapi = PyDLL(None)
556+
try:
557+
pythonapi = PyDLL(None)
558+
except OSError:
559+
pythonapi = None
557560

558561

559562
if _os.name == "nt":
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :exc:`OSError` when importing :mod:`ctypes` from a statically linked
2+
interpreter.

0 commit comments

Comments
 (0)