def __import__(name, globals=None, locals=None, fromlist=None): if globals is None: globals = {} if locals is None: locals = {} if fromlist is None: fromlist = [] check = not sys.modules.has_key(name) and name[:10] != 'pychecker.' pymodule = _orig__import__(name, globals, locals, fromlist) if check: try: # FIXME: can we find a good moduleDir ? module = pcmodules.PyCheckerModule(pymodule.__name__) if module.initModule(pymodule): warnings = warn.find([module], _cfg, _suppressions) _printWarnings(_get_unique_warnings(warnings)) else: print 'Unable to load module', pymodule.__name__ except Exception: name = getattr(pymodule, '__name__', utils.safestr(pymodule)) # FIXME: can we use it here ? utils.importError(name) return pymodule
def processFiles(files, cfg=None, pre_process_cb=None): """ @type files: list of str @type cfg: L{Config.Config} @param pre_process_cb: callable notifying of module name, filename @type pre_process_cb: callable taking (str, str) """ warnings = [] # insert this here, so we find files in the local dir before std library if sys.path[0] != '': sys.path.insert(0, '') # ensure we have a config object, it's necessary global _cfg if cfg is not None: _cfg = cfg elif _cfg is None: _cfg = Config.Config() if _cfg.ignoreImportErrors: install_ignore__import__() utils.initConfig(_cfg) utils.debug('Processing %d files' % len(files)) for file, (moduleName, moduleDir) in zip(files, getModules(files)): if callable(pre_process_cb): pre_process_cb("module %s (%s)" % (moduleName, file)) # create and load the PyCheckerModule, tricking sys.path temporarily oldsyspath = sys.path[:] if moduleDir is not None: sys.path.insert(0, moduleDir) pcmodule = pcmodules.PyCheckerModule(moduleName, moduleDir=moduleDir) loaded = pcmodule.load() sys.path = oldsyspath if not loaded: w = Warning(pcmodule.filename(), 1, msgs.Internal("NOT PROCESSED UNABLE TO IMPORT")) warnings.append(w) utils.debug('Processed %d files' % len(files)) utils.popConfig() return warnings
def fixupBuiltinModules(needs_init=0): for moduleName in sys.builtin_module_names: # Skip sys since it will reset sys.stdout in IDLE and cause # stdout to go to the real console rather than the IDLE console. # FIXME: this breaks test42 # if moduleName == 'sys': # continue if needs_init: _ = pcmodules.PyCheckerModule(moduleName, 0) # builtin modules don't have a moduleDir module = pcmodules.getPCModule(moduleName) if module is not None: try: m = imp.init_builtin(moduleName) except ImportError: pass else: extra_attrs = _BUILTIN_MODULE_ATTRS.get(moduleName, []) module.attributes = ['__dict__'] + dir(m) + extra_attrs
def __import__(name, globals=None, locals=None, fromlist=None, level=None): if globals is None: globals = {} if locals is None: locals = {} if fromlist is None: fromlist = [] check = not sys.modules.has_key(name) and name[:10] != 'pychecker.' if level: pymodule = _orig__import__(name, globals, locals, fromlist, level) else: pymodule = _orig__import__(name, globals, locals, fromlist) if check: try: # FIXME: can we find a good moduleDir ? # based on possible module.__file__, check if it's from # sys.path, and if not, extract moduleDir moduleDir = os.path.dirname(pymodule.__file__) for path in sys.path: if os.path.abspath(moduleDir) == os.path.abspath(path): moduleDir = None break # FIXME: could it possibly be from a higher-level package, # instead of the current dir ? Loop up with __init__.py ? module = pcmodules.PyCheckerModule(pymodule.__name__, moduleDir=moduleDir) if module.initModule(pymodule): warnings = warn.find([module], _cfg, _suppressions) _printWarnings(_get_unique_warnings(warnings)) else: print 'Unable to load module', pymodule.__name__ except Exception: name = getattr(pymodule, '__name__', utils.safestr(pymodule)) # FIXME: can we use it here ? utils.importError(name) return pymodule