Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.05; 'purpose.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'thread': 0.14; "'q'": 0.16; '4:35': 0.16; 'kasper': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scope,': 0.16; 'scope.': 0.16; 'variables,': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'stack': 0.19; 'user.': 0.19; 'settings': 0.22; '>>>': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'of.': 0.24; 'received:comcast.net': 0.24; 'refers': 0.24; 'decide': 0.24; 'earlier': 0.24; 'sort': 0.25; 'this:': 0.26; 'pass': 0.26; 'certain': 0.27; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'along': 0.30; 'easier': 0.31; 'directly,': 0.31; 'forces': 0.31; 'values.': 0.31; 'skip:c 30': 0.32; 'url:python': 0.33; 'call.': 0.33; 'could': 0.34; 'problem': 0.35; 'created': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'there': 0.35; 'object,': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'behind': 0.37; 'two': 0.37; 'being': 0.38; 'requiring': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'track': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'act': 0.63; 'different': 0.65; 'here': 0.66; 'side': 0.67; 'respect': 0.70; 'around,': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: PyEval_GetLocals and unreferenced variables Date: Tue, 02 Dec 2014 06:04:20 -0500 References: <20141202093510.4af9791e@tigger> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <20141202093510.4af9791e@tigger> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417518273 news.xs4all.nl 2904 [2001:888:2000:d::a6]:47202 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:81799 On 12/2/14 4:35 AM, Kasper Peeters wrote: >>> def fun(): >>> cfun_that_creates_q_in_local_scope() >>> def fun2(): >>> cfun_that_wants_to_see_if_q_is_available() >>> >>> So the Python side actually doesn't see 'q' directly at all. >> >> I think you will need to elaborate. > > Ok, here goes (and thanks for listening). > > The behaviour of the C side is determined by certain > settings/preferences. I want these settings to respect the Python > scope. I could in principle decide to make these settings a proper > Python object, and ask the user to create one and pass it along at > every C-function call. Something like > > def fun(): > settings = Cpp_Options() > settings.set_some_flag() > cfun(settings, ...) > def fun2(): > settings = Cpp_Options(settings) > settings.set_some_other_flag() > cfun(settings, ...) > > Then Python would automatically take care of the scope of 'settings'. > > However, this is difficult for the user to keep track of. So my > idea was to allow for > > def fun(): > set_some_flag() > cfun(...) > def fun2(): > set_some_other_flag() > cfun(...) > > I let the C side create a Cpp_Options object on the locals stack > behind the scenes, and the 'cfun()' function takes it from there > directly, without requiring the user to pass it. Much easier for the > user. > > This works, but the problem is that the C side does not see the > settings that were created in fun() when it gets called from fun2(). In > fun2(), the locals do not contain objects constructed earlier in fun(), > unless the Python side explicitly refers to them. So adding a line > > settings.do_something() > > inside fun2() would work and forces Python to pull the settings object > created in fun() into scope, but that sort of defeats the purpose. > I would use thread locals for this: https://docs.python.org/2/library/threading.html#threading.local They act like global variables, in that they are available implicitly without being passed around, but like locals in that two separate threads will have different values. -- Ned Batchelder, http://nedbatchelder.com