diff --git a/Doc/library/subprocess.rst b/Doc/library/subprocess.rst --- a/Doc/library/subprocess.rst +++ b/Doc/library/subprocess.rst @@ -480,10 +480,9 @@ .. versionadded:: 3.2 The *pass_fds* parameter was added. - If *cwd* is not ``None``, the child's current directory will be changed to *cwd* - before it is executed. Note that this directory is not considered when - searching the executable, so you can't specify the program's path relative to - *cwd*. + If *cwd* is not ``None``, the function changes the working directory to + *cwd* before executing the child. The function also looks for *executable* + (or for the first item in *args*) relative to *cwd*. If *restore_signals* is True (the default) all signals that Python has set to SIG_IGN are restored to SIG_DFL in the child process before the exec. diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -191,28 +191,92 @@ p.wait() self.assertEqual(p.stderr, None) + def test_cwd(self): + # Check that cwd changes the cwd for the child process. + tmpdir = tempfile.gettempdir() + # We cannot use os.path.realpath to canonicalize the path, + # since it doesn't expand Tru64 {memb} strings. See bug 1063571. + cwd = os.getcwd() + os.chdir(tmpdir) + tmpdir = os.getcwd() + os.chdir(cwd) + p = subprocess.Popen([sys.executable, "-c", + 'import sys,os;' + 'sys.stdout.write(os.getcwd())'], + stdout=subprocess.PIPE, + cwd=tmpdir) + self.addCleanup(p.stdout.close) + normcase = os.path.normcase + self.assertEqual(normcase(p.stdout.read().decode("utf-8")), + normcase(tmpdir)) + @unittest.skipIf(sys.base_prefix != sys.prefix, 'Test is not venv-compatible') - def test_executable_with_cwd(self): - python_dir = os.path.dirname(os.path.realpath(sys.executable)) - p = subprocess.Popen(["somethingyoudonthave", "-c", - "import sys; sys.exit(47)"], - executable=sys.executable, cwd=python_dir) + def test_cwd_wrong(self): + # Check that Popen looks for args[0] relative to cwd (failing case). + python_path = os.path.realpath(sys.executable) + python_dir, python_base = os.path.split(python_path) + wrong_cwd = os.path.join(python_dir, 'Doc') + self.assertRaises(FileNotFoundError, subprocess.Popen, + [python_base], cwd=wrong_cwd) + + def _call_popen_and_assert(self, python_arg, **kwargs): + # Assert that invoking Python via Popen succeeds. + p = subprocess.Popen([python_arg, "-c", "import sys; sys.exit(47)"], + **kwargs) p.wait() self.assertEqual(p.returncode, 47) @unittest.skipIf(sys.base_prefix != sys.prefix, 'Test is not venv-compatible') + def test_cwd_with_relative_arg(self): + # Check that Popen looks for args[0] relative to cwd (succeeding case). + python_path = os.path.realpath(sys.executable) + python_dir, python_base = os.path.split(python_path) + wrong_cwd = os.path.join(python_dir, 'Doc') + relative_python = os.path.join(os.pardir, python_base) + self._call_popen_and_assert(python_arg=relative_python, cwd=wrong_cwd) + + @unittest.skipIf(sys.base_prefix != sys.prefix, + 'Test is not venv-compatible') + def test_cwd_with_relative_executable(self): + # Check that Popen looks for executable relative to cwd (and that + # executable takes precedence over args[0]). + python_path = os.path.realpath(sys.executable) + python_dir, python_base = os.path.split(python_path) + wrong_cwd = os.path.join(python_dir, 'Doc') + relative_python = os.path.join(os.pardir, python_base) + self._call_popen_and_assert(python_arg="somethingyoudonthave", + executable=relative_python, cwd=wrong_cwd) + + @unittest.skipIf(sys.base_prefix != sys.prefix, + 'Test is not venv-compatible') + def test_cwd_with_absolute_executable(self): + # Check that an absolute path for executable works when passing cwd + # (and that executable takes precedence over args[0]). + python_dir = os.path.dirname(os.path.realpath(sys.executable)) + self._call_popen_and_assert(python_arg="somethingyoudonthave", + executable=sys.executable, cwd=python_dir) + + @unittest.skipIf(sys.base_prefix != sys.prefix, + 'Test is not venv-compatible') + def test_cwd_with_absolute_executable_and_wrong_cwd(self): + # Check that an absolute path for executable works when passing a + # "wrong" cwd (and that executable takes precedence over args[0]). + python_dir = os.path.dirname(os.path.realpath(sys.executable)) + wrong_cwd = os.path.join(python_dir, 'Doc') + self._call_popen_and_assert(python_arg="somethingyoudonthave", + executable=sys.executable, cwd=wrong_cwd) + + @unittest.skipIf(sys.base_prefix != sys.prefix, + 'Test is not venv-compatible') @unittest.skipIf(sysconfig.is_python_build(), "need an installed Python. See #7774") def test_executable_without_cwd(self): # For a normal installation, it should work without 'cwd' # argument. For test runs in the build directory, see #7774. - p = subprocess.Popen(["somethingyoudonthave", "-c", - "import sys; sys.exit(47)"], - executable=sys.executable) - p.wait() - self.assertEqual(p.returncode, 47) + self._call_popen_and_assert(python_arg="somethingyoudonthave", + executable=sys.executable) def test_stdin_pipe(self): # stdin redirection @@ -369,24 +433,6 @@ p.wait() self.assertEqual(p.stdin, None) - def test_cwd(self): - tmpdir = tempfile.gettempdir() - # We cannot use os.path.realpath to canonicalize the path, - # since it doesn't expand Tru64 {memb} strings. See bug 1063571. - cwd = os.getcwd() - os.chdir(tmpdir) - tmpdir = os.getcwd() - os.chdir(cwd) - p = subprocess.Popen([sys.executable, "-c", - 'import sys,os;' - 'sys.stdout.write(os.getcwd())'], - stdout=subprocess.PIPE, - cwd=tmpdir) - self.addCleanup(p.stdout.close) - normcase = os.path.normcase - self.assertEqual(normcase(p.stdout.read().decode("utf-8")), - normcase(tmpdir)) - def test_env(self): newenv = os.environ.copy() newenv["FRUIT"] = "orange"