diff options
author | Tim Golden <mail@timgolden.me.uk> | 2010-08-08 16:17:48 (GMT) |
---|---|---|
committer | Tim Golden <mail@timgolden.me.uk> | 2010-08-08 16:17:48 (GMT) |
commit | e3f76168a96c939564e03064f4f9f43e96a3bacb (patch) | |
tree | e26f4e34ee8ae0136a97dfdf311f49191c60ee17 | |
parent | 419e384601c6af03feddace9c0254e7f3920b141 (diff) | |
download | cpython-e3f76168a96c939564e03064f4f9f43e96a3bacb.zip cpython-e3f76168a96c939564e03064f4f9f43e96a3bacb.tar.gz cpython-e3f76168a96c939564e03064f4f9f43e96a3bacb.tar.bz2 |
Issue #2304: Add additional quotes when using cmd shell on Windows. Original patch from Gabriel Genellina
-rw-r--r-- | Lib/subprocess.py | 2 | ||||
-rw-r--r-- | Lib/test/test_subprocess.py | 42 |
2 files changed, 43 insertions, 1 deletions
diff --git a/Lib/subprocess.py b/Lib/subprocess.py index ae033d4..760b2a9 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -853,7 +853,7 @@ class Popen(object): startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW startupinfo.wShowWindow = _subprocess.SW_HIDE comspec = os.environ.get("COMSPEC", "cmd.exe") - args = comspec + " /c " + args + args = comspec + " /c " + '"%s"' % args if (_subprocess.GetVersion() >= 0x80000000 or os.path.basename(comspec).lower() == "command.com"): # Win9x, or using command.com on NT. We need to diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py index 95da107..c4bef9e 100644 --- a/Lib/test/test_subprocess.py +++ b/Lib/test/test_subprocess.py @@ -1015,6 +1015,7 @@ class Win32ProcessTestCase(BaseTestCase): self._kill_process('terminate') + # The module says: # "NB This only works (and is only relevant) for UNIX." # @@ -1041,6 +1042,46 @@ class CommandTests(unittest.TestCase): if dir is not None: os.rmdir(dir) +class CommandsWithSpaces (BaseTestCase): + + def setUp(self): + super().setUp() + f, fname = mkstemp(".py", "te st") + self.fname = fname.lower () + os.write(f, b"import sys;" + b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))" + ) + os.close(f) + + def tearDown(self): + os.remove(self.fname) + super().tearDown() + + def with_spaces(self, *args, **kwargs): + kwargs['stdout'] = subprocess.PIPE + p = subprocess.Popen(*args, **kwargs) + self.assertEqual( + p.stdout.read ().decode("mbcs"), + "2 [%r, 'ab cd']" % self.fname + ) + + def test_shell_string_with_spaces(self): + # call() function with string argument with spaces on Windows + self.with_spaces('"%s" "%s"' % (self.fname, "ab cd"), shell=1) + + def test_shell_sequence_with_spaces(self): + # call() function with sequence argument with spaces on Windows + self.with_spaces([self.fname, "ab cd"], shell=1) + + def test_noshell_string_with_spaces(self): + # call() function with string argument with spaces on Windows + self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname, + "ab cd")) + + def test_noshell_sequence_with_spaces(self): + # call() function with sequence argument with spaces on Windows + self.with_spaces([sys.executable, self.fname, "ab cd"]) + @unittest.skipUnless(getattr(subprocess, '_has_poll', False), "poll system call not supported") @@ -1093,6 +1134,7 @@ def test_main(): Win32ProcessTestCase, ProcessTestCasePOSIXPurePython, CommandTests, + CommandsWithSpaces, ProcessTestCaseNoPoll, HelperFunctionTests) |