Proposal: add sys to __builtins__

Rick Wotnaz desparn at wtf.com
Tue Sep 6 23:17:27 EDT 2005


"Michael J. Fromberger"
<Michael.J.Fromberger at Clothing.Dartmouth.EDU> wrote in
news:Michael.J.Fromberger-F58CC8.20585806092005 at localhost: 

> In article <Xns96C86FC4A4820reederz at 63.223.7.253>,
>  Rick Wotnaz <desparn at wtf.com> wrote:
> 
>> You're right that there is no necessity for such a change. I
>> was not actually talking about importing *any* module in every
>> case, but rather about importing, say, 'sys' when, for example,
>> sys.argv appeared in the code and no import had been specified.
> 
> I think I must have missed that post; I will go back and look at
> it.  However, while I'm here, how would your proposal deal with
> code like this:
> 
>    import foobar
> 
>    # ... some while later ...
>    def f( ... ):
>       ...
>       global foobar, sys
>       sys = foobar
>       ...
> 
>    # ... some while even later ...
>    f( ... )
>    sys.wallaby("Fear and loathing!")
> 
> In particular, we have no import of sys, but the name "sys" is 
> meaningful as a local alias for a different module.  I'm not
> saying you couldn't deal with this, but it rules out some of the
> more obvious ways of detecting and automatically handling this
> kind of substitution. 
> 
> Naturally, you might well ask, "why would you do such a fool
> thing?"  To this I can only respond:  "Never underestimate the
> ingenuity of fools." 
> 

I don't know that this would cause any particular problem with the 
[not exactly-]proposed method. The automagic lookup would not be 
triggered until a NameError occurred, which would not happen in this 
case. As you say, why would anyone -- at least anyone who wanted to 
rely on sys.xxx being automatically resolved -- do such a thing? Even 
under the current scheme, occluding 'sys' would prevent correct 
interpretation of sys.argv. An error is an error in either case.

Now, if 'wallaby' is not part of the foobar namespace, the automagic 
system would kick in an incorrectly import sys, and on retry would 
still not find 'wallaby' in the namespace. Much merriment would 
ensue, I'm sure. At that point, I'd want such a system to have a 
nervous breakdown and allow the debugging to begin. Whether I hand-
entered an import statement or not wouldn't change that.

-- 
rzed



More information about the Python-list mailing list