Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > get return or locals from "exec" str in "environment"

Reply
Thread Tools

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

 
 
lucas
Guest
Posts: n/a
 
      08-30-2012
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
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      08-30-2012
On Fri, Aug 31, 2012 at 8:11 AM, lucas <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
lucas
Guest
Posts: n/a
 
      08-30-2012
> Far as I can see, you never actually called that function anywhere.
> ChrisA


doesn't the exec command call the function?
 
Reply With Quote
 
lucas
Guest
Posts: n/a
 
      08-30-2012
> Far as I can see, you never actually called that function anywhere.
> ChrisA


doesn't the exec command call the function?
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      08-30-2012
On Fri, Aug 31, 2012 at 8:25 AM, lucas <(E-Mail Removed)> 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
 
Reply With Quote
 
lucas
Guest
Posts: n/a
 
      08-30-2012
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
 
Reply With Quote
 
lucas
Guest
Posts: n/a
 
      08-30-2012
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
 
Reply With Quote
 
lucas
Guest
Posts: n/a
 
      08-30-2012
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?
 
Reply With Quote
 
lucas
Guest
Posts: n/a
 
      08-30-2012
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?
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      08-31-2012
On Fri, Aug 31, 2012 at 9:54 AM, lucas <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if len(str(a)) == len(str(r)) and isMult(a, r): faster if isMult isslow? maestro Python 1 08-11-2008 01:17 PM
str.equals(null) or str==null ? Stefan Ram Java 21 08-03-2006 06:43 PM
It is fun.the result of str.lower(str()) Sullivan WxPyQtKinter Python 5 03-09-2006 08:09 AM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM
what's the deference between str=null and str=" " ???????? David Java 2 08-03-2003 04:10 PM



Advertisments