Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Remove additional references to the imp module
  • Loading branch information
warsaw committed Apr 27, 2023
commit f560b9928321d317741ad88e6367dac409557a79
2 changes: 2 additions & 0 deletions Doc/c-api/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ Importing Modules
.. versionchanged:: 3.3
Uses :func:`imp.source_from_cache()` in calculating the source path if
only the bytecode path is provided.
.. versionchanged:: 3.12
No longer uses the removed ``imp`` module.


.. c:function:: long PyImport_GetMagicNumber()
Expand Down
1 change: 0 additions & 1 deletion Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,6 @@ are always available. They are listed here in alphabetical order.

.. index::
statement: import
module: imp

.. note::

Expand Down
3 changes: 2 additions & 1 deletion Doc/reference/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1077,4 +1077,5 @@ methods to finders and loaders.
.. [#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.
:ref:`portingpythoncode` for more details. Note that the ``imp`` module
was removed in Python 3.12.
1 change: 0 additions & 1 deletion Doc/tools/.nitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ Doc/library/http.cookiejar.rst
Doc/library/http.cookies.rst
Doc/library/http.server.rst
Doc/library/idle.rst
Doc/library/imp.rst
Doc/library/importlib.resources.abc.rst
Doc/library/importlib.resources.rst
Doc/library/importlib.rst
Expand Down
2 changes: 1 addition & 1 deletion Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def getdocloc(self, object, basedir=sysconfig.get_path('stdlib')):

basedir = os.path.normcase(basedir)
if (isinstance(object, type(os)) and
(object.__name__ in ('errno', 'exceptions', 'gc', 'imp',
(object.__name__ in ('errno', 'exceptions', 'gc',
'marshal', 'posix', 'signal', 'sys',
'_thread', 'zipimport') or
(file.startswith(basedir) and
Expand Down
5 changes: 2 additions & 3 deletions Lib/test/test_importlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ def uncache(*names):

"""
for name in names:
if name in ('sys', 'marshal', 'imp'):
raise ValueError(
"cannot uncache {0}".format(name))
if name in ('sys', 'marshal'):
raise ValueError("cannot uncache {}".format(name))
try:
del sys.modules[name]
except KeyError:
Expand Down
12 changes: 0 additions & 12 deletions Modules/_testmultiphase.c
Original file line number Diff line number Diff line change
Expand Up @@ -884,15 +884,3 @@ PyInit__test_module_state_shared(void)
}
return module;
}


/*** Helper for imp test ***/

static PyModuleDef imp_dummy_def = TEST_MODULE_DEF("imp_dummy", main_slots, testexport_methods);

PyMODINIT_FUNC
PyInit_imp_dummy(void)
{
return PyModuleDef_Init(&imp_dummy_def);
}

8 changes: 4 additions & 4 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,11 @@ _PyImport_ClearModulesByIndex(PyInterpreterState *interp)
/*
It may help to have a big picture view of what happens
when an extension is loaded. This includes when it is imported
for the first time or via imp.load_dynamic().
for the first time.

Here's a summary, using imp.load_dynamic() as the starting point:
Here's a summary, using importlib._boostrap._load() as a starting point.

1. imp.load_dynamic() -> importlib._bootstrap._load()
1. importlib._bootstrap._load()
2. _load(): acquire import lock
3. _load() -> importlib._bootstrap._load_unlocked()
4. _load_unlocked() -> importlib._bootstrap.module_from_spec()
Expand Down Expand Up @@ -3794,7 +3794,7 @@ _imp_source_hash_impl(PyObject *module, long key, Py_buffer *source)


PyDoc_STRVAR(doc_imp,
"(Extremely) low-level import machinery bits as used by importlib and imp.");
"(Extremely) low-level import machinery bits as used by importlib.");

static PyMethodDef imp_methods[] = {
_IMP_EXTENSION_SUFFIXES_METHODDEF
Expand Down
30 changes: 12 additions & 18 deletions Python/makeopcodetargets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@
import sys


try:
from importlib.machinery import SourceFileLoader
except ImportError:
import imp

def find_module(modname):
"""Finds and returns a module in the local dist/checkout.
"""
modpath = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "Lib")
return imp.load_module(modname, *imp.find_module(modname, [modpath]))
else:
def find_module(modname):
"""Finds and returns a module in the local dist/checkout.
"""
modpath = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py")
return SourceFileLoader(modname, modpath).load_module()
# 2023-04-27(warsaw): Pre-Python 3.12, this would catch ImportErrors and try to
# import imp, and then use imp.load_module(). The imp module was removed in
# Python 3.12 (and long deprecated before that), and it's unclear under what
# conditions this import will now fail, so the fallback was simply removed.
from importlib.machinery import SourceFileLoader

def find_module(modname):
"""Finds and returns a module in the local dist/checkout.
"""
modpath = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py")
return SourceFileLoader(modname, modpath).load_module()


def write_contents(f):
Expand Down
7 changes: 3 additions & 4 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2185,10 +2185,9 @@ add_main_module(PyInterpreterState *interp)
Py_DECREF(bimod);
}

/* Main is a little special - imp.is_builtin("__main__") will return
* False, but BuiltinImporter is still the most appropriate initial
* setting for its __loader__ attribute. A more suitable value will
* be set if __main__ gets further initialized later in the startup
/* Main is a little special - BuiltinImporter is the most appropriate
* initial setting for its __loader__ attribute. A more suitable value
* will be set if __main__ gets further initialized later in the startup
* process.
*/
loader = _PyDict_GetItemStringWithError(d, "__loader__");
Expand Down
1 change: 0 additions & 1 deletion Tools/c-analyzer/TODO
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ Python/import.c:PyImport_ImportModuleLevelObject():PyId___path__ _Py_IDENTIFIER(
Python/import.c:PyImport_ImportModuleLevelObject():PyId___spec__ _Py_IDENTIFIER(__spec__)
Python/import.c:PyImport_ImportModuleLevelObject():PyId__handle_fromlist _Py_IDENTIFIER(_handle_fromlist)
Python/import.c:PyImport_ImportModuleLevelObject():PyId__lock_unlock_module _Py_IDENTIFIER(_lock_unlock_module)
Python/import.c:PyImport_ReloadModule():PyId_imp _Py_IDENTIFIER(imp)
Python/import.c:PyImport_ReloadModule():PyId_reload _Py_IDENTIFIER(reload)
Python/import.c:_PyImportZip_Init():PyId_zipimporter _Py_IDENTIFIER(zipimporter)
Python/import.c:import_find_and_load():PyId__find_and_load _Py_IDENTIFIER(_find_and_load)
Expand Down
14 changes: 7 additions & 7 deletions Tools/importbench/importbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
from test.test_importlib import util
import decimal
import imp
from importlib.util import cache_from_source
import importlib
import importlib.machinery
import json
Expand Down Expand Up @@ -65,7 +65,7 @@ def source_wo_bytecode(seconds, repeat):
name = '__importlib_test_benchmark__'
# Clears out sys.modules and puts an entry at the front of sys.path.
with util.create_modules(name) as mapping:
assert not os.path.exists(imp.cache_from_source(mapping[name]))
assert not os.path.exists(cache_from_source(mapping[name]))
sys.meta_path.append(importlib.machinery.PathFinder)
loader = (importlib.machinery.SourceFileLoader,
importlib.machinery.SOURCE_SUFFIXES)
Expand All @@ -80,7 +80,7 @@ def _wo_bytecode(module):
name = module.__name__
def benchmark_wo_bytecode(seconds, repeat):
"""Source w/o bytecode: {}"""
bytecode_path = imp.cache_from_source(module.__file__)
bytecode_path = cache_from_source(module.__file__)
if os.path.exists(bytecode_path):
os.unlink(bytecode_path)
sys.dont_write_bytecode = True
Expand Down Expand Up @@ -108,9 +108,9 @@ def source_writing_bytecode(seconds, repeat):
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
def cleanup():
sys.modules.pop(name)
os.unlink(imp.cache_from_source(mapping[name]))
os.unlink(cache_from_source(mapping[name]))
for result in bench(name, cleanup, repeat=repeat, seconds=seconds):
assert not os.path.exists(imp.cache_from_source(mapping[name]))
assert not os.path.exists(cache_from_source(mapping[name]))
yield result


Expand All @@ -121,7 +121,7 @@ def writing_bytecode_benchmark(seconds, repeat):
assert not sys.dont_write_bytecode
def cleanup():
sys.modules.pop(name)
os.unlink(imp.cache_from_source(module.__file__))
os.unlink(cache_from_source(module.__file__))
yield from bench(name, cleanup, repeat=repeat, seconds=seconds)

writing_bytecode_benchmark.__doc__ = (
Expand All @@ -141,7 +141,7 @@ def source_using_bytecode(seconds, repeat):
importlib.machinery.SOURCE_SUFFIXES)
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
py_compile.compile(mapping[name])
assert os.path.exists(imp.cache_from_source(mapping[name]))
assert os.path.exists(cache_from_source(mapping[name]))
yield from bench(name, lambda: sys.modules.pop(name), repeat=repeat,
seconds=seconds)

Expand Down