Commit graph

883 commits

Author SHA1 Message Date
Stefano Rivera
656a64b37f
gh-141930: Use the regular IO stack to write .pyc files for a better error message on failure (GH-141931)
* Use open() to write the bytecode
* Convert to unittest style asserts
* Tweak news, thanks @vstinner
* Tidy
* reword NEWS, avoid word "retried"
2025-11-27 19:17:59 +00:00
Guo Ci
453d886f85
GH-90344: replace single-call io.IncrementalNewlineDecoder usage with non-incremental newline decoders (GH-30276)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Brett Cannon <brett@python.org>
2025-11-15 00:13:37 +00:00
Serhiy Storchaka
d8e6bdc0d0
gh-135801: Add the module parameter to compile() etc (GH-139652)
Many functions related to compiling or parsing Python code, such as
compile(), ast.parse(), symtable.symtable(),
and importlib.abc.InspectLoader.source_to_code() now allow to pass
the module name used when filtering syntax warnings.
2025-11-13 13:21:32 +02:00
Filipe Laíns
ede5693be1
GH-119668: expose importlib.machinery.NamespacePath (#119669)
* GH-119668: expose importlib.NamespacePath

Signed-off-by: Filipe Laíns <lains@riseup.net>

* add news

Signed-off-by: Filipe Laíns <lains@riseup.net>

* add to docs

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Apply suggestions from code review

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>

* Fix news (importlib.NamespacePath > importlib.machinery.NamespacePath)

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Link to module.__path__ in NamespacePath docs

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Mention the path argument in the documentation

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Simplify docs text

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Highlight argument names in docs text

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Update Lib/importlib/_bootstrap_external.py

Co-authored-by: Brett Cannon <brett@python.org>

* Rewrite NamespacePath's doc

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Specify path_finder's type in the NamespacePath docstring

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Fix doc tests

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Apply suggestions from code review

Co-authored-by: Barry Warsaw <barry@python.org>

* Fix doc lint

Signed-off-by: Filipe Laíns <lains@riseup.net>

* Update Doc/library/importlib.rst

Co-authored-by: Brett Cannon <brett@python.org>

---------

Signed-off-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Brett Cannon <brett@python.org>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Barry Warsaw <barry@python.org>
2025-11-01 00:39:48 +00:00
Petr Viktorin
3416e7c8dc
gh-140633: AppleFrameworkLoader: Ignore AttributeError when setting __file__ (GH-140635) 2025-10-27 10:09:22 +01:00
Bartosz Sławecki
bcced02604
gh-140141: Properly break exception chain in importlib.metadata.Distribution.from_name (#140142) 2025-10-15 12:49:14 -04:00
Semyon Moroz
8d17d79299
gh-138044: Remove deprecated parameter alias for importlib.resources.files (#138059) 2025-10-04 14:53:43 +01:00
Lisa Roach
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
Jacob Walls
92b2a8a04d
gh-93334: Fix homonym edge case in PathFinder.find_spec() (GH-98100) 2025-09-05 15:41:01 -07:00
Jason R. Coombs
9b38c6698a
Reapply "gh-132947: Apply changes from importlib_metadata 8.7 (#137885)" (#137924) (#137930)
* Reapply "gh-132947: Apply changes from importlib_metadata 8.7 (#137885)" (#137924)

This reverts commit 3706ef66ef.

* Skip the triggering test on buildbots only.
2025-09-01 11:27:01 -04:00
Jason R. Coombs
3706ef66ef
Revert "gh-132947: Apply changes from importlib_metadata 8.7 (#137885)" (#137924)
This reverts commit 5292fc00f2.
2025-08-18 20:57:36 +00:00
Jason R. Coombs
5292fc00f2
gh-132947: Apply changes from importlib_metadata 8.7 (#137885)
* Copied files from python/importlib_metadata@b67ac80c49.
2025-08-18 10:54:10 -07:00
Petr Viktorin
d8a9466e29
bpo-38735: Don't fail when importing from / with sys.pycache_prefix set (GH-30456)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2025-08-18 13:53:01 +02:00
Jason R. Coombs
ec4021c6d7
gh-120492: Sync importlib_metadata 8.2.0 (#124033)
* Sync with importlib_metadata 8.2.0

Removes deprecated behaviors, including support for `PackageMetadata.__getitem__` returning None for missing keys and Distribution subclasses not implementing abstract methods.
Prioritizes valid dists to invalid dists when retrieving by name (python/cpython/#120492). Adds SimplePath to `importlib.metadata.__all__`.

* Add blurb
2025-08-15 14:19:23 -07:00
Brett Cannon
7140b99b0d
GH-137426: Remove code deprecation of importlib.abc.ResourceLoader (GH-137567)
Enough other classes in `importlib.abc` inherit from the class and the deprecation was to redirect people to `TraversableResources`. The documentation now makes it clear the class only exists for backwards compatibility.

---------

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
2025-08-11 16:18:54 -07:00
sobolevn
f7305a06c7
gh-115942: Add locked to several multiprocessing locks (#115944)
Co-authored-by: mpage <mpage@cs.stanford.edu>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
2025-04-08 11:14:12 +03:00
Sam Gross
857bdba0ac
gh-130094: Fix race conditions in importlib (gh-130101)
Entries may be added or removed from `sys.meta_path` concurrently. For
example, setuptools temporarily adds and removes the `distutils` finder from
the beginning of the list. The local copy ensures that we don't skip over any
entries.

Some packages modify `sys.modules` during import. For example, `collections`
inserts the entry for `collections.abc`  into `sys.modules` during import. We
need to ensure that we re-check `sys.modules` *after* the parent module is
fully initialized.
2025-02-18 18:02:42 -05:00
Tomas R.
aa81a6f6e4
gh-97850: Update the deprecation warning of importlib.abc.Loader.load_module (GH-129855) 2025-02-11 11:04:16 -08:00
Alex Willmer
a8dc6d6d44
gh-115911: Ignore PermissionError during import from cwd (#116131)
Ignore PermissionError when checking cwd during import

On macOS `getcwd(3)` can return EACCES if a path component isn't readable,
resulting in PermissionError. `PathFinder.find_spec()` now catches these and
ignores them - the same treatment as a missing/deleted cwd.

Introduces `test.support.os_helper.save_mode(path, ...)`, a context manager
that restores the mode of a path on exit.

This is allows finer control of exception handling and robust environment
restoration across platforms in `FinderTests.test_permission_error_cwd()`.

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
Co-authored-by: Brett Cannon <brett@python.org>
2025-01-26 19:00:28 +00:00
Jason R. Coombs
b543b32eff
gh-123987: Fix NotADirectoryError in NamespaceReader when sentinel present (#124018) 2025-01-26 16:23:54 +00:00
Thomas Grainger
d05140f9f7
gh-121604: fix ResourceLoader deprecation warning message (GH-128859) 2025-01-15 21:13:59 +00:00
Tomas R.
bd3baa8b1a
gh-121604: Make sure all deprecated items in importlib raise DeprecationWarning (#128007)
Co-authored-by: rashansmith <smith.rashan@gmail.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Brett Cannon <brett@python.org>
2025-01-14 16:48:46 -08:00
CF Bolz-Tereick
c695e37a3f
GH-126606: don't write incomplete pyc files (GH-126627)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Co-authored-by: Brett Cannon <brett@python.org>
2024-11-13 21:39:10 +00:00
Alex Waygood
c5c21fee7a
gh-125519: Improve traceback if importlib.reload() is called with a non-module object (#125520) 2024-10-21 07:53:21 +01:00
Jason R. Coombs
a53812df12
gh-123085: Fix issue in inferred caller when resources package has no source (#123102)
gh-123085: Fix issue in inferred caller when resources package has no source.

From importlib_resources 6.4.3 (python/importlib_resources#314).
2024-09-12 19:08:06 +00:00
Jason R. Coombs
ba687d9481
gh-121735: Fix module-adjacent references in zip files (#123037)
* gh-116608: Apply style and compatibility changes from importlib_metadata.

* gh-121735: Ensure module-adjacent resources are loadable from a zipfile.

* gh-121735: Allow all modules to be processed by the ZipReader.

* Add blurb

* Remove update-zips script, unneeded.

* Remove unnecessary references to removed static fixtures.

* Remove zipdata fixtures, unused.
2024-09-11 22:33:07 -04:00
abstractee
1f4a49ea53
Fix typos in warnings, docstrings, comments and text files (#123597) 2024-09-03 02:20:40 +02:00
Jason R. Coombs
e913d2c87f
gh-116608: Apply style and compatibility changes from importlib_metadata. (#123028) 2024-08-15 15:32:05 -04:00
Eric Snow
ee1b8ce26e
gh-122907: Fix Builds Without HAVE_DYNAMIC_LOADING Set (gh-122952)
As of 529a160 (gh-118204), building with HAVE_DYNAMIC_LOADING stopped working.  This is a minimal fix just to get builds working again.  There are actually a number of long-standing deficiencies with HAVE_DYNAMIC_LOADING builds that need to be resolved separately.
2024-08-13 14:44:57 -06:00
Serhiy Storchaka
03b88522f5
gh-122188: Remove _imp.pyc_magic_number (GH-122503)
_imp.pyc_magic_number_token should be enough.
2024-08-02 13:12:19 +03:00
Bénédikt Tran
88030861e2
gh-122555: Remove removed functions from Doc/data/refcounts.dat (#122556) 2024-08-01 12:26:09 +02:00
Michael Droettboom
af0a00f022
gh-122188: Move magic number to its own file (#122243)
* 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>
2024-07-30 15:31:05 -04:00
Mark Shannon
2e14a52cce
GH-122160: Remove BUILD_CONST_KEY_MAP opcode. (GH-122164) 2024-07-25 16:24:29 +01:00
Chris Markiewicz
94f50f8ee6
gh-117983: Defer import of threading for lazy module loading (#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.
2024-07-03 20:50:46 +00:00
Jason R. Coombs
1ba0bb21ed
gh-120910: Fix issue resolving relative paths outside site-packages. (#120911)
Incorporates changes from importlib_metadata 7.2.1.
2024-06-23 13:06:07 -04:00
Mark Shannon
9cefcc0ee7
GH-120507: Lower the BEFORE_WITH and BEFORE_ASYNC_WITH instructions. (#120640)
* Remove BEFORE_WITH and BEFORE_ASYNC_WITH instructions.

* Add LOAD_SPECIAL instruction

* Reimplement `with` and `async with` statements using LOAD_SPECIAL
2024-06-18 12:17:46 +01:00
Victor Stinner
05df063ad8
gh-120417: Fix "imported but unused" linter warnings (#120461)
Add __all__ to the following modules:
importlib.machinery, importlib.util and xml.sax.

Add also "# noqa: F401" in collections.abc,
subprocess and xml.sax.

* Sort __all__; remove collections.abc.__all__; remove private names

* Add tests
2024-06-14 20:39:50 +02:00
Victor Stinner
4c6d4f5cb3
gh-120417: Remove unused imports in the stdlib (#120420) 2024-06-12 20:56:42 +02:00
Jason R. Coombs
8d63c8d47b
gh-106531: Apply changes from importlib_resources 6.3.2 (#117054)
Apply changes from importlib_resources 6.3.2.
2024-06-04 06:36:28 +00:00
Hugo van Kemenade
0751511d24
gh-93963: Remove deprecated names from importlib.abc (#119720)
Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
2024-05-29 11:08:27 -06:00
Jelle Zijlstra
a9a74da4a0
gh-119311: Fix name mangling with PEP 695 generic classes (#119464)
Fixes #119311. Fixes #119395.
2024-05-27 19:50:38 -07:00
Jelle Zijlstra
84be5244de
gh-119180: Update the magic number (#119397)
PR #119321 added a comment about the magic number bump
but did not actually apply the new magic number.
2024-05-24 17:32:17 -07:00
Jelle Zijlstra
98e855fcc1
gh-119180: Add LOAD_COMMON_CONSTANT opcode (#119321)
The PEP 649 implementation will require a way to load NotImplementedError
from the bytecode. @markshannon suggested implementing this by converting
LOAD_ASSERTION_ERROR into a more general mechanism for loading constants.

This PR adds this new opcode. I will work on the rest of the implementation
of the PEP separately.

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
2024-05-22 00:46:39 +00:00
wim glenn
b7778d5902
Fix typo inaccuracy in _bootstrap_external.py (GH-118619) 2024-05-07 10:52:48 +02:00
Serhiy Storchaka
153b3f7530
gh-118465: Add __firstlineno__ attribute to class (GH-118475)
It is set by compiler with the line number of the first line of
the class definition.
2024-05-06 12:02:37 +03:00
wim glenn
d5e6c7cb66
fix comment typo in importlib (#118567) 2024-05-04 14:46:32 +01:00
Chris Markiewicz
19a2202067
gh-117182: Allow lazily loaded modules to modify their own __class__ 2024-04-09 04:08:48 +01:00
Petr Viktorin
9ceaee74db
gh-116608: importlib.resources: Un-deprecate functional API & add subdirectory support (GH-116609) 2024-04-05 13:55:59 +02:00
Chris Markiewicz
9a1e55b8c5
gh-117178: Recover lazy loading of self-referential modules (#117179) 2024-03-28 12:59:31 +02:00
Tim Hatch
b44898299a
gh-89739: gh-77140: Support zip64 in zipimport (GH-94146)
* Reads zip64 files as produced by the zipfile module
* Include tests (somewhat slow, however, because of the need to create "large" zips)
* About the same amount of strictness reading invalid zip files as zipfile has
* Still works on files with prepended data (like pex)

There are a lot more test cases at https://github.com/thatch/zipimport64/ that give me confidence that this works for real-world files.

Fixes #89739 and #77140.

---------

Co-authored-by: Itamar Ostricher <itamarost@gmail.com>
Reviewed-by: Gregory P. Smith <greg@krypto.org>
2024-03-28 06:54:51 +00:00