8000 gh-118761: Improve import time of `subprocess` (GH-129427) · python/cpython@49f2465 · GitHub
[go: up one dir, main page]

Skip to content

Commit 49f2465

Browse files
authored
gh-118761: Improve import time of subprocess (GH-129427)
* subprocess: lazy import signal and locale to improve module import time
1 parent 5ff2fbc commit 49f2465

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

Lib/subprocess.py

Lines changed: 14 additions & 2 deletions
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
@@ -144,6 +142,8 @@ def __init__(self, returncode, cmd, output=None, stderr=None):
144142

145143
def __str__(self):
146144
if self.returncode and self.returncode < 0:
145+
# Lazy import to improve module import time
146+
import signal
147147
try:
148148
return "Command '%s' died with %r." % (
149149
self.cmd, signal.Signals(-self.returncode))
@@ -381,6 +381,8 @@ def _text_encoding():
381381
if sys.flags.utf8_mode:
382382
return "utf-8"
383383
else:
384+
# Lazy import to improve module import time
385+
import locale
384386
return locale.getencoding()
385387

386388

@@ -1664,6 +1666,9 @@ def send_signal(self, sig):
16641666
# Don't signal a process that we know has already died.
16651667
if self.returncode is not None:
16661668
return
1669+
1670+
# Lazy import to improve module import time
1671+
import signal
16671672
if sig == signal.SIGTERM:
16681673
self.terminate()
16691674
elif sig == signal.CTRL_C_EVENT:
@@ -1765,6 +1770,9 @@ def _posix_spawn(self, args, executable, env, restore_signals, close_fds,
17651770
"""Execute program using os.posix_spawn()."""
17661771
kwargs = {}
17671772
if restore_signals:
1773+
# Lazy import to improve module import time
1774+
import signal
1775+
17681776
# See _Py_RestoreSignals() in Python/pylifecycle.c
17691777
sigset = []
17701778
for signame in ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ'):
@@ -2214,9 +2222,13 @@ def send_signal(self, sig):
22142222
def terminate(self):
22152223
"""Terminate the process with SIGTERM
22162224
"""
2225+
# Lazy import to improve module import time
2226+
import signal
22172227
self.send_signal(signal.SIGTERM)
22182228

22192229
def kill(self):
22202230
"""Kill the process with SIGKILL
22212231
"""
2232+
# Lazy import to improve module import time
2233+
import signal
22222234
self.send_signal(signal.SIGKILL)
Lines changed: 2 additions & 0 deletions
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)
0