1.1 --- a/inspector.py Mon Sep 05 00:12:56 2016 +0200
1.2 +++ b/inspector.py Mon Sep 05 19:50:21 2016 +0200
1.3 @@ -57,6 +57,8 @@
1.4 def __repr__(self):
1.5 return "InspectedModule(%r, %r)" % (self.name, self.importer)
1.6
1.7 + # Principal methods.
1.8 +
1.9 def parse(self, filename):
1.10
1.11 "Parse the file having the given 'filename'."
1.12 @@ -82,6 +84,10 @@
1.13
1.14 self.stop_tracking_in_module()
1.15
1.16 + # Collect external name references.
1.17 +
1.18 + self.collect_names()
1.19 +
1.20 def complete(self):
1.21
1.22 "Complete the module inspection."
1.23 @@ -98,6 +104,54 @@
1.24
1.25 self.propagate()
1.26
1.27 + # Accessory methods.
1.28 +
1.29 + def collect_names(self):
1.30 +
1.31 + "Collect the names used by each scope."
1.32 +
1.33 + for path in self.names_used.keys():
1.34 + self.collect_names_for_path(path)
1.35 +
1.36 + def collect_names_for_path(self, path):
1.37 +
1.38 + "Collect the names used by the given 'path'."
1.39 +
1.40 + names = self.names_used.get(path)
1.41 + if not names:
1.42 + return
1.43 +
1.44 + in_function = self.function_locals.has_key(path)
1.45 +
1.46 + for name in names:
1.47 + if name in predefined_constants or in_function and name in self.function_locals[path]:
1.48 + continue
1.49 +
1.50 + # Find local definitions (within static namespaces).
1.51 +
1.52 + key = "%s.%s" % (path, name)
1.53 + ref = self.get_resolved_object(key)
1.54 + if ref:
1.55 + self.importer.all_name_references[key] = self.name_references[key] = ref.alias(key)
1.56 + continue
1.57 +
1.58 + # Find global or built-in definitions.
1.59 +
1.60 + ref = self.get_resolved_global_or_builtin(name)
1.61 + if ref:
1.62 + self.importer.all_name_references[key] = self.name_references[key] = ref
1.63 + continue
1.64 +
1.65 + print >>sys.stderr, "Name not recognised: %s in %s" % (name, path)
1.66 + init_item(self.names_missing, path, set)
1.67 + self.names_missing[path].add(name)
1.68 +
1.69 + def get_resolved_global_or_builtin(self, name):
1.70 +
1.71 + "Return the resolved global or built-in object with the given 'name'."
1.72 +
1.73 + return self.get_global(name) or self.importer.get_object("__builtins__.%s" % name)
1.74 +
1.75 def set_invocation_usage(self):
1.76
1.77 """
1.78 @@ -734,7 +788,6 @@
1.79 ref = self.import_name_from_module(op, "operator")
1.80
1.81 # Record the imported name and provide the resolved name reference.
1.82 - # NOTE: Maybe use a different class.
1.83
1.84 value = ResolvedNameRef(n.name, ref)
1.85 self.set_special(n.name, value)
1.86 @@ -1198,7 +1251,7 @@
1.87 # name initialisers are resolved once a module has been inspected.
1.88
1.89 elif isinstance(value, InvocationRef):
1.90 - return None
1.91 + return value.reference()
1.92
1.93 else:
1.94 return value