This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: ntpath.expandvars doesn't expand Windows-style variables.
Type: enhancement Stage: test needed
Components: Windows Versions: Python 2.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: aleax, amaury.forgeotdarc, jemfinch, johahn
Priority: low Keywords: easy

Created on 2003-10-15 20:32 by jemfinch, last changed 2022-04-10 16:11 by admin. This issue is now closed.

Messages (4)
msg18642 - (view) Author: Jeremy Fincher (jemfinch) Date: 2003-10-15 20:32
ntpath.expandvars is exactly the same as
posixpath.expandvars -- it only expands $vars.  But in
windows, environment variables are represented by
%vars%, not $vars.

I can write a patch if necessary.  Having read the
code, I noticed that it went to great lengths to do
what could quite easily be done with a regular
expression, so I assume using regular expressions isn't
kosher in the *path modules?
msg18643 - (view) Author: Johan M. Hahn (johahn) Date: 2003-10-20 18:26
Logged In: YES 

Hi Jeremy

   The purpose of path.expandvars is to retrieve environment 
variables in a platform independant way into a user supplied 
string, as in path.expandvars('$PYTHON/Lib/site-packages'). 
Using %vars% on windows and $vars on unix would make the 
method pointless. So this is no bug, though it might be 
explained a little vague in the docstring (IMHO the overall 
purpose and the phrase "left unchanged" are both unclear):

>>> help(path.expandvars)

Help on function expandvars in module ntpath:


    Expand shell variables of form $var and ${var}.

    Unknown variables are left unchanged.

   Though, the module docs offer a better explanation:

"Return the argument with environment variables expanded. 
Substrings of the form "$name" or "${name}" are replaced by 
the value of environment variable name. Malformed variable 
names and references to non-existing variables are left 
unchanged. On the Macintosh, this always returns path 

   Still, what does "unchanged" mean? On my windows system 
non-existing variables are IGNORED, they are not left 
unchanged anywhere:

>>> path.expandvars('$MADEUP/Lib/site-packages')


   Is it me or is that a bug? :)

msg18644 - (view) Author: Alex Martelli (aleax) * (Python committer) Date: 2003-11-02 17:56
Logged In: YES 

it seems to me that the behavior of os.path.expandvars is clearly documented (both in the docstrings and the library reference) and therefore i marked this as a feature request with slightly lower priority (features are unlikely to be added within 2.3 any more -- you may want to target 2.4 for this, therefore).

msg90178 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-07-06 13:03
This is already part of 2.6:

>>> os.path.expandvars("%WIndir%/foo")
>>> os.path.expandvars("%invalid%")
Date User Action Args
2022-04-10 16:11:45adminsetgithub: 39413
2009-07-06 13:03:06amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg90178

resolution: out of date
2009-04-22 17:21:06ajaksu2setkeywords: + easy
2009-02-13 04:33:46ajaksu2setstage: test needed
versions: + Python 2.7
2003-10-15 20:32:55jemfinchcreate