[PATCH 5 of 6] import-checker: remove useless stdlib_modules and related code paths
FUJIWARA Katsunori
foozy at lares.dti.ne.jp
Wed May 13 11:53:58 CDT 2015
# HG changeset patch
# User FUJIWARA Katsunori <foozy at lares.dti.ne.jp>
# Date 1431535750 -32400
# Thu May 14 01:49:10 2015 +0900
# Node ID 28a977036067ea7bb0af50218edbee9abe82331a
# Parent 9b0ee3a4e1c6ca73bd12c0d7ab16b066c807428c
import-checker: remove useless stdlib_modules and related code paths
Previous patches make "stdlib_modules" useless.
diff --git a/contrib/import-checker.py b/contrib/import-checker.py
--- a/contrib/import-checker.py
+++ b/contrib/import-checker.py
@@ -1,13 +1,6 @@
import ast
-import os
import sys
-# Import a minimal set of stdlib modules needed for list_stdlib_modules()
-# to work when run from a virtualenv. The modules were chosen empirically
-# so that the return value matches the return value without virtualenv.
-import BaseHTTPServer
-import zlib
-
def dotted_name_of_path(path, trimpure=False):
"""Given a relative path to a source file, return its dotted module name.
@@ -27,83 +20,6 @@ def dotted_name_of_path(path, trimpure=F
return '.'.join(parts)
-def list_stdlib_modules():
- """List the modules present in the stdlib.
-
- >>> mods = set(list_stdlib_modules())
- >>> 'BaseHTTPServer' in mods
- True
-
- os.path isn't really a module, so it's missing:
-
- >>> 'os.path' in mods
- False
-
- sys requires special treatment, because it's baked into the
- interpreter, but it should still appear:
-
- >>> 'sys' in mods
- True
-
- >>> 'collections' in mods
- True
-
- >>> 'cStringIO' in mods
- True
- """
- for m in sys.builtin_module_names:
- yield m
- # These modules only exist on windows, but we should always
- # consider them stdlib.
- for m in ['msvcrt', '_winreg']:
- yield m
- # These get missed too
- for m in 'ctypes', 'email':
- yield m
- yield 'builtins' # python3 only
- for m in 'fcntl', 'grp', 'pwd', 'termios': # Unix only
- yield m
- stdlib_prefixes = set([sys.prefix, sys.exec_prefix])
- # We need to supplement the list of prefixes for the search to work
- # when run from within a virtualenv.
- for mod in (BaseHTTPServer, zlib):
- try:
- # Not all module objects have a __file__ attribute.
- filename = mod.__file__
- except AttributeError:
- continue
- dirname = os.path.dirname(filename)
- for prefix in stdlib_prefixes:
- if dirname.startswith(prefix):
- # Then this directory is redundant.
- break
- else:
- stdlib_prefixes.add(dirname)
- for libpath in sys.path:
- # We want to walk everything in sys.path that starts with
- # something in stdlib_prefixes. check-code suppressed because
- # the ast module used by this script implies the availability
- # of any().
- if not any(libpath.startswith(p) for p in stdlib_prefixes): # no-py24
- continue
- if 'site-packages' in libpath:
- continue
- for top, dirs, files in os.walk(libpath):
- for name in files:
- if name == '__init__.py':
- continue
- if not (name.endswith('.py') or name.endswith('.so')
- or name.endswith('.pyd')):
- continue
- full_path = os.path.join(top, name)
- if 'site-packages' in full_path:
- continue
- rel_path = full_path[len(libpath) + 1:]
- mod = dotted_name_of_path(rel_path)
- yield mod
-
-stdlib_modules = set(list_stdlib_modules())
-
def imported_modules(source, ignore_nested=False):
"""Given the source of a file as a string, yield the names
imported by that file.
More information about the Mercurial-devel
mailing list