cpython/Lib
Miss Islington (bot) 65ed1b7429
[3.13] gh-117983: Defer import of threading for lazy module loading (GH-120233) (GH-121349)
gh-117983: Defer import of threading for lazy module loading (GH-120233)

As noted in gh-117983, the import importlib.util can be triggered at
interpreter startup under some circumstances, so adding threading makes
it a potentially obligatory load.
Lazy loading is not used in the stdlib, so this removes an unnecessary
load for the majority of users and slightly increases the cost of the
first lazily loaded module.

An obligatory threading load breaks gevent, which monkeypatches the
stdlib. Although unsupported, there doesn't seem to be an offsetting
benefit to breaking their use case.

For reference, here are benchmarks for the current main branch:

```
❯ hyperfine -w 8 './python -c "import importlib.util"'
Benchmark 1: ./python -c "import importlib.util"
  Time (mean ± σ):       9.7 ms ±   0.7 ms    [User: 7.7 ms, System: 1.8 ms]
  Range (min … max):     8.4 ms …  13.1 ms    313 runs
```

And with this patch:

```
❯ hyperfine -w 8 './python -c "import importlib.util"'
Benchmark 1: ./python -c "import importlib.util"
  Time (mean ± σ):       8.4 ms ±   0.7 ms    [User: 6.8 ms, System: 1.4 ms]
  Range (min … max):     7.2 ms …  11.7 ms    352 runs
```

Compare to:

```
❯ hyperfine -w 8 './python -c pass'
Benchmark 1: ./python -c pass
  Time (mean ± σ):       7.6 ms ±   0.6 ms    [User: 5.9 ms, System: 1.6 ms]
  Range (min … max):     6.7 ms …  11.3 ms    390 runs
```

This roughly halves the import time of importlib.util.
(cherry picked from commit 94f50f8ee6)

Co-authored-by: Chris Markiewicz <effigies@gmail.com>
2024-07-03 21:14:42 +00:00
..
__phello__
_pyrepl [3.13] gh-118908: Use __main__ for the default PyREPL namespace (GH-121054) (#121059) 2024-06-26 19:25:38 +00:00
asyncio [3.13] gh-87744: fix waitpid race while calling send_signal in asyncio (GH-121126) (#121194) 2024-07-01 05:11:47 +00:00
collections
concurrent [3.13] gh-120121: Add InvalidStateError to concurrent.futures.__all__ (GH-120123) (#120273) 2024-06-08 16:35:10 +00:00
ctypes
curses
dbm [3.13] gh-120417: Remove unused imports in the stdlib (GH-120420) (#120429) 2024-06-12 19:27:32 +00:00
email [3.13] gh-118643: Fix AttributeError in the email module (GH-119099) (GH-119389) 2024-05-22 14:07:38 +03:00
encodings
ensurepip [3.13] gh-120888: Bump bundled pip to 24.1.1 (GH-120889) (#121080) 2024-06-27 12:53:01 +02:00
html
http gh-66543: Add mimetypes.guess_file_type() (GH-117258) 2024-05-06 15:50:52 +03:00
idlelib [3.13] gh-121008: Fix idlelib.run tests (GH-121046) (#121049) 2024-06-26 14:08:31 +00:00
importlib [3.13] gh-117983: Defer import of threading for lazy module loading (GH-120233) (GH-121349) 2024-07-03 21:14:42 +00:00
json gh-95382: Improve performance of json encoder with indent (GH-118105) 2024-05-06 11:04:39 +03:00
logging [3.13] gh-105623 Fix performance degradation in logging RotatingFileHandler (GH-105887) (GH-121117) 2024-06-28 09:23:38 +00:00
multiprocessing gh-83856: Honor atexit for all multiprocessing start methods (GH-114279) 2024-05-03 11:45:46 -07:00
pathlib [3.13] GH-119169: Implement `pathlib.Path.walk()` using `os.walk()` (GH-119573) (#119750) 2024-05-29 21:24:42 +00:00
pydoc_data Python 3.13.0b3 2024-06-27 15:06:51 +02:00
re
site-packages
sqlite3 gh-118221: Always use the default row factory in sqlite3.iterdump() (#118223) 2024-04-25 10:11:45 +02:00
sysconfig gh-116622: Android sysconfig updates (#118352) 2024-05-01 16:47:54 +00:00
test [3.13] gh-118714: Make the pdb post-mortem restart/quit behavior more reasonable (GH-118725) (#121346) 2024-07-03 12:15:53 -07:00
tkinter [3.13] gh-120211: Fix tkinter.ttk with Tcl/Tk 9.0 (GH-120213) (GH-120215) 2024-06-07 15:00:30 +00:00
tomllib
turtledemo [3.13] gh-120633: Move scrollbar and remove tear-off menus in turtledemo (GH-120634) (#120725) 2024-06-19 06:46:13 +00:00
unittest [3.13] gh-120732: Fix `name` passing to `Mock`, when using kwargs to `create_autospec` (GH-120737) (#120760) 2024-06-19 20:59:28 +00:00
urllib [3.13] gh-67693: Fix urlunparse() and urlunsplit() for URIs with path starting with multiple slashes and no authority (GH-113563) (GH-119023) 2024-05-14 09:47:05 +00:00
venv
wsgiref
xml [3.13] gh-119577: Adjust DeprecationWarning when testing element truth values in ElementTree (GH-119762) (GH-120189) 2024-06-07 03:43:17 +00:00
xmlrpc
zipfile [3.13] gh-119588: Implement zipfile.Path.is_symlink (zipp 3.19.0). (GH-119591) (#119985) 2024-06-04 10:56:06 -04:00
zoneinfo
__future__.py
__hello__.py
_aix_support.py
_android_support.py gh-116622: Redirect stdout and stderr to system log when embedded in an Android app (#118063) 2024-04-30 16:00:31 +02:00
_collections_abc.py
_colorize.py gh-117225: Move colorize functionality to own internal module (#118283) 2024-05-01 12:27:06 -06:00
_compat_pickle.py
_compression.py
_ios_support.py [3.13] gh-119253: use ImportError in _ios_support (GH-119254) (#119265) 2024-05-20 22:21:28 +00:00
_markupbase.py
_opcode_metadata.py GH-118095: Use broader specializations of CALL in tier 1, for better tier 2 support of calls. (GH-118322) 2024-05-04 12:11:11 +01:00
_osx_support.py
_py_abc.py
_pydatetime.py [3.13] gh-120713: Normalize year with century for datetime.strftime (GH-120820) (GH-121144) 2024-06-29 06:57:33 +00:00
_pydecimal.py gh-118164: str(10**10000) hangs if the C _decimal module is missing (#118503) 2024-05-04 18:22:33 -05:00
_pyio.py
_pylong.py gh-118610: Centralize power caching in `_pylong.py` (#118611) 2024-05-07 19:09:09 -05:00
_sitebuiltins.py
_strptime.py
_threading_local.py
_weakrefset.py
abc.py
antigravity.py
argparse.py [3.13] gh-121018: Fix more cases of exiting in argparse when exit_on_error=False (GH-121056) (GH-121128) 2024-06-28 15:04:44 +00:00
ast.py gh-116126: Implement PEP 696 (#116129) 2024-05-03 06:17:32 -07:00
base64.py [3.13] Clarify base64.a85encode docs: *wrapcols* doesn't count the newline (GH-119409) (GH-119483) 2024-05-28 16:55:37 +02:00
bdb.py [3.13] gh-58933: Make pdb return to caller frame correctly when f_trace is not set (GH-118979) (#119007) 2024-05-13 20:18:03 +01:00
bisect.py
bz2.py
cProfile.py
calendar.py [3.13] gh-120567: Clarify weekday return in calendar.monthrange docstring (GH-120570) (#120597) 2024-06-16 21:09:54 +00:00
cmd.py
code.py [3.13] gh-118893: Evaluate all statements in the new REPL separately (GH-119318) (#119408) 2024-05-22 16:22:01 +00:00
codecs.py
codeop.py [3.13] gh-119521: Rename IncompleteInputError to _IncompleteInputError and remove from public API/ABI (GH-119680, GH-120955) (GH-120944) 2024-06-24 20:23:30 +02:00
colorsys.py
compileall.py
configparser.py
contextlib.py
contextvars.py
copy.py [3.13] gh-121300: Add `replace` to `copy.__all__` (GH-121302) (#121337) 2024-07-03 15:28:57 +00:00
copyreg.py
csv.py
dataclasses.py [3.13] gh-120417: Remove unused imports in the stdlib (GH-120420) (#120429) 2024-06-12 19:27:32 +00:00
datetime.py
decimal.py
difflib.py
dis.py gh-118335: Configure Tier 2 interpreter at build time (#118339) 2024-04-30 18:26:34 -07:00
doctest.py gh-117225: Move colorize functionality to own internal module (#118283) 2024-05-01 12:27:06 -06:00
enum.py gh-118650: Exclude `_repr_*` methods from Enum's _sunder_ reservation (GH-118651) 2024-05-07 12:35:51 +02:00
filecmp.py
fileinput.py
fnmatch.py
fractions.py
ftplib.py
functools.py [3.13] gh-121027: Add a future warning in functools.partial.__get__ (GH-121086) (#121092) 2024-06-27 12:13:01 +00:00
genericpath.py
getopt.py
getpass.py
gettext.py
glob.py [3.13] GH-119169: Implement `pathlib.Path.walk()` using `os.walk()` (GH-119573) (#119750) 2024-05-29 21:24:42 +00:00
graphlib.py
gzip.py [3.13] gh-112346: Always set OS byte to 255, simpler gzip.compress function. (GH-120486) (#120563) 2024-06-15 19:10:50 +00:00
hashlib.py
heapq.py
hmac.py
imaplib.py
inspect.py [3.13] gh-121027: Add a future warning in functools.partial.__get__ (GH-121086) (#121092) 2024-06-27 12:13:01 +00:00
io.py
ipaddress.py [3.13] gh-120128: fix description of argument to ipaddress.collapse_addresses() (GH-120131) (#120135) 2024-06-12 09:23:24 +03:00
keyword.py
linecache.py
locale.py
lzma.py
mailbox.py
mimetypes.py gh-66543: Add mimetypes.guess_file_type() (GH-117258) 2024-05-06 15:50:52 +03:00
modulefinder.py
netrc.py
ntpath.py [3.13] gh-120417: Remove unused imports in the stdlib (GH-120420) (#120429) 2024-06-12 19:27:32 +00:00
nturl2path.py
numbers.py
opcode.py
operator.py gh-118285: Fix signatures of operator.{attrgetter,itemgetter,methodcaller} instances (GH-118316) 2024-04-29 19:30:48 +03:00
optparse.py
os.py [3.13] GH-89727: Fix FD leak on `os.fwalk()` generator finalization. (GH-119766) (#119767) 2024-05-30 04:40:21 +00:00
pdb.py [3.13] gh-118714: Make the pdb post-mortem restart/quit behavior more reasonable (GH-118725) (#121346) 2024-07-03 12:15:53 -07:00
pickle.py [3.13] gh-120380: fix Python implementation of `pickle.Pickler` for `bytes` and `bytearray` objects in protocol version 5. (GH-120422) (GH-120832) 2024-06-26 12:11:28 +02:00
pickletools.py
pkgutil.py
platform.py [3.13] gh-119132: Update sys.version to identify free-threaded or not. (gh-119134) (#119153) 2024-05-18 16:49:11 -04:00
plistlib.py
poplib.py
posixpath.py gh-118263: Add additional arguments to path_t (Argument Clinic type) in posixmodule (GH-118355) 2024-05-24 18:26:06 +00:00
pprint.py
profile.py
pstats.py
pty.py
py_compile.py
pyclbr.py
pydoc.py [3.13] gh-120541: Improve the "less" prompt in pydoc (GH-120543) (GH-120562) 2024-06-15 18:21:13 +00:00
queue.py
quopri.py
random.py gh-118131: Command-line interface for the `random` module (#118132) 2024-05-05 06:30:03 +00:00
reprlib.py
rlcompleter.py [3.13] gh-113978: Ignore warnings on text completion inside REPL (GH-113979) (#119429) 2024-05-22 23:13:32 +02:00
runpy.py
sched.py
secrets.py
selectors.py
shelve.py
shlex.py
shutil.py [3.13] GH-89727: Fix `shutil.rmtree()` recursion error on deep trees (GH-119808) (#119918) 2024-06-01 20:38:55 +01:00
signal.py
site.py [3.13] gh-121245: Correct pyrepl import in site.py (GH-121255) (#121261) 2024-07-02 13:40:05 +02:00
smtplib.py
socket.py [3.13] gh-110383: Document `socket.makefile()` accepts combined modes (GH-119150) (#119324) 2024-05-21 18:44:53 +00:00
socketserver.py
sre_compile.py
sre_constants.py
sre_parse.py
ssl.py
stat.py [3.13] gh-120417: Remove unused imports in the stdlib (GH-120420) (#120429) 2024-06-12 19:27:32 +00:00
statistics.py Fix negative bandwidth test and add online code path test. (gh-118600) 2024-05-05 12:29:23 -05:00
string.py
stringprep.py
struct.py
subprocess.py [3.13] GH-118844: Fix build failures when combining --disable-gil with --enable-experimental-jit (GH-118959) 2024-05-13 04:53:30 +00:00
symtable.py [3.13] gh-119698: fix `symtable.Class.get_methods` and document its behaviour correctly (GH-120151) (#120777) 2024-06-19 22:27:23 -07:00
tabnanny.py [3.13] gh-120495: Fix incorrect exception handling in Tab Nanny (GH-120498) (#120548) 2024-06-15 11:28:28 +00:00
tarfile.py
tempfile.py
textwrap.py
this.py
threading.py
timeit.py
token.py
tokenize.py
trace.py [3.13] gh-103956: Fix `trace` output in case of missing source line (GH-103958) (GH-118834) 2024-05-09 13:05:37 +00:00
traceback.py [3.13] gh-99180: Make `StackSummary.should_show_carets` private (GH-119554) (#119556) 2024-05-25 17:30:57 +00:00
tracemalloc.py
tty.py
turtle.py
types.py
typing.py [3.13] gh-114053: Fix another edge case involving `get_type_hints`, PEP 695 and PEP 563 (GH-120272) (#121003) 2024-06-25 16:30:08 +00:00
uuid.py
warnings.py
wave.py
weakref.py
webbrowser.py
zipapp.py
zipimport.py [3.13] Remove references to private symbols from zipimport module docstring (GH-119071) 2024-05-15 16:52:48 +00:00