Title: exec() does not work when namespace is passed
Type: behaviour Severity: critical
Components: Core Versions: 2.5.1
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: fabioz, thobes
Priority: Keywords:

Created on 2009-03-29.20:53:46 by fabioz, last changed 2009-03-30.07:19:30 by thobes.

msg4379 (view) Author: Fabio Zadrozny (fabioz) Date: 2009-03-29.20:53:45
The version is actually 2.5b3 (couldn't find it there).

The code below shows the error. exec() fails with:

TypeError: exec: argument 1 must be string, code or file object

But note that the if the namespace is not passed (globals and locals) it

I've marked it as critical because this seems like a real blocker
(cannot make a debugger work properly without it, as any expressions
won't work)

import sys
import unittest

class TestCase(unittest.TestCase):

    def testExec(self):
        frame = sys._getframe()
        exec('print self.__class__', {}, frame.f_locals)

if __name__ == '__main__':
msg4381 (view) Author: Tobias Ivarsson (thobes) Date: 2009-03-29.23:48:23
In this case the exec statement will be called with a tuple with 3 parts
as it's first argument. This emulates the 3.x behavior, the "normal" 2.x
behavior is "exec stmt [in globals [, locals]]".
Apparently exec should be able to accept a tuple as first "argument" in 2.x.
msg4382 (view) Author: Fabio Zadrozny (fabioz) Date: 2009-03-30.00:35:45
The results I got from Python 2.5 and 3.0 are below (both seem to accept
the form passed -- although one interprets it as a tuple and the other
from a method call, that form should be compatible across any version)

In Python 2.5.1

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> class Foo(object):
...   def foo(self):
...     exec('print self.__class__', {}, sys._getframe().f_locals)
>>> Foo().foo()
<class '__main__.Foo'>

In Python 3.0

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> class Foo(object):
...   def foo(self):
...     exec('print(self.__class__)', {}, sys._getframe().f_locals)
>>> Foo().foo()
<class '__main__.Foo'>
msg4384 (view) Author: Tobias Ivarsson (thobes) Date: 2009-03-30.07:19:22
Fixed in rev #6118.
Date User Action Args
2009-03-30 07:19:30thobessetstatus: open -> closed
resolution: fixed
messages: + msg4384
2009-03-30 00:35:47fabiozsetmessages: + msg4382
2009-03-29 23:48:23thobessetnosy: + thobes
messages: + msg4381
2009-03-29 20:53:46fabiozcreate