1.1 --- a/micropython/__init__.py Sat Jul 07 20:40:09 2012 +0200
1.2 +++ b/micropython/__init__.py Sun Jul 08 02:26:27 2012 +0200
1.3 @@ -94,6 +94,8 @@
1.4
1.5 "Finalise the program."
1.6
1.7 + self.importer.complete_modules()
1.8 +
1.9 # Need the tables to finalise.
1.10
1.11 objtable = self.get_object_table()
1.12 @@ -416,6 +418,10 @@
1.13
1.14 self.importers = {}
1.15
1.16 + # Modules involved in circular imports.
1.17 +
1.18 + self.circular_imports = set()
1.19 +
1.20 # Constant records.
1.21
1.22 self.constant_values = {}
1.23 @@ -440,6 +446,7 @@
1.24
1.25 # Status information.
1.26
1.27 + self.completed = 0
1.28 self.vacuumed = 0
1.29 self.finalised = 0
1.30
1.31 @@ -455,6 +462,18 @@
1.32
1.33 return self.modules[name]
1.34
1.35 + def complete_modules(self):
1.36 +
1.37 + "Complete the processing of modules."
1.38 +
1.39 + if self.completed:
1.40 + return
1.41 +
1.42 + self.get_module("__builtins__").complete()
1.43 + self.get_module("__main__").complete()
1.44 +
1.45 + self.completed = 1
1.46 +
1.47 # General maintenance.
1.48
1.49 def vacuum(self, objtable):
1.50 @@ -968,58 +987,43 @@
1.51 # the search path.
1.52
1.53 path = name.split(".")
1.54 - m = self.find_in_path(path[0])
1.55 - if not m:
1.56 - if self.verbose:
1.57 - print >>sys.stderr, "Not found (%s)" % path[0]
1.58 - return None # NOTE: Import error.
1.59 - d, filename = m
1.60 + path_so_far = []
1.61 + top = module = None
1.62
1.63 - # Either acquire a reference to an already-imported module, or load the
1.64 - # module from a file.
1.65 + for p in path:
1.66
1.67 - top = module = self.load_from_file(filename, path[0], importer)
1.68 -
1.69 - # For hierarchical names, traverse each path component...
1.70 + # Get the module's filesystem details.
1.71
1.72 - if len(path) > 1:
1.73 - if not d:
1.74 - if self.verbose:
1.75 - print >>sys.stderr, "No package (%s)" % filename
1.76 - return None # NOTE: Import error (package not found).
1.77 + if not path_so_far:
1.78 + m = self.find_in_path(p)
1.79 else:
1.80 - self.add_submodules(d, module)
1.81 + m = self.find(d, p)
1.82
1.83 - path_so_far = path[:1]
1.84 - for p in path[1:]:
1.85 - path_so_far.append(p)
1.86 + if not m:
1.87 + if self.verbose:
1.88 + print >>sys.stderr, "Not found (%s)" % p
1.89 + return None # NOTE: Import error.
1.90
1.91 - # Find the package or module concerned.
1.92 + # Get the module itself.
1.93
1.94 - m = self.find(d, p)
1.95 - if not m:
1.96 - if self.verbose:
1.97 - print >>sys.stderr, "Not found (%s)" % p
1.98 - return None # NOTE: Import error.
1.99 - d, filename = m
1.100 - module_name = ".".join(path_so_far)
1.101 + d, filename = m
1.102 + path_so_far.append(p)
1.103 + module_name = ".".join(path_so_far)
1.104 + submodule = self.load_from_file(filename, module_name, importer)
1.105
1.106 - # Either reference an imported module or load one from a file.
1.107 -
1.108 - submodule = self.load_from_file(filename, module_name, importer)
1.109 -
1.110 - if d:
1.111 - self.add_submodules(d, module)
1.112 -
1.113 + if module is None:
1.114 + top = submodule
1.115 + else:
1.116 # Store the submodule within its parent module.
1.117
1.118 module.set_module(p, submodule)
1.119 - module = submodule
1.120 +
1.121 + module = submodule
1.122
1.123 - # Stop descending if no package was found.
1.124 + # Stop descending if no package was found.
1.125
1.126 - if not d:
1.127 - break
1.128 + if not d:
1.129 + break
1.130
1.131 # Return either the deepest or the uppermost module.
1.132