# HG changeset patch # User Paul Boddie # Date 1358036381 -3600 # Node ID be6dee8789489437fdfaea68fc1bfcf917fab6e9 # Parent 83db2cdbc1d082c77aeea668745676a565578792 Added some support for classes defined using the same name in the same namespace. diff -r 83db2cdbc1d0 -r be6dee878948 micropython/__init__.py --- a/micropython/__init__.py Sat Jan 12 18:38:34 2013 +0100 +++ b/micropython/__init__.py Sun Jan 13 01:19:41 2013 +0100 @@ -311,12 +311,12 @@ elif isinstance(obj, Class): + full_name = obj.full_name() + # Prevent ambiguous classes. - full_name = obj.full_name() - - #if obj.module.has_key(name) and obj.module[name].defines_ambiguous_class(): - # raise TableGenerationError, "Class %r in module %r is ambiguously defined." % (name, obj.module.full_name()) + #if obj.module.has_key(obj.name) and obj.module[obj.name].defines_ambiguous_class(): + # raise TableGenerationError, "Class %r in module %r is ambiguously defined." % (obj.name, obj.module.full_name()) # Define a table entry for the class. @@ -329,6 +329,13 @@ if descendant in all_objects: attributes["#" + name] = descendant + # Merge ambiguous classes. + + if t.has(full_name): + old_attributes = t.get(full_name) + old_attributes.update(attributes) + attributes = old_attributes + t.add(full_name, attributes) return self.objtable diff -r 83db2cdbc1d0 -r be6dee878948 micropython/table.py --- a/micropython/table.py Sat Jan 12 18:38:34 2013 +0100 +++ b/micropython/table.py Sun Jan 13 01:19:41 2013 +0100 @@ -3,7 +3,7 @@ """ Preparation of run-time attribute lookup tables. -Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie +Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -233,6 +233,18 @@ except KeyError: raise TableError, "Name %r is not registered as an attribute in the table." % attrname + def has(self, objname): + + "Return whether the table has a definition for 'objname'." + + return self.table.has_key(objname) + + def get(self, objname): + + "Return the attributes for 'objname'." + + return self.table[objname] + def add(self, objname, attributes): "For the given 'objname' add the given 'attributes' to the table." diff -r 83db2cdbc1d0 -r be6dee878948 tests/class_multiple_same_name.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/class_multiple_same_name.py Sun Jan 13 01:19:41 2013 +0100 @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +class A: + x = 1 + +class B(A): + y = 2 + +class B(A): + z = 3 + +result_1 = A.x +result_3 = B.z + +# vim: tabstop=4 expandtab shiftwidth=4