summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/_bootstrap.py
Commit message (Collapse)AuthorAgeFilesLines
* ``importlib/_bootstrap.py``: Reduce size of ``_List`` instances (GH-114747)Kirill Podoprigora2024-01-301-1/+1
| | | Reduce size of _List instances
* gh-112414: Fix `AttributeError` when calling `repr()` on a namespace package ↵Alex Waygood2023-11-271-2/+8
| | | | imported with a custom loader (#112425)
* GH-106176, GH-104702: Fix reference leak when importing across multiple ↵Brett Cannon2023-08-291-12/+103
| | | | threads (#108497)
* gh-98040: Remove find_loader, find_module and other deprecated APIs (#98059)Barry Warsaw2023-05-031-40/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Remove deprecated classes from pkgutil * Remove some other PEP 302 obsolescence * Use find_spec instead of load_module * Remove more tests of PEP 302 obsolete APIs * Remove another bunch of tests using obsolete load_modules() * Remove deleted names from __all__ * Remove obsolete footnote * imp is removed * Remove `imp` from generated stdlib names * What's new and blurb * Update zipimport documentation for the removed methods * Fix some Windows tests * Remove any test (or part of a test) that references `find_module()`. * Use assertIsNone() / assertIsNotNone() consistently. * Update Doc/reference/import.rst * We don't need pkgutil._get_spec() any more either * test.test_importlib.fixtures.NullFinder * ...BadLoaderFinder.find_module * ...test_api.InvalidatingNullFinder.find_module * ...test.test_zipimport test of z.find_module * Suppress cross-references to find_loader and find_module * Suppress cross-references to Finder * Suppress cross-references to pkgutil.ImpImporter and pkgutil.ImpLoader --------- Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net> Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
* Remove double space in import error message (#103458)Ned Batchelder2023-04-141-1/+1
|
* GH-102700: allow built-in modules to be submodules (GH-103162)Brett Cannon2023-04-061-2/+0
|
* gh-101766: Fix refleak for _BlockingOnManager resources from test suite ↵Dong-hee Na2023-02-171-5/+0
| | | | level (gh-101988)
* gh-101766: Fix refleak for _BlockingOnManager resources (gh-101942)Dong-hee Na2023-02-171-0/+5
|
* gh-91351: Fix some bugs in importlib handling of re-entrant imports (GH-94504)Jean-Paul Calderone2023-01-211-37/+196
| | | Co-authored-by: Brett Cannon <brett@python.org>
* gh-98139: enhance namespace package reprAnh71me2022-11-061-1/+1
|
* bpo-38693: Use f-strings instead of str.format() within importlib (#17058)Gregory P. Smith2022-10-061-26/+23
| | | | | | | | | This is a small performance improvement, especially for one or two hot places such as _handle_fromlist() that are called a lot and the .format() method was being used just to join two strings with a dot. Otherwise it is merely a readability improvement. We keep `_ERR_MSG` and `_ERR_MSG_PREFIX` as those may be used elsewhere for canonical looking error messages.
* gh-65961: Raise `DeprecationWarning` when `__package__` differs from ↵Brett Cannon2022-10-051-1/+1
| | | | | | | `__spec__.parent` (#97879) Also remove `importlib.util.set_package()` which was already slated for removal. Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* gh-97850: Remove all known instances of module_repr() (#97876)Barry Warsaw2022-10-051-22/+0
| | | Remove all known instances of module_repr()
* gh-94619: Remove long deprecated methods module_repr() and load_module() ↵Barry Warsaw2022-08-051-6/+0
| | | | | | | | | | | | | | | (#94624) * gh-94619: Remove long deprecated methods module_repr() and load_module() Closes #94619 * Update Misc/NEWS.d/next/Library/2022-07-06-14-57-33.gh-issue-94619.PRqKVX.rst Fix typo Co-authored-by: Brett Cannon <brett@python.org> Co-authored-by: Brett Cannon <brett@python.org>
* bpo-35673: Add a public alias for namespace package __loader__ attribute ↵Barry Warsaw2021-10-201-2/+2
| | | | | | | | (#29049) Rename namespace package __loader__ class to be public. Make the old name, i.e. _NamespaceLoader, an alias for the public name, for backward compatibility.
* bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)Eric Snow2021-10-141-41/+136
| | | | | | | | | | | | | | | | | | | | | | | | 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
* bpo-45020: Identify which frozen modules are actually aliases. (gh-28655)Eric Snow2021-10-051-4/+29
| | | | | | | 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
* bpo-45324: Capture data in FrozenImporter.find_spec() to use in ↵Eric Snow2021-10-051-8/+24
| | | | | | | | | | | 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
* bpo-43392: Optimize repeated calls to `__import__()` (GH-24735)Germán Méndez Bravo2021-08-121-5/+16
| | | | | | | | | | | | | | | | | | | | | Implements a two steps check in `importlib._bootstrap._find_and_load()` to avoid locking when the module has been already imported and it's ready. --- Using `importlib.__import__()`, after this, does show a big difference: Before: ``` $ ./python -c 'import timeit; print(timeit.timeit("__import__(\"timeit\")", setup="from importlib import __import__"))' 15.92248619502061 ``` After: ``` $ ./python -c 'import timeit; print(timeit.timeit("__import__(\"timeit\")", setup="from importlib import __import__"))' 1.206068897008663 ``` ---
* bpo-44717: improve AttributeError on circular imports of submodules (GH-27338)Filipe Laíns2021-07-241-2/+13
|
* Revert "bpo-44717: improve AttributeError on circular imports of submodules ↵Pablo Galindo Salgado2021-07-241-13/+2
| | | | | (GH-27299)" (GH-27331) This reverts commit 8072a1181dd64135f700b44372fbf7bf91e68072.
* bpo-44717: improve AttributeError on circular imports of submodules (GH-27299)Filipe Laíns2021-07-241-2/+13
| | | | | Signed-off-by: Filipe Laíns <lains@riseup.net> Co-authored-by: Łukasz Langa <lukasz@langa.pl>
* bpo-42135: Deprecate implementations of find_module() and find_loader() ↵Brett Cannon2021-04-061-0/+6
| | | | (GH-25169)
* bpo-42134: Raise ImportWarning when calling find_module() in the import ↵Brett Cannon2021-03-301-2/+3
| | | | system (GH-25044)
* bpo-42136: Deprecate module_repr() as found in importlib (GH-25022)Brett Cannon2021-03-261-2/+4
|
* bpo-42137: have ModuleType.__repr__ prefer __spec__ over module_repr() ↵Brett Cannon2021-03-241-14/+6
| | | | | (GH-24953) This is to work towards the removal of the use of module_repr() in Python 3.12 (documented as deprecated since 3.4).
* bpo-26131: Deprecate usage of load_module() (GH-23469)Brett Cannon2020-12-041-6/+18
| | | Raise an ImportWarning when the import system falls back on load_module(). As for implementations of load_module(), raise a DeprecationWarning.
* bpo-42403: Use @staticmethod in importlib (GH-23395)Victor Stinner2020-11-201-6/+6
| | | | | | | | | | | | | Use @staticmethod on methods using @classmethod but don't use their cls parameter on the following classes: * BuiltinImporter * FrozenImporter * WindowsRegistryFinder * PathFinder Leave methods using @_requires_builtin or @_requires_frozen unchanged, since this decorator requires the wrapped method to have an extra parameter (cls or self).
* bpo-42403: Simplify importlib external bootstrap (GH-23397)Victor Stinner2020-11-191-0/+7
| | | | | | | Simplify the importlib external bootstrap code: importlib._bootstrap_external now uses regular imports to import builtin modules. When it is imported, the builtin __import__() function is already fully working and so can be used to import builtin modules like sys.
* bpo-38091: Import deadlock detection causes deadlock (GH-17518)Armin Rigo2020-03-031-0/+9
| | | Automerge-Triggered-By: @brettcannon
* bpo-39336: Allow packages to not let their child modules be set on them (#18006)Dino Viehland2020-01-231-1/+6
| | | | | * bpo-39336: Allow setattr to fail on modules which aren't assignable When attaching a child module to a package if the object in sys.modules raises an AttributeError (e.g. because it is immutable) it causes the whole import to fail. This now allows immutable packages to exist and an ImportWarning is reported and the AttributeError exception is ignored.
* bpo-35923: Update the BuiltinImporter to use loader._ORIGIN instead of a ↵Dong-hee Na2019-09-111-2/+4
| | | | | | hard-coded value (GH-15651)
* bpo-37685: Fixed __eq__, __lt__ etc implementations in some classes. (GH-14952)Serhiy Storchaka2019-08-081-1/+1
| | | | They now return NotImplemented for unsupported type of the other operand.
* bpo-37444: Update differing exception between builtins and importlib (GH-14869)Ngalim Siregar2019-08-031-1/+1
| | | | | | | | | | Imports now raise `TypeError` instead of `ValueError` for relative import failures. This makes things consistent between `builtins.__import__` and `importlib.__import__` as well as using a more natural import for the failure. https://bugs.python.org/issue37444 Automerge-Triggered-By: @brettcannon
* bpo-35887: Add make regen-importlib step to importlib._bootstrap docstring ↵Nina Zakharenko2019-02-131-3/+3
| | | | (GH-11777)
* bpo-35321: Set the spec origin to frozen in frozen modules (#11732)Nina Zakharenko2019-02-051-2/+4
| | | | | | | | | | * bpo-35321: Set the spec origin to frozen in frozen modules This fix correctly sets the spec origin to "frozen" for the _frozen_importlib module. Note that the origin was already correctly set in _frozen_importlib_external. * 📜🤖 Added by blurb_it.
* bpo-33331: Clean modules in the reversed order in PyImport_Cleanup(). (GH-6565)Serhiy Storchaka2018-10-291-56/+62
| | | | | | Modules imported last are now cleared first at interpreter shutdown. A newly imported module is moved to the end of sys.modules, behind modules on which it depends.
* bpo-32946: Speed up "from ... import ..." from non-packages. (GH-5873)Serhiy Storchaka2018-03-111-26/+27
|
* bpo-32303 - Consistency fixes for namespace loaders (#5481)Barry Warsaw2018-02-021-0/+12
| | | | | | | * Make sure ``__spec__.loader`` matches ``__loader__`` for namespace packages. * Make sure ``__spec__.origin` matches ``__file__`` for namespace packages. https://bugs.python.org/issue32303 https://bugs.python.org/issue32305
* bpo-21720: Restore the Python 2.7 logic in handling a fromlist. (#4118)Serhiy Storchaka2017-10-261-7/+13
| | | | | | BytesWarning no longer emitted when the fromlist argument of __import__() or the __all__ attribute of the module contain bytes instances.
* bpo-31642: Restore blocking "from" import by setting None in sys.modules. ↵Serhiy Storchaka2017-10-081-1/+2
| | | | (#3834)
* Trivial cleanups following bpo-31370 (#3649)Antoine Pitrou2017-09-181-13/+1
| | | | | | * Trivial cleanups following bpo-31370 * Also cleanup the "importlib._bootstrap_external" module
* bpo-31070: Fix a race condition in importlib _get_module_lock(). (#3033)Serhiy Storchaka2017-08-091-2/+12
|
* bpo-30891: Fix again importlib _find_and_load() (#2665)Victor Stinner2017-07-211-5/+5
| | | | | | | | | Use sys.modules.get() in the "with _ModuleLockManager(name):" block to protect the dictionary key with the module lock and use an atomic get to prevent race condition. Remove also _bootstrap._POPULATE since it was unused (_bootstrap_external now has its own _POPULATE object), add a new _SENTINEL object instead.
* bpo-30876: Relative import from unloaded package now reimports the package ↵Serhiy Storchaka2017-07-121-4/+0
| | | | | | | | | (#2639) instead of failing with SystemError. Relative import from non-package now fails with ImportError rather than SystemError.
* bpo-30891: Fix importlib _find_and_load() race condition (#2646)Victor Stinner2017-07-101-28/+30
| | | | | | * Rewrite importlib _get_module_lock(): it is now responsible to hold the imp lock directly. * _find_and_load() now holds the module lock to check if name is in sys.modules to prevent a race condition
* bpo-30814: Fixed a race condition when import a submodule from a package. ↵Serhiy Storchaka2017-07-061-14/+14
| | | | (#2580)
* bpo-22257: Small changes for PEP 432. (#1728)Eric Snow2017-05-231-1/+4
| | | PEP 432 specifies a number of large changes to interpreter startup code, including exposing a cleaner C-API. The major changes depend on a number of smaller changes. This patch includes all those smaller changes.
* Issue #28026: Raise ImportError when exec_module() exists but ↵Eric Snow2016-09-081-3/+2
| | | | create_module() is missing.
* Issue #15767: Use ModuleNotFoundError.Eric Snow2016-09-071-7/+7
|