Skip to content

Commit cb66eb0

Browse files
committed
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
importlib.machinery that provide the suffix details for import. The attributes were not put on imp so as to compartmentalize everything importlib needs for setting up imports in importlib.machinery. This also led to an indirect deprecation of inspect.getmoduleinfo() as it directly returned imp.get_suffix's returned tuple which no longer makes sense.
1 parent 810c64d commit cb66eb0

17 files changed

Lines changed: 159 additions & 68 deletions

File tree

Doc/library/imp.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ This module provides an interface to the mechanisms used to implement the
3030
:const:`PY_SOURCE`, :const:`PY_COMPILED`, or :const:`C_EXTENSION`, described
3131
below.
3232

33+
.. deprecated:: 3.3
34+
Use the constants defined on :mod:`importlib.machinery` instead.
35+
3336

3437
.. function:: find_module(name[, path])
3538

Doc/library/importlib.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,43 @@ are also provided to help in implementing the core ABCs.
477477
This module contains the various objects that help :keyword:`import`
478478
find and load modules.
479479

480+
.. attribute:: SOURCE_SUFFIXES
481+
482+
A list of strings representing the recognized file suffixes for source
483+
modules.
484+
485+
.. versionadded:: 3.3
486+
487+
.. attribute:: DEBUG_BYTECODE_SUFFIXES
488+
489+
A list of strings representing the file suffixes for non-optimized bytecode
490+
modules.
491+
492+
.. versionadded:: 3.3
493+
494+
.. attribute:: OPTIMIZED_BYTECODE_SUFFIXES
495+
496+
A list of strings representing the file suffixes for optimized bytecode
497+
modules.
498+
499+
.. versionadded:: 3.3
500+
501+
.. attribute:: BYTECODE_SUFFIXES
502+
503+
A list of strings representing the recognized file suffixes for bytecode
504+
modules. Set to either :attr:`DEBUG_BYTECODE_SUFFIXES` or
505+
:attr:`OPTIMIZED_BYTECODE_SUFFIXES` based on whether ``__debug__`` is true.
506+
507+
.. versionadded:: 3.3
508+
509+
.. attribute:: EXTENSION_SUFFIXES
510+
511+
A list of strings representing the the recognized file suffixes for
512+
extension modules.
513+
514+
.. versionadded:: 3.3
515+
516+
480517
.. class:: BuiltinImporter
481518

482519
An :term:`importer` for built-in modules. All known built-in modules are

Doc/library/inspect.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ attributes:
190190
compared to the constants defined in the :mod:`imp` module; see the
191191
documentation for that module for more information on module types.
192192

193+
.. deprecated:: 3.3
194+
You may check the file path's suffix against the supported suffixes
195+
listed in :mod:`importlib.machinery` to infer the same information.
196+
193197

194198
.. function:: getmodulename(path)
195199

Lib/ctypes/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def find_msvcrt():
4040
clibname = 'msvcr%d' % (version * 10)
4141

4242
# If python was built with in debug mode
43-
import imp
44-
if imp.get_suffixes()[0][0] == '_d.pyd':
43+
import importlib.machinery
44+
if '_d.pyd' in importlib.machinery.EXTENSION_SUFFIXES:
4545
clibname += 'd'
4646
return clibname+'.dll'
4747

Lib/idlelib/PathBrowser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import sys
33
import imp
4+
import importlib.machinery
45

56
from idlelib.TreeWidget import TreeItem
67
from idlelib.ClassBrowser import ClassBrowser, ModuleBrowserTreeItem
@@ -70,7 +71,9 @@ def ispackagedir(self, file):
7071

7172
def listmodules(self, allnames):
7273
modules = {}
73-
suffixes = imp.get_suffixes()
74+
suffixes = importlib.machinery.EXTENSION_SUFFIXES[:]
75+
suffixes += importlib.machinery.SOURCE_SUFFIXES[:]
76+
suffixes += importlib.machinery.BYTECODE_SUFFIXES[:]
7477
sorted = []
7578
for suff, mode, flag in suffixes:
7679
i = -len(suff)

Lib/imp.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
from importlib._bootstrap import cache_from_source
1818

1919
from importlib import _bootstrap
20+
from importlib import machinery
2021
import os
2122
import sys
2223
import tokenize
24+
import warnings
2325

2426

2527
# XXX "deprecate" once find_module(), load_module(), and get_suffixes() are
@@ -37,9 +39,12 @@
3739

3840

3941
def get_suffixes():
42+
warnings.warn('imp.get_suffixes() is deprecated; use the constants '
43+
'defined on importlib.machinery instead',
44+
DeprecationWarning, 2)
4045
extensions = [(s, 'rb', C_EXTENSION) for s in extension_suffixes()]
41-
source = [(s, 'U', PY_SOURCE) for s in _bootstrap._SOURCE_SUFFIXES]
42-
bytecode = [(_bootstrap._BYTECODE_SUFFIX, 'rb', PY_COMPILED)]
46+
source = [(s, 'U', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES]
47+
bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES]
4348

4449
return extensions + source + bytecode
4550

@@ -61,7 +66,7 @@ def source_from_cache(path):
6166
raise ValueError('expected only 2 dots in '
6267
'{!r}'.format(pycache_filename))
6368
base_filename = pycache_filename.partition('.')[0]
64-
return os.path.join(head, base_filename + _bootstrap._SOURCE_SUFFIXES[0])
69+
return os.path.join(head, base_filename + machinery.SOURCE_SUFFIXES[0])
6570

6671

6772
class NullImporter:
@@ -126,7 +131,7 @@ def load_compiled(name, pathname, file=None):
126131
# XXX deprecate
127132
def load_package(name, path):
128133
if os.path.isdir(path):
129-
extensions = _bootstrap._SOURCE_SUFFIXES + [_bootstrap._BYTECODE_SUFFIX]
134+
extensions = machinery.SOURCE_SUFFIXES[:] + [machinery.BYTECODE_SUFFIXES]
130135
for extension in extensions:
131136
path = os.path.join(path, '__init__'+extension)
132137
if os.path.exists(path):
@@ -190,19 +195,21 @@ def find_module(name, path=None):
190195

191196
for entry in path:
192197
package_directory = os.path.join(entry, name)
193-
for suffix in ['.py', _bootstrap._BYTECODE_SUFFIX]:
198+
for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]:
194199
package_file_name = '__init__' + suffix
195200
file_path = os.path.join(package_directory, package_file_name)
196201
if os.path.isfile(file_path):
197202
return None, package_directory, ('', '', PKG_DIRECTORY)
198-
for suffix, mode, type_ in get_suffixes():
199-
file_name = name + suffix
200-
file_path = os.path.join(entry, file_name)
201-
if os.path.isfile(file_path):
202-
break
203-
else:
204-
continue
205-
break # Break out of outer loop when breaking out of inner loop.
203+
with warnings.catch_warnings():
204+
warnings.simplefilter('ignore')
205+
for suffix, mode, type_ in get_suffixes():
206+
file_name = name + suffix
207+
file_path = os.path.join(entry, file_name)
208+
if os.path.isfile(file_path):
209+
break
210+
else:
211+
continue
212+
break # Break out of outer loop when breaking out of inner loop.
206213
else:
207214
raise ImportError('No module name {!r}'.format(name), name=name)
208215

Lib/importlib/_bootstrap.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,14 @@ def new_module(name):
163163

164164
_PYCACHE = '__pycache__'
165165

166-
_SOURCE_SUFFIXES = ['.py'] # _setup() adds .pyw as needed.
166+
SOURCE_SUFFIXES = ['.py'] # _setup() adds .pyw as needed.
167167

168-
_DEBUG_BYTECODE_SUFFIX = '.pyc'
169-
_OPT_BYTECODE_SUFFIX = '.pyo'
170-
_BYTECODE_SUFFIX = _DEBUG_BYTECODE_SUFFIX if __debug__ else _OPT_BYTECODE_SUFFIX
168+
DEBUG_BYTECODE_SUFFIXES = ['.pyc']
169+
OPTIMIZED_BYTECODE_SUFFIXES = ['.pyo']
170+
if __debug__:
171+
BYTECODE_SUFFIXES = DEBUG_BYTECODE_SUFFIXES
172+
else:
173+
BYTECODE_SUFFIXES = OPTIMIZED_BYTECODE_SUFFIXES
171174

172175
def cache_from_source(path, debug_override=None):
173176
"""Given the path to a .py file, return the path to its .pyc/.pyo file.
@@ -181,10 +184,13 @@ def cache_from_source(path, debug_override=None):
181184
182185
"""
183186
debug = __debug__ if debug_override is None else debug_override
184-
suffix = _DEBUG_BYTECODE_SUFFIX if debug else _OPT_BYTECODE_SUFFIX
187+
if debug:
188+
suffixes = DEBUG_BYTECODE_SUFFIXES
189+
else:
190+
suffixes = OPTIMIZED_BYTECODE_SUFFIXES
185191
head, tail = _path_split(path)
186192
base_filename, sep, _ = tail.partition('.')
187-
filename = ''.join([base_filename, sep, _TAG, suffix])
193+
filename = ''.join([base_filename, sep, _TAG, suffixes[0]])
188194
return _path_join(head, _PYCACHE, filename)
189195

190196

@@ -1147,15 +1153,15 @@ def _setup(sys_module, _imp_module):
11471153
setattr(self_module, '_MAGIC_NUMBER', _imp_module.get_magic())
11481154
setattr(self_module, '_TAG', _imp.get_tag())
11491155
if builtin_os == 'nt':
1150-
_SOURCE_SUFFIXES.append('.pyw')
1156+
SOURCE_SUFFIXES.append('.pyw')
11511157

11521158

11531159
def _install(sys_module, _imp_module):
11541160
"""Install importlib as the implementation of import."""
11551161
_setup(sys_module, _imp_module)
11561162
extensions = ExtensionFileLoader, _imp_module.extension_suffixes(), False
1157-
source = SourceFileLoader, _SOURCE_SUFFIXES, True
1158-
bytecode = SourcelessFileLoader, [_BYTECODE_SUFFIX], True
1163+
source = SourceFileLoader, SOURCE_SUFFIXES, True
1164+
bytecode = SourcelessFileLoader, BYTECODE_SUFFIXES, True
11591165
supported_loaders = [extensions, source, bytecode]
11601166
sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
11611167
sys.meta_path.extend([BuiltinImporter, FrozenImporter, PathFinder])

Lib/importlib/machinery.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
"""The machinery of importlib: finders, loaders, hooks, etc."""
22

3+
import _imp
4+
5+
from ._bootstrap import (SOURCE_SUFFIXES, DEBUG_BYTECODE_SUFFIXES,
6+
OPTIMIZED_BYTECODE_SUFFIXES, BYTECODE_SUFFIXES)
37
from ._bootstrap import BuiltinImporter
48
from ._bootstrap import FrozenImporter
59
from ._bootstrap import PathFinder
610
from ._bootstrap import FileFinder
711
from ._bootstrap import SourceFileLoader
812
from ._bootstrap import SourcelessFileLoader
913
from ._bootstrap import ExtensionFileLoader
14+
15+
EXTENSION_SUFFIXES = _imp.extension_suffixes()

Lib/importlib/test/benchmark.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import decimal
1010
import imp
1111
import importlib
12-
import importlib._bootstrap
1312
import importlib.machinery
1413
import json
1514
import os
@@ -72,7 +71,7 @@ def source_wo_bytecode(seconds, repeat):
7271
assert not os.path.exists(imp.cache_from_source(mapping[name]))
7372
sys.meta_path.append(importlib.machinery.PathFinder)
7473
loader = (importlib.machinery.SourceFileLoader,
75-
importlib._bootstrap._SOURCE_SUFFIXES, True)
74+
importlib.machinery.SOURCE_SUFFIXES, True)
7675
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
7776
for result in bench(name, lambda: sys.modules.pop(name), repeat=repeat,
7877
seconds=seconds):
@@ -110,7 +109,7 @@ def source_writing_bytecode(seconds, repeat):
110109
with source_util.create_modules(name) as mapping:
111110
sys.meta_path.append(importlib.machinery.PathFinder)
112111
loader = (importlib.machinery.SourceFileLoader,
113-
importlib._bootstrap._SOURCE_SUFFIXES, True)
112+
importlib.machinery.SOURCE_SUFFIXES, True)
114113
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
115114
def cleanup():
116115
sys.modules.pop(name)
@@ -145,7 +144,7 @@ def source_using_bytecode(seconds, repeat):
145144
with source_util.create_modules(name) as mapping:
146145
sys.meta_path.append(importlib.machinery.PathFinder)
147146
loader = (importlib.machinery.SourceFileLoader,
148-
importlib._bootstrap._SOURCE_SUFFIXES, True)
147+
importlib.machinery.SOURCE_SUFFIXES, True)
149148
sys.path_hooks.append(importlib.machinery.FileFinder.path_hook(loader))
150149
py_compile.compile(mapping[name])
151150
assert os.path.exists(imp.cache_from_source(mapping[name]))
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import imp
2+
from importlib import machinery
23
import os
34
import sys
45

56
PATH = None
67
EXT = None
78
FILENAME = None
89
NAME = '_testcapi'
9-
_file_exts = [x[0] for x in imp.get_suffixes() if x[2] == imp.C_EXTENSION]
1010
try:
1111
for PATH in sys.path:
12-
for EXT in _file_exts:
12+
for EXT in machinery.EXTENSION_SUFFIXES:
1313
FILENAME = NAME + EXT
1414
FILEPATH = os.path.join(PATH, FILENAME)
1515
if os.path.exists(os.path.join(PATH, FILENAME)):
@@ -18,4 +18,3 @@
1818
PATH = EXT = FILENAME = FILEPATH = None
1919
except StopIteration:
2020
pass
21-
del _file_exts

0 commit comments

Comments
 (0)