summaryrefslogtreecommitdiffstats
path: root/Python/import.c
Commit message (Collapse)AuthorAgeFilesLines
* gh-142776: Ensure fp file descriptor is closed on all code paths in import.c ↵stratakis2025-12-191-10/+4
| | | | (GH-142777)
* gh-142029: Raise `ModuleNotFoundError` instead of crashing on nonexsistent ↵dr-carlos2025-12-151-10/+22
| | | | | | module name given to `create_builtin()` (#142054) Co-authored-by: Brett Cannon <brett@python.org> Co-authored-by: Victor Stinner <vstinner@python.org>
* gh-142666: Remove unused variable package in import logic (GH-142667)Yongtao Huang2025-12-141-2/+0
| | | | | The variable was previously used, but became unused after 133138a284b. Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
* gh-140550: Correct error message for PyModExport (PEP 793) hook (GH-142583)Petr Viktorin2025-12-121-2/+2
|
* gh-142029: Raise `ValueError` instead of crashing on empty name given to ↵dr-carlos2025-12-101-0/+6
| | | | | `create_builtin()` (#142033) Co-authored-by: Victor Stinner <vstinner@python.org>
* gh-141780: Make PyModule_FromSlotsAndSpec enable GIL if needed (GH-141785)Petr Viktorin2025-11-241-18/+28
|
* gh-137422: Fix race condition in PyImport_AddModuleRef (gh-141822)Sam Gross2025-11-211-6/+17
|
* gh-116146: Add C-API to create module from spec and initfunc (GH-139196)Itamar Oren2025-11-141-21/+53
| | | | | Co-authored-by: Kumar Aditya <kumaraditya@python.org> Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Victor Stinner <vstinner@python.org>
* gh-140550: Use a bool for the Py_mod_gil value (GH-141519)Petr Viktorin2025-11-141-12/+14
| | | | | | | | 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.
* gh-141376: Fix exported symbols (GH-141377)Victor Stinner2025-11-111-1/+1
| | | | | | | | | | * 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
* gh-140550: Initial implementation of PEP 793 – PyModExport (GH-140556)Petr Viktorin2025-11-051-26/+87
| | | | Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Kumar Aditya <kumaraditya@python.org>
* gh-140544: Always assume that thread locals are available (GH-140690)Peter Bierma2025-10-281-14/+0
| | | 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.
* gh-140641: Break out of inittab search on match (GH-140642)Itamar Oren2025-10-271-0/+1
|
* gh-138310: Adds sys.audit event for import_module (#138311)Lisa Roach2025-09-191-27/+0
| | | | * Updates sys.audit calls for imports to include import_module * Adds unit tests for existing and new functionality
* GH-137623: Use an AC decorator for docstring line length enforcement (#137690)Adam Turner2025-08-181-1/+2
|
* gh-135443: Sometimes Fall Back to __main__.__dict__ For Globals (gh-135491)Eric Snow2025-06-161-6/+9
| | | | | | | | | | | | | | | | | 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.
* gh-132775: Expand the Capability of Interpreter.call() (gh-133484)Eric Snow2025-05-301-1/+3
| | | It now supports most callables, full args, and return values.
* gh-108512: Add and use new replacements for PySys_GetObject() (GH-111035)Serhiy Storchaka2025-05-281-6/+6
| | | | | Add functions PySys_GetAttr(), PySys_GetAttrString(), PySys_GetOptionalAttr() and PySys_GetOptionalAttrString().
* gh-134100: Fix use-after-free in `PyImport_ImportModuleLevelObject` (#134117)Nico-Posada2025-05-181-1/+3
|
* gh-133644: remove deprecated `PyImport_ImportModuleNoBlock` (#133655)Bénédikt Tran2025-05-081-1/+3
|
* gh-102567: Add -X importtime=2 for logging an importtime message for ↵Noah Kim2025-05-061-8/+28
| | | | | already-loaded modules (#118655) Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
* gh-132775: Drop PyUnstable_InterpreterState_GetMainModule() (gh-132978)Eric Snow2025-04-281-0/+14
| | | | | We replace it with _Py_GetMainModule(), and add _Py_CheckMainModule(), but both in the internal-only C-API. We also add _PyImport_GetModulesRef(), which is the equivalent of _PyImport_GetModules(), but which increfs before the lock is released. This is used by a later change related to pickle and handling __main__.
* gh-131238: Remove more includes from pycore_interp.h (#131480)Victor Stinner2025-03-191-0/+1
|
* GH-131238: More refactoring of core header files (GH-131351)Mark Shannon2025-03-171-0/+1
| | | | Adds new pycore_stats.h header file to help break dependencies involving the pycore_code.h header.
* gh-111178: Fix function signatures in misc files (#131180)Victor Stinner2025-03-131-2/+3
|
* gh-130163: Fix crashes related to PySys_GetObject() (GH-130503)Serhiy Storchaka2025-02-251-12/+24
| | | | | | | | The use of PySys_GetObject() and _PySys_GetAttr(), which return a borrowed reference, has been replaced by using one of the following functions, which return a strong reference and distinguish a missing attribute from an error: _PySys_GetOptionalAttr(), _PySys_GetOptionalAttrString(), _PySys_GetRequiredAttr(), and _PySys_GetRequiredAttrString().
* gh-111924: Fix data races when swapping allocators (gh-130287)Sam Gross2025-02-201-36/+7
| | | | | | | | | | | | | | | CPython current temporarily changes `PYMEM_DOMAIN_RAW` to the default allocator during initialization and shutdown. The motivation is to ensure that core runtime structures are allocated and freed using the same allocator. However, modifying the current allocator changes global state and is not thread-safe even with the GIL. Other threads may be allocating or freeing objects use PYMEM_DOMAIN_RAW; they are not required to hold the GIL to call PyMem_RawMalloc/PyMem_RawFree. This adds new internal-only functions like `_PyMem_DefaultRawMalloc` that aren't affected by calls to `PyMem_SetAllocator()`, so they're appropriate for Python runtime initialization and finalization. Use these calls in places where we previously swapped to the default raw allocator.
* gh-129354: Fix grammar in PyErr_FormatUnraisable() (#129475)Victor Stinner2025-01-311-5/+10
| | | Replace "on verb+ing" with "while verb+ing".
* gh-128911: Add PyImport_ImportModuleAttr() function (#128912)Victor Stinner2025-01-301-4/+4
| | | | | | | | | | | Add PyImport_ImportModuleAttr() and PyImport_ImportModuleAttrString() functions. * Add unit tests. * Replace _PyImport_GetModuleAttr() with PyImport_ImportModuleAttr(). * Replace _PyImport_GetModuleAttrString() with PyImport_ImportModuleAttrString(). * Remove "pycore_import.h" includes, no longer needed.
* gh-127350: Add Py_fopen() and Py_fclose() functions (#127821)Victor Stinner2025-01-061-1/+1
|
* gh-127864: Fix compiler warning (-Wstringop-truncation) (GH-127878)Tomas R.2024-12-161-2/+3
|
* gh-127865: Fix build failure for systems without thread local support ↵velemas2024-12-121-4/+4
| | | | | | (GH-127866) This PR fixes the build issue introduced by the commit 628f6eb from GH-112207 on systems without thread local support.
* gh-127208: Reject null character in _imp.create_dynamic() (#127400)Victor Stinner2024-11-291-3/+5
| | | | _imp.create_dynamic() now rejects embedded null characters in the path and in the module name.
* gh-126688: Reinit import lock after fork (#126692)Sam Gross2024-11-121-0/+7
| | | | | | The PyMutex implementation supports unlocking after fork because we clear the list of waiters in parking_lot.c. This doesn't work as well for _PyRecursiveMutex because on some systems, such as SerenityOS, the thread id is not preserved across fork().
* gh-126171: fix possible null dereference in _imp_find_frozen_impl (#126566)Valery Fedorenko2024-11-081-1/+1
|
* gh-125604: Move _Py_AuditHookEntry, etc. Out of pycore_runtime.h (gh-125605)Eric Snow2024-10-181-1/+2
| | | | | | | | | | | | This is essentially a cleanup, moving a handful of API declarations to the header files where they fit best, creating new ones when needed. We do the following: * add pycore_debug_offsets.h and move _Py_DebugOffsets, etc. there * inline struct _getargs_runtime_state and struct _gilstate_runtime_state in _PyRuntimeState * move struct _reftracer_runtime_state to the existing pycore_object_state.h * add pycore_audit.h and move to it _Py_AuditHookEntry , _PySys_Audit(), and _PySys_ClearAuditHooks * add audit.h and cpython/audit.h and move the existing audit-related API there *move the perfmap/trampoline API from cpython/sysmodule.h to cpython/ceval.h, and remove the now-empty cpython/sysmodule.h
* GH-125174: Make immortal objects more robust, following design from PEP 683 ↵Mark Shannon2024-10-101-1/+1
| | | | (GH-125251)
* gh-123880: Allow recursive import of single-phase-init modules (GH-123950)Petr Viktorin2024-09-201-5/+13
| | | | Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Co-authored-by: Brett Cannon <brett@python.org>
* gh-124160: Pass main_tstate to update_global_state_for_extension() (#124164)luk13372024-09-191-1/+1
|
* gh-123091: Use more _Py_IsImmortalLoose() (GH-123602)Petr Viktorin2024-09-021-1/+1
| | | | | | | Switch more _Py_IsImmortal(...) assertions to _Py_IsImmortalLoose(...) The remaining calls to _Py_IsImmortal are in free-threaded-only code, initialization of core objects, tests, and guards that fall back to code that works with mortal objects.
* gh-122854: Add Py_HashBuffer() function (#122855)Victor Stinner2024-08-301-1/+1
|
* gh-122188: Remove _imp.pyc_magic_number (GH-122503)Serhiy Storchaka2024-08-021-5/+1
| | | _imp.pyc_magic_number_token should be enough.
* gh-122188: Move magic number to its own file (#122243)Michael Droettboom2024-07-301-15/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * gh-122188: Move magic number to its own file * Add versionadded directive * Do work in C * Integrate launcher.c * Make _pyc_magic_number private * Remove metadata * Move sys.implementation -> _imp * Modernize comment * Move _RAW_MAGIC_NUMBER to the C side as well * _pyc_magic_number -> pyc_magic_number * Remove unused import * Update docs * Apply suggestions from code review Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com> * Fix typo in tests --------- Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* gh-121110: Fix Extension Module Tests Under Py_TRACE_REFS Builds (gh-121503)Eric Snow2024-07-081-19/+43
| | | The change in gh-118157 (b2cd54a) should have also updated clear_singlephase_extension() but didn't. We fix that here. Note that clear_singlephase_extension() (AKA _PyImport_ClearExtension()) is only used in tests.
* gh-120838: Add _PyThreadState_WHENCE_FINI (gh-121010)Eric Snow2024-06-251-2/+2
| | | | | We also add _PyThreadState_NewBound() and drop _PyThreadState_SetWhence(). This change only affects internal API.
* gh-113993: Allow interned strings to be mortal, and fix related issues ↵Petr Viktorin2024-06-211-3/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (GH-120520) * Add an InternalDocs file describing how interning should work and how to use it. * Add internal functions to *explicitly* request what kind of interning is done: - `_PyUnicode_InternMortal` - `_PyUnicode_InternImmortal` - `_PyUnicode_InternStatic` * Switch uses of `PyUnicode_InternInPlace` to those. * Disallow using `_Py_SetImmortal` on strings directly. You should use `_PyUnicode_InternImmortal` instead: - Strings should be interned before immortalization, otherwise you're possibly interning a immortalizing copy. - `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to `SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in backports, as they are now part of public API and version-specific ABI. * Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery. * Make sure the statically allocated string singletons are unique. This means these sets are now disjoint: - `_Py_ID` - `_Py_STR` (including the empty string) - one-character latin-1 singletons Now, when you intern a singleton, that exact singleton will be interned. * Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic). * Intern `_Py_STR` singletons at startup. * For free-threaded builds, intern `_Py_LATIN1_CHR` singletons at startup. * Beef up the tests. Cover internal details (marked with `@cpython_only`). * Add lots of assertions Co-Authored-By: Eric Snow <ericsnowcurrently@gmail.com>
* gh-120726: Fix compiler warnings on is_core_module() (#120727)Kirill Podoprigora2024-06-191-1/+1
| | | | | Fix compiler warnings on is_core_module() and check_interpreter_whence(): only define them when assertions are built.
* gh-115649: Copy the filename into main interpreter before intern in import.c ↵AN Long2024-06-171-1/+11
| | | | | (#120315) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
* gh-117657: Fix TSAN race involving import lock (#118523)Sam Gross2024-06-061-76/+7
| | | | | This adds a `_PyRecursiveMutex` type based on `PyMutex` and uses that for the import lock. This fixes some data races in the free-threaded build and generally simplifies the import lock code.
* Fix typos in documentation and comments (#119763)Xie Yanbo2024-06-041-1/+1
|