1.1 --- a/micropython/__init__.py Sat Jun 30 00:15:09 2012 +0200
1.2 +++ b/micropython/__init__.py Sat Jun 30 17:06:37 2012 +0200
1.3 @@ -254,8 +254,9 @@
1.4 # Get the raw version for the architecture.
1.5
1.6 if arch_item is not None:
1.7 + arch_item.finalise_location(with_builtins)
1.8 self.raw_code += arch_item.as_raw(objtable, paramtable, with_builtins)
1.9 - arch_item.finalise_location(with_builtins)
1.10 + arch_item.finalise_body_location(with_builtins)
1.11 else:
1.12 self.raw_code.append(item)
1.13
1.14 @@ -266,8 +267,6 @@
1.15 if not with_builtins and module.name in ("__builtins__", "native"):
1.16 continue
1.17
1.18 - module.code_location = module.blocks[0].location
1.19 -
1.20 self.code_location = self.importer.modules["__main__"].code_location
1.21 return self.raw_code
1.22
1.23 @@ -415,6 +414,10 @@
1.24 self.modules_ordered = []
1.25 self.loading = set()
1.26
1.27 + # Importers responsible for initially importing modules.
1.28 +
1.29 + self.importers = {}
1.30 +
1.31 # Constant records.
1.32
1.33 self.constant_values = {}
1.34 @@ -948,12 +951,16 @@
1.35 else:
1.36 return None
1.37
1.38 - def load(self, name, return_leaf=0):
1.39 + def load(self, name, return_leaf=0, importer=None):
1.40
1.41 """
1.42 Load the module or package with the given 'name'. Return an object
1.43 referencing the loaded module or package, or None if no such module or
1.44 package exists.
1.45 +
1.46 + If the given 'importer' is specified, it will be associated with the
1.47 + imported module if it is responsible for importing the module for the
1.48 + first time.
1.49 """
1.50
1.51 if return_leaf:
1.52 @@ -961,7 +968,7 @@
1.53 else:
1.54 name_for_return = name.split(".")[0]
1.55
1.56 - if self.modules.has_key(name) and self.modules[name].loaded:
1.57 + if self.modules.has_key(name):
1.58 #print >>sys.stderr, "Cached (%s)" % name
1.59 return self.modules[name_for_return]
1.60
1.61 @@ -982,7 +989,7 @@
1.62 # Either acquire a reference to an already-imported module, or load the
1.63 # module from a file.
1.64
1.65 - top = module = self.load_from_file(filename, path[0])
1.66 + top = module = self.load_from_file(filename, path[0], importer)
1.67
1.68 # For hierarchical names, traverse each path component...
1.69
1.70 @@ -1010,7 +1017,7 @@
1.71
1.72 # Either reference an imported module or load one from a file.
1.73
1.74 - submodule = self.load_from_file(filename, module_name)
1.75 + submodule = self.load_from_file(filename, module_name, importer)
1.76
1.77 if d:
1.78 self.add_submodules(d, module)
1.79 @@ -1032,10 +1039,14 @@
1.80 else:
1.81 return top
1.82
1.83 - def load_from_file(self, name, module_name=None):
1.84 + def load_from_file(self, name, module_name=None, importer=None):
1.85
1.86 """
1.87 Load the module with the given 'name' (which may be a full module path).
1.88 +
1.89 + If the given 'importer' is specified, it will be associated with the
1.90 + imported module if it is responsible for importing the module for the
1.91 + first time.
1.92 """
1.93
1.94 if module_name is None:
1.95 @@ -1052,6 +1063,13 @@
1.96 self.loading.remove(module)
1.97 module.loaded = 1
1.98
1.99 + # Record each module as imported by any importer.
1.100 +
1.101 + if importer:
1.102 + if not self.importers.has_key(importer):
1.103 + self.importers[importer] = []
1.104 + self.importers[importer].append(module)
1.105 +
1.106 # Record the module.
1.107
1.108 self.use_object(module.full_name())