[Python-checkins] r83957 - in python/branches/release27-maint/Lib: subprocess.py test/test_subprocess.py

tim.golden python-checkins at python.org
Thu Aug 12 13:00:35 CEST 2010


Author: tim.golden
Date: Thu Aug 12 13:00:35 2010
New Revision: 83957

Log:
#2304: fix incorporating Eric Smith's .format suggestion and tested on Ubuntu as well as Windows

Modified:
   python/branches/release27-maint/Lib/subprocess.py
   python/branches/release27-maint/Lib/test/test_subprocess.py

Modified: python/branches/release27-maint/Lib/subprocess.py
==============================================================================
--- python/branches/release27-maint/Lib/subprocess.py	(original)
+++ python/branches/release27-maint/Lib/subprocess.py	Thu Aug 12 13:00:35 2010
@@ -853,8 +853,8 @@
                 startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
                 startupinfo.wShowWindow = _subprocess.SW_HIDE
                 comspec = os.environ.get("COMSPEC", "cmd.exe")
-                args = comspec + " /c " + args
-                if (_subprocess.GetVersion() >= 0x80000000L or
+                args = '{} /c "{}"'.format (comspec, args)
+                if (_subprocess.GetVersion() >= 0x80000000 or
                         os.path.basename(comspec).lower() == "command.com"):
                     # Win9x, or using command.com on NT. We need to
                     # use the w9xpopen intermediate program. For more

Modified: python/branches/release27-maint/Lib/test/test_subprocess.py
==============================================================================
--- python/branches/release27-maint/Lib/test/test_subprocess.py	(original)
+++ python/branches/release27-maint/Lib/test/test_subprocess.py	Thu Aug 12 13:00:35 2010
@@ -872,12 +872,54 @@
         self.assertEqual([(256, 999), (666,), (666,)], record_calls)
 
 
+ at unittest.skipUnless(mswindows, "mswindows only")
+class CommandsWithSpaces (BaseTestCase):
+
+    def setUp(self):
+        super(CommandsWithSpaces, self).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(CommandsWithSpaces, self).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"])
+
 def test_main():
     unit_tests = (ProcessTestCase,
                   POSIXProcessTestCase,
                   Win32ProcessTestCase,
                   ProcessTestCaseNoPoll,
-                  HelperFunctionTests)
+                  HelperFunctionTests,
+                  CommandsWithSpaces)
 
     test_support.run_unittest(*unit_tests)
     test_support.reap_children()


More information about the Python-checkins mailing list