Deprecate os.popen* and popen2 module in favor of the subprocess module.
authorNeal Norwitz <nnorwitz@gmail.com>
Fri, 11 May 2007 06:57:33 +0000 (06:57 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Fri, 11 May 2007 06:57:33 +0000 (06:57 +0000)
Lib/os.py
Lib/plat-mac/pimp.py
Lib/popen2.py
Lib/test/test___all__.py
Lib/test/test_bz2.py
Lib/test/test_cmd_line.py
Lib/test/test_popen2.py
Misc/NEWS
Tools/msi/msilib.py

index 991716ed1fab824554212420370ce0dcd761f133..206aa371b3436c72b89bfe480e7dc428abd1f567 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -666,9 +666,15 @@ if _exists("fork"):
             is a string it will be passed to the shell (as with os.system()). If
             'bufsize' is specified, it sets the buffer size for the I/O pipes.  The
             file objects (child_stdin, child_stdout) are returned."""
-            import popen2
-            stdout, stdin = popen2.popen2(cmd, bufsize)
-            return stdin, stdout
+            import warnings
+            msg = "os.popen2 is deprecated.  Use the subprocess module."
+            warnings.warn(msg, DeprecationWarning, stacklevel=2)
+
+            import subprocess
+            PIPE = subprocess.PIPE
+            p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
+                                 stdin=PIPE, stdout=PIPE, close_fds=True)
+            return p.stdin, p.stdout
         __all__.append("popen2")
 
     if not _exists("popen3"):
@@ -679,9 +685,16 @@ if _exists("fork"):
             is a string it will be passed to the shell (as with os.system()). If
             'bufsize' is specified, it sets the buffer size for the I/O pipes.  The
             file objects (child_stdin, child_stdout, child_stderr) are returned."""
-            import popen2
-            stdout, stdin, stderr = popen2.popen3(cmd, bufsize)
-            return stdin, stdout, stderr
+            import warnings
+            msg = "os.popen3 is deprecated.  Use the subprocess module."
+            warnings.warn(msg, DeprecationWarning, stacklevel=2)
+
+            import subprocess
+            PIPE = subprocess.PIPE
+            p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
+                                 stdin=PIPE, stdout=PIPE, stderr=PIPE,
+                                 close_fds=True)
+            return p.stdin, p.stdout, p.stderr
         __all__.append("popen3")
 
     if not _exists("popen4"):
@@ -692,9 +705,16 @@ if _exists("fork"):
             is a string it will be passed to the shell (as with os.system()). If
             'bufsize' is specified, it sets the buffer size for the I/O pipes.  The
             file objects (child_stdin, child_stdout_stderr) are returned."""
-            import popen2
-            stdout, stdin = popen2.popen4(cmd, bufsize)
-            return stdin, stdout
+            import warnings
+            msg = "os.popen4 is deprecated.  Use the subprocess module."
+            warnings.warn(msg, DeprecationWarning, stacklevel=2)
+
+            import subprocess
+            PIPE = subprocess.PIPE
+            p = subprocess.Popen(cmd, shell=True, bufsize=bufsize,
+                                 stdin=PIPE, stdout=PIPE,
+                                 stderr=subprocess.STDOUT, close_fds=True)
+            return p.stdin, p.stdout
         __all__.append("popen4")
 
 import copy_reg as _copy_reg
index 456427c1cd296422ac82cab94843d95fb0278917..e58f36d4ad9c46afeca822b0241b2b3235909d70 100644 (file)
@@ -14,7 +14,7 @@ intention is that the end user will use this through a GUI.
 """
 import sys
 import os
-import popen2
+import subprocess
 import urllib
 import urllib2
 import urlparse
@@ -101,10 +101,11 @@ def _cmd(output, dir, *cmditems):
         output.write("+ %s\n" % cmd)
     if NO_EXECUTE:
         return 0
-    child = popen2.Popen4(cmd)
-    child.tochild.close()
+    child = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
+                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    child.stdin.close()
     while 1:
-        line = child.fromchild.readline()
+        line = child.stdout.readline()
         if not line:
             break
         if output:
index ab30463e24ad0b730bf89c13d8ee42cdcecfc5d1..145b3406cf5c7b3363dba4629742b6b5568cde9e 100644 (file)
@@ -8,6 +8,9 @@ and popen3(cmd) which return two or three pipes to the spawned command.
 
 import os
 import sys
+import warnings
+warnings.warn("The popen2 module is deprecated.  Use the subprocess module.",
+              DeprecationWarning, stacklevel=2)
 
 __all__ = ["popen2", "popen3", "popen4"]
 
index 5e7d34e1d64eaa6251345dfeb44400d2e130c218..2b9f1dedea446f91dc5a5445ab422da5a0b033a5 100644 (file)
@@ -9,6 +9,8 @@ warnings.filterwarnings("ignore",
                         "<string>")
 warnings.filterwarnings("ignore", "the sets module is deprecated",
                         DeprecationWarning, "<string>")
+warnings.filterwarnings("ignore", ".*popen2 module is deprecated.*",
+                        DeprecationWarning)
 
 class AllTest(unittest.TestCase):
 
index 709850d2298eae185f11c775716af7ea0a4a0755..b8b3c031027136ced4d167654284cab16ccb0665 100644 (file)
@@ -5,7 +5,7 @@ from test.test_support import TESTFN
 import unittest
 from cStringIO import StringIO
 import os
-import popen2
+import subprocess
 import sys
 
 import bz2
@@ -21,18 +21,20 @@ class BaseTest(unittest.TestCase):
 
     if has_cmdline_bunzip2:
         def decompress(self, data):
-            pop = popen2.Popen3("bunzip2", capturestderr=1)
-            pop.tochild.write(data)
-            pop.tochild.close()
-            ret = pop.fromchild.read()
-            pop.fromchild.close()
+            pop = subprocess.Popen("bunzip2", shell=True,
+                                   stdin=subprocess.PIPE,
+                                   stdout=subprocess.PIPE,
+                                   stderr=subprocess.STDOUT)
+            pop.stdin.write(data)
+            pop.stdin.close()
+            ret = pop.stdout.read()
+            pop.stdout.close()
             if pop.wait() != 0:
                 ret = bz2.decompress(data)
             return ret
 
     else:
-        # popen2.Popen3 doesn't exist on Windows, and even if it did, bunzip2
-        # isn't available to run.
+        # bunzip2 isn't available to run on Windows.
         def decompress(self, data):
             return bz2.decompress(data)
 
index cacae7a697aa74ecd38c804d0f5b264a13d6522d..d3f07c7f134ddc49e1c90636e343b910a9034846 100644 (file)
@@ -1,18 +1,19 @@
 
 import test.test_support, unittest
 import sys
-import popen2
 import subprocess
 
 class CmdLineTest(unittest.TestCase):
     def start_python(self, cmd_line):
-        outfp, infp = popen2.popen4('"%s" %s' % (sys.executable, cmd_line))
-        infp.close()
-        data = outfp.read()
-        outfp.close()
+        cmd = '"%s" %s' % (sys.executable, cmd_line)
+        p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
+                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+        p.stdin.close()
+        data = p.stdout.read()
+        p.stdout.close()
         # try to cleanup the child so we don't appear to leak when running
         # with regrtest -R.  This should be a no-op on Windows.
-        popen2._cleanup()
+        subprocess._cleanup()
         return data
 
     def exit_code(self, *args):
index 31f22d6adf1b94640dcc8f4b324639b1f1c845a6..023871f598b2bca69e3fce6b2037a44e950265e8 100644 (file)
@@ -1,6 +1,12 @@
 #! /usr/bin/env python
 """Test script for popen2.py"""
 
+import warnings
+warnings.filterwarnings("ignore", ".*popen2 module is deprecated.*",
+                        DeprecationWarning)
+warnings.filterwarnings("ignore", "os\.popen. is deprecated.*",
+                        DeprecationWarning)
+
 import os
 import sys
 import unittest
index 80bdc0a11f1624d6efe65820532e65fb3d61f8d7..32531f6c40e23253d1aaaf765dbee419ceb00d92 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -207,6 +207,8 @@ Core and builtins
 Library
 -------
 
+- The popen2 module and os.popen* are deprecated.  Use the subprocess module.
+
 - Added an optional credentials argument to SMTPHandler, for use with SMTP
   servers which require authentication.
 
index d7b86ccb0dc3ed2d8e5e753f36a47265557803fd..548b640bc31a2aed14cd48d97d835e225a5d805b 100644 (file)
@@ -5,7 +5,7 @@ import win32com.client.gencache
 import win32com.client
 import pythoncom, pywintypes
 from win32com.client import constants
-import re, string, os, sets, glob, popen2, sys, _winreg, struct
+import re, string, os, sets, glob, subprocess, sys, _winreg, struct
 
 try:
     basestring
@@ -388,8 +388,10 @@ class CAB:
         else:
             print "WARNING: cabarc.exe not found in registry"
             cabarc = "cabarc.exe"
-        f = popen2.popen4(r'"%s" -m lzx:21 n %s.cab @%s.txt' % (cabarc, self.name, self.name))[0]
-        for line in f:
+        cmd = r'"%s" -m lzx:21 n %s.cab @%s.txt' % (cabarc, self.name, self.name)
+        p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
+                             stdout=subprocess.PIPE, stderr=subprocess.STDOUT)[0]
+        for line in (p.stdout, p.stdin):
             if line.startswith("  -- adding "):
                 sys.stdout.write(".")
             else: