[Tutor] os.environ weirdness

Gregor Lingl glingl@aon.at
Tue Dec 10 07:41:07 2002


Poor Yorick schrieb:

> Another phenomenon I haven't made sense of:
>
> >>> for i in os.environ:
>    print i
>
>   Traceback (most recent call last):
>  File "<pyshell#13>", line 1, in ?
>    for i in os.environ:
>  File "C:\Python22\lib\os.py", line 387, in __getitem__
>    return self.data[key.upper()]
> AttributeError: 'int' object has no attribute 'upper'

For me this works (although it's not a subclassed dict, but
a subclassed IterableUserDict, which also implements __iter__(),
as a look at os.py shows.):

 >>> import os
 >>> for i in os.environ:
    print i,

   
TMP USERNAME COMPUTERNAME LOGONSERVER COMSPEC USERDOMAIN HOME TFLIBDIR 
TCL_LIBRARY COMMONPROGRAMFILES PROCESSOR_IDENTIFIER PROGRAMFILES 
PROCESSOR_REVISION PATHEXT SYSTEMROOT PATH APPDATA TEMP HOMEDRIVE 
SYSTEMDRIVE PROCESSOR_ARCHITECTURE NUMBER_OF_PROCESSORS ALLUSERSPROFILE 
PROCESSOR_LEVEL TK_LIBRARY HOMEPATH OS2LIBPATH USERPROFILE OS WINDIR
 >>>

Your errormessage shows, that there is a key in your os.environ, which 
is an 'int',
whereas it is assumed. that all keys are of type string. so they have a 
method upper.
Did you tinker around with os.eviron?

Regards, Gregor


>
> I realize that os.environ is not a builtin dictionary, but some sort 
> of subclassed dictionary, but these commands work:
>
> os.environ.keys()
> os.environ.items()
>
> doesn't the "in" statement just resolve to one of those functions?
>
> Regards,
>
> Poor Yorick
> gp@pooryorick.com
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>