From 1ed0ec674efe63912865183c0a1c66cc7d193fda Mon Sep 17 00:00:00 2001 From: Jonathan Dung Date: Sat, 28 Mar 2026 11:21:41 +0800 Subject: [PATCH 1/6] Load sysconfig only when needed --- Lib/ctypes/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 1c822759eca912..6db67afb597b1e 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -2,7 +2,6 @@ import os as _os import sys as _sys -import sysconfig as _sysconfig import types as _types from _ctypes import Union, Structure, Array @@ -550,7 +549,9 @@ def LoadLibrary(self, name): pythonapi = PyDLL("python dll", None, _sys.dllhandle) elif _sys.platform in ["android", "cygwin"]: # These are Unix-like platforms which use a dynamically-linked libpython. - pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY")) + import sysconfig # delay import + pythonapi = PyDLL(sysconfig.get_config_var("LDLIBRARY")) + del sysconfig else: pythonapi = PyDLL(None) From 3fae642c7924e8c2da9008879bdaee2c34e57a14 Mon Sep 17 00:00:00 2001 From: Jonathan Dung Date: Sat, 4 Apr 2026 21:01:08 +0800 Subject: [PATCH 2/6] Integrate @encukou's suggestion to use lazy imports in ctypes --- Lib/ctypes/util.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 3b21658433b2ed..35ac5b6bfd6a37 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -1,8 +1,9 @@ import os -import shutil -import subprocess import sys +lazy import shutil +lazy import subprocess + # find_library(name) returns the pathname of a library, or None. if os.name == "nt": From 3c82f0e3b948fb15eec4bed89f9a728158669dc6 Mon Sep 17 00:00:00 2001 From: Jonathan Dung Date: Sat, 4 Apr 2026 21:07:08 +0800 Subject: [PATCH 3/6] Change warnings module import in ctypes._layout to lazy --- Lib/ctypes/_layout.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/ctypes/_layout.py b/Lib/ctypes/_layout.py index 2048ccb6a1c93f..61b4c6c70fb875 100644 --- a/Lib/ctypes/_layout.py +++ b/Lib/ctypes/_layout.py @@ -5,11 +5,12 @@ """ import sys -import warnings from _ctypes import CField, buffer_info import ctypes +lazy import warnings + def round_down(n, multiple): assert n >= 0 assert multiple > 0 From bfcbc3d438a0023fbd972ef12547ac18c742fbf5 Mon Sep 17 00:00:00 2001 From: Jonathan Dung Date: Mon, 6 Apr 2026 21:48:57 +0800 Subject: [PATCH 4/6] Change `sysconfig` import to lazy as well --- Lib/ctypes/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 6db67afb597b1e..6d7cb56f6c354e 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -4,6 +4,8 @@ import sys as _sys import types as _types +lazy import sysconfig as _sysconfig + from _ctypes import Union, Structure, Array from _ctypes import _Pointer from _ctypes import CFuncPtr as _CFuncPtr @@ -549,9 +551,7 @@ def LoadLibrary(self, name): pythonapi = PyDLL("python dll", None, _sys.dllhandle) elif _sys.platform in ["android", "cygwin"]: # These are Unix-like platforms which use a dynamically-linked libpython. - import sysconfig # delay import - pythonapi = PyDLL(sysconfig.get_config_var("LDLIBRARY")) - del sysconfig + pythonapi = PyDLL(_sysconfig.get_config_var("LDLIBRARY")) else: pythonapi = PyDLL(None) From 3fbaf2c9113f63886185bc49ef2876278158c698 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 6 Apr 2026 13:51:53 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst diff --git a/Misc/NEWS.d/next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst b/Misc/NEWS.d/next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst new file mode 100644 index 00000000000000..16ddef74114fb4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst @@ -0,0 +1 @@ +Use lazy imports for :mod:`shutil`, :mod:`warnings`, :mod:`sysconfig` and :mod:`subprocess` in :mod:`ctypes` and submodules. Contributed by Jonathan Dung. From 1e27106a1e3f647541f0f6756d50a9e6bef0b1cc Mon Sep 17 00:00:00 2001 From: Jonathan Dung Date: Tue, 7 Apr 2026 20:52:56 +0800 Subject: [PATCH 6/6] Delete news entry --- .../next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst diff --git a/Misc/NEWS.d/next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst b/Misc/NEWS.d/next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst deleted file mode 100644 index 16ddef74114fb4..00000000000000 --- a/Misc/NEWS.d/next/Library/2026-04-06-13-51-51.gh-issue-146547.7jw-eL.rst +++ /dev/null @@ -1 +0,0 @@ -Use lazy imports for :mod:`shutil`, :mod:`warnings`, :mod:`sysconfig` and :mod:`subprocess` in :mod:`ctypes` and submodules. Contributed by Jonathan Dung.