[IronPython] Metaclass Error
Nathan R. Ernst
ernsnat at iit.edu
Fri Apr 7 04:36:59 CEST 2006
With the following (contrived) metaclass sample, I get a NotImplementedError
on the indicated line. It appears to be due to IPythonContainer interface
members being unimplemented in IronPython.Runtime.ReflectedType. I've
attached the .Net stack separately as it was a little ugly.
#######Begin Code Block 1 #######
#!/usr/bin/env python
class DataType(type):
def __init__(cls, name, bases, dct):
dct = dict([(k, cls.make_prop(cls, k, v)) for k, v in
dct.iteritems()])
type.__init__(cls, name, bases, dct)
def make_prop(cls, name, type, read_only=False):
prop_name = '_%s__%s' % (cls.__name__, name)
def setter(self, __x):
setattr(self, prop_name, type(__x))
def getter(self):
return getattr(self, prop_name)
if read_only: ## Error occurs here.
return property(getter)
else:
return property(getter, setter)
class Test(object):
__metaclass__ = DataType
foo = str
bar = int
t = Test()
t.foo = 'Hello World'
t.bar = 42
print '%s: %d' % (t.foo, t.bar)
#######End Code Block 1 #######
IronPython stack:
File .\metatest.py, line 23, in Initialize
File .\metatest.py, line 6, in __init__
File .\metatest.py, line 18, in make_prop
NotImplementedError: The method or operation is not implemented.
CPython output:
Hello World: 42
if I comment out the "if read_only.else" lines yielding the second code
block, the code works.
#######Begin Code Block 2 #######
#!/usr/bin/env python
class DataType(type):
def __init__(cls, name, bases, dct):
dct = dict([(k, cls.make_prop(cls, k, v)) for k, v in
dct.iteritems()])
type.__init__(cls, name, bases, dct)
def make_prop(cls, name, type, read_only=False):
prop_name = '_%s__%s' % (cls.__name__, name)
def setter(self, __x):
setattr(self, prop_name, type(__x))
def getter(self):
return getattr(self, prop_name)
return property(getter, setter)
class Test(object):
__metaclass__ = DataType
foo = str
bar = int
t = Test()
t.foo = 'Hello World'
t.bar = 42
print '%s: %d' % (t.foo, t.bar)
#######End Code Block 2#######
Output:
Hello World: 42
I've not yet had a chance to take a crack at a patch, but I wanted to bring
this up as soon as possible.
Thanks, as always, for all your hard work, guys.
-Nathan Ernst
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20060406/91f28e3e/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: .Net Stack.txt
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20060406/91f28e3e/attachment.txt>
More information about the Ironpython-users
mailing list