[New-bugs-announce] [issue4381] Cannot declare multiple classes via exec when inside a function.

Kevin Fitch report at bugs.python.org
Fri Nov 21 21:33:45 CET 2008


New submission from Kevin Fitch <kfitch42 at gmail.com>:

codeText = """
class foo(object): pass
class bar(object):
  baz = foo()
"""

def doExec(text):
  exec text

### This works:
# Although if I do this before the doExec below, then the
# doExec doesn't fail.
# exec codeText

### But this does not:
doExec(codeText)


The output I get is:
---------------------------------------------------------------------------
<type 'exceptions.NameError'>             Traceback (most recent call last)

/home/kfitch/<ipython console> in <module>()

/home/kfitch/<ipython console> in doExec(text)

/home/kfitch/<string> in <module>()

/home/kfitch/<string> in bar()

<type 'exceptions.NameError'>: name 'foo' is not defined



I don't fully understand why the version in the function doesn't work,
but I suspect it is a bug related to scoping, since foo is really
doExec.foo (I think).

This is with python 2.5.2 under Linux (Ubuntu 8.04)

----------
messages: 76202
nosy: kfitch
severity: normal
status: open
title: Cannot declare multiple classes via exec when inside a function.
versions: Python 2.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4381>
_______________________________________


More information about the New-bugs-announce mailing list