From 33a0a06d318a3d7164f6269c209b2309781af767 Mon Sep 17 00:00:00 2001 From: Sjoerd Mullender Date: Tue, 16 Jan 2007 16:42:38 +0000 Subject: Fixed ntpath.expandvars to not replace references to non-existing variables with nothing. Also added tests. This fixes bug #494589. --- Lib/ntpath.py | 8 ++++++-- Lib/test/test_ntpath.py | 22 ++++++++++++++++++++++ Lib/test/test_posixpath.py | 2 ++ Misc/NEWS | 2 ++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Lib/ntpath.py b/Lib/ntpath.py index b32ec16..23d5127 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -344,8 +344,10 @@ def expandvars(path): var = path[:index] if var in os.environ: res = res + os.environ[var] + else: + res = res + '${' + var + '}' except ValueError: - res = res + path + res = res + '${' + path index = pathlen - 1 else: var = '' @@ -357,8 +359,10 @@ def expandvars(path): c = path[index:index + 1] if var in os.environ: res = res + os.environ[var] + else: + res = res + '$' + var if c != '': - res = res + c + index = index - 1 else: res = res + c index = index + 1 diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 139aa1f..6bc2a05 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -115,6 +115,28 @@ tester("ntpath.normpath('K:../.././..')", r'K:..\..\..') tester("ntpath.normpath('C:////a/b')", r'C:\a\b') tester("ntpath.normpath('//machine/share//a/b')", r'\\machine\share\a\b') +oldenv = os.environ.copy() +try: + os.environ.clear() + os.environ["foo"] = "bar" + os.environ["{foo"] = "baz1" + os.environ["{foo}"] = "baz2" + tester('ntpath.expandvars("foo")', "foo") + tester('ntpath.expandvars("$foo bar")', "bar bar") + tester('ntpath.expandvars("${foo}bar")', "barbar") + tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar") + tester('ntpath.expandvars("$bar bar")', "$bar bar") + tester('ntpath.expandvars("$?bar")', "$?bar") + tester('ntpath.expandvars("${foo}bar")', "barbar") + tester('ntpath.expandvars("$foo}bar")', "bar}bar") + tester('ntpath.expandvars("${foo")', "${foo") + tester('ntpath.expandvars("${{foo}}")', "baz1}") + tester('ntpath.expandvars("$foo$foo")', "barbar") + tester('ntpath.expandvars("$bar$bar")', "$bar$bar") +finally: + os.environ.clear() + os.environ.update(oldenv) + # ntpath.abspath() can only be used on a system with the "nt" module # (reasonably), so we protect this test with "import nt". This allows # the rest of the tests for the ntpath module to be run to completion diff --git a/Lib/test/test_posixpath.py b/Lib/test/test_posixpath.py index 3984157..20a1fc5 100644 --- a/Lib/test/test_posixpath.py +++ b/Lib/test/test_posixpath.py @@ -374,6 +374,8 @@ class PosixPathTest(unittest.TestCase): self.assertEqual(posixpath.expandvars("$foo}bar"), "bar}bar") self.assertEqual(posixpath.expandvars("${foo"), "${foo") self.assertEqual(posixpath.expandvars("${{foo}}"), "baz1}") + self.assertEqual(posixpath.expandvars("$foo$foo"), "barbar") + self.assertEqual(posixpath.expandvars("$bar$bar"), "$bar$bar") finally: os.environ.clear() os.environ.update(oldenv) diff --git a/Misc/NEWS b/Misc/NEWS index b889b09..3d742f5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -109,6 +109,8 @@ Core and builtins Library ------- +- Bug #494589: make ntpath.expandvars behave according to its docstring. + - Changed platform module API python_version_tuple() to actually return a tuple (it used to return a list) -- cgit v0.12