1.1 --- a/micropython/__init__.py Wed Oct 17 01:00:30 2007 +0200
1.2 +++ b/micropython/__init__.py Wed Oct 17 01:31:33 2007 +0200
1.3 @@ -52,7 +52,7 @@
1.4
1.5 self.path = path or [os.getcwd()]
1.6 self.modules = {}
1.7 - self.loaded = set()
1.8 + self.loading = set()
1.9
1.10 def get_modules(self):
1.11
1.12 @@ -143,7 +143,8 @@
1.13 """
1.14
1.15 print "Loading", name
1.16 - if self.modules.has_key(name) and self.modules[name] in self.loaded:
1.17 + if self.modules.has_key(name) and self.modules[name].loaded:
1.18 + print "Cached", name
1.19 return self.modules[name]
1.20
1.21 # Split the name into path components, and try to find the uppermost in
1.22 @@ -152,21 +153,20 @@
1.23 path = name.split(".")
1.24 m = self.find_in_path(path[0])
1.25 if not m:
1.26 + print "Not found", path[0]
1.27 return None # NOTE: Import error.
1.28 d, filename = m
1.29
1.30 # Either acquire a reference to an already-imported module, or load the
1.31 # module from a file.
1.32
1.33 - if self.modules.has_key(path[0]):
1.34 - top = module = self.modules[path[0]]
1.35 - else:
1.36 - top = module = self.load_from_file(filename, path[0])
1.37 + top = module = self.load_from_file(filename, path[0])
1.38
1.39 # For hierarchical names, traverse each path component...
1.40
1.41 if len(path) > 1:
1.42 if not d:
1.43 + print "No package", filename
1.44 return None # NOTE: Import error (package not found).
1.45 else:
1.46 self.add_submodules(d, module)
1.47 @@ -179,16 +179,14 @@
1.48
1.49 m = self.find(d, p)
1.50 if not m:
1.51 + print "Not found", p
1.52 return None # NOTE: Import error.
1.53 d, filename = m
1.54 module_name = ".".join(path_so_far)
1.55
1.56 # Either reference an imported module or load one from a file.
1.57
1.58 - if self.modules.has_key(module_name):
1.59 - submodule = self.modules[module_name]
1.60 - else:
1.61 - submodule = self.load_from_file(filename, module_name)
1.62 + submodule = self.load_from_file(filename, module_name)
1.63
1.64 if d:
1.65 self.add_submodules(d, module)
1.66 @@ -215,15 +213,17 @@
1.67 module_name = "__main__"
1.68
1.69 module = self.add_module(module_name)
1.70 -
1.71 - print "Parsing", name
1.72 - module.parse(name)
1.73 - print "Done", name
1.74 + if not module.loaded and module not in self.loading:
1.75 + self.loading.add(module)
1.76 + print "Parsing", name
1.77 + module.parse(name)
1.78 + print "Done", name
1.79 + self.loading.remove(module)
1.80 + module.loaded = 1
1.81
1.82 # Record the module.
1.83
1.84 - self.loaded.add(module)
1.85 - #print "Loaded", module_name, "with namespace", module.namespace.keys()
1.86 + print "Loaded", module_name #, "with namespace", module.namespace.keys()
1.87 return module
1.88
1.89 def add_module(self, module_name):
2.1 --- a/micropython/inspect.py Wed Oct 17 01:00:30 2007 +0200
2.2 +++ b/micropython/inspect.py Wed Oct 17 01:31:33 2007 +0200
2.3 @@ -62,6 +62,7 @@
2.4
2.5 self.name = name
2.6 self.importer = importer
2.7 + self.loaded = 0
2.8
2.9 # Module namespace.
2.10
2.11 @@ -140,6 +141,12 @@
2.12
2.13 visitAssTuple = visitAssList
2.14
2.15 + visitAugAssign = NOP
2.16 +
2.17 + visitBitand = NOP
2.18 +
2.19 + visitBitor = NOP
2.20 +
2.21 visitCallFunc = NOP
2.22
2.23 def visitClass(self, node):
2.24 @@ -183,10 +190,14 @@
2.25
2.26 for name, alias in node.names:
2.27 if name != "*":
2.28 - self.namespace[alias or name] = module.namespace[name]
2.29 + self.namespace[alias or name] = attr = module.namespace[name]
2.30 + if isinstance(attr, Module) and not attr.loaded:
2.31 + self.importer.load(attr.name)
2.32 else:
2.33 for n in module.namespace.keys():
2.34 - self.namespace[n] = module.namespace[n]
2.35 + self.namespace[n] = attr = module.namespace[n]
2.36 + if isinstance(attr, Module) and not attr.loaded:
2.37 + self.importer.load(attr.name)
2.38
2.39 return None
2.40
2.41 @@ -221,7 +232,10 @@
2.42 raise InspectError, "Please use the micropython.Importer class for code which uses the 'import' statement."
2.43
2.44 for name, alias in node.names:
2.45 - self.namespace[alias or name] = self.importer.load(name, 1)
2.46 + if alias is not None:
2.47 + self.namespace[alias] = self.importer.load(name, 1)
2.48 + else:
2.49 + self.namespace[name.split(".")[0]] = self.importer.load(name)
2.50
2.51 return None
2.52
2.53 @@ -232,6 +246,8 @@
2.54 def visitModule(self, node):
2.55 return self.dispatch(node.node)
2.56
2.57 + visitMul = NOP
2.58 +
2.59 def visitName(self, node):
2.60 name = node.name
2.61 if name == "self":
3.1 --- a/test.py Wed Oct 17 01:00:30 2007 +0200
3.2 +++ b/test.py Wed Oct 17 01:31:33 2007 +0200
3.3 @@ -4,5 +4,6 @@
3.4 import sys
3.5 i = micropython.Importer(sys.path)
3.6 m = i.load("micropython")
3.7 +#m = i.load_from_file("micropython/__init__.py")
3.8
3.9 # vim: tabstop=4 expandtab shiftwidth=4