[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 49f24650e4)

Co-authored-by: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2025-01-29 17:48:48 +01:00 committed by GitHub
parent 0e54315c31
commit f65aa0d1bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 2 deletions

View file

@ -43,10 +43,8 @@ getstatusoutput(...): Runs a command in the shell, waits for it to complete,
import builtins import builtins
import errno import errno
import io import io
import locale
import os import os
import time import time
import signal
import sys import sys
import threading import threading
import warnings import warnings
@ -138,6 +136,8 @@ class CalledProcessError(SubprocessError):
def __str__(self): def __str__(self):
if self.returncode and self.returncode < 0: if self.returncode and self.returncode < 0:
# Lazy import to improve module import time
import signal
try: try:
return "Command '%s' died with %r." % ( return "Command '%s' died with %r." % (
self.cmd, signal.Signals(-self.returncode)) self.cmd, signal.Signals(-self.returncode))
@ -375,6 +375,8 @@ def _text_encoding():
if sys.flags.utf8_mode: if sys.flags.utf8_mode:
return "utf-8" return "utf-8"
else: else:
# Lazy import to improve module import time
import locale
return locale.getencoding() return locale.getencoding()
@ -1655,6 +1657,9 @@ class Popen:
# Don't signal a process that we know has already died. # Don't signal a process that we know has already died.
if self.returncode is not None: if self.returncode is not None:
return return
# Lazy import to improve module import time
import signal
if sig == signal.SIGTERM: if sig == signal.SIGTERM:
self.terminate() self.terminate()
elif sig == signal.CTRL_C_EVENT: elif sig == signal.CTRL_C_EVENT:
@ -1759,6 +1764,9 @@ class Popen:
kwargs = {} kwargs = {}
if restore_signals: if restore_signals:
# Lazy import to improve module import time
import signal
# See _Py_RestoreSignals() in Python/pylifecycle.c # See _Py_RestoreSignals() in Python/pylifecycle.c
sigset = [] sigset = []
for signame in ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ'): for signame in ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ'):
@ -2208,9 +2216,13 @@ class Popen:
def terminate(self): def terminate(self):
"""Terminate the process with SIGTERM """Terminate the process with SIGTERM
""" """
# Lazy import to improve module import time
import signal
self.send_signal(signal.SIGTERM) self.send_signal(signal.SIGTERM)
def kill(self): def kill(self):
"""Kill the process with SIGKILL """Kill the process with SIGKILL
""" """
# Lazy import to improve module import time
import signal
self.send_signal(signal.SIGKILL) self.send_signal(signal.SIGKILL)

View file

@ -0,0 +1,2 @@
Improve import time of :mod:`subprocess` by lazy importing ``locale`` and
``signal``. Patch by Taneli Hukkinen.