[Patches] [ python-Patches-768442 ] Warning 'assignment shadows builtin' __builtins__ fix
SourceForge.net
noreply@sourceforge.net
Wed, 16 Jul 2003 11:31:00 -0700
Patches item #768442, was opened at 2003-07-09 13:13
Message generated for change (Comment added) made by nascheme
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=768442&group_id=5470
Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Troels Therkelsen (troelst)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Warning 'assignment shadows builtin' __builtins__ fix
Initial Comment:
Python 2.3b2 (#1, Jun 30 2003, 13:04:39)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import sys, new
>>> d = {'__builtins__':{}}
>>> d['mod'] = new.module('mod')
>>> d['mod'].__builtins__ = d['__builtins__']
>>> exec "mod.reload = 42" in d
<string>:1: DeprecationWarning: assignment shadows
builtin
>>> d['sys'] = sys
>>> exec "print sys._getframe().f_builtins" in d
{}
Surely, the DeprecationWarning shouldn't be printed if
you use __builtins__ to explicitly define your own builtins
dict? I mean, the 'reload' name in this case is an
unknown name until it is defined in mod.
The submitted patch fixes Objects/moduleobject.c so
that the warning is only done if the module global you're
assigning to exists in f_builtins. The patch is somehwat
naive I admit, and it reuses as much as the old code as
possible. Alternatively, instead of all the work done, one
could just check directly in f_builtins.
----------------------------------------------------------------------
>Comment By: Neil Schemenauer (nascheme)
Date: 2003-07-16 18:31
Message:
Logged In: YES
user_id=35752
I think the attached patch does the trick. Please review.
I've changed the fuction that builds the set of "official"
builtin names to use PyThreadState_Get()->interp->builtins
rather than PyEval_GetBuiltins(). We don't want to end up
using some overridden builtins as the "official" builtin
names. Next, I've changed the module setattr to check if
PyThreadState_Get()->interp->builtins ==
PyEval_GetBuiltins(). If not then the builtins are
overridden and the shadow test is skipped.
Is using PyThreadState_Get()->interp->builtins okay? You
had previously suggested finding the builtin names using
sys.modules['__builtin__'].
----------------------------------------------------------------------
Comment By: Neil Schemenauer (nascheme)
Date: 2003-07-16 17:30
Message:
Logged In: YES
user_id=35752
No vacation for me. I'll take a crack at it.
----------------------------------------------------------------------
Comment By: Guido van Rossum (gvanrossum)
Date: 2003-07-16 16:10
Message:
Logged In: YES
user_id=6380
Not warning if __builtins__ is overridden sounds like a fine
compromise to me. I believe Neal is on vacation -- who's
going to fix this?
----------------------------------------------------------------------
Comment By: Neil Schemenauer (nascheme)
Date: 2003-07-10 18:21
Message:
Logged In: YES
user_id=35752
I'm not sure what's the right thing to do here. One simple
(I think) solution would be to not warn if __builtins__ are
overridden. We don't want to implement anything too
complicated since the 2.3 is quickly approaching. Hopefully
Guido can give me a clue as to what kind of a solution he
would like to see.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=768442&group_id=5470