delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2008/11/13/10:21:43

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
Date: Thu, 13 Nov 2008 10:20:50 -0500
From: Christopher Faylor <cgf-use-the-mailinglist-please AT cygwin DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: Python: exec in a function can't create shadows of variables if these are declared "global" in another function of the same module
Message-ID: <20081113152050.GB24198@ednor.casa.cgf.cx>
Reply-To: cygwin AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
References: <20081113145017 DOT GA32353 AT smaug DOT linux DOT pwf DOT cam DOT ac DOT uk>
MIME-Version: 1.0
In-Reply-To: <20081113145017.GA32353@smaug.linux.pwf.cam.ac.uk>
User-Agent: Mutt/1.5.16 (2007-06-09)
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

On Thu, Nov 13, 2008 at 02:50:17PM +0000, Silas S. Brown wrote:
>Consider this Python code:
>
>setting1 = "val1"
>setting2 = "val2"
>
>def dummy():
>    global setting1
>
>def f(x):
>    exec(x)
>    return setting1, setting2
>
>print f("setting1='new' ; setting2='new'")
>
>Expected result: ('new', 'new')
>
>Actual result: ('val1', 'new')
>
>The presence of "global setting1" in a totally
>different function somehow stops a shadowed
>setting1 from being created by the exec.
>
>This can be worked around by adding a real
>assignment before the exec, i.e.:
>
>def f(x):
>    setting1 = 0
>    exec(x)
>    return setting1, setting2
>
>Observed in:
>Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) on Cygwin
>
>Not observed in:
>Python 2.5.1 (r251:54863, Aug  1 2008, 00:32:16) on SUSE Linux

Observed on:

Python 2.5.2 on 2.6.26-gentoo-r1 (i.e., linux)

So this isn't just a Cygwin-specific behavior.  It probably would be best
to take this to a python mailing list.

I agree that it does seem like strange behavior, though.

cgf

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019