Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28142 > unrolled thread

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

Started byChris Angelico <rosuav@gmail.com>
First post2012-08-31 08:19 +1000
Last post2012-08-30 15:25 -0700
Articles 9 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: get return or locals from "exec" str in "environment" Chris Angelico <rosuav@gmail.com> - 2012-08-31 08:19 +1000
    Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 15:25 -0700
      Re: get return or locals from "exec" str in "environment" Chris Angelico <rosuav@gmail.com> - 2012-08-31 08:45 +1000
        Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 16:54 -0700
          Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 16:56 -0700
          Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 16:56 -0700
          Re: get return or locals from "exec" str in "environment" Chris Angelico <rosuav@gmail.com> - 2012-08-31 10:13 +1000
        Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 16:54 -0700
    Re: get return or locals from "exec" str in "environment" lucas <sjlukacs@gmail.com> - 2012-08-30 15:25 -0700

#28142 — Re: get return or locals from "exec" str in "environment"

FromChris Angelico <rosuav@gmail.com>
Date2012-08-31 08:19 +1000
SubjectRe: get return or locals from "exec" str in "environment"
Message-ID<mailman.3987.1346365155.4697.python-list@python.org>
On Fri, Aug 31, 2012 at 8:11 AM, lucas <sjlukacs@gmail.com> wrote:
> 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 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

[toc] | [next] | [standalone]


#28143

Fromlucas <sjlukacs@gmail.com>
Date2012-08-30 15:25 -0700
Message-ID<599e5709-22ab-4e85-8443-181a2ae19c17@googlegroups.com>
In reply to#28142
> Far as I can see, you never actually called that function anywhere.
> ChrisA

doesn't the exec command call the function?

[toc] | [prev] | [next] | [standalone]


#28147

FromChris Angelico <rosuav@gmail.com>
Date2012-08-31 08:45 +1000
Message-ID<mailman.3991.1346366717.4697.python-list@python.org>
In reply to#28143
On Fri, Aug 31, 2012 at 8:25 AM, lucas <sjlukacs@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#28149

Fromlucas <sjlukacs@gmail.com>
Date2012-08-30 16:54 -0700
Message-ID<1616cffa-23ea-4377-b415-fa143d0c5e5d@googlegroups.com>
In reply to#28147
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

[toc] | [prev] | [next] | [standalone]


#28151

Fromlucas <sjlukacs@gmail.com>
Date2012-08-30 16:56 -0700
Message-ID<a5ac6d90-84e2-4cbf-bb72-c6f9b235c8da@googlegroups.com>
In reply to#28149
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?

[toc] | [prev] | [next] | [standalone]


#28152

Fromlucas <sjlukacs@gmail.com>
Date2012-08-30 16:56 -0700
Message-ID<mailman.3994.1346371014.4697.python-list@python.org>
In reply to#28149
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?

[toc] | [prev] | [next] | [standalone]


#28154

FromChris Angelico <rosuav@gmail.com>
Date2012-08-31 10:13 +1000
Message-ID<mailman.3995.1346372034.4697.python-list@python.org>
In reply to#28149
On Fri, Aug 31, 2012 at 9:54 AM, lucas <sjlukacs@gmail.com> wrote:
> 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.

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

[toc] | [prev] | [next] | [standalone]


#28150

Fromlucas <sjlukacs@gmail.com>
Date2012-08-30 16:54 -0700
Message-ID<mailman.3993.1346370867.4697.python-list@python.org>
In reply to#28147
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

[toc] | [prev] | [next] | [standalone]


#28144

Fromlucas <sjlukacs@gmail.com>
Date2012-08-30 15:25 -0700
Message-ID<mailman.3988.1346365520.4697.python-list@python.org>
In reply to#28142
> Far as I can see, you never actually called that function anywhere.
> ChrisA

doesn't the exec command call the function?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web