Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32482
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'explicitly': 0.04; 'modify': 0.05; 'retrieval': 0.05; "'a'": 0.07; 'exception.': 0.07; 'exec': 0.07; '(defined': 0.09; 'fetch': 0.09; 'namespace': 0.09; 'variables,': 0.09; 'slightly': 0.15; "'b'": 0.16; 'dictionary,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'globals.': 0.16; 'gobal': 0.16; 'it),': 0.16; 'oct': 0.16; 'uppercase': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'received:209.85.214.174': 0.21; 'modifying': 0.22; 'ones.': 0.22; "i'd": 0.22; 'example': 0.23; 'work.': 0.23; 'header:In-Reply-To:1': 0.25; 'values': 0.26; 'message- id:@mail.gmail.com': 0.27; "i'm": 0.29; 'code': 0.31; 'gets': 0.32; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'ability': 0.36; 'but': 0.36; 'subject:with': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'apply': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'most': 0.61; 'leading': 0.61; 'lower': 0.61; '30,': 0.62; 'more': 0.63; 'direct': 0.69; '(is': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=imFSlKIe37zw0YX4NFkPVy/30LoFfCysf9CWS3/5mzg=; b=v7GUMxlQClJ5pCctHA9OObhWBsgpyGAm8SsOkGFx7SCRn05vcyrIpCNOsWcmk212UF JN/EAvSj79BlFXlnxBZsWx2TLDNE18VCA493wLgZsVlgFJgdq7mlIOeBy7+pkkMsAiGd D/5Y8aJimkNCYgv7/KC5N6KlY6qiMxAxyc4dg4sIoXJvf7SQpHvJUDpSGNxEAouv4bXY HLzOc3txqzPodgPSyo99bEaFEMLRD2pdK7Rs+TBE2aivlEM05i+Viro0NACZeneWf4xI c25e5JbSjmVtUKn7zX55s7X8pX/sfrz6r2QWhL7NmCq5mkN31OGULHcX9hXnAg6I+MR0 oF3A== |
| MIME-Version | 1.0 |
| In-Reply-To | <af9tq3FnmkaU1@mid.dfncis.de> |
| References | <af9tq3FnmkaU1@mid.dfncis.de> |
| Date | Tue, 30 Oct 2012 23:28:43 +1100 |
| Subject | Re: exec with partial globals |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3086.1351600127.27098.python-list@python.org> (permalink) |
| Lines | 40 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1351600127 news.xs4all.nl 6949 [2001:888:2000:d::a6]:45347 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:32482 |
Show key headers only | View raw
On Tue, Oct 30, 2012 at 11:00 PM, Helmut Jarausch
<jarausch@igpm.rwth-aachen.de> wrote:
> Hi,
>
> I'd like to give the user the ability to enter code which may only rebind
> a given set of names but not all ones.
>
> How can 'filter' the gobal namespace such that modifying 'A' is allowed
> but any attempt to modify 'B' should give an exception.
I don't know of any way to do that _as such_, but you can simply
follow up the exec with direct retrieval from the globals.
>>> a=1; b=2
>>> code=compile("a=7",'','exec')
>>> ns={'a':0}
>>> exec(code,ns)
>>> a=ns["a"]
(Incidentally, it's normal to use lower case for most names, reserving
the leading uppercase letter for types/classes.)
The exec'd code gets its own namespace (defined by the dictionary,
same as you were doing - note that the 'a' inside that namespace has
nothing to do with the 'a' outside it), and then you explicitly fetch
the values you want.
A slightly more sophisticated example might include a list of shared
variables, for example:
shared = ['a']
outer = globals()
ns = {v:outer[v] for v in shared}
exec(code,ns);
for v in shared: outer[v]=ns[v]
Untested but should work. (Is there any way to directly apply filter()
to a dictionary? That's what I'm really looking for here.)
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
exec with partial globals Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2012-10-30 12:00 +0000
Re: exec with partial globals Chris Angelico <rosuav@gmail.com> - 2012-10-30 23:28 +1100
Re: exec with partial globals Dave Angel <d@davea.name> - 2012-10-30 08:33 -0400
Re: exec with partial globals Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2012-10-30 12:57 +0000
Re: exec with partial globals Chris Angelico <rosuav@gmail.com> - 2012-10-31 00:18 +1100
Re: exec with partial globals Dave Angel <d@davea.name> - 2012-10-30 09:39 -0400
csiph-web