From 9828588e9795640c4df1e626d3a7bf2edbc2cb74 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 4 Oct 2018 19:22:19 +0000 Subject: [PATCH] master: group is_stdlib_name() with other module functions. --- mitogen/master.py | 55 ++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/mitogen/master.py b/mitogen/master.py index 4e0745c8..d4ee607a 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -83,6 +83,34 @@ def _stdlib_paths(): for p in prefixes) +def is_stdlib_name(modname): + """Return :data:`True` if `modname` appears to come from the standard + library. + """ + if imp.is_builtin(modname) != 0: + return True + + module = sys.modules.get(modname) + if module is None: + return False + + # six installs crap with no __file__ + modpath = os.path.abspath(getattr(module, '__file__', '')) + return is_stdlib_path(modpath) + + +_STDLIB_PATHS = _stdlib_paths() + + +def is_stdlib_path(path): + return any( + os.path.commonprefix((libpath, path)) == libpath + and 'site-packages' not in path + and 'dist-packages' not in path + for libpath in _STDLIB_PATHS + ) + + def get_child_modules(path): """Return the suffixes of submodules directly neated beneath of the package directory at `path`. @@ -306,33 +334,6 @@ class LogForwarder(object): return 'LogForwarder(%r)' % (self._router,) -_STDLIB_PATHS = _stdlib_paths() - - -def is_stdlib_path(path): - return any( - os.path.commonprefix((libpath, path)) == libpath - and 'site-packages' not in path - and 'dist-packages' not in path - for libpath in _STDLIB_PATHS - ) - - -def is_stdlib_name(modname): - """Return :data:`True` if `modname` appears to come from the standard - library.""" - if imp.is_builtin(modname) != 0: - return True - - module = sys.modules.get(modname) - if module is None: - return False - - # six installs crap with no __file__ - modpath = os.path.abspath(getattr(module, '__file__', '')) - return is_stdlib_path(modpath) - - class ModuleFinder(object): def __init__(self): #: Import machinery is expensive, keep :py:meth`:get_module_source`