1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/docs/compatibility.txt Sun Mar 09 02:10:12 2008 +0100
1.3 @@ -0,0 +1,27 @@
1.4 +This document describes various compatibility differences between micropython
1.5 +and other Python implementations.
1.6 +
1.7 +Class Attribute Assignment
1.8 +--------------------------
1.9 +
1.10 + Python micropython
1.11 + ------ -----------
1.12 +
1.13 +class C:
1.14 + ...
1.15 +
1.16 +# cls refers to C
1.17 +
1.18 +cls.attr = value Class attribute Run-time error
1.19 + assigned
1.20 +
1.21 +Module Attribute Assignment
1.22 +---------------------------
1.23 +
1.24 + Python micropython
1.25 + ------ -----------
1.26 +
1.27 +import somemod
1.28 +
1.29 +somemod.attr = value Module attribute Run-time error
1.30 + assigned
2.1 --- a/micropython/__init__.py Tue Mar 04 00:41:53 2008 +0100
2.2 +++ b/micropython/__init__.py Sun Mar 09 02:10:12 2008 +0100
2.3 @@ -94,6 +94,10 @@
2.4 if not with_builtins and module_name == "__builtins__":
2.5 continue
2.6
2.7 + # Fix the attributes.
2.8 +
2.9 + module.finalise_attributes()
2.10 +
2.11 pos = len(image)
2.12
2.13 # Position the module in the image and make a translation.
2.14 @@ -122,6 +126,11 @@
2.15 # Append classes and functions to the image.
2.16
2.17 for obj in module.all_objects:
2.18 +
2.19 + # Fix the attributes.
2.20 +
2.21 + obj.finalise_attributes()
2.22 +
2.23 if isinstance(obj, micropython.inspect.Class):
2.24
2.25 # Position the class in the image.
3.1 --- a/micropython/ast.py Tue Mar 04 00:41:53 2008 +0100
3.2 +++ b/micropython/ast.py Sun Mar 09 02:10:12 2008 +0100
3.3 @@ -170,6 +170,9 @@
3.4 # NOTE: Only simple cases are used for optimisations.
3.5
3.6 last = self.last_op()
3.7 +
3.8 + # Where the last operation yields a constant...
3.9 +
3.10 if isinstance(last, (LoadName, LoadAttr)) and last.attr.assignments == 1:
3.11
3.12 # Get the details of the access.
3.13 @@ -178,7 +181,16 @@
3.14 target_name = target.full_name()
3.15 table_entry = self.objtable.table[target_name]
3.16 pos = table_entry[attrname]
3.17 - self.replace_op(AttrInstruction(pos))
3.18 + self.replace_op(AttrInstruction(pos + target.location))
3.19 +
3.20 + # Where the last operation involves the special 'self' name, check to
3.21 + # see if the attribute is acceptably positioned.
3.22 +
3.23 + elif isinstance(last, LoadName) and last.attr.name == "self" and not self.unit.is_relocated(attrname):
3.24 + attr = self.unit.parent.all_attributes()[attrname]
3.25 + self.new_op(AttrInstruction(attr))
3.26 +
3.27 + # Otherwise, perform a normal operation.
3.28
3.29 else:
3.30 index = self.objtable.get_index(attrname)
3.31 @@ -491,7 +503,7 @@
3.32 # Only store the name when visiting this node from outside.
3.33
3.34 if self.unit is not node.unit:
3.35 - self.new_op(LoadConst(node.unit))
3.36 + self.new_op(LoadConst(Const(node.unit)))
3.37 self._visitName(node, (StoreName, StoreAttr))
3.38
3.39 # Visiting of the code occurs when get_code is invoked on this node.
4.1 --- a/micropython/inspect.py Tue Mar 04 00:41:53 2008 +0100
4.2 +++ b/micropython/inspect.py Sun Mar 09 02:10:12 2008 +0100
4.3 @@ -151,10 +151,6 @@
4.4 attr.assignments = 1
4.5 attr.assignment_values.add(value)
4.6
4.7 - "A specialised set operation for parameters."
4.8 -
4.9 - set_parameter = set_module
4.10 -
4.11 def _set(self, name, value):
4.12
4.13 "The underlying set operation associating 'name' with 'value'."
4.14 @@ -220,7 +216,7 @@
4.15
4.16 class Attr:
4.17
4.18 - "An attribute entry."
4.19 + "An attribute entry having a parent as context."
4.20
4.21 def __init__(self, position, parent, name, value=None, assignments=None):
4.22 self.position = position
4.23 @@ -261,10 +257,11 @@
4.24
4.25 class Const:
4.26
4.27 - "A constant object."
4.28 + "A constant object with no parent defined as its context."
4.29
4.30 def __init__(self, value):
4.31 self.value = value
4.32 + self.parent = None
4.33
4.34 # Image generation details.
4.35
4.36 @@ -287,6 +284,12 @@
4.37 "An inspected class."
4.38
4.39 def __init__(self, name, parent, global_namespace=None, node=None):
4.40 +
4.41 + """
4.42 + Initialise the class with the given 'name', 'parent' object, optional
4.43 + 'global_namespace' and optional AST 'node'.
4.44 + """
4.45 +
4.46 NamespaceDict.__init__(self, global_namespace)
4.47 self.name = name
4.48 self.parent = parent
4.49 @@ -512,7 +515,7 @@
4.50
4.51 d = {}
4.52 for i, name in enumerate(self._get_position_list(positions)):
4.53 - d[name] = Attr(i, None, None, name)
4.54 + d[name] = Attr(i, Instance(), name, None)
4.55 return d
4.56
4.57 def _cmp_positions(self, a, b):
4.58 @@ -586,7 +589,7 @@
4.59 if isinstance(name, tuple):
4.60 self._add_parameters(name)
4.61 else:
4.62 - self.set_parameter(name, None)
4.63 + self.set(name, None)
4.64
4.65 def __repr__(self):
4.66 if self.location is not None:
4.67 @@ -633,6 +636,29 @@
4.68 del self.localnames[name]
4.69 return self.localnames
4.70
4.71 + def is_relocated(self, name):
4.72 +
4.73 + """
4.74 + Determine whether the given attribute 'name' is relocated for instances
4.75 + having this function as a method.
4.76 + """
4.77 +
4.78 + for cls in self.parent.descendants:
4.79 + if name in cls.relocated:
4.80 + return 1
4.81 + return 0
4.82 +
4.83 + def finalise_attributes(self):
4.84 +
4.85 + "Make sure all attributes are fully defined."
4.86 +
4.87 + for i, name in enumerate(self.argnames):
4.88 + self[name].position = i
4.89 +
4.90 + j = i
4.91 + for i, attr in enumerate(self.locals()):
4.92 + attr.position = i + j
4.93 +
4.94 class UnresolvedName(NamespaceDict):
4.95
4.96 "A module, class or function which was mentioned but could not be imported."
4.97 @@ -657,6 +683,13 @@
4.98 else:
4.99 return self.parent_name
4.100
4.101 +class Instance:
4.102 +
4.103 + "A placeholder indicating the involvement of an instance."
4.104 +
4.105 + def __repr__(self):
4.106 + return "Instance()"
4.107 +
4.108 class Module(NamespaceDict):
4.109
4.110 "An inspected module's core details."
5.1 --- a/micropython/rsvp.py Tue Mar 04 00:41:53 2008 +0100
5.2 +++ b/micropython/rsvp.py Sun Mar 09 02:10:12 2008 +0100
5.3 @@ -3,7 +3,7 @@
5.4 """
5.5 RSVP instruction classes.
5.6
5.7 -Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
5.8 +Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk>
5.9
5.10 This program is free software; you can redistribute it and/or modify it under
5.11 the terms of the GNU General Public License as published by the Free Software
5.12 @@ -19,6 +19,8 @@
5.13 this program. If not, see <http://www.gnu.org/licenses/>.
5.14 """
5.15
5.16 +from micropython.inspect import Const, Instance
5.17 +
5.18 class Instruction:
5.19
5.20 "A generic instruction."