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: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'initialize': 0.05; 'modify': 0.05; "'a'": 0.07; 'exception.': 0.07; 'globals': 0.09; 'namespace': 0.09; 'cc:addr:python-list': 0.10; "'b'": 0.16; 'gobal': 0.16; 'wrote:': 0.17; 'variables': 0.17; 'code,': 0.18; 'trying': 0.21; 'import': 0.21; 'back.': 0.22; 'modifying': 0.22; 'ones.': 0.22; 'skip:_ 20': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'clever': 0.29; 'loop,': 0.29; 'prints': 0.29; 'code': 0.31; 'you?': 0.32; 'file': 0.32; 'could': 0.32; 'hi,': 0.33; 'another': 0.33; 'thanks': 0.34; 'really': 0.36; 'ability': 0.36; 'but': 0.36; 'subject:with': 0.36; 'should': 0.36; 'one,': 0.37; 'does': 0.37; 'subject:: ': 0.38; 'copying': 0.38; 'some': 0.38; 'things': 0.38; 'delete': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'more': 0.63; 'dangerous': 0.66; 'header:Reply-To:1': 0.68; 'further,': 0.71; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'do:': 0.91; 'try.': 0.91 Date: Tue, 30 Oct 2012 08:33:38 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1 MIME-Version: 1.0 To: Helmut Jarausch Subject: Re: exec with partial globals References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:DBmUS2yXnUSZJDVJv/GZ22wwmtigJA55DEl9EP9glgH 5DPjWTQ/P+71YEPOgWO9IRI9oYw0wsMZ7B3UgziN1pvb92PQOU hQ+XsB9Nwas+SFZpp8DE8+2ULHlyQFWGfQH19lmQQqhqNrJaND U0X7UbEyI1WlCvTo4aL7dz2q9RUQkLrFSraogtGzBSVLVNrX57 eE5NfyEftQivcwDi/E44cHb8/ZeakbUvWErBbp4aOWe9QZ/Z9c Irp+limwCbA867pRo2q0Nh9+T9H+9PEj1xqsIV1Jt++Di6kGbH 4zcm20R2hqEltlP8yH80HcCClbM2jxwbN3ogzjC194H9gsGqQ= = Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351600438 news.xs4all.nl 6965 [2001:888:2000:d::a6]:48123 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32483 On 10/30/2012 08:00 AM, Helmut Jarausch 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. > This does NOT work > A=1 > B=2 > Code=compile('A=7','','exec') > exec(Code,{'A':0}) > print("I've got A={}".format(A)) # prints 1 > > > How can 'filter' the gobal namespace such that modifying 'A' is allowed > but any attempt to modify 'B' should give an exception. > > > Many thanks for a hint, > Helmut. A=1 B=2 Code=compile('A=7','','exec') vars = {'A':A} exec(Code, vars) A = vars["A"] print("I've got A={}".format(A)) # prints 1 That now prints "I've got A=7" More generally, you could write a loop, copying globals into vars, and another one, copying them back. No idea what you're really after; this is one of the more dangerous things to try. Although you can constrain the globals seen by the code, that code can still use builtins, do imports, delete files, etc. Further, if your user is clever enough, he can do: Code=compile('A=7; print("howdy"); import __main__; __main__.B=42','','exec') What are you really trying to permit him to do? Initialize some variables for you? How about an .ini file ? -- DaveA