get return or locals from "exec" str in "environment"

Discussion in 'Python' started by lucas, Aug 30, 2012.

  1. lucas

    lucas Guest

    ok, i am stuck. i tried some test code attempts and i am stuck. so here is some sample code:

    xx2 = """
    def lucas53():
    harry = (4+16)/2
    rtn = dict(harry=harry)
    return rtn
    """

    and then i run:

    env = {}
    exec xx2 in env
    lst = env

    and lst returns a huge dictionary of many types, some excerpts are:
    {...
    ....'globals': <built-in function globals>, ...
    ....'vars': <built-in function vars>, ...
    ....'locals': <built-in function locals>, ...
    ....'lucas53': <function lucas53 at 0x214348c>}

    and i can see my executed function in there as a type function, and local and global vars, but i can not access or find "harry" or "rtn" the variableswithin the function lucas53. i do not know how to access the local variables within lucas53 or the locals to find harry or rtn. i really just want the return dictionary. make sense?

    anyway, python impresses me with its graceful and concise code, but i really have not found the solution to this mess.

    please advise and thank you in advance. lucas
     
    lucas, Aug 30, 2012
    #1
    1. Advertising

  2. On Fri, Aug 31, 2012 at 8:11 AM, lucas <> wrote:
    > and i can see my executed function in there as a type function, and localand global vars, but i can not access or find "harry" or "rtn" the variables within the function lucas53. i do not know how to access the local variables within lucas53 or the locals to find harry or rtn. i really just want the return dictionary. make sense?


    Far as I can see, you never actually called that function anywhere.

    ChrisA
     
    Chris Angelico, Aug 30, 2012
    #2
    1. Advertising

  3. lucas

    lucas Guest

    > Far as I can see, you never actually called that function anywhere.
    > ChrisA


    doesn't the exec command call the function?
     
    lucas, Aug 30, 2012
    #3
  4. lucas

    lucas Guest

    > Far as I can see, you never actually called that function anywhere.
    > ChrisA


    doesn't the exec command call the function?
     
    lucas, Aug 30, 2012
    #4
  5. On Fri, Aug 31, 2012 at 8:25 AM, lucas <> wrote:
    >> Far as I can see, you never actually called that function anywhere.
    >> ChrisA

    >
    > doesn't the exec command call the function?


    (Side point: You don't have to post to both comp.lang.python and
    python-list - they mirror each other.)

    What you executed included the 'def' statement. That's an executable
    statement that creates a function object:

    'lucas53': <function lucas53 at 0x214348c>

    In Python, functions are objects just like dictionaries, strings, and
    integers; you can construct them (usually with 'def' or 'lambda'),
    pass them around, tinker with their attribututes, and ultimately, call
    them.

    But unless you do actually call that function, none of its code will
    be executed. You can test this by putting a 'print' inside the
    function; you'll see screen output when the function's called, and
    your code above won't show that.

    To access the local variables/names from the function itself, you'll
    need to put a call to locals() inside that function, because as soon
    as it finishes, those locals disappear. Try this:

    >>> xx2 = """

    def lucas53():
    harry = (4+16) / 2
    rtn = dict(harry=harry)
    return rtn

    foo = lucas53()
    """
    >>> env = {}
    >>> exec(xx2,env)


    (This is Python 3 syntax, exec is now a function - otherwise
    equivalent to what you did.)

    You'll now see a name 'foo' in env, with the mapping returned from
    your function. There's no peeking into locals here, just a straight
    function return value.

    Is this what you were looking for?

    ChrisA
     
    Chris Angelico, Aug 30, 2012
    #5
  6. lucas

    lucas Guest

    oh, yeah that was perfect. got it working and it is graceful too. sorry about the double post, i thought i was only posting to this one.

    one final concern, if this code is running under a function in a multi-threaded, multi-session kind of environment, does exec cross threads or sessions? like, i am afraid that i will get cross-over or bleeding into other threads or sessions. does exec do that kind of common memory space wherein i have to be very very careful about executing such code and my daemon crashing or security holes and the like.

    lucas
     
    lucas, Aug 31, 2012
    #6
  7. lucas

    lucas Guest

    oh, yeah that was perfect. got it working and it is graceful too. sorry about the double post, i thought i was only posting to this one.

    one final concern, if this code is running under a function in a multi-threaded, multi-session kind of environment, does exec cross threads or sessions? like, i am afraid that i will get cross-over or bleeding into other threads or sessions. does exec do that kind of common memory space wherein i have to be very very careful about executing such code and my daemon crashing or security holes and the like.

    lucas
     
    lucas, Aug 31, 2012
    #7
  8. lucas

    lucas Guest

    also, does that environment space, what i am assigning as env, have any such common memory space or cross thread problem with simultaneous threads or sessions?
     
    lucas, Aug 31, 2012
    #8
  9. lucas

    lucas Guest

    also, does that environment space, what i am assigning as env, have any such common memory space or cross thread problem with simultaneous threads or sessions?
     
    lucas, Aug 31, 2012
    #9
  10. On Fri, Aug 31, 2012 at 9:54 AM, lucas <> wrote:
    > oh, yeah that was perfect. got it working and it is graceful too. sorryabout the double post, i thought i was only posting to this one.


    Hehe, you're still posting to both. I don't see the duplicates myself,
    but I'm sure others do. Just pick one and ignore the other.

    > one final concern, if this code is running under a function in a multi-threaded, multi-session kind of environment, does exec cross threads or sessions? like, i am afraid that i will get cross-over or bleeding into other threads or sessions. does exec do that kind of common memory space wherein i have to be very very careful about executing such code and my daemon crashing or security holes and the like.


    Not that I am aware of, and I would be extremely surprised if there
    were any. But exec is not the sort of thing you'll normally want to
    use. What are you trying to accomplish? There's usually an
    alternative.

    The only time I've used an exec-like feature is when I'm actually
    writing something that loads code from the disk at run-time, such as
    my MUD with room files that look like this:

    @sdesc Short Description
    @ldesc This is the long description of the room, blah blah
    @cmds thwap #do_something_when_user_types_thwap()

    VERY unusual sort of thing to do - having real code in a data file.

    ChrisA
     
    Chris Angelico, Aug 31, 2012
    #10
    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. Giles Brown

    Question about "exec in globals, locals"

    Giles Brown, Jul 4, 2003, in forum: Python
    Replies:
    2
    Views:
    368
    Adrien Di Mascio
    Jul 4, 2003
  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. Replies:
    2
    Views:
    373
  5. rocksportrocker

    problem with exec and locals()

    rocksportrocker, Jul 1, 2008, in forum: Python
    Replies:
    4
    Views:
    300
    Gabriel Genellina
    Jul 11, 2008
Loading...

Share This Page