# HG changeset patch # User Paul Boddie # Date 1372632463 -7200 # Node ID e148fb5f222a56cf71b3ecf398f8056bf645a19c # Parent 46ef6f4f6d03b178dd06c5d70daa44502c2911cb Simplified __builtins__ module accesses, assuming that such accesses performed in the translations of various constructs involve unmodified constant module attributes. Fixed the nodes produced by module_attribute. Added a constant_attribute function and support for constant name-based accesses. diff -r 46ef6f4f6d03 -r e148fb5f222a docs/annotations.txt --- a/docs/annotations.txt Sun Jun 30 22:46:32 2013 +0200 +++ b/docs/annotations.txt Mon Jul 01 00:47:43 2013 +0200 @@ -33,7 +33,9 @@ ------------------ _attr notes the result associated with an attribute access - operation during inspection + operation during inspection; this may be a general + instance providing no specific information about the + nature of an attribute _expr records the result of evaluating an expression used in an attribute access operation diff -r 46ef6f4f6d03 -r e148fb5f222a micropython/syspython.py --- a/micropython/syspython.py Sun Jun 30 22:46:32 2013 +0200 +++ b/micropython/syspython.py Mon Jul 01 00:47:43 2013 +0200 @@ -34,15 +34,18 @@ # Convenience definitions. -module_attribute = compiler.ast.Getattr +constant_attribute = compiler.ast.Getattr special_name = compiler.ast.Name -def quoted_ref(obj): - return compiler.ast.CallFunc("__static__", [compiler.ast.Const(obj.full_name())]) - def quoted_name(s): return compiler.ast.Const(s) +def quoted_ref(obj): + return compiler.ast.CallFunc("__static__", [quoted_name(obj.full_name())]) + +def module_attribute(module_name, attrname): + return compiler.ast.Getattr(special_name(module_name), attrname) + # Special function names. # NOTE: Some of the assignment operations should not be supported unless # NOTE: attribute usage observations are being made. @@ -244,13 +247,11 @@ else_nodes = node.else_ and self.dispatch(node.else_).nodes or [] return compiler.ast.Stmt([ - # __storetemp__(_it, __loadattr__(__builtins__, iter)()) + # __storetemp__(_it, __builtins__.iter()) compiler.ast.CallFunc(special_name("__storetemp__"), [ temp, compiler.ast.CallFunc( - compiler.ast.CallFunc(special_name("__loadattr__"), - [special_name("__builtins__"), special_name("iter")] - ), + module_attribute("__builtins__", "iter"), [self.dispatch(node.list)] ) ]), @@ -547,7 +548,7 @@ if node.attrname == "__class__": # __storetemp__(n, ) - # __isclass__(n) and __loadattr__(__builtins__, type) or + # __isclass__(n) and __builtins__.type or temp = quoted_name(unit.temp_usage) unit.temp_usage += 1 @@ -560,10 +561,7 @@ special_name("__isclass__"), [compiler.ast.CallFunc(special_name("__loadtemp__"), [temp])] ), - compiler.ast.CallFunc( - special_name("__loadattr__"), - [special_name("__builtins__"), special_name("type")] - ) + module_attribute("__builtins__", "type") ]), access ]) @@ -798,13 +796,11 @@ unit.temp_usage += 1 return compiler.ast.Stmt([ - # __storetemp__(_out, __loadattr__(__builtins__, list)()) + # __storetemp__(_out, __builtins__.list()) compiler.ast.CallFunc(special_name("__storetemp__"), [ temp, compiler.ast.CallFunc( - compiler.ast.CallFunc(special_name("__loadattr__"), - [special_name("__builtins__"), special_name("list")] - ), + module_attribute("__builtins__", "list"), [] ) ]), @@ -850,13 +846,11 @@ # Wrap the above body in the loop construct. return compiler.ast.Stmt([ - # __storetemp__(_it, __loadattr__(__builtins__, iter)()) + # __storetemp__(_it, __builtins__.iter()) compiler.ast.CallFunc(special_name("__storetemp__"), [ temp, compiler.ast.CallFunc( - compiler.ast.CallFunc(special_name("__loadattr__"), - [special_name("__builtins__"), special_name("iter")] - ), + module_attribute("__builtins__", "iter"), [self.dispatch(node.list)] ) ]), @@ -946,10 +940,13 @@ # Other attributes should already be resolved. elif attr is not None and not isinstance(attr, Instance): - return compiler.ast.CallFunc( - special_name("__loadattr__"), - [quoted_ref(attr.parent), special_name(node.name)] - ) + if attr.is_constant(): + return constant_attribute(quoted_ref(attr.parent), node.name) + else: + return compiler.ast.CallFunc( + special_name("__loadattr__"), + [quoted_ref(attr.parent), special_name(node.name)] + ) # Function globals are referenced via the module.