Skip to content

Commit f65aa0d

Browse files
[3.12] gh-118761: Improve import time of subprocess (GH-129427) (#129448)
gh-118761: Improve import time of `subprocess` (GH-129427) * subprocess: lazy import signal and locale to improve module import time (cherry picked from commit 49f2465) Co-authored-by: Taneli Hukkinen <[email protected]>
1 parent 0e54315 commit f65aa0d

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Diff for: Lib/subprocess.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@
4343
import builtins
4444
import errno
4545
import io
46-
import locale
4746
import os
4847
import time
49-
import signal
5048
import sys
5149
import threading
5250
import warnings
@@ -138,6 +136,8 @@ def __init__(self, returncode, cmd, output=None, stderr=None):
138136

139137
def __str__(self):
140138
if self.returncode and self.returncode < 0:
139+
# Lazy import to improve module import time
140+
import signal
141141
try:
142142
return "Command '%s' died with %r." % (
143143
self.cmd, signal.Signals(-self.returncode))
@@ -375,6 +375,8 @@ def _text_encoding():
375375
if sys.flags.utf8_mode:
376376
return "utf-8"
377377
else:
378+
# Lazy import to improve module import time
379+
import locale
378380
return locale.getencoding()
379381

380382

@@ -1655,6 +1657,9 @@ def send_signal(self, sig):
16551657
# Don't signal a process that we know has already died.
16561658
if self.returncode is not None:
16571659
return
1660+
1661+
# Lazy import to improve module import time
1662+
import signal
16581663
if sig == signal.SIGTERM:
16591664
self.terminate()
16601665
elif sig == signal.CTRL_C_EVENT:
@@ -1759,6 +1764,9 @@ def _posix_spawn(self, args, executable, env, restore_signals,
17591764

17601765
kwargs = {}
17611766
if restore_signals:
1767+
# Lazy import to improve module import time
1768+
import signal
1769+
17621770
# See _Py_RestoreSignals() in Python/pylifecycle.c
17631771
sigset = []
17641772
for signame in ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ'):
@@ -2208,9 +2216,13 @@ def send_signal(self, sig):
22082216
def terminate(self):
22092217
"""Terminate the process with SIGTERM
22102218
"""
2219+
# Lazy import to improve module import time
2220+
import signal
22112221
self.send_signal(signal.SIGTERM)
22122222

22132223
def kill(self):
22142224
"""Kill the process with SIGKILL
22152225
"""
2226+
# Lazy import to improve module import time
2227+
import signal
22162228
self.send_signal(signal.SIGKILL)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve import time of :mod:`subprocess` by lazy importing ``locale`` and
2+
``signal``. Patch by Taneli Hukkinen.

0 commit comments

Comments
 (0)