Index: Lib/os.py =================================================================== --- Lib/os.py (revision 74553) +++ Lib/os.py (working copy) @@ -668,9 +668,11 @@ msg = "os.popen2 is deprecated. Use the subprocess module." warnings.warn(msg, DeprecationWarning, stacklevel=2) + use_shell = isinstance(cmd, basestring) + import subprocess PIPE = subprocess.PIPE - p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, + p = subprocess.Popen(cmd, shell=use_shell, bufsize=bufsize, stdin=PIPE, stdout=PIPE, close_fds=True) return p.stdin, p.stdout __all__.append("popen2") @@ -687,9 +689,11 @@ msg = "os.popen3 is deprecated. Use the subprocess module." warnings.warn(msg, DeprecationWarning, stacklevel=2) + use_shell = isinstance(cmd, basestring) + import subprocess PIPE = subprocess.PIPE - p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, + p = subprocess.Popen(cmd, shell=use_shell, bufsize=bufsize, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) return p.stdin, p.stdout, p.stderr @@ -707,9 +711,11 @@ msg = "os.popen4 is deprecated. Use the subprocess module." warnings.warn(msg, DeprecationWarning, stacklevel=2) + use_shell = isinstance(cmd, basestring) + import subprocess PIPE = subprocess.PIPE - p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, + p = subprocess.Popen(cmd, shell=use_shell, bufsize=bufsize, stdin=PIPE, stdout=PIPE, stderr=subprocess.STDOUT, close_fds=True) return p.stdin, p.stdout Index: Lib/test/test_popen2.py =================================================================== --- Lib/test/test_popen2.py (revision 74553) +++ Lib/test/test_popen2.py (working copy) @@ -78,6 +78,16 @@ def test_os_popen2(self): # same test as test_popen2(), but using the os.popen*() API + if os.name == 'posix': + w, r = os.popen2([self.cmd]) + self.validate_output(self.teststr, self.expected, r, w) + + # Make sure to test the list form of a command with an argument, + # too. + w, r = os.popen2(["echo", self.teststr]) + got = r.read() + self.assertEquals(got, self.teststr + "\n") + w, r = os.popen2(self.cmd) self.validate_output(self.teststr, self.expected, r, w) @@ -87,10 +97,32 @@ w, r, e = os.popen3([self.cmd]) self.validate_output(self.teststr, self.expected, r, w, e) + # Make sure to test the list form of a command with an argument, + # too. + w, r, e = os.popen3(["echo", self.teststr]) + got = r.read() + self.assertEquals(got, self.teststr + "\n") + got = e.read() + self.assertFalse(got, "unexpected %r on stderr" % got) + w, r, e = os.popen3(self.cmd) self.validate_output(self.teststr, self.expected, r, w, e) + def test_os_popen4(self): + if os.name == 'posix': + w, r = os.popen4([self.cmd]) + self.validate_output(self.teststr, self.expected, r, w) + # Make sure to test the list form of a command with an argument, + # too. + w, r = os.popen4(["echo", self.teststr]) + got = r.read() + self.assertEquals(got, self.teststr + "\n") + + w, r = os.popen4(self.cmd) + self.validate_output(self.teststr, self.expected, r, w) + + def test_main(): run_unittest(Popen2Test)