# HG changeset patch # User Paul Boddie # Date 1358206359 -3600 # Node ID 74ad0468297b2cea4d3f7525a3ef9bdbeff998dd # Parent c1c95a183e21ea32aea927b73bb071feceab14ee Added support for multiple class definitions in the same namespace with the same name. Made classes attribute users. diff -r c1c95a183e21 -r 74ad0468297b TO_DO.txt --- a/TO_DO.txt Mon Jan 14 23:26:31 2013 +0100 +++ b/TO_DO.txt Tue Jan 15 00:32:39 2013 +0100 @@ -31,6 +31,9 @@ define another form with that name. Currently, such multiple definitions are treated like "unions" in the object table. + Consider functions as well as classes which are supported using "shadow" names for the + second and subsequent definitions of classes in the same namespace. + Class and Module Attribute Assignment ===================================== diff -r c1c95a183e21 -r 74ad0468297b micropython/data.py --- a/micropython/data.py Mon Jan 14 23:26:31 2013 +0100 +++ b/micropython/data.py Tue Jan 15 00:32:39 2013 +0100 @@ -1476,9 +1476,20 @@ if premade.has_key(name) and module.full_name() == "__builtins__": cls = premade[name] cls.set_context(parent, module, node) - return cls else: - return Class(name, parent, module, node) + # Where names are reused in a namespace, differentiate between classes + # using a name index. + + if parent.has_key(name): + name = "%s#%d" % (name, parent[name].assignments + 1) + + cls = Class(name, parent, module, node) + + # Add a reference for the class's "shadow" name. + + parent.use_specific_attribute(parent.full_name(), name) + + return cls # Lambda sequence numbering. diff -r c1c95a183e21 -r 74ad0468297b micropython/inspect.py --- a/micropython/inspect.py Mon Jan 14 23:26:31 2013 +0100 +++ b/micropython/inspect.py Tue Jan 15 00:32:39 2013 +0100 @@ -1014,6 +1014,7 @@ self.namespaces.pop() self.store(node.name, cls) + self.define_attribute_user(node) self.add_object(cls) # Process the class body in its own namespace. diff -r c1c95a183e21 -r 74ad0468297b tests/class_multiple_same_name_explicit_redefinition.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/class_multiple_same_name_explicit_redefinition.py Tue Jan 15 00:32:39 2013 +0100 @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +class A: + x = 1 + +class B(A): + y = 2 + +result_2 = B.y + +class B2(A): + z = 3 + +B = B2 + +result_1 = A.x +result_3 = B.z + +# vim: tabstop=4 expandtab shiftwidth=4