Index: Lib/distutils/core.py =================================================================== --- Lib/distutils/core.py (revision 62148) +++ Lib/distutils/core.py (working copy) @@ -210,6 +210,7 @@ _setup_stop_after = stop_after save_argv = sys.argv + save_dir = os.getcwd() g = {'__file__': script_name} l = {} try: @@ -219,6 +220,7 @@ sys.argv[1:] = script_args execfile(script_name, g, l) finally: + os.chdir(save_dir) sys.argv = save_argv _setup_stop_after = None except SystemExit: Index: Lib/distutils/tests/test_core.py =================================================================== --- Lib/distutils/tests/test_core.py (revision 62148) +++ Lib/distutils/tests/test_core.py (working copy) @@ -27,7 +27,16 @@ setup() """ +setup_changing_cwd = """\ +from distutils.core import setup +import os +os.chdir(os.pardir) # Not so great; assumes we're not at the top dir. + +setup() +""" + + class CoreTestCase(unittest.TestCase): def setUp(self): @@ -73,7 +82,14 @@ output = output[:-1] self.assertEqual(cwd, output) + def test_run_setup_saves_and_restores_cwd(self): + starting_dir = os.getcwd() + distutils.core.run_setup( + self.write_setup(setup_changing_cwd)) + ending_dir = os.getcwd() + self.assertEqual(starting_dir, ending_dir) + def test_suite(): return unittest.makeSuite(CoreTestCase)