[IronPython] Problems with exec

Michael Twomey micktwomey at gmail.com
Fri Jan 27 14:14:53 CET 2006


I've started work on an ironpython implementation of the _socket
module so I can use standard python sockets. I was making good
progress until I hit a problem with socket.py's use of exec.

In the socket._socketobject class exec is used to create wrappers for
various methods in the socket class in _socket. When the code is run
you get an attributeerror because the exec statement hasn't set the
attributes on the class.

This example demonstrates the problem:

class MyRealObj(object):
    """The real object"""
    def foo(self):
        return "foo"

class MyObj(object):
    """The public wrapper"""
    def __init__(self):
        self.realobj = MyRealObj()

    #reproducing socket.py's exec statement
    _s = ("def %s(self, *args): return self.realobj.%s(*args)\n\n"
          "%s.__doc__ = MyRealObj.%s.__doc__\n")
    for _m in ["foo"]:
        exec _s % (_m, _m, _m, _m)
    del _m, _s

o = MyObj()
print o.foo()

Looking at it closer it appears that the foo method has been placed
into the module instead of the class, the socket module winds up with
the methods instead of the socket class. I suspect it's a case of
dealing with the locals a bit differently inside a class definition.

I'll have a go at fixing this but I'm still finding my way around the
ironpython code (I'm up to ops.cs and statements.cs).


More information about the Ironpython-users mailing list