diff -r 3fb84c1da8c5 Lib/distutils/tests/test_util.py --- a/Lib/distutils/tests/test_util.py Sat Oct 27 03:48:40 2012 -0700 +++ b/Lib/distutils/tests/test_util.py Sun Oct 28 23:32:55 2012 -0400 @@ -1,10 +1,12 @@ """Tests for distutils.util.""" +import os import sys import unittest from test.test_support import run_unittest from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError -from distutils.util import byte_compile +from distutils.util import byte_compile, change_root + class UtilTestCase(unittest.TestCase): @@ -18,6 +20,34 @@ finally: sys.dont_write_bytecode = old_dont_write_bytecode + + def test_linux_change_root(self): + orig_name = os.environ + self.addCleanup(setattr, os, 'name', orig_name) + os.name = 'posix' + + new_root = "/" + pathname = "/path/to/install" + expected_pathname = "/path/to/install" + path = change_root(new_root, pathname) + self.assertEqual(path, expected_pathname) + + new_root = "/home" + expected_pathname = "/home/path/to/install" + path = change_root(new_root, pathname) + self.assertEqual(path, expected_pathname) + + pathname = "path/to/install" + new_root = "/" + expected_pathname = "/path/to/install" + path = change_root(new_root, pathname) + self.assertEqual(path, expected_pathname) + + new_root = "/home" + expected_pathname = "/home/path/to/install" + path = change_root(new_root, pathname) + self.assertEqual(path, expected_pathname) + def test_suite(): return unittest.makeSuite(UtilTestCase) diff -r 3fb84c1da8c5 Lib/distutils/util.py --- a/Lib/distutils/util.py Sat Oct 27 03:48:40 2012 -0700 +++ b/Lib/distutils/util.py Sun Oct 28 23:32:55 2012 -0400 @@ -219,26 +219,11 @@ """Return 'pathname' with 'new_root' prepended. If 'pathname' is relative, this is equivalent to "os.path.join(new_root,pathname)". Otherwise, it requires making 'pathname' relative and then joining the - two, which is tricky on DOS/Windows and Mac OS. + two. """ - if os.name == 'posix': - if not os.path.isabs(pathname): - return os.path.join(new_root, pathname) - else: - return os.path.join(new_root, pathname[1:]) - - elif os.name == 'nt': + if os.name in ['posix', 'nt', 'os2']: (drive, path) = os.path.splitdrive(pathname) - if path[0] == '\\': - path = path[1:] - return os.path.join(new_root, path) - - elif os.name == 'os2': - (drive, path) = os.path.splitdrive(pathname) - if path[0] == os.sep: - path = path[1:] - return os.path.join(new_root, path) - + return os.path.join(new_root, path.lstrip(os.sep)) else: raise DistutilsPlatformError, \ "nothing known about platform '%s'" % os.name