Skip to content

Commit e56fa83

Browse files
committed
Replace use of imp with importlib.util
imp is deprecated and will go away in Python 3.12 (which is very soon). This implementation based on the importlib docs [1] and passes the quite robust tests that are already present for the urlparser module. Unfortunately there's no good way to be 100% certain that this works for all the many ways that Paste can do an import. So this patch is mostly hoping that test coverage is sufficient. [1] https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly Fixes #75
1 parent 59eaf31 commit e56fa83

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

paste/urlparser.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import os
88
import six
99
import sys
10-
import imp
10+
import importlib.util as imputil
1111
import mimetypes
1212
try:
1313
import pkg_resources
@@ -376,7 +376,6 @@ def load_module_from_name(environ, filename, module_name, errors):
376376
return None
377377
f.write('#\n')
378378
f.close()
379-
fp = None
380379
if module_name in sys.modules:
381380
return sys.modules[module_name]
382381
if '.' in module_name:
@@ -386,14 +385,16 @@ def load_module_from_name(environ, filename, module_name, errors):
386385
parent_name, errors)
387386
else:
388387
base_name = module_name
389-
fp = None
390-
try:
391-
fp, pathname, stuff = imp.find_module(
392-
base_name, [os.path.dirname(filename)])
393-
module = imp.load_module(module_name, fp, pathname, stuff)
394-
finally:
395-
if fp is not None:
396-
fp.close()
388+
module = None
389+
390+
# This replaces a previous implementation that used imp with the canonical
391+
# steps from importlib.
392+
spec = imputil.spec_from_file_location(base_name, filename)
393+
if spec is not None:
394+
module = imputil.module_from_spec(spec)
395+
sys.modules[base_name] = module
396+
spec.loader.exec_module(module)
397+
397398
return module
398399

399400
def make_py(parser, environ, filename):

0 commit comments

Comments
 (0)