https://github.com/python/cpython/commit/24ddd48a2a89e9f3de1245db9b86b6a80ae2d0d2
commit: 24ddd48a2a89e9f3de1245db9b86b6a80ae2d0d2
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: ZeroIntensity <[email protected]>
date: 2026-09-28T00:52:58Z
summary:

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

See astral-sh/python-build-standalone#873.
(cherry picked from commit 5637f4e38a68cd015daa25cce91e902c01c1279b)

Co-authored-by: Peter Bierma <[email protected]>

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 e39e398b85151fc..94d33ab9b6a4c34 100644
--- a/Doc/library/ctypes.rst
+++ b/Doc/library/ctypes.rst
@@ -1803,6 +1803,9 @@ object is 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 7223fc499773aa6..1aeb21eec7e0874 100644
--- a/Lib/ctypes/__init__.py
+++ b/Lib/ctypes/__init__.py
@@ -564,7 +564,10 @@ def LoadLibrary(self, name):
     # These are Unix-like platforms which use a dynamically-linked libpython.
     pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY"))
 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