/usr/share/pyshared/pychecker2/ImportChecks.py is in pychecker 0.8.19-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | from pychecker2.Check import Check
from pychecker2.Check import Warning
from pychecker2 import util
from compiler import walk
class ModuleReference:
def __init__(self, localname, remotename, module, nodes):
self.localname = localname
self.remotename = remotename
self.module = module
self.nodes = nodes
def __getstate__(self):
return (self.localname, self.remotename, self.module.__name__,
self.nodes)
def __setstate__(self, data):
(self.localname, self.remotename, module, self.nodes) = data
self.module = __import__(module, globals(), {}, [''])
class ImportCheck(Check):
'''
Get 'from module import *' names hauled into the file and modules.
Figure out which names come from 'import name'.
'''
importError = Warning('Report/ignore imports that may fail',
'Error trying to import %s: %s')
duplicateImport = Warning('Report/ignore duplicate imports',
'Import of "%s" is duplicate%s')
shadowImport = Warning('Report imports which shadow names from '
'other imports',
'Import of "%s" duplicates import from '
'module %s at %d')
def check(self, file, checker):
def try_import(name, node):
try:
return __import__(name, globals(), {}, [''])
except ImportError, detail:
file.warning(node, ImportCheck.importError, name, str(detail))
except Exception, detail:
file.warning(node, ImportCheck.importError, name, str(detail))
return None
def add_import(node, local, remote, module):
scopes = util.enclosing_scopes(file.scopes, node)
for scope in scopes:
try:
ref = scope.imports[local]
if not util.try_if_exclusive(ref.nodes, node):
if ref.module == module:
if scope == scopes[0]:
extra = " in current scope"
else:
extra = " of import in parent scope %s" % scope
file.warning(node, ImportCheck.duplicateImport,
local, extra)
else:
file.warning(node, ImportCheck.shadowImport,
local,
ref.module.__name__,
ref.nodes.lineno)
except KeyError:
pass
scopes[0].imports[local] = ModuleReference(local, remote,
module, node)
checker.check_module(module)
class FromImportVisitor:
def visitFrom(self, node):
m = try_import(node.modname, node)
if m:
for module_name, local_name in node.names:
if module_name == '*':
for name in dir(m):
if not name.startswith('_'):
add_import(node, name, name, m)
else:
add_import(node, local_name or module_name,
module_name, m)
def visitImport(self, node):
for module, name in node.names:
m = try_import(module, node)
if m:
add_import(node, name or module, None, m)
if file.root_scope:
walk(file.root_scope.node, FromImportVisitor())
|