Exec woes

Discussion in 'Python' started by Hendrik van Rooyen, Jan 27, 2009.

  1. It starts with the conspiracy of silence at the interactive prompt:

    Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.

    IDLE 1.1.3 ==== No Subprocess ====
    >>> help(exec)

    SyntaxError: invalid syntax
    >>>

    Its the same under Linux SuSe, Python 2.5.1.

    I think this is a BUG.

    Anyway, my real problem looks like this:

    >>> def Somefunc():

    def excrescence():
    exec('BUILD = "someString"')
    return BUILD

    SyntaxError: unqualified exec is not allowed in function 'excrescence
    it is a nested function (<pyshell#11>, line 3)
    >>>


    Now this works if it is not nested, and it works if it is a method in a class.
    Why the complaint about the nestedness?

    I would appreciate it if someone would explain, as the library reference
    simply says that it would default to globals() and locals(), which also does
    not seem to happen - also true for a "nested" eval after a compile - you have
    to specify the globals() or you get a NameError - global BUILD is not defined.

    So there is some namespace thing happening that is too subtle for me, and I
    would like to know what "unqualified" means in the above message, and
    what one must do to "qualify" the statement, if that is what is needed.

    The original thing came up because I really need relative imports - I have
    some version.py files in various directories that have a single line in them
    that looks like the string I am feeding the exec.

    By the way, execfile works in this situation, but also only if you specify
    globals() explicitly. I would really like to understand what is going on.

    - Hendrik
     
    Hendrik van Rooyen, Jan 27, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Vaughan
    Replies:
    11
    Views:
    1,155
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,124
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    478
    Duncan Booth
    Jul 22, 2004
  4. Guillermo Riojas
    Replies:
    0
    Views:
    184
    Guillermo Riojas
    Nov 26, 2010
  5. Random Task
    Replies:
    12
    Views:
    675
    Joe Smith
    Dec 4, 2005
Loading...

Share This Page