Skip to content

Commit 5637f4e

Browse files
gh-81241: Fix importing ctypes from statically linked interpreters (GH-153890)
See astral-sh/python-build-standalone#873.
1 parent 1c4fa4f commit 5637f4e

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
@@ -1755,6 +1755,9 @@ These prefabricated library loaders are available:
17551755
:c:expr:`int`, which is of course not always the truth, so you have to assign
17561756
the correct :attr:`!restype` attribute to use these functions.
17571757

1758+
Note that if the Python interpreter is statically linked, this will be
1759+
``None``, as ``dlopen`` is not possible in this case.
1760+
17581761
.. audit-event:: ctypes.dlopen name ctypes.LibraryLoader
17591762

17601763
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
@@ -561,7 +561,10 @@ def LoadLibrary(self, name):
561561
elif _sys.platform == "cygwin":
562562
pythonapi = PyDLL(_sysconfig.get_config_var("DLLLIBRARY"))
563563
else:
564-
pythonapi = PyDLL(None)
564+
try:
565+
pythonapi = PyDLL(None)
566+
except OSError:
567+
pythonapi = None
565568

566569

567570
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)