937 Commits
Author SHA1 Message Date
e60e7bdc92 [3.15] gh-159652: Fix import lock leak when the C stack is nearly exhausted (GH-159660) (GH-160779)
gh-159652: Fix import lock leak when the C stack is nearly exhausted (GH-159660)

When an import failed with a stack overflow RecursionError, releasing the import locks on the error path could itself raise RecursionError, because the cleanup runs in a new C frame. This left the global import lock or a module lock held forever, and later imports in other threads blocked indefinitely.

_imp.acquire_lock() now raises RecursionError when there is not enough stack left to release the lock. importlib calls it before taking any import lock, so this covers the per-module locks too. The module lock weakref callback skips cleanup in that case instead of
reporting an unraisable exception.
(cherry picked from commit 926b0bf311)

Co-authored-by: Cary Hawkins <[email protected]>
2026-10-27 22:17:27 +00:00
33a05de00a [3.15] gh-157384: Set package context in PyImport_CreateModuleFromInitfunc() (GH-157387) (GH-159316)
A single-phase init function that creates its module using only the
last component of a dotted name (as pybind11 does for submodules) got
a module whose __name__ was the short name.  Set the package context
while calling the init function, as the dynamic loader does, so the
module gets the full name from the spec.
(cherry picked from commit 04862280a2)

Co-authored-by: Itamar Oren <[email protected]>
2026-10-14 13:49:31 +02:00
f8375c23b9 [3.15] gh-158820: Add null checks to avoid segfaults with lazy imports at shutdown (GH-158821) (#159062)
gh-158820: Add null checks to avoid segfaults with lazy imports at shutdown (GH-158821)

Destructors can run after finalization has cleared the interpreter's
lazy_modules set and lazy_pending_submodules dict. Declaring or
resolving a lazy import from one of them passed NULL to PySet_Add(),
PySet_Discard() or the pending-submodules lookup.

Treat the bookkeeping as a no-op once the state is gone, as the other
readers of this state already do.
(cherry picked from commit a6e636148e)

Co-authored-by: Brittany Reynoso <[email protected]>
2026-10-09 12:14:23 -07:00
03bb26f89e [3.15] gh-158140: Resolve lazily imported sibling submodules independently (GH-158521) (#158613)
Co-authored-by: Pablo Galindo Salgado <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: T. Wouters <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>
2026-10-02 18:48:03 +00:00
c2eb1a8591 [3.15] gh-155695: Remove resolved names from sys.lazy_modules more consistently (GH-157714) (#158607)
gh-155695: Remove resolved names from sys.lazy_modules more consistently (GH-157714)
(cherry picked from commit be87a85e2f)

Co-authored-by: Brittany Reynoso <[email protected]>
Co-authored-by: Pablo Galindo Salgado <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
2026-10-02 19:04:35 +02:00
021f634ed8 [3.15] gh-142349: Simplify lazy import resolution (GH-158282) (#158569)
Backport the shared lazy import resolver to Python 3.15, regenerate
interpreter files, and update the ABI snapshot.

Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>
2026-10-01 16:56:39 +00:00
1a2d24e3bf [3.15] gh-157757: Fix to make lazy import a.b as c import the module a.b (GH-158092) (#158257)
gh-157757: Fix to make `lazy import a.b as c` import the module `a.b` (#158092)

* Import the module a lazy `import a.b as c` names

`import a.b as c` compiles to `IMPORT_NAME a.b` followed by `IMPORT_FROM b`.
Lazily, IMPORT_NAME leaves a placeholder holding "a.b", and IMPORT_FROM
rewrote it into the placeholder `lazy from a import b` produces.  Reification
then imported `a` alone and read `b` off it, so the module `a.b` was never
imported under its own name: an attribute of the package shadowing it answered
instead, and `math.pi`, which no module backs, bound the float where the eager
statement raises ModuleNotFoundError.

Mark the dotted import on the placeholder and keep the whole name on it.
Reification imports that name and then walks its components with IMPORT_FROM,
which is what the eager statement does.

The test pinning `lazy import math.pi as pi` as working is inverted, since the
eager statement raises.

* Stop excluding test_trace from the lazy-imports-all run

It passes now that a lazy `import a.b as c` imports the module: the
KeyError on 'test.tracedmodules.testmod' came from the submodule never
being imported under its own name.

* Rename lz_submodule to lz_dotted_as and trim the comments

The flag means "bind the whole dotted name, not the root", which the old
name did not say, and import.c already has unrelated lazy_pending_submodules
machinery to be confused with.

* Chain lazy IMPORT_FROM placeholders instead of flagging dotted imports

Each deferred IMPORT_FROM off a placeholder without a fromlist now keeps
the previous placeholder in lz_from and the attribute name in lz_attr.
Reification walks back to the placeholder IMPORT_NAME left, runs that
import, and replays the lookups in order with _PyEval_ImportFrom, which
is what the eager bytecode does.  This drops the lz_dotted_as flag and
also follows a custom __lazy_import__ that returns a placeholder for a
different module name.

* Chain every deferred IMPORT_FROM onto the previous placeholder

`lazy from a import b` now records its lookup the same way as `import
a.b as c`, so a placeholder holds either the module name and fromlist or
the previous placeholder and an attribute name, and reification has a
single path.  The import passes only the name being resolved as the
fromlist, so accessing b still does not import the other names'
submodules.

* Treat an empty fromlist on a lazy import placeholder as no fromlist

__import__("a.b", fromlist=()) returns the top-level package `a`, the same
as fromlist=None, but the placeholder kept the empty tuple.  Every consumer
of lz_attr then read it as a real fromlist: reification narrowed it to the
chained attribute and replayed the lookups on `a.b`, _PyEval_LazyImportFrom
took the attribute off sys.modules["a.b"], and the repr named `a.b.attr`.

_PyLazyImport_New already collapses None to NULL for exactly this reason,
so collapse an empty tuple there too and every site follows.

* gh-157757: Preserve empty fromlists for custom import hooks

---------


(cherry picked from commit 1e8ff18a12)

Co-authored-by: Brittany Reynoso <[email protected]>
2026-09-26 21:13:07 +00:00
e325fae357 [3.15] gh-156126: Fix crash in -X importtime with unencodable module names (GH-156137) (#156259)
Co-authored-by: Kirill Podoprigora <[email protected]>
2026-09-12 21:47:23 +03:00
Brittany ReynosoandGitHub e531dd4b7e [3.15] gh-155194: Fix not raising on non-module import (GH-155189) (#155983) 2026-08-19 11:01:31 +01:00
0cb0723178 [3.15] gh-153236: Propagate lazy submodule import errors (GH-153237) (#153936)
gh-153236: Propagate lazy submodule import errors (GH-153237)
(cherry picked from commit 0a09dafb03)

Co-authored-by: Pablo Galindo Salgado <[email protected]>
2026-07-18 16:04:40 +00:00
9fcebd38b5 [3.15] gh-151644: Fix data race in sys.setdlopenflags/getdlopenflags under free-threading (gh-151768) (#153790)
gh-151644: Fix data race in sys.setdlopenflags/getdlopenflags under free-threading (gh-151768)

In free-threading builds, concurrent calls to sys.getdlopenflags() and
sys.setdlopenflags() race on interp->imports.dlopenflags.  Fix by
using FT_ATOMIC_LOAD_INT_RELAXED / FT_ATOMIC_STORE_INT_RELAXED in
_PyImport_GetDLOpenFlags and _PyImport_SetDLOpenFlags, consistent with
how analogous interpreter-state integer fields (lazy_imports_mode,
pystats_enabled) are protected.

Relaxed ordering is correct here: dlopenflags is a standalone config
integer with no ordering relationship to other memory.
(cherry picked from commit cde31ec135)

Co-authored-by: Jiucheng(Oliver) <[email protected]>
Co-authored-by: Claude Sonnet 4.6 <[email protected]>
2026-07-16 15:53:07 +05:30
2b92f57cac [3.15] GH-151672: __lazy_import__ always resolves to the module being imported (GH-151827) (#152534)
GH-151672: `__lazy_import__` always resolves to the module being imported (GH-151827)
(cherry picked from commit 2d0003c0b2)

Co-authored-by: Brandt Bucher <[email protected]>
2026-06-29 10:23:25 +01:00
5751633fac [3.15] gh-150633: Properly handle null characters in the name when importing frozen modules (GH-150634) (GH-151100)
(cherry picked from commit 54de5475cd)

Co-authored-by: Thomas Kowalski <[email protected]>
2026-06-08 18:58:11 +00:00
acd402ecdb [3.15] gh-150374: Fix double release of import lock in lazy import reification (GH-150376) (#150378)
gh-150374: Fix double release of import lock in lazy import reification (GH-150376)
(cherry picked from commit 5498eba545)

Co-authored-by: pengyu lee <[email protected]>
2026-05-25 09:36:22 +00:00
Serhiy StorchakaandGitHub cf73b17adf [3.15] gh-150285: Fix too long docstrings in Argument Clinic code (GH-150338) (GH-150350)
(cherry picked from commit 287c98f4cb)
2026-05-24 22:31:19 +03:00
Miss Islington (bot)andGitHub ad2f0cb997 [3.15] PEP 810 - Update some error strings (GH-150126) (#150135) 2026-05-20 15:17:28 +00:00
Dino ViehlandandGitHub 6847f4bc60 [3.15] gh-150052: Resolve un-loaded lazily loaded submodules via module.__getattr__ instead of publishing lazy values (#150055) 2026-05-19 21:23:30 +00:00
Dino ViehlandandGitHub 94c8bac2cd [3.15] gh-148587: Make sys.lazy_modules match PEP and keep internal lazy submodules tra… (#150014)
Make sys.lazy_modules match PEP and keep internal lazy submodules tracking internal
2026-05-18 17:53:54 -07:00
508b49845d gh-149044: Implement PEP 820 – PySlot: Unified slot system for the C API (GH-149055)
Co-authored-by: Peter Bierma <[email protected]>
2026-05-05 09:18:04 +02:00
Pablo Galindo SalgadoandGitHub ca960b6f38 gh-148110: Resolve lazy import filter names for relative imports (#148111) 2026-04-06 22:29:02 +01:00
Bartosz SławeckiandGitHub a5b9d60a69 gh-145059: Record lazy modules without submodules in sys.lazy_modules (#146081)
Record simple lazy modules as well
2026-03-27 14:37:11 -07:00
1887a95f51 gh-128341: Use _Py_ABI_SLOT in stdlib modules (#145770)
Rename from _Py_INTERNAL_ABI_SLOT to _Py_ABI_SLOT
and define the macro using _PyABIInfo_DEFAULT.

Use the ABI slot in stdlib extension modules to enable running
a check of ABI version compatibility.

_tkinter, _tracemalloc and readline don't use the slots, hence they need
explicit handling.

Co-authored-by: Victor Stinner <[email protected]>
2026-03-24 17:47:55 +00:00
Victor StinnerandGitHub c0ecf211b2 gh-145055: Accept frozendict for globals in exec() and eval() (#145072) 2026-03-05 12:35:43 +01:00
Jelle ZijlstraandGitHub bd13cc09fa gh-145376: Fix various reference leaks (GH-145377) 2026-03-03 16:23:30 +01:00
Bartosz SławeckiandGitHub 8bcb3eaa67 gh-144851: Fix __lazy_import__ crash with user-defined filters (#144852) 2026-03-02 12:01:32 +01:00
Stan UlbrychandGitHub e1bd0cd37b gh-145058: Add input validation to _PyImport_LazyImportModuleLevelObject (#145068) 2026-02-21 12:52:40 +00:00
46d5106cfa gh-142349: Implement PEP 810 - Explicit lazy imports (#142351)
Co-authored-by: T. Wouters <[email protected] >
Co-authored-by: Brittany Reynoso <[email protected]>
Co-authored-by: Dino Viehland <[email protected]>
2026-02-12 00:15:33 +00:00
ac8b5b6890 gh-143650: Fix importlib race condition on import failure (GH-143651)
Fix a race condition where a thread could receive a partially-initialized
module when another thread's import fails. The race occurs when:

1. Thread 1 starts importing, adds module to sys.modules
2. Thread 2 sees the module in sys.modules via the fast path
3. Thread 1's import fails, removes module from sys.modules
4. Thread 2 returns a stale module reference not in sys.modules

The fix adds verification after the "skip lock" optimization in both Python
and C code paths to check if the module is still in sys.modules. If the
module was removed (due to import failure), we retry the import so the
caller receives the actual exception from the import failure rather than
a stale module reference.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
2026-02-10 14:08:33 +01:00
Peter BiermaandGitHub fd6b639a49 gh-144601: Avoid sharing exception objects raised in a PyInit function across multiple interpreters (GH-144602) 2026-02-09 16:57:03 +01:00
stratakisandGitHub 6a4f10325d gh-142776: Ensure fp file descriptor is closed on all code paths in import.c (GH-142777) 2025-12-19 10:14:52 -08:00
cd2ca74c53 gh-142029: Raise ModuleNotFoundError instead of crashing on nonexsistent module name given to create_builtin() (#142054)
Co-authored-by: Brett Cannon <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
2025-12-15 13:20:19 +01:00
Yongtao HuangandGitHub e90e6d155a gh-142666: Remove unused variable package in import logic (GH-142667)
The variable was previously used, but became unused after 133138a284.

Signed-off-by: Yongtao Huang <[email protected]>
2025-12-14 11:39:45 +02:00
Petr ViktorinandGitHub 15313dd3d7 gh-140550: Correct error message for PyModExport (PEP 793) hook (GH-142583) 2025-12-12 17:48:43 +01:00
70671267c1 gh-142029: Raise ValueError instead of crashing on empty name given to create_builtin() (#142033)
Co-authored-by: Victor Stinner <[email protected]>
2025-12-10 12:01:57 +05:30
Petr ViktorinandGitHub bf66bce4ee gh-141780: Make PyModule_FromSlotsAndSpec enable GIL if needed (GH-141785) 2025-11-24 13:26:35 +01:00
Sam GrossandGitHub 2d50dd242e gh-137422: Fix race condition in PyImport_AddModuleRef (gh-141822) 2025-11-21 13:30:33 -05:00
1e4e59bb37 gh-116146: Add C-API to create module from spec and initfunc (GH-139196)
Co-authored-by: Kumar Aditya <[email protected]>
Co-authored-by: Petr Viktorin <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
2025-11-14 10:43:25 +01:00
Petr ViktorinandGitHub a4dd66275b gh-140550: Use a bool for the Py_mod_gil value (GH-141519)
This needs a single bit, but was stored as a void* in the module
struct. This didn't matter due to packing, but now that there's
another bool in the struct, we can save a bit of memory by
making md_gil a bool.

Variables that changed type are renamed, to detect conflicts.
2025-11-14 10:38:49 +01:00
Victor StinnerandGitHub 8435a2278f gh-141376: Fix exported symbols (GH-141377)
* gh-141376: Fix exported symbols

* _io module: add "_Py_" prefix to "spec" variables. For example,
  rename bufferedrandom_spec to _Py_bufferedrandom_spec.
* typevarobject.c: add "static" to "spec" and "slots" variables.
* import.c: add "static" to "pkgcontext" variable.

* No longer export textiowrapper_slots
2025-11-11 09:21:24 +01:00
589a03a8ce gh-140550: Initial implementation of PEP 793 – PyModExport (GH-140556)
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
2025-11-05 12:31:42 +01:00
Peter BiermaandGitHub 2cefa70eb9 gh-140544: Always assume that thread locals are available (GH-140690)
Python has required thread local support since 3.12 (see GH-103324). By assuming that thread locals are always supported, we can improve the performance of third-party extensions by allowing them to access the attached thread and interpreter states directly.
2025-10-28 09:07:19 -04:00
Itamar OrenandGitHub e8b5cb8f33 gh-140641: Break out of inittab search on match (GH-140642) 2025-10-27 15:18:58 +02:00
Lisa RoachandGitHub 2fd43a1ffe gh-138310: Adds sys.audit event for import_module (#138311)
* Updates sys.audit calls for imports to include import_module
* Adds unit tests for existing and new functionality
2025-09-19 06:21:42 -07:00
Adam TurnerandGitHub 918e3ba6c0 GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
Eric SnowandGitHub a450a0ddec gh-135443: Sometimes Fall Back to __main__.__dict__ For Globals (gh-135491)
For several builtin functions, we now fall back to __main__.__dict__ for the globals
when there is no current frame and _PyInterpreterState_IsRunningMain() returns
true.  This allows those functions to be run with Interpreter.call().

The affected builtins:

* exec()
* eval()
* globals()
* locals()
* vars()
* dir()

We take a similar approach with "stateless" functions, which don't use any
global variables.
2025-06-16 17:34:19 -06:00
Eric SnowandGitHub 52deabefd0 gh-132775: Expand the Capability of Interpreter.call() (gh-133484)
It now supports most callables, full args, and return values.
2025-05-30 09:15:00 -06:00
Serhiy StorchakaandGitHub bac3fcba5b gh-108512: Add and use new replacements for PySys_GetObject() (GH-111035)
Add functions PySys_GetAttr(), PySys_GetAttrString(),
PySys_GetOptionalAttr() and PySys_GetOptionalAttrString().
2025-05-28 20:11:09 +03:00
Nico-PosadaandGitHub 4e9005d32f gh-134100: Fix use-after-free in PyImport_ImportModuleLevelObject (#134117) 2025-05-18 12:41:38 +05:30
Bénédikt TranandGitHub a2c4467d06 gh-133644: remove deprecated PyImport_ImportModuleNoBlock (#133655) 2025-05-08 13:08:43 +00:00
c4bcc6a778 gh-102567: Add -X importtime=2 for logging an importtime message for already-loaded modules (#118655)
Co-authored-by: Adam Turner <[email protected]>
2025-05-06 01:03:55 +01:00