Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-98040: Remove find_loader, find_module and other deprecated APIs #98059

Merged
merged 39 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e714588
Checkpointing
warsaw Oct 7, 2022
90ca658
Remove deprecated classes from pkgutil
warsaw Oct 7, 2022
571daa4
Adjust some tests
warsaw Oct 7, 2022
e798342
More test fixing
warsaw Oct 7, 2022
acb5fb4
Remove more obsolete tests
warsaw Oct 7, 2022
40d9201
Remove some other PEP 302 obsolescence
warsaw Oct 7, 2022
455cc68
Use find_spec instead of load_module
warsaw Oct 7, 2022
bef8f9f
Remove more tests of PEP 302 obsolete APIs
warsaw Oct 7, 2022
96cdada
Remove another bunch of tests using obsolete load_modules()
warsaw Oct 7, 2022
d6adbd5
Merge branch 'main' into find_loader
warsaw Oct 7, 2022
897998f
Remove deleted names from __all__
warsaw Oct 7, 2022
1b514bb
Merge branch 'main' into find_loader
warsaw Oct 9, 2022
9708e1c
Remove obsolete footnote
warsaw Oct 9, 2022
5e33c7e
imp is removed
warsaw Oct 9, 2022
73988d8
Remove `imp` from generated stdlib names
warsaw Oct 9, 2022
4832f66
What's new and blurb
warsaw Oct 9, 2022
64d16c9
More cleanup
warsaw Oct 9, 2022
cbed6ce
Merge branch 'main' into find_loader
warsaw Oct 10, 2022
dd9d60f
Merge branch 'main' into find_loader
warsaw Oct 15, 2022
d61d2d6
Merge branch 'main' into find_loader
warsaw Oct 16, 2022
a801a7b
Merge branch 'main' into find_loader
warsaw Oct 17, 2022
8671f31
Merge branch 'main' into find_loader
warsaw Oct 18, 2022
5c5d749
Merge branch 'main' into find_loader
warsaw Oct 18, 2022
4825990
Merge branch 'main' into find_loader
warsaw Apr 29, 2023
6c0d6b6
Clean up a few docs
warsaw Apr 29, 2023
80909dd
Merge branch 'main' into find_loader
warsaw Apr 29, 2023
8e5ae79
Update zipimport documentation for the removed methods
warsaw Apr 29, 2023
afd921a
Revert back to using pkgutil._get_spec()
warsaw Apr 29, 2023
c1ab102
Fix some Windows tests
warsaw Apr 29, 2023
e266721
Update Doc/reference/import.rst
warsaw Apr 29, 2023
797a8f8
Merge branch 'main' into find_loader
warsaw Apr 29, 2023
0ae7690
We don't need pkgutil._get_spec() any more either
warsaw Apr 29, 2023
746d515
More things we don't need anymore
warsaw Apr 29, 2023
3299a19
Merge branch 'main' into find_loader
warsaw May 1, 2023
d06a9d0
Merge branch 'main' into find_loader
warsaw May 2, 2023
41ed787
Suppress cross-references to find_loader and find_module
AA-Turner May 2, 2023
378c3d9
Suppress cross-references to Finder
AA-Turner May 2, 2023
d7100cb
Suppress cross-references to pkgutil.ImpImporter and pkgutil.ImpLoader
AA-Turner May 2, 2023
2e24b8b
Merge branch 'main' into find_loader
warsaw May 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 0 additions & 108 deletions Doc/library/importlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,6 @@ Functions
.. versionchanged:: 3.3
Parent packages are automatically imported.

.. function:: find_loader(name, path=None)

Find the loader for a module, optionally within the specified *path*. If the
module is in :attr:`sys.modules`, then ``sys.modules[name].__loader__`` is
returned (unless the loader would be ``None`` or is not set, in which case
:exc:`ValueError` is raised). Otherwise a search using :attr:`sys.meta_path`
is done. ``None`` is returned if no loader is found.

A dotted name does not have its parents implicitly imported as that requires
loading them and that may not be desired. To properly import a submodule you
will need to import all parent packages of the submodule and use the correct
argument to *path*.

.. versionadded:: 3.3

.. versionchanged:: 3.4
If ``__loader__`` is not set, raise :exc:`ValueError`, just like when the
attribute is set to ``None``.

.. deprecated:: 3.4
Use :func:`importlib.util.find_spec` instead.

.. function:: invalidate_caches()

Invalidate the internal caches of finders stored at
Expand Down Expand Up @@ -247,7 +225,6 @@ are also provided to help in implementing the core ABCs.
ABC hierarchy::

object
+-- Finder (deprecated)
+-- MetaPathFinder
+-- PathEntryFinder
+-- Loader
Expand All @@ -258,28 +235,6 @@ ABC hierarchy::
+-- SourceLoader


.. class:: Finder
AA-Turner marked this conversation as resolved.
Show resolved Hide resolved

An abstract base class representing a :term:`finder`.

.. deprecated:: 3.3
Use :class:`MetaPathFinder` or :class:`PathEntryFinder` instead.

.. abstractmethod:: find_module(fullname, path=None)

An abstract method for finding a :term:`loader` for the specified
module. Originally specified in :pep:`302`, this method was meant
for use in :data:`sys.meta_path` and in the path-based import subsystem.

.. versionchanged:: 3.4
Returns ``None`` when called instead of raising
:exc:`NotImplementedError`.

.. deprecated:: 3.10
Implement :meth:`MetaPathFinder.find_spec` or
:meth:`PathEntryFinder.find_spec` instead.


.. class:: MetaPathFinder

An abstract base class representing a :term:`meta path finder`.
Expand All @@ -303,25 +258,6 @@ ABC hierarchy::

.. versionadded:: 3.4

.. method:: find_module(fullname, path)

A legacy method for finding a :term:`loader` for the specified
module. If this is a top-level import, *path* will be ``None``.
Otherwise, this is a search for a subpackage or module and *path*
will be the value of :attr:`__path__` from the parent
package. If a loader cannot be found, ``None`` is returned.

If :meth:`find_spec` is defined, backwards-compatible functionality is
provided.

.. versionchanged:: 3.4
Returns ``None`` when called instead of raising
:exc:`NotImplementedError`. Can use :meth:`find_spec` to provide
functionality.

.. deprecated:: 3.4
Use :meth:`find_spec` instead.

.. method:: invalidate_caches()

An optional method which, when called, should invalidate any internal
Expand Down Expand Up @@ -356,36 +292,6 @@ ABC hierarchy::

.. versionadded:: 3.4

.. method:: find_loader(fullname)

A legacy method for finding a :term:`loader` for the specified
module. Returns a 2-tuple of ``(loader, portion)`` where ``portion``
is a sequence of file system locations contributing to part of a namespace
package. The loader may be ``None`` while specifying ``portion`` to
signify the contribution of the file system locations to a namespace
package. An empty list can be used for ``portion`` to signify the loader
is not part of a namespace package. If ``loader`` is ``None`` and
``portion`` is the empty list then no loader or location for a namespace
package were found (i.e. failure to find anything for the module).

If :meth:`find_spec` is defined then backwards-compatible functionality is
provided.

.. versionchanged:: 3.4
Returns ``(None, [])`` instead of raising :exc:`NotImplementedError`.
Uses :meth:`find_spec` when available to provide functionality.

.. deprecated:: 3.4
Use :meth:`find_spec` instead.

.. method:: find_module(fullname)

A concrete implementation of :meth:`Finder.find_module` which is
equivalent to ``self.find_loader(fullname)[0]``.

.. deprecated:: 3.4
Use :meth:`find_spec` instead.

.. method:: invalidate_caches()

An optional method which, when called, should invalidate any internal
Expand Down Expand Up @@ -881,13 +787,6 @@ find and load modules.
is no longer valid then ``None`` is returned but no value is cached
in :data:`sys.path_importer_cache`.

.. classmethod:: find_module(fullname, path=None)

A legacy wrapper around :meth:`find_spec`.

.. deprecated:: 3.4
Use :meth:`find_spec` instead.

.. classmethod:: invalidate_caches()

Calls :meth:`importlib.abc.PathEntryFinder.invalidate_caches` on all
Expand Down Expand Up @@ -938,13 +837,6 @@ find and load modules.

.. versionadded:: 3.4

.. method:: find_loader(fullname)

Attempt to find the loader to handle *fullname* within :attr:`path`.

.. deprecated:: 3.10
Use :meth:`find_spec` instead.

.. method:: invalidate_caches()

Clear out the internal cache.
Expand Down
27 changes: 0 additions & 27 deletions Doc/library/pkgutil.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,6 @@ support.
this function to raise an exception (in line with :func:`os.path.isdir`
behavior).


.. class:: ImpImporter(dirname=None)

:pep:`302` Finder that wraps Python's "classic" import algorithm.

If *dirname* is a string, a :pep:`302` finder is created that searches that
directory. If *dirname* is ``None``, a :pep:`302` finder is created that
searches the current :data:`sys.path`, plus any modules that are frozen or
built-in.

Note that :class:`ImpImporter` does not currently support being used by
placement on :data:`sys.meta_path`.

.. deprecated:: 3.3
This emulation is no longer needed, as the standard import mechanism
is now fully :pep:`302` compliant and available in :mod:`importlib`.


.. class:: ImpLoader(fullname, file, filename, etc)

:term:`Loader <loader>` that wraps Python's "classic" import algorithm.

.. deprecated:: 3.3
This emulation is no longer needed, as the standard import mechanism
is now fully :pep:`302` compliant and available in :mod:`importlib`.


.. function:: find_loader(fullname)

Retrieve a module :term:`loader` for the given *fullname*.
Expand Down
27 changes: 5 additions & 22 deletions Doc/library/zipimport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ zipimporter Objects
:exc:`ZipImportError` is raised if *archivepath* doesn't point to a valid ZIP
archive.

.. versionchanged:: 3.12

Methods ``find_loader()`` and ``find_module()``, deprecated in 3.10 are
now removed. Use :meth:`find_spec` instead.

.. method:: create_module(spec)

Implementation of :meth:`importlib.abc.Loader.create_module` that returns
Expand All @@ -89,28 +94,6 @@ zipimporter Objects
.. versionadded:: 3.10


.. method:: find_loader(fullname, path=None)

An implementation of :meth:`importlib.abc.PathEntryFinder.find_loader`.

.. deprecated:: 3.10

Use :meth:`find_spec` instead.


.. method:: find_module(fullname, path=None)

Search for a module specified by *fullname*. *fullname* must be the fully
qualified (dotted) module name. It returns the zipimporter instance itself
if the module was found, or :const:`None` if it wasn't. The optional
*path* argument is ignored---it's there for compatibility with the
importer protocol.

.. deprecated:: 3.10

Use :meth:`find_spec` instead.


.. method:: find_spec(fullname, target=None)

An implementation of :meth:`importlib.abc.PathEntryFinder.find_spec`.
Expand Down
16 changes: 8 additions & 8 deletions Doc/reference/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ modules, and one that knows how to import modules from an :term:`import path`
Use of :meth:`~importlib.abc.MetaPathFinder.find_module` by the import system
now raises :exc:`ImportWarning`.

.. versionchanged:: 3.12
``find_module()`` has been removed. Use ``find_spec()`` instead.
warsaw marked this conversation as resolved.
Show resolved Hide resolved


Loading
=======
Expand Down Expand Up @@ -837,7 +840,7 @@ stores finder objects rather than being limited to :term:`importer` objects).
In this way, the expensive search for a particular :term:`path entry`
location's :term:`path entry finder` need only be done once. User code is
free to remove cache entries from :data:`sys.path_importer_cache` forcing
the path based finder to perform the path entry search again [#fnpic]_.
the path based finder to perform the path entry search again.

If the path entry is not present in the cache, the path based finder iterates
over every callable in :data:`sys.path_hooks`. Each of the :term:`path entry
Expand Down Expand Up @@ -887,7 +890,7 @@ module. ``find_spec()`` returns a fully populated spec for the module.
This spec will always have "loader" set (with one exception).

To indicate to the import machinery that the spec represents a namespace
:term:`portion`, the path entry finder sets "submodule_search_locations" to
:term:`portion`, the path entry finder sets ``submodule_search_locations`` to
a list containing the portion.

.. versionchanged:: 3.4
Expand Down Expand Up @@ -924,6 +927,9 @@ a list containing the portion.
:meth:`~importlib.abc.PathEntryFinder.find_loader` by the import
system will raise :exc:`ImportWarning`.

.. versionchanged:: 3.12
``find_module()`` and ``find_loader()`` have been removed.


Replacing the standard import system
====================================
Expand Down Expand Up @@ -1073,9 +1079,3 @@ methods to finders and loaders.
module may replace itself in :data:`sys.modules`. This is
implementation-specific behavior that is not guaranteed to work in other
Python implementations.

.. [#fnpic] In legacy code, it is possible to find instances of
:class:`imp.NullImporter` in the :data:`sys.path_importer_cache`. It
is recommended that code be changed to use ``None`` instead. See
:ref:`portingpythoncode` for more details. Note that the ``imp`` module
was removed in Python 3.12.
16 changes: 11 additions & 5 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -972,14 +972,20 @@ Removed
* Many previously deprecated cleanups in :mod:`importlib` have now been
completed:

* References to, and support for ``module_repr()`` has been eradicated.
* References to, and support for ``module_repr()`` has been removed.
(Contributed by Barry Warsaw in :gh:`97850`.)

* ``importlib.util.set_package`` has been removed.
(Contributed by Brett Cannon in :gh:`65961`.)
* ``importlib.util.set_package`` has been removed. (Contributed by Brett
Cannon in :gh:`65961`.)

* Support for ``find_loader()`` and ``find_module()`` APIs have been
removed. (Contributed by Barry Warsaw in :gh:`98040`.)

* ``importlib.abc.Finder``, ``pkg.ImpImporter``, and ``pkg.ImpLoader`` have
been removed. (Contributed by Barry Warsaw in :gh:`98040`.)

* The ``imp`` module has been removed. (Contributed by Barry Warsaw in
:gh:`98040`.)
* The ``imp`` module has been removed. (Contributed by Barry Warsaw in
:gh:`98040`.)

* Removed the ``suspicious`` rule from the documentation Makefile, and
removed ``Doc/tools/rstlint.py``, both in favor of `sphinx-lint
Expand Down
34 changes: 0 additions & 34 deletions Lib/importlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,40 +70,6 @@ def invalidate_caches():
finder.invalidate_caches()


def find_loader(name, path=None):
"""Return the loader for the specified module.

This is a backward-compatible wrapper around find_spec().

This function is deprecated in favor of importlib.util.find_spec().

"""
warnings.warn('Deprecated since Python 3.4 and slated for removal in '
'Python 3.12; use importlib.util.find_spec() instead',
DeprecationWarning, stacklevel=2)
try:
loader = sys.modules[name].__loader__
if loader is None:
raise ValueError(f'{name}.__loader__ is None')
else:
return loader
except KeyError:
pass
except AttributeError:
raise ValueError(f'{name}.__loader__ is not set') from None

spec = _bootstrap._find_spec(name, path)
# We won't worry about malformed specs (missing attributes).
if spec is None:
return None
if spec.loader is None:
if spec.submodule_search_locations is None:
raise ImportError(f'spec for {name} missing loader', name=name)
raise ImportError('namespace packages do not have loaders',
name=name)
return spec.loader


def import_module(name, package=None):
"""Import a module.

Expand Down
Loading