Skip to content

Commit 03072b4

Browse files
committed
fix: remove references to imp in untouched code
CI runs flake8 to check for syntax errors and it threw errors on old imp.XYZ code that was not actually called. It looks like the code was only called when resolving modules like ssl, _ssl, importlib, importlib.resources .. rather than files/directory paths. In Roundup's use case it only runs over files/directories.
1 parent d2f04eb commit 03072b4

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

roundup/pygettext.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -269,35 +269,13 @@ def _get_modpkg_path(dotted_name, pathlist=None):
269269
a package. Return None if the name is not found, or is a builtin or
270270
extension module.
271271
"""
272-
# split off top-most name
273-
parts = dotted_name.split('.', 1)
274-
275-
if len(parts) > 1:
276-
# we have a dotted path, import top-level package
277-
try:
278-
file, pathname, description = importlib.find_module(parts[0], pathlist)
279-
if file: file.close()
280-
except ImportError:
281-
return None
282-
283-
# check if it's indeed a package
284-
if description[2] == imp.PKG_DIRECTORY:
285-
# recursively handle the remaining name parts
286-
pathname = _get_modpkg_path(parts[1], [pathname])
287-
else:
288-
pathname = None
289-
else:
290-
# plain name
291-
try:
292-
file, pathname, description = imp.find_module(
293-
dotted_name, pathlist)
294-
if file:
295-
file.close()
296-
if description[2] not in [imp.PY_SOURCE, imp.PKG_DIRECTORY]:
297-
pathname = None
298-
except ImportError:
299-
pathname = None
272+
pathname = None
273+
r = importlib.util.find_spec(dotted_name, pathlist)
300274

275+
if r.loader.is_package(dotted_name):
276+
pathname = r.submodule_search_locations[0]
277+
elif issubclass(r.loader.__class__,(importlib.abc.SourceLoader)):
278+
pathname = r.origin
301279
return pathname
302280

303281

@@ -325,8 +303,7 @@ def getFilesForName(name):
325303
# get extension for python source files
326304
if '_py_ext' not in globals():
327305
global _py_ext
328-
_py_ext = [triple[0] for triple in imp.get_suffixes()
329-
if triple[2] == imp.PY_SOURCE][0]
306+
_py_ext = importlib.machinery.SOURCE_SUFFIXES
330307
for root, dirs, files in os.walk(name):
331308
# don't recurse into CVS directories
332309
if 'CVS' in dirs:

0 commit comments

Comments
 (0)