https://github.com/python/cpython/commit/042b49246b0832f9eafc6529566c9a31324c46f7
commit: 042b49246b0832f9eafc6529566c9a31324c46f7
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2025-02-16T18:56:18Z
summary:

[3.12] gh-118761: Revert "Improve import time of `subprocess` (GH-129427)" 
(GH-130201) (#130205)

gh-118761: Revert "Improve import time of `subprocess` (GH-129427)" (GH-130201)

* Revert "gh-118761: Improve import time of `subprocess` (GH-129427)"

This reverts commit 49f24650e4541456872490ec2b59d6d186204891.
Also known as f502c8f6a6db4be27c97a0e5466383d117859b7f in 3.13 (PR GH-129447)
Also known as f65aa0d1bf7b636ab8f9d226429205854b24cd7a in 3.12 (PR GH-129448)

This caused bugs in the `__del__` finalizer:
 https://github.com/python/cpython/issues/118761#issuecomment-2661504264

---------
(cherry picked from commit ae3064608935367c860182dc1b364631082ecdff)

Co-authored-by: Gregory P. Smith <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-02-16-10-12-27.gh-issue-118761.TNw5ZC.rst
M Lib/subprocess.py

diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 3ec39ca3e61e7a..881a9ce800ae99 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
@@ -136,8 +138,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))
@@ -375,8 +375,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()
 
 
@@ -1657,9 +1655,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:
@@ -1764,9 +1759,6 @@ def _posix_spawn(self, args, executable, env, 
restore_signals,
 
             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'):
@@ -2216,13 +2208,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)
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..198fd0c28f4f3c
--- /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``.

_______________________________________________
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