Commit Graph

299 Commits

Author SHA1 Message Date
Florian Bruhin 19710145b4
test_threaded_import: Fix unittest.main spelling (GH-93114) 2022-05-25 10:59:32 +03:00
Filipe Laíns c7d699969c
gh-92265: set meta_path and path_hooks correctly in test_reload_namespace_changed (GH-92275)
Previously, we were blocking the frozen imports and forcing the source
version to be used, but we did not fix up sys.meta_path or
sys.path_hooks, causing the frozen importers to leak into the source
version of the test.
2022-05-16 16:05:47 -07:00
Serhiy Storchaka 086c6b1b0f
bpo-45046: Support context managers in unittest (GH-28045)
Add methods enterContext() and enterClassContext() in TestCase.
Add method enterAsyncContext() in IsolatedAsyncioTestCase.
Add function enterModuleContext().
2022-05-08 17:49:09 +03:00
Jason R. Coombs 7659681556
gh-91298: Refine traversable (apply changes from importlib_resources 5.7.1) (#91623)
* bpo-47142: Refine traversable (apply changes from importlib_resources 5.7.1)

* Replace changelog referencing github issue.
2022-04-17 11:10:36 -04:00
Jason R. Coombs 67712e71b3
bpo-46126: Restore docstrings in importlib.metadata tests. (#32288) 2022-04-17 11:10:26 -04:00
Christian Heimes 2b16a08bc7
bpo-40280: Detect missing threading on WASM platforms (GH-32352)
Co-authored-by: Brett Cannon <brett@python.org>
2022-04-07 09:22:47 +02:00
Christian Heimes deeaac49e2
bpo-40280: Skip socket, fork, subprocess tests on Emscripten (GH-31986)
- Add requires_fork and requires_subprocess to more tests
- Skip extension import tests if dlopen is not available
- Don't assume that _testcapi is a shared extension
- Skip a lot of socket tests that don't work on Emscripten
- Skip mmap tests, mmap emulation is incomplete
- venv does not work yet
- Cannot get libc from executable

The "entire" test suite is now passing on Emscripten with EMSDK from git head (91 suites are skipped).
2022-03-22 03:04:36 -07:00
Jason R. Coombs b1e2868607
bpo-47004: Sync with importlib_metadata 4.11.3. (#31854) 2022-03-13 15:53:29 -04:00
Arie Bovenberg dd76b3f7d3
bpo-46246: add missing __slots__ to importlib.metadata.DeprecatedList (GH-30452)
Confirmed with @jaraco that this indeed needs a fix.

A question that came up while I was digging into the code: I think `SelectableGroups` could similarly use `__slots__ = ()`, since its purpose seems only for convenience around `dict`, not to have attributes of its own.

Automerge-Triggered-By: GH:jaraco
2022-02-10 16:56:21 -08:00
Steve Dower 89a0a90c2e
bpo-46616: Ensures test_importlib.test_windows cleans up registry keys after completion (GH-31086) 2022-02-02 19:54:27 +00:00
Jason R. Coombs 443dec6c9a
bpo-46474: Apply changes from importlib_metadata 4.10.0 (GH-30802) 2022-01-22 21:39:00 -05:00
Jason R. Coombs d888ff5381
bpo-46425: Partially revert "bpo-46425: fix direct invocation of `test_importlib` (GH-30682)" (GH-30799)
This reverts commit 57316c52ba for files pertaining to importlib.metadata and importlib.resources.
2022-01-22 21:38:26 -05:00
Nikita Sobolev 57316c52ba
bpo-46425: fix direct invocation of `test_importlib` (GH-30682) 2022-01-22 18:05:05 +02:00
Jason R. Coombs e712a5b277
bpo-46118: Move importlib.resources to its own package. (#30176)
* bpo-46118: Move importlib.resources to its own package.

* Expand compatibility shims with documentation and explicit imports.
2021-12-30 21:00:48 -05:00
Jason R. Coombs 9b52920173
bpo-46125: Refactor tests to test traversable API directly. Includes changes from importlib_resources 5.4.0. (GH-30189) 2021-12-18 21:28:49 -05:00
Jason R. Coombs 04deaee4c8
bpo-44893: Implement EntryPoint as simple class with attributes. (GH-30150)
* bpo-44893: Implement EntryPoint as simple class and deprecate tuple access in favor of attribute access. Syncs with importlib_metadata 4.8.1.

* Apply refactorings found in importlib_metadata 4.8.2.
2021-12-16 15:49:42 -05:00
Jason R. Coombs 109d966021
bpo-46105: Honor spec when generating requirement specs with urls and extras. (GH-30151) 2021-12-16 15:48:35 -05:00
Jason R. Coombs d5cd2effa6
bpo-45514: Deprecate importlib resources legacy functions. (GH-29036)
* bpo-45514: Apply changes from importlib_resources@a3ef4128c6

* Mark legacy functions as deprecated in the docs and link to the migration docs in importlib_resources docs.

* Apply changes from importlib_resources@329ae9d5f2c.

* Indicate importlib.resources as a module.

Co-authored-by: Filipe Laíns <lains@riseup.net>
2021-11-24 02:51:37 -05:00
Miro Hrončok ae1965ccb4
bpo-45703: Invalidate _NamespacePath cache on importlib.invalidate_ca… (GH-29384)
Consider the following directory structure:

    .
    └── PATH1
        └── namespace
            └── sub1
                └── __init__.py

And both PATH1 and PATH2 in sys path:

    $ PYTHONPATH=PATH1:PATH2 python3.11
    >>> import namespace
    >>> import namespace.sub1
    >>> namespace.__path__
    _NamespacePath(['.../PATH1/namespace'])
    >>> ...

While this interpreter still runs, PATH2/namespace/sub2 is created:

    .
    ├── PATH1
    │   └── namespace
    │       └── sub1
    │           └── __init__.py
    └── PATH2
        └── namespace
            └── sub2
                └── __init__.py

The newly created module cannot be imported:

    >>> ...
    >>> namespace.__path__
    _NamespacePath(['.../PATH1/namespace'])
    >>> import namespace.sub2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'namespace.sub2'

Calling importlib.invalidate_caches() now newly allows to import it:

    >>> import importlib
    >>> importlib.invalidate_caches()
    >>> namespace.__path__
    _NamespacePath(['.../PATH1/namespace'])
    >>> import namespace.sub2
    >>> namespace.__path__
    _NamespacePath(['.../PATH1/namespace', '.../PATH2/namespace'])

This was not previously possible.
2021-11-23 07:38:02 -08:00
Barry Warsaw 876fc7fcec
bpo-35673: Add a public alias for namespace package __loader__ attribute (#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.
2021-10-20 14:05:29 -07:00
Eric Snow 79cf20e48d
bpo-21736: Set __file__ on frozen stdlib modules. (gh-28656)
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-14 15:32:18 -06:00
Eric Snow 08285d563e
bpo-45020: Identify which frozen modules are actually aliases. (gh-28655)
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-05 11:26:37 -06:00
Eric Snow 69f6dabb9c
Rearrage the finder tests. (gh-28740)
This makes the tests a bit cleaner and makes adding more tests a little simpler.

https://bugs.python.org/issue45324
2021-10-05 10:37:14 -06:00
Eric Snow c3d9ac8b34
bpo-45324: Capture data in FrozenImporter.find_spec() to use in 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-05 10:01:27 -06:00
Eric Snow 7e5c107541
bpo-45020: Add more test cases for frozen modules. (gh-28664)
I've added a number of test-only modules. Some of those cases are covered by the recently frozen stdlib modules (and some will be once we add encodings back in). However, I figured we'd play it safe by having a set of modules guaranteed to be there during tests.

https://bugs.python.org/issue45020
2021-09-30 18:38:52 -06:00
Mohamad Mansour 8f943ca257
[codemod] Fix non-matching bracket pairs (GH-28473)
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-09-22 01:09:00 +02:00
Serhiy Storchaka 40348acc18
bpo-45229: Remove test_main in many tests (GH-28405)
Instead of explicitly enumerate test classes for run_unittest()
use the unittest ability to discover tests. This also makes these
tests discoverable and runnable with unittest.

load_tests() can be used for dynamic generating tests and adding
doctests. setUpModule(), tearDownModule() and addModuleCleanup()
can be used for running code before and after all module tests.
2021-09-19 15:27:33 +03:00
Eric Snow 3814e2036d
bpo-45019: Clean up the frozen __hello__ module. (gh-28374)
Here's one more small cleanup that should have been in PR gh-28319. We eliminate stdout side-effects from importing the frozen __hello__ module, and update tests accordingly. We also move the module's source file into Lib/ from Toos/freeze/flag.py.

https://bugs.python.org/issue45019
2021-09-15 14:15:32 -06:00
Eric Snow a65c86889e
bpo-45020: Add -X frozen_modules=[on|off] to explicitly control use of 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-14 17:31:45 -06:00
Jason R. Coombs 1cf8424a62
bpo-44784: Apply changes from importlib_metadata 4.6.3 (GH-27508)
Addressing issues with tests under error on warnings.

Automerge-Triggered-By: GH:jaraco
2021-07-31 06:08:13 -07:00
Jason R. Coombs aaa83cdfab
bpo-44771: Apply changes from importlib_resources 5.2.1 (GH-27436)
* bpo-44771: Apply changes from importlib_resources@3b24bd6307

* Add blurb

* Exclude namespacedata01 from eol conversion.
2021-07-29 21:05:05 -04:00
Miro Hrončok df1502e47f
bpo-44451: Reset DeprecationWarning filters in test_importlib.test_entry_points_by_index (GH-26784)
This avoids the following error if DeprecationWarnings are ignored.

    ======================================================================
    ERROR: test_entry_points_by_index (test.test_importlib.test_metadata_api.APITests)
    Prior versions of Distribution.entry_points would return a
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/builddir/build/BUILD/Python-3.10.0b3/Lib/test/test_importlib/test_metadata_api.py", line 145, in test_entry_points_by_index
        expected = next(iter(caught))
    StopIteration
    ----------------------------------------------------------------------
    Ran 1402 tests in 2.125s
    FAILED (errors=1, skipped=18, expected failures=1)
2021-06-18 16:02:45 -04:00
Jason R. Coombs c34ed08d97
bpo-44246: Restore compatibility in entry_points (GH-26468)
* bpo-44246: Entry points performance improvements.

From importlib_metadata 4.3.1.

* bpo-44246: Sync with importlib_metadata 4.4
2021-05-31 12:19:42 -04:00
Jason R. Coombs 410b70d39d
bpo-44246: Entry points performance improvements. (GH-26467)
From importlib_metadata 4.3.1.
2021-05-31 11:52:29 -04:00
Jason R. Coombs f6fbdb90ee
bpo-38693: Prefer f-strings in importlib.resources (importlib_resources 5.0.6). (GH-26387)
Automerge-Triggered-By: GH:jaraco
2021-05-26 13:16:11 -07:00
Jason R. Coombs 875b3d84b3
bpo-43643: Sync with python/importlib_resources@c17a610aad. (GH-26284)
* bpo-43643: Sync with python/importlib_resources@c17a610aad.
* Sync with python/importlib_resources@89fd5e961a.
2021-05-21 13:00:40 -04:00
Jason R. Coombs 37e0c7850d
bpo-43926: Cleaner metadata with PEP 566 JSON support. (GH-25565)
* bpo-43926: Cleaner metadata with PEP 566 JSON support.

* Add blurb

* Add versionchanged and versionadded declarations for changes to metadata.

* Use descriptor for PEP 566
2021-05-02 17:03:40 -04:00
Shreyan Avigyan e3bf179642
bpo-43864: Silence deprecation warning in test_importlib.test_module_found and test_importlib.test_module_not_found (GH-25656) 2021-04-27 16:56:08 +01:00
Jason R. Coombs c6ca368867
bpo-43780: Sync with importlib_metadata 3.10 (GH-25297)
* bpo-43780: Sync with importlib_metadata 3.10.

* Add blurb

* Apply changes from importlib_metadata 3.10.1.
2021-04-24 10:13:51 -04:00
Brett Cannon 22556d84bc
Silence find_module() DeprecationWarning on Windows tests (GH-25563) 2021-04-23 14:40:18 -07:00
Steve Dower 04732ca993
bpo-43105: Importlib now resolves relative paths when creating module spec objects from file locations (GH-25121) 2021-04-07 01:02:07 +01:00
Brett Cannon 57c6cb5100
bpo-42135: Deprecate implementations of find_module() and find_loader() (GH-25169) 2021-04-06 08:56:57 -07:00
Inada Naoki c8e5eb904e
bpo-43651: PEP 597: Fix EncodingWarning in some tests (GH-25181)
* Fix test_shutil
* Fix test_imp
* Fix test_import
* Fix test_importlib
2021-04-05 13:11:23 +09:00
Brett Cannon f97dc80068
bpo-43672: raise ImportWarning when calling find_loader() (GH-25119) 2021-04-02 12:35:32 -07:00
Brett Cannon a7ff6df60c
bpo-42134: Raise ImportWarning when calling find_module() in the import system (GH-25044) 2021-03-30 08:43:03 -07:00
Brett Cannon 1899087b21
bpo-42136: Deprecate module_repr() as found in importlib (GH-25022) 2021-03-26 11:55:07 -07:00
Brett Cannon 9cb31d6716
bpo-42137: have ModuleType.__repr__ prefer __spec__ over module_repr() (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).
2021-03-24 08:26:56 -07:00
Antoine Pitrou 2fd16ef406
bpo-43517: Fix false positive in detection of circular imports (#24895) 2021-03-20 20:07:44 +01:00
Jason R. Coombs f917efccf8
bpo-43428: Sync with importlib_metadata 3.7. (GH-24782)
* bpo-43428: Sync with importlib_metadata 3.7.2 (67234b6)

* Add blurb

* Reformat blurb to create separate paragraphs for each change included.
2021-03-13 11:31:45 -05:00
Jason R. Coombs 6714825414
bpo-42129: Add support for resources in namespaces (GH-24670)
* Unify behavior in ResourceReaderDefaultsTests and align with the behavior found in importlib_resources.
* Equip NamespaceLoader with a NamespaceReader.
* Apply changes from importlib_resources 5.0.4
2021-03-04 13:43:00 -05:00