# HG changeset patch # User Paul Boddie # Date 1209227718 -7200 # Node ID 445e08d9d4fb50f70fb940541f7af18efb905b85 # Parent 4b3cde1edd1994ad1e761fb9844ca338c5eaef4e Removed the optional nature of the importer when visiting modules with InspectedModule. Added keyword argument name tracking and the filtering of unused parameter table entries (whose parameter names never get used as keyword arguments). Note that this potentially affects things like **args, but we may choose not to support such things anyway. diff -r 4b3cde1edd19 -r 445e08d9d4fb micropython/__init__.py --- a/micropython/__init__.py Sat Apr 26 02:31:44 2008 +0200 +++ b/micropython/__init__.py Sat Apr 26 18:35:18 2008 +0200 @@ -229,6 +229,9 @@ if self.paramtable is None: t = self.paramtable = micropython.table.Table() + + # Visit each module, getting function and method details. + for module in self.get_modules(): for obj in module.all_objects: if isinstance(obj, micropython.inspect.Function): @@ -241,6 +244,21 @@ elif isinstance(obj, micropython.inspect.Class): t.add(obj.full_name(), obj.get_instantiator().parameters()) + # Filter out all parameter table entries not referenced by keyword + # arguments. + + keyword_names = set() + + for module in self.get_modules(): + keyword_names.update(module.keyword_names) + + for function_name, parameters in t.table.items(): + for name in parameters.keys(): + if name in keyword_names: + break + else: + del t.table[function_name] + return self.paramtable # Import methods. diff -r 4b3cde1edd19 -r 445e08d9d4fb micropython/inspect.py --- a/micropython/inspect.py Sat Apr 26 02:31:44 2008 +0200 +++ b/micropython/inspect.py Sat Apr 26 18:35:18 2008 +0200 @@ -773,6 +773,10 @@ self.constant_values = {} self.constant_list = None # cache for constants + # Keyword records. + + self.keyword_names = set() + # Image generation details. self.location = None @@ -821,7 +825,13 @@ capable of being used as an AST visitor. """ - def __init__(self, name, importer=None): + def __init__(self, name, importer): + + """ + Initialise this visitor with a module 'name' and an 'importer' which is + used to provide access to other modules when required. + """ + ASTVisitor.__init__(self) Module.__init__(self, name) self.visitor = self @@ -902,6 +912,9 @@ self.namespaces[-2].add_instance_attribute(name) def get_parent(self): + + "Return the parent (or most recent) namespace currently exposed." + return (self.namespaces[-1:] or [self])[0] # Visitor methods. @@ -1038,10 +1051,6 @@ self.in_loop = 0 def visitFrom(self, node): - if self.importer is None: - raise InspectError(self.full_name(), node, - "Please use the micropython.Importer class for code which uses the 'from' statement.") - module = self.importer.load(node.modname, 1) #if module is None: @@ -1152,10 +1161,6 @@ visitIfExp = NOP def visitImport(self, node): - if self.importer is None: - raise InspectError(self.full_name(), node, - "Please use the micropython.Importer class for code which uses the 'import' statement.") - for name, alias in node.names: if alias is not None: self.store(alias, self.importer.load(name, 1) or UnresolvedName(None, name, self)) @@ -1170,6 +1175,7 @@ self.dispatch(node.expr) const = Const(node.name) self.constant_values[node.name] = const + self.keyword_names.add(node.name) return None visitLambda = NOP