[IronPython] Metaclass Error

Dino Viehland dinov at exchange.microsoft.com
Fri Apr 7 17:58:41 CEST 2006


Thanks for the bug report.  It actually appears to be a bug in our method call logic, and is definitely and interesting corner case.

In this case we think make_prop is a method on the class DataType and we think when you call it with cls.(...) that we should add cls in as the first instance parameter (which is quite wrong).  Therefore your arguments are shifted over by one, and read_only is a type (which when you check to see if it's true throws not-implemented for it's length).

One work around that'll make this work in both CPython and IronPython is to explicitly declare make_prop static:

      @staticmethod
      def make_prop(cls, name, type, read_only=False):
            prop_name = '_%s__%s' % (cls.__name__, name)

We should be able to get this one fixed for beta 6.


Do you want to help develop Dynamic languages on CLR?<http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F0-45DF-8B78-DC1B43134038> (http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F0-45DF-8B78-DC1B43134038)
________________________________
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Nathan R. Ernst
Sent: Thursday, April 06, 2006 7:37 PM
To: users at lists.ironpython.com
Subject: [IronPython] Metaclass Error

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/20060407/16315477/attachment.html>


More information about the Ironpython-users mailing list