From 030d35759a50e3355a2c04376e38c5a6af9af862 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 16 Feb 2025 10:08:14 -0800 Subject: [PATCH 1/3] Revert "gh-118761: Improve import time of `subprocess` (GH-129427)" This reverts commit 49f24650e4541456872490ec2b59d6d186204891. This caused bugs in the `__del__` finalizer: https://github.com/python/cpython/issues/118761#issuecomment-2661504264 --- Lib/subprocess.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 1948ac8a3a1dfe..749c728db729ae 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -43,8 +43,10 @@ import builtins import errno import io +import locale import os import time +import signal import sys import threading import warnings @@ -142,8 +144,6 @@ def __init__(self, returncode, cmd, output=None, stderr=None): def __str__(self): if self.returncode and self.returncode < 0: - # Lazy import to improve module import time - import signal try: return "Command '%s' died with %r." % ( self.cmd, signal.Signals(-self.returncode)) @@ -381,8 +381,6 @@ def _text_encoding(): if sys.flags.utf8_mode: return "utf-8" else: - # Lazy import to improve module import time - import locale return locale.getencoding() @@ -1665,9 +1663,6 @@ def send_signal(self, sig): # Don't signal a process that we know has already died. if self.returncode is not None: return - - # Lazy import to improve module import time - import signal if sig == signal.SIGTERM: self.terminate() elif sig == signal.CTRL_C_EVENT: @@ -1769,9 +1764,6 @@ def _posix_spawn(self, args, executable, env, restore_signals, close_fds, """Execute program using os.posix_spawn().""" kwargs = {} if restore_signals: - # Lazy import to improve module import time - import signal - # See _Py_RestoreSignals() in Python/pylifecycle.c sigset = [] for signame in ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ'): @@ -2221,13 +2213,9 @@ def send_signal(self, sig): def terminate(self): """Terminate the process with SIGTERM """ - # Lazy import to improve module import time - import signal self.send_signal(signal.SIGTERM) def kill(self): """Kill the process with SIGKILL """ - # Lazy import to improve module import time - import signal self.send_signal(signal.SIGKILL) From 7e08f86eac6304923427bc3c4cc04890914f82e1 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 16 Feb 2025 10:12:37 -0800 Subject: [PATCH 2/3] NEWS-entry --- .../Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst diff --git a/Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst b/Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst new file mode 100644 index 00000000000000..d6dfaae0e88b9e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst @@ -0,0 +1,4 @@ +Reverts a change in the previous release attempting to make some stdlib +imports used within the :mod:`subprocess` module lazy as this was causing +errors during `__del__` finalizers calling methods such as ``terminate``, or +``kill``, or ``send_signal``. From b51da5c27f02dba941c16d562831522fc1aebed7 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 16 Feb 2025 10:16:02 -0800 Subject: [PATCH 3/3] Update Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst b/Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst index d6dfaae0e88b9e..198fd0c28f4f3c 100644 --- a/Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst +++ b/Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst @@ -1,4 +1,4 @@ Reverts a change in the previous release attempting to make some stdlib imports used within the :mod:`subprocess` module lazy as this was causing -errors during `__del__` finalizers calling methods such as ``terminate``, or +errors during ``__del__`` finalizers calling methods such as ``terminate``, or ``kill``, or ``send_signal``.