Author: dmeyer
Date: Fri Feb 29 13:33:45 2008
New Revision: 3135
Log:
The metaclass must inherit from type or super() will not
work in such classes. As a downside, the member __initerface__
to init the interface is now gone.
Modified:
trunk/base/src/utils.py
trunk/base/test/interface.py
Modified: trunk/base/src/utils.py
==============================================================================
--- trunk/base/src/utils.py (original)
+++ trunk/base/src/utils.py Fri Feb 29 13:33:45 2008
@@ -286,37 +286,26 @@
# list of interface definitions
_interfaces = {}
-class implements(object):
+def implements(*interfaces):
"""
Metaclass class generator that will inherit the object from all interfaces
defined on __init__. This can be used to inherit from a class which is not
visible when the base class is defined.
"""
- def __init__(self, *cls):
- self._cls = cls
-
- def __call__(self, name, bases, dict):
- """
- The metadata class
- """
- def create(*args, **kwargs):
+ class MetaClass(type):
+ def __new__(cls, name, bases, dict):
+ """
+ The metadata class
+ """
inherit = list(bases)
- for interface in self._cls:
+ for interface in interfaces:
if not interface in _interfaces:
raise AttributeError('%s is no valid interface' %
interface)
inherit.append(_interfaces[interface])
- from new import classobj
if object in inherit:
inherit.remove(object)
- obj = classobj(name, tuple(inherit), dict)(*args, **kwargs)
- for interface in self._cls:
- # call hidden __interface__ functions for __init__
- func = getattr(_interfaces[interface], '__interface__', None)
- if func is not None:
- func(obj)
- return obj
-
- return create
+ return type.__new__(cls, name, tuple(inherit), dict)
+ return MetaClass
def add_interface(cls, name):
"""
Modified: trunk/base/test/interface.py
==============================================================================
--- trunk/base/test/interface.py (original)
+++ trunk/base/test/interface.py Fri Feb 29 13:33:45 2008
@@ -6,9 +6,7 @@
class FooInterface(object):
- def __interface__(self):
- # __init__ function for interfaces, no args allowed
- # will be called after __init__ of the real class
+ def init_FooInterface(self):
print 'Init FooInterface:', self.get_value()
def get_value(self):
@@ -25,6 +23,9 @@
def func3(self):
raise NotImplementedError
+ def x(self):
+ print 2
+
kaa.add_interface(FooInterface, 'test.Foo')
class BarInterface(object):
@@ -42,6 +43,10 @@
class MyObject(object):
__metaclass__ = kaa.implements('test.Foo', 'bar')
+ def __init__(self):
+ print 'Init MyObject'
+ self.init_FooInterface()
+
def get_value(self):
# required by test.Foo
return 1
@@ -49,7 +54,12 @@
def func1(self, string):
print string
+ def x(self):
+ print 1
+ super(MyObject, self).x()
+
p = MyObject() # print Init FooInterface: 1
+p.x() # print 1 and 2
p.func1('x') # --> print x
p.is_also_foo() # --> print True
p.func2() # --> print already defined
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog