[ python-Bugs-1177811 ] Exec Inside A Function

SourceForge.net noreply at sourceforge.net
Tue Nov 22 21:28:18 CET 2005


Bugs item #1177811, was opened at 2005-04-06 16:30
Message generated for change (Comment added) made by birkenfeld
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1177811&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Andrew Wilkinson (andrew_j_w)
Assigned to: Nobody/Anonymous (nobody)
Summary: Exec Inside A Function

Initial Comment:
When 'exec'ing code that creates a function inside a function the 
defined function (fact in the example below) is created with the 
module level namespace as it's parent scope. 
 
The following should return 2 however it raises a NameError as fact 
is not defined. 
 
def f(): 
    exec """ 
def fact(x): 
    if x==1: 
        return 1 
    else: 
        return x*fact(x-1) 
""" 
    return fact 
 
f()(2) 
 
If you run following code... 
 
def f(): 
    exec """ 
def fact(x): 
    if x==1: 
        return 1 
    else: 
        return x*fact(x-1) 
""" in locals() 
    return fact 
 
... it works as expected. 
 
The documentation states that "In all cases, if the optional parts 
are omitted, the code is executed in the current scope." That is 
clearly not the case here as the 'fact' function is set with the 
module level scope as it's parent scope. 
 
It would appear to me that either this a documentation bug or a 
flaw in exec. I sincerely hope this a bug in exec and not the 
desired behaviour as it doesn't make any sense to me... 
 

----------------------------------------------------------------------

>Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-11-22 21:28

Message:
Logged In: YES 
user_id=1188172

I'd say this is too esoteric a use case that it can be
considered a bug. Besides, as the OP demonstrates, there is
a workaround, so I'm closing this.

----------------------------------------------------------------------

Comment By: Fredrik Lundh (effbot)
Date: 2005-11-12 17:59

Message:
Logged In: YES 
user_id=38376

As described in the language reference, scope analysis is
done statically, during compilation. If the compiler cannot
see your innermost function (because you've hidden it inside
a string literal), it obviously cannot take any variables in
there into account when generating code for the outer scopes.

(I sincerely hope that this is a contrived example, and that
you're not trying to use exec in this way in real code.
Python might be a highly dynamic language, but it's not this
dynamic.)

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1177811&group_id=5470


More information about the Python-bugs-list mailing list