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)


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Nathan R. Ernst
Sent: Thursday, April 06, 2006 7:37 PM
To: users@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

_______________________________________________
users mailing list
users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to