https://github.com/python/cpython/commit/e3d8d3bb1893714ed158fc671d8b4a3f50cc85f7 commit: e3d8d3bb1893714ed158fc671d8b4a3f50cc85f7 branch: 3.13 author: Miss Islington (bot) <[email protected]> committer: ZeroIntensity <[email protected]> date: 2026-09-28T00:44:44Z summary:
[3.13] gh-81241: Fix importing `ctypes` from statically linked interpreters (GH-153890) (GH-158306) 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 985472a8d2f76d8..9545b7e0372a33d 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1625,6 +1625,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 7fc1181f25a2643..c430f9faa47dde1 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -492,7 +492,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]
