Index: Lib/test/test_subprocess.py =================================================================== --- Lib/test/test_subprocess.py (revision 77358) +++ Lib/test/test_subprocess.py (working copy) @@ -26,7 +26,7 @@ def remove_stderr_debug_decorations(stderr): return re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr) -class ProcessTestCase(unittest.TestCase): +class _BaseProcessTestCase(unittest.TestCase): def setUp(self): # Try to minimize the number of children we have so this test # doesn't crash on some buildbots (Alphas in particular). @@ -46,10 +46,9 @@ else: fname = tempfile.mktemp() return os.open(fname, os.O_RDWR|os.O_CREAT), fname - - # - # Generic tests - # + + +class ProcessTestCase(_BaseProcessTestCase): def test_call_seq(self): # call() function with sequence argument rc = subprocess.call([sys.executable, "-c", @@ -338,19 +337,20 @@ # This test is Linux specific for simplicity to at least have # some coverage. It is not a platform specific bug. - if os.path.isdir('/proc/%d/fd' % os.getpid()): - # Test for the fd leak reported in http://bugs.python.org/issue2791. - def test_communicate_pipe_fd_leak(self): - fd_directory = '/proc/%d/fd' % os.getpid() - num_fds_before_popen = len(os.listdir(fd_directory)) - p = subprocess.Popen([sys.executable, '-c', 'print()'], - stdout=subprocess.PIPE) - p.communicate() - num_fds_after_communicate = len(os.listdir(fd_directory)) - del p - num_fds_after_destruction = len(os.listdir(fd_directory)) - self.assertEqual(num_fds_before_popen, num_fds_after_destruction) - self.assertEqual(num_fds_before_popen, num_fds_after_communicate) + @unittest.skipIf(not os.path.isdir('/proc/%d/fd' % os.getpid()), + "Linux specific") + # Test for the fd leak reported in http://bugs.python.org/issue2791. + def test_communicate_pipe_fd_leak(self): + fd_directory = '/proc/%d/fd' % os.getpid() + num_fds_before_popen = len(os.listdir(fd_directory)) + p = subprocess.Popen([sys.executable, '-c', 'print()'], + stdout=subprocess.PIPE) + p.communicate() + num_fds_after_communicate = len(os.listdir(fd_directory)) + del p + num_fds_after_destruction = len(os.listdir(fd_directory)) + self.assertEqual(num_fds_before_popen, num_fds_after_destruction) + self.assertEqual(num_fds_before_popen, num_fds_after_communicate) def test_communicate_returns(self): # communicate() should return None if no redirection is active @@ -536,239 +536,237 @@ if err.errno != 2: # ignore "no such file" raise - # - # POSIX tests - # - if not mswindows: - def test_exceptions(self): - # catched & re-raised exceptions - try: - p = subprocess.Popen([sys.executable, "-c", ""], - cwd="/this/path/does/not/exist") - except OSError, e: - # The attribute child_traceback should contain "os.chdir" - # somewhere. - self.assertNotEqual(e.child_traceback.find("os.chdir"), -1) - else: - self.fail("Expected OSError") - def _suppress_core_files(self): - """Try to prevent core files from being created. - Returns previous ulimit if successful, else None. - """ - try: - import resource - old_limit = resource.getrlimit(resource.RLIMIT_CORE) - resource.setrlimit(resource.RLIMIT_CORE, (0,0)) - return old_limit - except (ImportError, ValueError, resource.error): - return None +@unittest.skipIf(sys.platform == "win32", "POSIX specific tests") +class POSIXProcessTestCase(_BaseProcessTestCase): + def test_exceptions(self): + # catched & re-raised exceptions + try: + p = subprocess.Popen([sys.executable, "-c", ""], + cwd="/this/path/does/not/exist") + except OSError, e: + # The attribute child_traceback should contain "os.chdir" + # somewhere. + self.assertNotEqual(e.child_traceback.find("os.chdir"), -1) + else: + self.fail("Expected OSError") - def _unsuppress_core_files(self, old_limit): - """Return core file behavior to default.""" - if old_limit is None: - return - try: - import resource - resource.setrlimit(resource.RLIMIT_CORE, old_limit) - except (ImportError, ValueError, resource.error): - return + def _suppress_core_files(self): + """Try to prevent core files from being created. + Returns previous ulimit if successful, else None. + """ + try: + import resource + old_limit = resource.getrlimit(resource.RLIMIT_CORE) + resource.setrlimit(resource.RLIMIT_CORE, (0,0)) + return old_limit + except (ImportError, ValueError, resource.error): + return None - def test_run_abort(self): - # returncode handles signal termination - old_limit = self._suppress_core_files() - try: - p = subprocess.Popen([sys.executable, - "-c", "import os; os.abort()"]) - finally: - self._unsuppress_core_files(old_limit) - p.wait() - self.assertEqual(-p.returncode, signal.SIGABRT) + def _unsuppress_core_files(self, old_limit): + """Return core file behavior to default.""" + if old_limit is None: + return + try: + import resource + resource.setrlimit(resource.RLIMIT_CORE, old_limit) + except (ImportError, ValueError, resource.error): + return - def test_preexec(self): - # preexec function - p = subprocess.Popen([sys.executable, "-c", - 'import sys,os;' \ - 'sys.stdout.write(os.getenv("FRUIT"))'], - stdout=subprocess.PIPE, - preexec_fn=lambda: os.putenv("FRUIT", "apple")) - self.assertEqual(p.stdout.read(), "apple") + def test_run_abort(self): + # returncode handles signal termination + old_limit = self._suppress_core_files() + try: + p = subprocess.Popen([sys.executable, + "-c", "import os; os.abort()"]) + finally: + self._unsuppress_core_files(old_limit) + p.wait() + self.assertEqual(-p.returncode, signal.SIGABRT) - def test_args_string(self): - # args is a string - f, fname = self.mkstemp() - os.write(f, "#!/bin/sh\n") - os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % - sys.executable) - os.close(f) - os.chmod(fname, 0700) - p = subprocess.Popen(fname) - p.wait() - os.remove(fname) - self.assertEqual(p.returncode, 47) + def test_preexec(self): + # preexec function + p = subprocess.Popen([sys.executable, "-c", + 'import sys,os;' \ + 'sys.stdout.write(os.getenv("FRUIT"))'], + stdout=subprocess.PIPE, + preexec_fn=lambda: os.putenv("FRUIT", "apple")) + self.assertEqual(p.stdout.read(), "apple") - def test_invalid_args(self): - # invalid arguments should raise ValueError - self.assertRaises(ValueError, subprocess.call, - [sys.executable, - "-c", "import sys; sys.exit(47)"], - startupinfo=47) - self.assertRaises(ValueError, subprocess.call, - [sys.executable, - "-c", "import sys; sys.exit(47)"], - creationflags=47) + def test_args_string(self): + # args is a string + f, fname = self.mkstemp() + os.write(f, "#!/bin/sh\n") + os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % + sys.executable) + os.close(f) + os.chmod(fname, 0700) + p = subprocess.Popen(fname) + p.wait() + os.remove(fname) + self.assertEqual(p.returncode, 47) - def test_shell_sequence(self): - # Run command through the shell (sequence) - newenv = os.environ.copy() - newenv["FRUIT"] = "apple" - p = subprocess.Popen(["echo $FRUIT"], shell=1, - stdout=subprocess.PIPE, - env=newenv) - self.assertEqual(p.stdout.read().strip(), "apple") + def test_invalid_args(self): + # invalid arguments should raise ValueError + self.assertRaises(ValueError, subprocess.call, + [sys.executable, + "-c", "import sys; sys.exit(47)"], + startupinfo=47) + self.assertRaises(ValueError, subprocess.call, + [sys.executable, + "-c", "import sys; sys.exit(47)"], + creationflags=47) - def test_shell_string(self): - # Run command through the shell (string) - newenv = os.environ.copy() - newenv["FRUIT"] = "apple" - p = subprocess.Popen("echo $FRUIT", shell=1, - stdout=subprocess.PIPE, - env=newenv) - self.assertEqual(p.stdout.read().strip(), "apple") + def test_shell_sequence(self): + # Run command through the shell (sequence) + newenv = os.environ.copy() + newenv["FRUIT"] = "apple" + p = subprocess.Popen(["echo $FRUIT"], shell=1, + stdout=subprocess.PIPE, + env=newenv) + self.assertEqual(p.stdout.read().strip(), "apple") - def test_call_string(self): - # call() function with string argument on UNIX - f, fname = self.mkstemp() - os.write(f, "#!/bin/sh\n") - os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % - sys.executable) - os.close(f) - os.chmod(fname, 0700) - rc = subprocess.call(fname) - os.remove(fname) - self.assertEqual(rc, 47) + def test_shell_string(self): + # Run command through the shell (string) + newenv = os.environ.copy() + newenv["FRUIT"] = "apple" + p = subprocess.Popen("echo $FRUIT", shell=1, + stdout=subprocess.PIPE, + env=newenv) + self.assertEqual(p.stdout.read().strip(), "apple") - def DISABLED_test_send_signal(self): - p = subprocess.Popen([sys.executable, - "-c", "input()"]) + def test_call_string(self): + # call() function with string argument on UNIX + f, fname = self.mkstemp() + os.write(f, "#!/bin/sh\n") + os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" % + sys.executable) + os.close(f) + os.chmod(fname, 0700) + rc = subprocess.call(fname) + os.remove(fname) + self.assertEqual(rc, 47) - self.assertTrue(p.poll() is None, p.poll()) - p.send_signal(signal.SIGINT) - self.assertNotEqual(p.wait(), 0) + def test_send_signal(self): + p = subprocess.Popen([sys.executable, + "-c", "input()"]) - def DISABLED_test_kill(self): - p = subprocess.Popen([sys.executable, - "-c", "input()"]) + self.assertTrue(p.poll() is None, p.poll()) + p.send_signal(signal.SIGINT) + self.assertNotEqual(p.wait(), None) - self.assertTrue(p.poll() is None, p.poll()) - p.kill() - self.assertEqual(p.wait(), -signal.SIGKILL) + def test_kill(self): + p = subprocess.Popen([sys.executable, + "-c", "input()"]) - def DISABLED_test_terminate(self): - p = subprocess.Popen([sys.executable, - "-c", "input()"]) + self.assertTrue(p.poll() is None, p.poll()) + p.kill() + self.assertEqual(p.wait(), -signal.SIGKILL) - self.assertTrue(p.poll() is None, p.poll()) - p.terminate() - self.assertEqual(p.wait(), -signal.SIGTERM) + def test_terminate(self): + p = subprocess.Popen([sys.executable, + "-c", "input()"]) - # - # Windows tests - # - if mswindows: - def test_startupinfo(self): - # startupinfo argument - # We uses hardcoded constants, because we do not want to - # depend on win32all. - STARTF_USESHOWWINDOW = 1 - SW_MAXIMIZE = 3 - startupinfo = subprocess.STARTUPINFO() - startupinfo.dwFlags = STARTF_USESHOWWINDOW - startupinfo.wShowWindow = SW_MAXIMIZE - # Since Python is a console process, it won't be affected - # by wShowWindow, but the argument should be silently - # ignored - subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], - startupinfo=startupinfo) + self.assertTrue(p.poll() is None, p.poll()) + p.terminate() + self.assertEqual(p.wait(), -signal.SIGTERM) - def test_creationflags(self): - # creationflags argument - CREATE_NEW_CONSOLE = 16 - sys.stderr.write(" a DOS box should flash briefly ...\n") - subprocess.call(sys.executable + - ' -c "import time; time.sleep(0.25)"', - creationflags=CREATE_NEW_CONSOLE) - def test_invalid_args(self): - # invalid arguments should raise ValueError - self.assertRaises(ValueError, subprocess.call, - [sys.executable, - "-c", "import sys; sys.exit(47)"], - preexec_fn=lambda: 1) - self.assertRaises(ValueError, subprocess.call, - [sys.executable, - "-c", "import sys; sys.exit(47)"], - stdout=subprocess.PIPE, - close_fds=True) +@unittest.skipUnless(sys.platform == "win32", "Windows specific tests") +class Win32ProcessTestCase(_BaseProcessTestCase): + def test_startupinfo(self): + # startupinfo argument + # We uses hardcoded constants, because we do not want to + # depend on win32all. + STARTF_USESHOWWINDOW = 1 + SW_MAXIMIZE = 3 + startupinfo = subprocess.STARTUPINFO() + startupinfo.dwFlags = STARTF_USESHOWWINDOW + startupinfo.wShowWindow = SW_MAXIMIZE + # Since Python is a console process, it won't be affected + # by wShowWindow, but the argument should be silently + # ignored + subprocess.call([sys.executable, "-c", "import sys; sys.exit(0)"], + startupinfo=startupinfo) - def test_close_fds(self): - # close file descriptors - rc = subprocess.call([sys.executable, "-c", - "import sys; sys.exit(47)"], - close_fds=True) - self.assertEqual(rc, 47) + def test_creationflags(self): + # creationflags argument + CREATE_NEW_CONSOLE = 16 + sys.stderr.write(" a DOS box should flash briefly ...\n") + subprocess.call(sys.executable + + ' -c "import time; time.sleep(0.25)"', + creationflags=CREATE_NEW_CONSOLE) - def test_shell_sequence(self): - # Run command through the shell (sequence) - newenv = os.environ.copy() - newenv["FRUIT"] = "physalis" - p = subprocess.Popen(["set"], shell=1, - stdout=subprocess.PIPE, - env=newenv) - self.assertNotEqual(p.stdout.read().find("physalis"), -1) + def test_invalid_args(self): + # invalid arguments should raise ValueError + self.assertRaises(ValueError, subprocess.call, + [sys.executable, + "-c", "import sys; sys.exit(47)"], + preexec_fn=lambda: 1) + self.assertRaises(ValueError, subprocess.call, + [sys.executable, + "-c", "import sys; sys.exit(47)"], + stdout=subprocess.PIPE, + close_fds=True) - def test_shell_string(self): - # Run command through the shell (string) - newenv = os.environ.copy() - newenv["FRUIT"] = "physalis" - p = subprocess.Popen("set", shell=1, - stdout=subprocess.PIPE, - env=newenv) - self.assertNotEqual(p.stdout.read().find("physalis"), -1) + def test_close_fds(self): + # close file descriptors + rc = subprocess.call([sys.executable, "-c", + "import sys; sys.exit(47)"], + close_fds=True) + self.assertEqual(rc, 47) - def test_call_string(self): - # call() function with string argument on Windows - rc = subprocess.call(sys.executable + - ' -c "import sys; sys.exit(47)"') - self.assertEqual(rc, 47) + def test_shell_sequence(self): + # Run command through the shell (sequence) + newenv = os.environ.copy() + newenv["FRUIT"] = "physalis" + p = subprocess.Popen(["set"], shell=1, + stdout=subprocess.PIPE, + env=newenv) + self.assertNotEqual(p.stdout.read().find("physalis"), -1) - def DISABLED_test_send_signal(self): - p = subprocess.Popen([sys.executable, - "-c", "input()"]) + def test_shell_string(self): + # Run command through the shell (string) + newenv = os.environ.copy() + newenv["FRUIT"] = "physalis" + p = subprocess.Popen("set", shell=1, + stdout=subprocess.PIPE, + env=newenv) + self.assertNotEqual(p.stdout.read().find("physalis"), -1) - self.assertTrue(p.poll() is None, p.poll()) - p.send_signal(signal.SIGTERM) - self.assertNotEqual(p.wait(), 0) + def test_call_string(self): + # call() function with string argument on Windows + rc = subprocess.call(sys.executable + + ' -c "import sys; sys.exit(47)"') + self.assertEqual(rc, 47) - def DISABLED_test_kill(self): - p = subprocess.Popen([sys.executable, - "-c", "input()"]) + def test_send_signal(self): + p = subprocess.Popen([sys.executable, + "-c", "input()"]) - self.assertTrue(p.poll() is None, p.poll()) - p.kill() - self.assertNotEqual(p.wait(), 0) + self.assertTrue(p.poll() is None, p.poll()) + p.send_signal(signal.SIGTERM) + self.assertNotEqual(p.wait(), None) - def DISABLED_test_terminate(self): - p = subprocess.Popen([sys.executable, - "-c", "input()"]) + def test_kill(self): + p = subprocess.Popen([sys.executable, + "-c", "input()"]) - self.assertTrue(p.poll() is None, p.poll()) - p.terminate() - self.assertNotEqual(p.wait(), 0) + self.assertTrue(p.poll() is None, p.poll()) + p.kill() + self.assertNotEqual(p.wait(), None) + def test_terminate(self): + p = subprocess.Popen([sys.executable, + "-c", "input()"]) -unit_tests = [ProcessTestCase] + self.assertTrue(p.poll() is None, p.poll()) + p.terminate() + self.assertNotEqual(p.wait(), None) + +unit_tests = [ProcessTestCase, POSIXProcessTestCase, Win32ProcessTestCase] + if getattr(subprocess, '_has_poll', False): class ProcessTestCaseNoPoll(ProcessTestCase): def setUp(self):