summaryrefslogtreecommitdiff
path: root/Python/import.c
AgeCommit message (Collapse)AuthorLines
2024-01-29[3.11] gh-114685: Fix incorrect use of PyBUF_READ in import.c (GH-114686) ↵Miss Islington (bot)-1/+1
(GH-114701) (cherry picked from commit 1ac1b2f9536a581f1656f0ac9330a7382420cda1) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
2023-10-07[3.11] gh-109521: Fix obscure cases handling in PyImport_GetImporter() ↵Serhiy Storchaka-2/+13
(GH-109522) (GH-109781) PyImport_GetImporter() now sets RuntimeError if it fails to get sys.path_hooks or sys.path_importer_cache or they are not list and dict correspondingly. Previously it could return NULL without setting error in obscure cases, crash or raise SystemError if these attributes have wrong type. (cherry picked from commit 62c7015e89cbdedb5218d4fedd45f971885f67a8)
2023-06-26[3.11] gh-105979: Fix exception handling in unmarshal_frozen_code ↵Kumar Aditya-0/+1
(`Python/import.c`) (GH-105980). (#106100) * [3.11] gh-105979: Fix exception handling in `unmarshal_frozen_code` (`Python/import.c`) (GH-105980). (cherry picked from commit cd5280367a3a7065d13b8f7234474f7a2e9a18fd) Co-authored-by: chgnrdv <52372310+chgnrdv@users.noreply.github.com>
2022-11-21gh-99578: Fix refleak in _imp.create_builtin() (GH-99642)Miss Islington (bot)-1/+2
Fix a reference bug in _imp.create_builtin() after the creation of the first sub-interpreter for modules "builtins" and "sys". (cherry picked from commit cb2ef8b2acbb231c207207d3375b2f8b0077a6ee) Co-authored-by: Victor Stinner <vstinner@python.org>
2022-06-14[3.11] gh-93741: Add private C API _PyImport_GetModuleAttrString() ↵Serhiy Storchaka-0/+31
(GH-93742) (GH-93792) It combines PyImport_ImportModule() and PyObject_GetAttrString() and saves 4-6 lines of code on every use. Add also _PyImport_GetModuleAttr() which takes Python strings as arguments. (cherry picked from commit 6fd4c8ec7740523bb81191c013118d9d6959bc9d)
2022-03-30bpo-47162: Add call trampoline to mitigate bad fpcasts on Emscripten (GH-32189)Christian Heimes-3/+9
2022-02-24bpo-45459: C API uses type names rather than structure names (GH-31528)Victor Stinner-1/+1
Thanks to the new pytypedefs.h, it becomes to use type names like PyObject rather like structure names like "struct _object".
2022-02-08bpo-46541: Replace core use of _Py_IDENTIFIER() with statically initialized ↵Eric Snow-62/+28
global objects. (gh-30928) We're no longer using _Py_IDENTIFIER() (or _Py_static_string()) in any core CPython code. It is still used in a number of non-builtin stdlib modules. The replacement is: PyUnicodeObject (not pointer) fields under _PyRuntimeState, statically initialized as part of _PyRuntime. A new _Py_GET_GLOBAL_IDENTIFIER() macro facilitates lookup of the fields (along with _Py_GET_GLOBAL_STRING() for non-identifier strings). https://bugs.python.org/issue46541#msg411799 explains the rationale for this change. The core of the change is in: * (new) Include/internal/pycore_global_strings.h - the declarations for the global strings, along with the macros * Include/internal/pycore_runtime_init.h - added the static initializers for the global strings * Include/internal/pycore_global_objects.h - where the struct in pycore_global_strings.h is hooked into _PyRuntimeState * Tools/scripts/generate_global_objects.py - added generation of the global string declarations and static initializers I've also added a --check flag to generate_global_objects.py (along with make check-global-objects) to check for unused global strings. That check is added to the PR CI config. The remainder of this change updates the core code to use _Py_GET_GLOBAL_IDENTIFIER() instead of _Py_IDENTIFIER() and the related _Py*Id functions (likewise for _Py_GET_GLOBAL_STRING() instead of _Py_static_string()). This includes adding a few functions where there wasn't already an alternative to _Py*Id(), replacing the _Py_Identifier * parameter with PyObject *. The following are not changed (yet): * stop using _Py_IDENTIFIER() in the stdlib modules * (maybe) get rid of _Py_IDENTIFIER(), etc. entirely -- this may not be doable as at least one package on PyPI using this (private) API * (maybe) intern the strings during runtime init https://bugs.python.org/issue46541
2022-02-07bpo-46670: Remove unused macros in the Python directory (GH-31192)Victor Stinner-2/+0
2022-02-04bpo-46608: exclude marshalled-frozen data if deep-freezing to save 300 KB ↵Kumar Aditya-4/+12
space (GH-31074) This reduces the size of the data segment by **300 KB** of the executable because if the modules are deep-frozen then the marshalled frozen data just wastes space. This was inspired by comment by @gvanrossum in https://github.com/python/cpython/pull/29118#issuecomment-958521863. Note: There is a new option `--deepfreeze-only` in `freeze_modules.py` to change this behavior, it is on be default to save disk space. ```console # du -s ./python before 27892 ./python # du -s ./python after 27524 ./python ``` Automerge-Triggered-By: GH:ericsnowcurrently
2021-11-24bpo-45020: Fix strict-prototypes warning (GH-29755)Christian Heimes-1/+1
2021-11-10bpo-45696: Deep-freeze selected modules (GH-29118)Guido van Rossum-0/+7
This gains 10% or more in startup time for `python -c pass` on UNIX-ish systems. The Makefile.pre.in generating code builds on Eric's work for bpo-45020, but the .c file generator is new. Windows version TBD.
2021-10-29bpo-45379: clarify FROZEN_EXCLUDED and FROZEN_INVALID documentation (GH-29189)Filipe Laíns-2/+7
Signed-off-by: Filipe Laíns <lains@riseup.net> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-10-28bpo-45395: Make custom frozen modules additions instead of replacements. ↵Eric Snow-46/+113
(gh-28778) Currently custom modules (the array set on PyImport_FrozenModules) replace all the frozen stdlib modules. That can be problematic and is unlikely to be what the user wants. This change treats the custom frozen modules as additions instead. They take precedence over all other frozen modules except for those needed to bootstrap the import system. If the "code" field of an entry in the custom array is NULL then that frozen module is treated as disabled, which allows a custom entry to disable a frozen stdlib module. This change allows us to get rid of is_essential_frozen_module() and simplifies the logic for which frozen modules should be ignored. https://bugs.python.org/issue45395
2021-10-28bpo-45379: add custom error string for FROZEN_DISABLED (GH-29190)Filipe Laíns-1/+3
Signed-off-by: Filipe Laíns <lains@riseup.net> Co-authored-by: Gareth Rees <gdr@garethrees.org>
2021-10-15bpo-45482: Rename namespaceobject.h to pycore_namespace.h (GH-28975)Victor Stinner-10/+10
Rename Include/namespaceobject.h to Include/internal/pycore_namespace.h. The _testmultiphase extension is now built with the Py_BUILD_CORE_MODULE macro defined to access _PyNamespace_Type. object.c: remove unused "pycore_context.h" include.
2021-10-14bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)Eric Snow-15/+32
Currently frozen modules do not have __file__ set. In their spec, origin is set to "frozen" and they are marked as not having a location. (Similarly, for frozen packages __path__ is set to an empty list.) However, for frozen stdlib modules we are able to extrapolate __file__ as long as we can determine the stdlib directory at runtime. (We now do so since gh-28586.) Having __file__ set is helpful for a number of reasons. Likewise, having a non-empty __path__ means we can import submodules of a frozen package from the filesystem (e.g. we could partially freeze the encodings module). This change sets __file__ (and adds to __path__) for frozen stdlib modules. It uses sys._stdlibdir (from gh-28586) and the frozen module alias information (from gh-28655). All that work is done in FrozenImporter (in Lib/importlib/_bootstrap.py). Also, if a frozen module is imported before importlib is bootstrapped (during interpreter initialization) then we fix up that module and its spec during the importlib bootstrapping step (i.e. imporlib._bootstrap._setup()) to match what gets set by FrozenImporter, including setting the file info (if the stdlib dir is known). To facilitate this, modules imported using PyImport_ImportFrozenModule() have __origname__ set using the frozen module alias info. __origname__ is popped off during importlib bootstrap. (To be clear, even with this change the new code to set __file__ during fixups in imporlib._bootstrap._setup() doesn't actually get triggered yet. This is because sys._stdlibdir hasn't been set yet in interpreter initialization at the point importlib is bootstrapped. However, we do fix up such modules at that point to otherwise match the result of importing through FrozenImporter, just not the __file__ and __path__ parts. Doing so will require changes in the order in which things happen during interpreter initialization. That can be addressed separately. Once it is, the file-related fixup code from this PR will kick in.) Here are things this change does not do: * set __file__ for non-stdlib modules (no way of knowing the parent dir) * set __file__ if the stdlib dir is not known (nor assume the expense of finding it) * relatedly, set __file__ if the stdlib is in a zip file * verify that the filename set to __file__ actually exists (too expensive) * update __path__ for frozen packages that alias a non-package (since there is no package dir) Other things this change skips, but we may do later: * set __file__ on modules imported using PyImport_ImportFrozenModule() * set co_filename when we unmarshal the frozen code object while importing the module (e.g. in FrozenImporter.exec_module()) -- this would allow tracebacks to show source lines * implement FrozenImporter.get_filename() and FrozenImporter.get_source() https://bugs.python.org/issue21736
2021-10-13bpo-45434: Mark the PyTokenizer C API as private (GH-28924)Victor Stinner-1/+0
Rename PyTokenize functions to mark them as private: * PyTokenizer_FindEncodingFilename() => _PyTokenizer_FindEncodingFilename() * PyTokenizer_FromString() => _PyTokenizer_FromString() * PyTokenizer_FromFile() => _PyTokenizer_FromFile() * PyTokenizer_FromUTF8() => _PyTokenizer_FromUTF8() * PyTokenizer_Free() => _PyTokenizer_Free() * PyTokenizer_Get() => _PyTokenizer_Get() Remove the unused PyTokenizer_FindEncoding() function. import.c: remove unused #include "errcode.h".
2021-10-06Fix typos in the Python directory (GH-28767)Christian Clauss-1/+1
2021-10-05bpo-45020: Identify which frozen modules are actually aliases. (gh-28655)Eric Snow-11/+70
In the list of generated frozen modules at the top of Tools/scripts/freeze_modules.py, you will find that some of the modules have a different name than the module (or .py file) that is actually frozen. Let's call each case an "alias". Aliases do not come into play until we get to the (generated) list of modules in Python/frozen.c. (The tool for freezing modules, Programs/_freeze_module, is only concerned with the source file, not the module it will be used for.) Knowledge of which frozen modules are aliases (and the identity of the original module) normally isn't important. However, this information is valuable when we go to set __file__ on frozen stdlib modules. This change updates Tools/scripts/freeze_modules.py to map aliases to the original module name (or None if not a stdlib module) in Python/frozen.c. We also add a helper function in Python/import.c to look up a frozen module's alias and add the result of that function to the frozen info returned from find_frozen(). https://bugs.python.org/issue45020
2021-10-05bpo-45324: Capture data in FrozenImporter.find_spec() to use in ↵Eric Snow-79/+187
exec_module(). (gh-28633) Before this change we end up duplicating effort and throwing away data in FrozenImporter.find_spec(). Now we do the work once in find_spec() and the only thing we do in FrozenImporter.exec_module() is turn the raw frozen data into a code object and then exec it. We've added _imp.find_frozen(), add an arg to _imp.get_frozen_object(), and updated FrozenImporter. We've also moved some code around to reduce duplication, get a little more consistency in outcomes, and be more efficient. Note that this change is mostly necessary if we want to set __file__ on frozen stdlib modules. (See https://bugs.python.org/issue21736.) https://bugs.python.org/issue45324
2021-10-05bpo-44050: Extension modules can share state when they don't support ↵Hai Shi-1/+3
sub-interpreters. (GH-27794) Automerge-Triggered-By: GH:encukou
2021-09-14bpo-45020: Add -X frozen_modules=[on|off] to explicitly control use of ↵Eric Snow-9/+81
frozen modules. (gh-28320) Currently we freeze several modules into the runtime. For each of these modules it is essential to bootstrapping the runtime that they be frozen. Any other stdlib module that we later freeze into the runtime is not essential. We can just as well import from the .py file. This PR lets users explicitly choose which should be used, with the new "-X frozen_modules=[on|off]" CLI flag. The default is "off" for now. https://bugs.python.org/issue45020
2021-09-13bpo-45019: Do some cleanup related to frozen modules. (gh-28319)Eric Snow-0/+41
There are a few things I missed in gh-27980. This is a follow-up that will make subsequent PRs cleaner. It includes fixes to tests and tools that reference the frozen modules. https://bugs.python.org/issue45019
2021-06-23bpo-44441: _PyImport_Fini2() resets PyImport_Inittab (GH-26874)Victor Stinner-0/+3
Py_RunMain() now resets PyImport_Inittab to its initial value at exit. It must be possible to call PyImport_AppendInittab() or PyImport_ExtendInittab() at each Python initialization.
2021-04-07bpo-43244: Remove Yield macro from pycore_ast.h (GH-25243)Victor Stinner-1/+0
* pycore_ast.h no longer defines the Yield macro. * Fix a compiler warning on Windows: "warning C4005: 'Yield': macro redefinition". * Python-ast.c now defines directly functions with their real _Py_xxx() name, rather than xxx(). * Remove "#undef Yield" in C files including pycore_ast.h.
2021-03-23bpo-43244: Remove ast.h, asdl.h, Python-ast.h headers (GH-24933)Victor Stinner-1/+0
These functions were undocumented and excluded from the limited C API. Most names defined by these header files were not prefixed by "Py" and so could create names conflicts. For example, Python-ast.h defined a "Yield" macro which was conflict with the "Yield" name used by the Windows <winbase.h> header. Use the Python ast module instead. * Move Include/asdl.h to Include/internal/pycore_asdl.h. * Move Include/Python-ast.h to Include/internal/pycore_ast.h. * Remove ast.h header file. * pycore_symtable.h no longer includes Python-ast.h.
2021-03-22bpo-43551: Fix PyImport_Import() for subinterpreters (GH-24929)junyixie-16/+21
Avoid static variables.
2021-03-20bpo-43517: Fix false positive in detection of circular imports (#24895)Antoine Pitrou-1/+1
2021-02-19bpo-43268: Pass interp rather than tstate to internal functions (GH-24580)Victor Stinner-8/+7
Pass the current interpreter (interp) rather than the current Python thread state (tstate) to internal functions which only use the interpreter. Modified functions: * _PyXXX_Fini() and _PyXXX_ClearFreeList() functions * _PyEval_SignalAsyncExc(), make_pending_calls() * _PySys_GetObject(), sys_set_object(), sys_set_object_id(), sys_set_object_str() * should_audit(), set_flags_from_config(), make_flags() * _PyAtExit_Call() * init_stdio_encoding() * etc.
2021-02-19bpo-43268: _Py_IsMainInterpreter() now expects interp (GH-24577)Victor Stinner-1/+1
The _Py_IsMainInterpreter() function now expects interp rather than tstate.
2021-01-12bpo-41994: Fix refcount issues in Python/import.c (GH-22632)Serhiy Storchaka-56/+50
https://bugs.python.org/issue41994
2020-11-18bpo-1635741: Convert _imp to multi-phase init (GH-23378)Victor Stinner-71/+110
Convert the _imp extension module to the multi-phase initialization API (PEP 489). * Add _PyImport_BootstrapImp() which fix a bootstrap issue: import the _imp module before importlib is initialized. * Add create_builtin() sub-function, used by _imp_create_builtin(). * Initialize PyInterpreterState.import_func earlier, in pycore_init_builtins(). * Remove references to _PyImport_Cleanup(). This function has been renamed to finalize_modules() and moved to pylifecycle.c.
2020-11-12bpo-42260: Initialize time and warnings earlier at startup (GH-23249)Victor Stinner-37/+0
* Call _PyTime_Init() and _PyWarnings_InitState() earlier during the Python initialization. * Inline _PyImportHooks_Init() into _PySys_InitCore(). * The _warnings initialization function no longer call _PyWarnings_InitState() to prevent resetting filters_version to 0. * _PyWarnings_InitState() now returns an int and no longer clear the state in case of error (it's done anyway at Python exit). * Rework init_importlib(), fix refleaks on errors.
2020-10-30bpo-42208: Move _PyImport_Cleanup() to pylifecycle.c (GH-23040)Victor Stinner-227/+0
Move _PyImport_Cleanup() to pylifecycle.c, rename it to finalize_modules(), split it (200 lines) into many smaller sub-functions and cleanup the code.
2020-10-30bpo-42208: Pass tstate to _PyGC_CollectNoFail() (GH-23038)Victor Stinner-2/+2
Move private _PyGC_CollectNoFail() to the internal C API. Remove the private _PyGC_CollectIfEnabled() which was just an alias to the public PyGC_Collect() function since Python 3.8. Rename functions: * collect() => gc_collect_main() * collect_with_callback() => gc_collect_with_callback() * collect_generations() => gc_collect_generations()
2020-10-26bpo-42152: Use PyDict_Contains and PyDict_SetDefault if appropriate. (GH-22986)Serhiy Storchaka-10/+14
If PyDict_GetItemWithError is only used to check whether the key is in dict, it is better to use PyDict_Contains instead. And if it is used in combination with PyDict_SetItem, PyDict_SetDefault can replace the combination.
2020-10-11bpo-41993: Fix possible issues in remove_module() (GH-22631)Serhiy Storchaka-10/+13
* PyMapping_HasKey() is not safe because it silences all exceptions and can return incorrect result. * Informative exceptions from PyMapping_DelItem() are overridden with RuntimeError and the original exception raised before calling remove_module() is lost. * There is a race condition between PyMapping_HasKey() and PyMapping_DelItem().
2020-06-02bpo-40232: _PyImport_ReInitLock() can now safely use its lock (GH-20597)Victor Stinner-5/+1
Since _PyImport_ReInitLock() now calls _PyThread_at_fork_reinit() on the import lock, the lock is now in a known state: unlocked. It became safe to acquire it after fork.
2020-06-02PyOS_AfterFork_Child() uses PyStatus (GH-20596)Victor Stinner-6/+7
PyOS_AfterFork_Child() helper functions now return a PyStatus: PyOS_AfterFork_Child() is now responsible to handle errors. * Move _PySignal_AfterFork() to the internal C API * Add #ifdef HAVE_FORK on _PyGILState_Reinit(), _PySignal_AfterFork() and _PyInterpreterState_DeleteExceptMain().
2020-05-01bpo-40417: Fix deprecation warning in PyImport_ReloadModule (GH-19750)Robert Rouhani-7/+7
I can add another commit with the new test case I wrote to verify that the warning was being printed before my change, stopped printing after my change, and that the function does not return null after my change. Automerge-Triggered-By: @brettcannon
2020-05-01bpo-40412: Nullify inittab_copy during finalization (GH-19746)Gregory Szorc-0/+1
Otherwise we leave a dangling pointer to free'd memory. If we then initialize a new interpreter in the same process and call PyImport_ExtendInittab, we will (likely) crash when calling PyMem_RawRealloc(inittab_copy, ...) since the pointer address is bogus. Automerge-Triggered-By: @brettcannon
2020-04-29bpo-40429: PyFrame_GetCode() now returns a strong reference (GH-19773)Victor Stinner-0/+1
2020-04-28bpo-40421: Add PyFrame_GetCode() function (GH-19757)Victor Stinner-2/+1
PyFrame_GetCode(frame): return a borrowed reference to the frame code. Replace frame->f_code with PyFrame_GetCode(frame) in most code, except in frameobject.c, genobject.c and ceval.c. Also add PyFrame_GetLineNumber() to the limited C API.
2020-04-15bpo-40268: Remove unused osdefs.h includes (GH-19532)Victor Stinner-1/+0
When the include is needed, add required symbol in a comment.
2020-04-15bpo-40268: Remove unused pycore_pymem.h includes (GH-19531)Victor Stinner-1/+1
2020-04-15bpo-40268: Remove explicit pythread.h includes (#19529)Victor Stinner-2/+0
Remove explicit pythread.h includes: it is always included by Python.h.
2020-04-15bpo-40232: Update PyOS_AfterFork_Child() to use _PyThread_at_fork_reinit() ↵Dong-hee Na-2/+3
(GH-19450)
2020-04-14bpo-40268: Remove a few pycore_pystate.h includes (GH-19510)Victor Stinner-2/+2
2020-04-14bpo-40268: Rename _PyInterpreterState_GET_UNSAFE() (GH-19509)Victor Stinner-3/+3
Rename _PyInterpreterState_GET_UNSAFE() to _PyInterpreterState_GET() for consistency with _PyThreadState_GET() and to have a shorter name (help to fit into 80 columns). Add also "assert(tstate != NULL);" to the function.