|
43 | 43 | import builtins |
44 | 44 | import errno |
45 | 45 | import io |
46 | | -import locale |
47 | 46 | import os |
48 | 47 | import time |
49 | | -import signal |
50 | 48 | import sys |
51 | 49 | import threading |
52 | 50 | import warnings |
@@ -144,6 +142,8 @@ def __init__(self, returncode, cmd, output=None, stderr=None): |
144 | 142 |
|
145 | 143 | def __str__(self): |
146 | 144 | if self.returncode and self.returncode < 0: |
| 145 | + # Lazy import to improve module import time |
| 146 | + import signal |
147 | 147 | try: |
148 | 148 | return "Command '%s' died with %r." % ( |
149 | 149 | self.cmd, signal.Signals(-self.returncode)) |
@@ -381,6 +381,8 @@ def _text_encoding(): |
381 | 381 | if sys.flags.utf8_mode: |
382 | 382 | return "utf-8" |
383 | 383 | else: |
| 384 | + # Lazy import to improve module import time |
| 385 | + import locale |
384 | 386 | return locale.getencoding() |
385 | 387 |
|
386 | 388 |
|
@@ -1664,6 +1666,9 @@ def send_signal(self, sig): |
1664 | 1666 | # Don't signal a process that we know has already died. |
1665 | 1667 | if self.returncode is not None: |
1666 | 1668 | return |
| 1669 | + |
| 1670 | + # Lazy import to improve module import time |
| 1671 | + import signal |
1667 | 1672 | if sig == signal.SIGTERM: |
1668 | 1673 | self.terminate() |
1669 | 1674 | elif sig == signal.CTRL_C_EVENT: |
@@ -1765,6 +1770,9 @@ def _posix_spawn(self, args, executable, env, restore_signals, close_fds, |
1765 | 1770 | """Execute program using os.posix_spawn().""" |
1766 | 1771 | kwargs = {} |
1767 | 1772 | if restore_signals: |
| 1773 | + # Lazy import to improve module import time |
| 1774 | + import signal |
| 1775 | + |
1768 | 1776 | # See _Py_RestoreSignals() in Python/pylifecycle.c |
1769 | 1777 | sigset = [] |
1770 | 1778 | for signame in ('SIGPIPE', 'SIGXFZ', 'SIGXFSZ'): |
@@ -2214,9 +2222,13 @@ def send_signal(self, sig): |
2214 | 2222 | def terminate(self): |
2215 | 2223 | """Terminate the process with SIGTERM |
2216 | 2224 | """ |
| 2225 | + # Lazy import to improve module import time |
| 2226 | + import signal |
2217 | 2227 | self.send_signal(signal.SIGTERM) |
2218 | 2228 |
|
2219 | 2229 | def kill(self): |
2220 | 2230 | """Kill the process with SIGKILL |
2221 | 2231 | """ |
| 2232 | + # Lazy import to improve module import time |
| 2233 | + import signal |
2222 | 2234 | self.send_signal(signal.SIGKILL) |
0 commit comments