[Python-bugs-list] ntpath.expandvars doesn't handle case properly (PR#162)

dwr2@ix.netcom.com dwr2@ix.netcom.com
Sun, 19 Dec 1999 08:38:24 -0500 (EST)


Full_Name: Donald Wallace Rouse II
Version: 1.5
OS: Windows 95
Submission from: roc-ny6-102.ix.netcom.com (198.211.253.102)


When accessing os.environ under Windows 95/NT/etc., lower-case environment
variable names are automatically converted to upper-case before use.
However, ntpath.expandvars does not accept lower-case variable names.
This is inconsistent.
This also happens in dospath.expandvars and may happen in os2path.expandvars.

Here is a Python session demonstrating the problem:

>>> import os
>>> os.name
'nt'
>>> os.environ['abc']='xyz'
>>> os.environ['abc']
'xyz'
>>> os.path.expandvars('$abc')
''
>>> os.path.expandvars('$ABC')
'xyz'
>>> 

Note that you can access os.environ using a lower-case name, but you must use
upper-case for os.path.expandvars (-->ntpath.expandvars).
This can easily be fixed by changing "os.environ.has_key(var)" to
"os.environ.has_key(string.upper(key))" in two places in each of
ntpath.expandvars, dospath.expandvars, and possibly os2path.expandvars.
Another solution would be to add the following function to os._Environ, in the
"if name in ('os2', 'nt', 'dos')" block:
    def has_key(self, key): return UserDict.UserDict.has_key(self,
string.upper(key))
The latter solution seems better to me, but I don't know the possible negative
ramifications of making such a change to a class used in so many places.
(On the other had, it is consistent with the definitions of _Environ.__getitem__
and _Environ.__setitem__.)

Further argument that lower-case should be accepted in ntpath.expandvars:
A DOS window opened in Windows 95 expands a lower-case variable name, despite
the actual variable name being stored in upper-case:
C:\WINDOWS>set abc=xyz
C:\WINDOWS>set
[...(most of variable list elided)...]
ABC=xyz
C:\WINDOWS>echo %abc%
xyz