is it a bug in exec?

Discussion in 'Python' started by longqian9509@gmail.com, Jan 21, 2011.

  1. Guest

    In pyhton 3.1, I found the following code will succeed with argument 1
    to 4 and fail with argument 5 to 9. It is really strange to me. I
    suspect it may be a buy in exec() function. Does anyone have some idea
    about it? Thanks.


    t1="""
    class foo:
    def fun():
    print('foo')
    def main():
    global foo
    foo.fun()
    main()
    """
    t2="""
    class foo:
    def fun():
    print('foo')
    def main():
    foo.fun()
    main()
    """

    import sys
    import copy
    if sys.argv[1]=='1':
    exec(t1)
    elif sys.argv[1]=='2':
    exec(t2)
    elif sys.argv[1]=='3':
    exec(t1,{},{})
    elif sys.argv[1]=='4':
    exec(t2,globals(),locals())
    elif sys.argv[1]=='5':
    exec(t2,{},{})
    elif sys.argv[1]=='6':
    exec(t2,globals(),{})
    elif sys.argv[1]=='7':
    exec(t2,{},locals())
    elif sys.argv[1]=='8':
    exec(t2,copy.copy(globals()),locals())
    elif sys.argv[1]=='9':
    exec(t2,globals(),copy.copy(locals()))
     
    , Jan 21, 2011
    #1
    1. Advertising

  2. On Thu, 20 Jan 2011 20:52:15 -0800, wrote:

    > In pyhton 3.1, I found the following code will succeed with argument 1
    > to 4 and fail with argument 5 to 9. It is really strange to me. I
    > suspect it may be a buy in exec() function. Does anyone have some idea
    > about it? Thanks.


    What makes you think it's a bug? Is there anything in the documentation
    of exec that suggests to you that some other behaviour should occur? What
    version of Python are you using?

    Without knowing what behaviour you expect and what behaviour you see, how
    are we supposed to know if you've found a bug or not?

    I suggest you fire up the interactive interpreter and try this:

    t1 = """
    class foo:
    def fun():
    print('foo')

    def main():
    global foo
    foo.fun()

    main()
    """

    dg = {}
    dl = {}

    exec(t1, dg, dl)

    then inspect the values of dg and dl and see if it helps. If not, write
    back with what you expect to happen, and what you see instead.


    --
    Steven
     
    Steven D'Aprano, Jan 21, 2011
    #2
    1. Advertising

  3. long Guest

    Of cause your code runs well. But if you remove the "global foo" in
    main(), it will fail. And it will succeed again if you call exec(t1)
    directly. I think this behavior is strange. Even I pass a shadow copy
    of globals and locals to exec, it still fails. So perhaps there is a
    basic difference between exec(t1,dg,dl) and
    exec(t1,globals(),locals()). What do you think about it? Thanks.


    On Jan 21, 2:14 am, Steven D'Aprano <steve
    > wrote:
    > On Thu, 20 Jan 2011 20:52:15 -0800, wrote:
    > > In pyhton 3.1, I found the following code will succeed with argument 1
    > > to 4 and fail with argument 5 to 9. It is really strange to me. I
    > > suspect it may be a buy in exec() function. Does anyone have some idea
    > > about it? Thanks.

    >
    > What makes you think it's a bug? Is there anything in the documentation
    > of exec that suggests to you that some other behaviour should occur? What
    > version of Python are you using?
    >
    > Without knowing what behaviour you expect and what behaviour you see, how
    > are we supposed to know if you've found a bug or not?
    >
    > I suggest you fire up the interactive interpreter and try this:
    >
    > t1 = """
    > class foo:
    >     def fun():
    >         print('foo')
    >
    > def main():
    >     global foo
    >     foo.fun()
    >
    > main()
    > """
    >
    > dg = {}
    > dl = {}
    >
    > exec(t1, dg, dl)
    >
    > then inspect the values of dg and dl and see if it helps. If not, write
    > back with what you expect to happen, and what you see instead.
    >
    > --
    > Steven
     
    long, Jan 21, 2011
    #3
  4. Peter Otten Guest

    wrote:

    > In pyhton 3.1, I found the following code will succeed with argument 1
    > to 4 and fail with argument 5 to 9. It is really strange to me. I
    > suspect it may be a buy in exec() function. Does anyone have some idea
    > about it? Thanks.
    >
    >
    > t1="""
    > class foo:
    > def fun():
    > print('foo')
    > def main():
    > global foo
    > foo.fun()
    > main()
    > """
    > t2="""
    > class foo:
    > def fun():
    > print('foo')
    > def main():
    > foo.fun()
    > main()
    > """
    >
    > import sys
    > import copy
    > if sys.argv[1]=='1':
    > exec(t1)
    > elif sys.argv[1]=='2':
    > exec(t2)
    > elif sys.argv[1]=='3':
    > exec(t1,{},{})
    > elif sys.argv[1]=='4':
    > exec(t2,globals(),locals())
    > elif sys.argv[1]=='5':
    > exec(t2,{},{})
    > elif sys.argv[1]=='6':
    > exec(t2,globals(),{})
    > elif sys.argv[1]=='7':
    > exec(t2,{},locals())
    > elif sys.argv[1]=='8':
    > exec(t2,copy.copy(globals()),locals())
    > elif sys.argv[1]=='9':
    > exec(t2,globals(),copy.copy(locals()))


    There are only two cases that matter: identical local/global namespaces and
    distinct local/global namespaces:

    >>> code = """\

    .... x = 42 # put x into the local namespace
    .... def f():
    .... print(x) # look up x in the global namespace
    .... f()
    .... """
    >>> exec(code, {}, {})

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<string>", line 4, in <module>
    File "<string>", line 3, in f
    NameError: global name 'x' is not defined
    >>> ns = {}
    >>> exec(code, ns, ns)

    42

    Also note that

    >>> globals() is locals()

    True

    on the module level.

    Peter
     
    Peter Otten, Jan 21, 2011
    #4
    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,139
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,098
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    473
    Duncan Booth
    Jul 22, 2004
  4. Guillermo Riojas
    Replies:
    0
    Views:
    175
    Guillermo Riojas
    Nov 26, 2010
  5. Random Task
    Replies:
    12
    Views:
    660
    Joe Smith
    Dec 4, 2005
Loading...

Share This Page