https://github.com/python/cpython/commit/5637f4e38a68cd015daa25cce91e902c01c1279b
commit: 5637f4e38a68cd015daa25cce91e902c01c1279b
branch: main
author: Peter Bierma <[email protected]>
committer: ZeroIntensity <[email protected]>
date: 2026-09-27T20:26:46-04:00
summary:

gh-81241: Fix importing `ctypes` from statically linked interpreters (GH-153890)

See astral-sh/python-build-standalone#873.

files:
A Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst
M Doc/library/ctypes.rst
M Lib/ctypes/__init__.py

diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst
index 1d33f593fffe943..35658aeba705056 100644
--- a/Doc/library/ctypes.rst
+++ b/Doc/library/ctypes.rst
@@ -1755,6 +1755,9 @@ These prefabricated library loaders are available:
       :c:expr:`int`, which is of course not always the truth, so you have to 
assign
       the correct :attr:`!restype` attribute to use these functions.
 
+      Note that if the Python interpreter is statically linked, this will be
+      ``None``, as ``dlopen`` is not possible in this case.
+
 .. audit-event:: ctypes.dlopen name ctypes.LibraryLoader
 
    Loading a library through any of these objects raises an
diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py
index 2e6893e3b8e513e..8b7089b6e578a9d 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -561,7 +561,10 @@ def LoadLibrary(self, name):
 elif _sys.platform == "cygwin":
     pythonapi = PyDLL(_sysconfig.get_config_var("DLLLIBRARY"))
 else:
-    pythonapi = PyDLL(None)
+    try:
+        pythonapi = PyDLL(None)
+    except OSError:
+        pythonapi = None
 
 
 if _os.name == "nt":
diff --git 
a/Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst 
b/Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst
new file mode 100644
index 000000000000000..6110b7404a811e4
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-18-04-09-44.gh-issue-81241.gGRbBr.rst
@@ -0,0 +1,2 @@
+Fix :exc:`OSError` when importing :mod:`ctypes` from a statically linked
+interpreter.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to