[IronPython] Problems with exec

Martin Maly Martin.Maly at microsoft.com
Fri Jan 27 18:08:21 CET 2006

Thanks for the report, Michael, this is certainly something for us to look into for the next release.


-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Twomey
Sent: Friday, January 27, 2006 5:15 AM
To: users at lists.ironpython.com
Subject: [IronPython] Problems with exec


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).

users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list