Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3f7616

Browse files
committed
Issue #2304: Add additional quotes when using cmd shell on Windows. Original patch from Gabriel Genellina
1 parent 419e384 commit e3f7616

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

Lib/subprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,
853853
startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
854854
startupinfo.wShowWindow = _subprocess.SW_HIDE
855855
comspec = os.environ.get("COMSPEC", "cmd.exe")
856-
args = comspec + " /c " + args
856+
args = comspec + " /c " + '"%s"' % args
857857
if (_subprocess.GetVersion() >= 0x80000000 or
858858
os.path.basename(comspec).lower() == "command.com"):
859859
# Win9x, or using command.com on NT. We need to

Lib/test/test_subprocess.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ def test_terminate(self):
10151015
self._kill_process('terminate')
10161016

10171017

1018+
10181019
# The module says:
10191020
# "NB This only works (and is only relevant) for UNIX."
10201021
#
@@ -1041,6 +1042,46 @@ def test_getoutput(self):
10411042
if dir is not None:
10421043
os.rmdir(dir)
10431044

1045+
class CommandsWithSpaces (BaseTestCase):
1046+
1047+
def setUp(self):
1048+
super().setUp()
1049+
f, fname = mkstemp(".py", "te st")
1050+
self.fname = fname.lower ()
1051+
os.write(f, b"import sys;"
1052+
b"sys.stdout.write('%d %s' % (len(sys.argv), [a.lower () for a in sys.argv]))"
1053+
)
1054+
os.close(f)
1055+
1056+
def tearDown(self):
1057+
os.remove(self.fname)
1058+
super().tearDown()
1059+
1060+
def with_spaces(self, *args, **kwargs):
1061+
kwargs['stdout'] = subprocess.PIPE
1062+
p = subprocess.Popen(*args, **kwargs)
1063+
self.assertEqual(
1064+
p.stdout.read ().decode("mbcs"),
1065+
"2 [%r, 'ab cd']" % self.fname
1066+
)
1067+
1068+
def test_shell_string_with_spaces(self):
1069+
# call() function with string argument with spaces on Windows
1070+
self.with_spaces('"%s" "%s"' % (self.fname, "ab cd"), shell=1)
1071+
1072+
def test_shell_sequence_with_spaces(self):
1073+
# call() function with sequence argument with spaces on Windows
1074+
self.with_spaces([self.fname, "ab cd"], shell=1)
1075+
1076+
def test_noshell_string_with_spaces(self):
1077+
# call() function with string argument with spaces on Windows
1078+
self.with_spaces('"%s" "%s" "%s"' % (sys.executable, self.fname,
1079+
"ab cd"))
1080+
1081+
def test_noshell_sequence_with_spaces(self):
1082+
# call() function with sequence argument with spaces on Windows
1083+
self.with_spaces([sys.executable, self.fname, "ab cd"])
1084+
10441085

10451086
@unittest.skipUnless(getattr(subprocess, '_has_poll', False),
10461087
"poll system call not supported")
@@ -1093,6 +1134,7 @@ def test_main():
10931134
Win32ProcessTestCase,
10941135
ProcessTestCasePOSIXPurePython,
10951136
CommandTests,
1137+
CommandsWithSpaces,
10961138
ProcessTestCaseNoPoll,
10971139
HelperFunctionTests)
10981140

0 commit comments

Comments
 (0)