1.1 --- a/docs/rationale.txt Mon Jun 16 01:56:46 2008 +0200
1.2 +++ b/docs/rationale.txt Sat Jun 21 17:23:23 2008 +0200
1.3 @@ -7,8 +7,9 @@
1.4 Motivations
1.5
1.6 * Run Python programs in "small" devices
1.7 - * Small programs
1.8 - * Few executed instructions
1.9 + * Small programs plus few executed instructions
1.10 + * Avoid expensive library code
1.11 + (small footprint, lots of executed instructions)
1.12
1.13 Python's flexibility comes at a cost
1.14
1.15 @@ -17,6 +18,13 @@
1.16 * Difficult to predict eventual behaviour before execution
1.17 * Difficult to generate optimisations when compiling
1.18
1.19 +Not all things are dynamic/equal in Python
1.20 +
1.21 + * Locals, modules and classes are special in some way
1.22 + * Locals don't tend to change unilaterally
1.23 + * Modules usually retain their identity
1.24 + * Classes differ from objects (despite metaclasses)
1.25 +
1.26 Attribute access
1.27
1.28 * Must do a full lookup every time:
1.29 @@ -34,7 +42,7 @@
1.30
1.31 Consequences of revised attribute access
1.32
1.33 - * Cannot extend the range of attributes on objects
1.34 + * Cannot extend the range of attributes on objects of existing classes
1.35 * Further optimisations:
1.36 * Restriction: attempt to control modification of attributes
1.37 * Result: further optimisation of accesses
2.1 --- a/micropython/inspect.py Mon Jun 16 01:56:46 2008 +0200
2.2 +++ b/micropython/inspect.py Sat Jun 21 17:23:23 2008 +0200
2.3 @@ -173,9 +173,26 @@
2.4
2.5 self.all_objects.add(obj)
2.6
2.7 + def store_module_attr(self, name, module):
2.8 +
2.9 + """
2.10 + Record module attribute 'name' in the given 'module' using the current
2.11 + expression.
2.12 + """
2.13 +
2.14 + if isinstance(self.expr, Attr):
2.15 + assigned_value = self.expr.value
2.16 + else:
2.17 + assigned_value = self.expr
2.18 +
2.19 + module.set(name, assigned_value, 0)
2.20 +
2.21 def store_class_attr(self, name):
2.22
2.23 - "Record class attribute 'name' in the current class."
2.24 + """
2.25 + Record class attribute 'name' in the current class using the current
2.26 + expression.
2.27 + """
2.28
2.29 if self.in_method and self.namespaces[-2].has_key(name):
2.30
2.31 @@ -300,9 +317,13 @@
2.32
2.33 def visitAssAttr(self, node):
2.34 expr = self.dispatch(node.expr)
2.35 - if isinstance(expr, Attr) and expr.name == "self":
2.36 - if not self.store_class_attr(node.attrname):
2.37 - self.store_instance_attr(node.attrname)
2.38 + if isinstance(expr, Attr):
2.39 + if expr.name == "self":
2.40 + if not self.store_class_attr(node.attrname):
2.41 + self.store_instance_attr(node.attrname)
2.42 + elif isinstance(expr.value, Module):
2.43 + self.store_module_attr(node.attrname, expr.value)
2.44 + print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.value.name)
2.45 return None
2.46
2.47 def visitAssList(self, node):
3.1 --- a/test.py Mon Jun 16 01:56:46 2008 +0200
3.2 +++ b/test.py Sat Jun 21 17:23:23 2008 +0200
3.3 @@ -4,6 +4,7 @@
3.4 from micropython.rsvp import raw
3.5 import rsvp
3.6 import sys
3.7 +import os
3.8
3.9 code = None
3.10
3.11 @@ -32,7 +33,15 @@
3.12
3.13 if __name__ == "__main__":
3.14 args = sys.argv[2:]
3.15 - i = micropython.Importer(sys.path, "-v" in args)
3.16 + path = sys.path[:]
3.17 +
3.18 + if len(sys.argv) > 1:
3.19 + program = os.path.abspath(sys.argv[1])
3.20 + path.append(os.path.split(program)[0])
3.21 + else:
3.22 + program = None
3.23 +
3.24 + i = micropython.Importer(path, "-v" in args)
3.25
3.26 if "-omax" in args:
3.27 requested_optimisations = i.supported_optimisations
3.28 @@ -45,11 +54,12 @@
3.29
3.30 try:
3.31 builtins = i.load_from_file("lib/builtins.py", "__builtins__")
3.32 - if len(sys.argv) < 2:
3.33 + if program is None:
3.34 + print "Loading module micropython ..."
3.35 m = i.load("micropython")
3.36 - #m = i.load_from_file("micropython/__init__.py")
3.37 else:
3.38 - m = i.load_from_file(sys.argv[1])
3.39 + print "Loading program", program, "..."
3.40 + m = i.load_from_file(program)
3.41 except micropython.ProcessingError, exc:
3.42 print repr(exc)
3.43 else:
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/failure/imported.py Sat Jun 21 17:23:23 2008 +0200
4.3 @@ -0,0 +1,6 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +attr = 123
4.7 +a = 10
4.8 +
4.9 +# vim: tabstop=4 expandtab shiftwidth=4
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/tests/failure/importer.py Sat Jun 21 17:23:23 2008 +0200
5.3 @@ -0,0 +1,18 @@
5.4 +#!/usr/bin/env python
5.5 +
5.6 +import imported
5.7 +
5.8 +imported.attr = 456 # detected with warning
5.9 +
5.10 +m = imported
5.11 +imported.a = 9 # detected with warning
5.12 +m.a = 8 # detected with warning (m is always imported)
5.13 +
5.14 +imported.x = 1 # detected with warning (despite no collision)
5.15 +
5.16 +n = None
5.17 +n = imported
5.18 +n.y = 2 # not detected due to reassignment of n
5.19 +n.y = 3 # not detected due to reassignment of n
5.20 +
5.21 +# vim: tabstop=4 expandtab shiftwidth=4