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

L

lucas

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
 
C

Chris Angelico

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
 
C

Chris Angelico

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:
def lucas53():
harry = (4+16) / 2
rtn = dict(harry=harry)
return rtn

foo = lucas53()
"""
(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
 
L

lucas

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
 
L

lucas

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
 
L

lucas

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?
 
L

lucas

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?
 
C

Chris Angelico

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top