mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Update pygettext.py to get ride of imp
Issue #26639: Replace imp with importlib in Tools/i18n/pygettext.py. Remove _get_modpkg_path(), replaced with importlib.util.find_spec().
This commit is contained in:
parent
a07614aa31
commit
328cb1fed0
1 changed files with 8 additions and 43 deletions
|
@ -156,7 +156,8 @@ If `inputfile' is -, standard input is read.
|
||||||
""")
|
""")
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import imp
|
import importlib.machinery
|
||||||
|
import importlib.util
|
||||||
import sys
|
import sys
|
||||||
import glob
|
import glob
|
||||||
import time
|
import time
|
||||||
|
@ -263,8 +264,7 @@ def _visit_pyfiles(list, dirname, names):
|
||||||
# get extension for python source files
|
# get extension for python source files
|
||||||
if '_py_ext' not in globals():
|
if '_py_ext' not in globals():
|
||||||
global _py_ext
|
global _py_ext
|
||||||
_py_ext = [triple[0] for triple in imp.get_suffixes()
|
_py_ext = importlib.machinery.SOURCE_SUFFIXES[0]
|
||||||
if triple[2] == imp.PY_SOURCE][0]
|
|
||||||
|
|
||||||
# don't recurse into CVS directories
|
# don't recurse into CVS directories
|
||||||
if 'CVS' in names:
|
if 'CVS' in names:
|
||||||
|
@ -277,45 +277,6 @@ def _visit_pyfiles(list, dirname, names):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _get_modpkg_path(dotted_name, pathlist=None):
|
|
||||||
"""Get the filesystem path for a module or a package.
|
|
||||||
|
|
||||||
Return the file system path to a file for a module, and to a directory for
|
|
||||||
a package. Return None if the name is not found, or is a builtin or
|
|
||||||
extension module.
|
|
||||||
"""
|
|
||||||
# split off top-most name
|
|
||||||
parts = dotted_name.split('.', 1)
|
|
||||||
|
|
||||||
if len(parts) > 1:
|
|
||||||
# we have a dotted path, import top-level package
|
|
||||||
try:
|
|
||||||
file, pathname, description = imp.find_module(parts[0], pathlist)
|
|
||||||
if file: file.close()
|
|
||||||
except ImportError:
|
|
||||||
return None
|
|
||||||
|
|
||||||
# check if it's indeed a package
|
|
||||||
if description[2] == imp.PKG_DIRECTORY:
|
|
||||||
# recursively handle the remaining name parts
|
|
||||||
pathname = _get_modpkg_path(parts[1], [pathname])
|
|
||||||
else:
|
|
||||||
pathname = None
|
|
||||||
else:
|
|
||||||
# plain name
|
|
||||||
try:
|
|
||||||
file, pathname, description = imp.find_module(
|
|
||||||
dotted_name, pathlist)
|
|
||||||
if file:
|
|
||||||
file.close()
|
|
||||||
if description[2] not in [imp.PY_SOURCE, imp.PKG_DIRECTORY]:
|
|
||||||
pathname = None
|
|
||||||
except ImportError:
|
|
||||||
pathname = None
|
|
||||||
|
|
||||||
return pathname
|
|
||||||
|
|
||||||
|
|
||||||
def getFilesForName(name):
|
def getFilesForName(name):
|
||||||
"""Get a list of module files for a filename, a module or package name,
|
"""Get a list of module files for a filename, a module or package name,
|
||||||
or a directory.
|
or a directory.
|
||||||
|
@ -330,7 +291,11 @@ def getFilesForName(name):
|
||||||
return list
|
return list
|
||||||
|
|
||||||
# try to find module or package
|
# try to find module or package
|
||||||
name = _get_modpkg_path(name)
|
try:
|
||||||
|
spec = importlib.util.find_spec(name)
|
||||||
|
name = spec.origin
|
||||||
|
except ImportError:
|
||||||
|
name = None
|
||||||
if not name:
|
if not name:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue