Deprecate os.popen* and popen2 module in favor of the subprocess module.

This commit is contained in:
Neal Norwitz 2007-05-11 06:57:33 +00:00
parent 82be218e97
commit 42dd86b8e2
9 changed files with 69 additions and 30 deletions

View File

@ -666,9 +666,15 @@ if _exists("fork"):
is a string it will be passed to the shell (as with os.system()). If 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 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
file objects (child_stdin, child_stdout) are returned.""" file objects (child_stdin, child_stdout) are returned."""
import popen2 import warnings
stdout, stdin = popen2.popen2(cmd, bufsize) msg = "os.popen2 is deprecated. Use the subprocess module."
return stdin, stdout 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") __all__.append("popen2")
if not _exists("popen3"): 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 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 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
file objects (child_stdin, child_stdout, child_stderr) are returned.""" file objects (child_stdin, child_stdout, child_stderr) are returned."""
import popen2 import warnings
stdout, stdin, stderr = popen2.popen3(cmd, bufsize) msg = "os.popen3 is deprecated. Use the subprocess module."
return stdin, stdout, stderr 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") __all__.append("popen3")
if not _exists("popen4"): 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 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 'bufsize' is specified, it sets the buffer size for the I/O pipes. The
file objects (child_stdin, child_stdout_stderr) are returned.""" file objects (child_stdin, child_stdout_stderr) are returned."""
import popen2 import warnings
stdout, stdin = popen2.popen4(cmd, bufsize) msg = "os.popen4 is deprecated. Use the subprocess module."
return stdin, stdout 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") __all__.append("popen4")
import copy_reg as _copy_reg import copy_reg as _copy_reg

View File

@ -14,7 +14,7 @@ intention is that the end user will use this through a GUI.
""" """
import sys import sys
import os import os
import popen2 import subprocess
import urllib import urllib
import urllib2 import urllib2
import urlparse import urlparse
@ -101,10 +101,11 @@ def _cmd(output, dir, *cmditems):
output.write("+ %s\n" % cmd) output.write("+ %s\n" % cmd)
if NO_EXECUTE: if NO_EXECUTE:
return 0 return 0
child = popen2.Popen4(cmd) child = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
child.tochild.close() stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
child.stdin.close()
while 1: while 1:
line = child.fromchild.readline() line = child.stdout.readline()
if not line: if not line:
break break
if output: if output:

View File

@ -8,6 +8,9 @@ and popen3(cmd) which return two or three pipes to the spawned command.
import os import os
import sys import sys
import warnings
warnings.warn("The popen2 module is deprecated. Use the subprocess module.",
DeprecationWarning, stacklevel=2)
__all__ = ["popen2", "popen3", "popen4"] __all__ = ["popen2", "popen3", "popen4"]

View File

@ -9,6 +9,8 @@ warnings.filterwarnings("ignore",
"<string>") "<string>")
warnings.filterwarnings("ignore", "the sets module is deprecated", warnings.filterwarnings("ignore", "the sets module is deprecated",
DeprecationWarning, "<string>") DeprecationWarning, "<string>")
warnings.filterwarnings("ignore", ".*popen2 module is deprecated.*",
DeprecationWarning)
class AllTest(unittest.TestCase): class AllTest(unittest.TestCase):

View File

@ -5,7 +5,7 @@ from test.test_support import TESTFN
import unittest import unittest
from cStringIO import StringIO from cStringIO import StringIO
import os import os
import popen2 import subprocess
import sys import sys
import bz2 import bz2
@ -21,18 +21,20 @@ class BaseTest(unittest.TestCase):
if has_cmdline_bunzip2: if has_cmdline_bunzip2:
def decompress(self, data): def decompress(self, data):
pop = popen2.Popen3("bunzip2", capturestderr=1) pop = subprocess.Popen("bunzip2", shell=True,
pop.tochild.write(data) stdin=subprocess.PIPE,
pop.tochild.close() stdout=subprocess.PIPE,
ret = pop.fromchild.read() stderr=subprocess.STDOUT)
pop.fromchild.close() pop.stdin.write(data)
pop.stdin.close()
ret = pop.stdout.read()
pop.stdout.close()
if pop.wait() != 0: if pop.wait() != 0:
ret = bz2.decompress(data) ret = bz2.decompress(data)
return ret return ret
else: else:
# popen2.Popen3 doesn't exist on Windows, and even if it did, bunzip2 # bunzip2 isn't available to run on Windows.
# isn't available to run.
def decompress(self, data): def decompress(self, data):
return bz2.decompress(data) return bz2.decompress(data)

View File

@ -1,18 +1,19 @@
import test.test_support, unittest import test.test_support, unittest
import sys import sys
import popen2
import subprocess import subprocess
class CmdLineTest(unittest.TestCase): class CmdLineTest(unittest.TestCase):
def start_python(self, cmd_line): def start_python(self, cmd_line):
outfp, infp = popen2.popen4('"%s" %s' % (sys.executable, cmd_line)) cmd = '"%s" %s' % (sys.executable, cmd_line)
infp.close() p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
data = outfp.read() stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
outfp.close() p.stdin.close()
data = p.stdout.read()
p.stdout.close()
# try to cleanup the child so we don't appear to leak when running # 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. # with regrtest -R. This should be a no-op on Windows.
popen2._cleanup() subprocess._cleanup()
return data return data
def exit_code(self, *args): def exit_code(self, *args):

View File

@ -1,6 +1,12 @@
#! /usr/bin/env python #! /usr/bin/env python
"""Test script for popen2.py""" """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 os
import sys import sys
import unittest import unittest

View File

@ -207,6 +207,8 @@ Core and builtins
Library Library
------- -------
- The popen2 module and os.popen* are deprecated. Use the subprocess module.
- Added an optional credentials argument to SMTPHandler, for use with SMTP - Added an optional credentials argument to SMTPHandler, for use with SMTP
servers which require authentication. servers which require authentication.

View File

@ -5,7 +5,7 @@ import win32com.client.gencache
import win32com.client import win32com.client
import pythoncom, pywintypes import pythoncom, pywintypes
from win32com.client import constants 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: try:
basestring basestring
@ -388,8 +388,10 @@ class CAB:
else: else:
print "WARNING: cabarc.exe not found in registry" print "WARNING: cabarc.exe not found in registry"
cabarc = "cabarc.exe" cabarc = "cabarc.exe"
f = popen2.popen4(r'"%s" -m lzx:21 n %s.cab @%s.txt' % (cabarc, self.name, self.name))[0] cmd = r'"%s" -m lzx:21 n %s.cab @%s.txt' % (cabarc, self.name, self.name)
for line in f: 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 "): if line.startswith(" -- adding "):
sys.stdout.write(".") sys.stdout.write(".")
else: else: