Kaydet (Commit) f5604853 authored tarafından Gregory P. Smith's avatar Gregory P. Smith

Get rid of the close_fds DeprecationWarning. Changes the default on a per

platform basis.  It remains False on Windows and changes to True on all
other platforms (POSIX).  Based on python-dev discussion and
http://bugs.python.org/issue7213.
üst 8d28a92e
......@@ -28,7 +28,7 @@ Using the subprocess Module
This module defines one class called :class:`Popen`:
.. class:: Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False)
.. class:: Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=_PLATFORM_DEFAULT, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False)
Arguments are:
......@@ -153,17 +153,14 @@ This module defines one class called :class:`Popen`:
If *close_fds* is true, all file descriptors except :const:`0`, :const:`1` and
:const:`2` will be closed before the child process is executed. (Unix only).
The recommended value for this argument is True.
The default varies by platform: :const:`False` on Windows and :const:`True`
on POSIX and other platforms.
On Windows, if *close_fds* is true then no handles will be inherited by the
child process. Note that on Windows, you cannot set *close_fds* to true and
also redirect the standard handles by setting *stdin*, *stdout* or *stderr*.
.. versionchanged:: 3.2
Callers should always specify a *close_fds* to avoid a DeprecationWarning.
The default behavior of this argument will be changing in Python 3.3.
If *shell* is :const:`True`, the specified command will be executed through the
shell.
The default was changed to True on non Windows platforms.
If *cwd* is not ``None``, the child's current directory will be changed to *cwd*
before it is executed. Note that this directory is not considered when
......@@ -654,4 +651,5 @@ Replacing functions from the :mod:`popen2` module
* ``stdin=PIPE`` and ``stdout=PIPE`` must be specified.
* popen2 closes all file descriptors by default, but you have to specify
``close_fds=True`` with :class:`Popen`.
``close_fds=True`` with :class:`Popen` to guarantee this behavior on
all platforms or past Python versions.
......@@ -27,7 +27,7 @@ This module defines one class called Popen:
class Popen(args, bufsize=0, executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False, shell=False,
preexec_fn=None, close_fds=_PLATFORM_DEFAULT, shell=False,
cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0,
restore_signals=True, start_new_session=False):
......@@ -39,12 +39,12 @@ args should be a string, or a sequence of program arguments. The
program to execute is normally the first item in the args sequence or
string, but can be explicitly set by using the executable argument.
On UNIX, with shell=False (default): In this case, the Popen class
On POSIX, with shell=False (default): In this case, the Popen class
uses os.execvp() to execute the child program. args should normally
be a sequence. A string will be treated as a sequence with the string
as the only item (the program to execute).
On UNIX, with shell=True: If args is a string, it specifies the
On POSIX, with shell=True: If args is a string, it specifies the
command string to execute through the shell. If args is a sequence,
the first item specifies the command string, and any additional items
will be treated as additional shell arguments.
......@@ -73,14 +73,16 @@ parent. Additionally, stderr can be STDOUT, which indicates that the
stderr data from the applications should be captured into the same
file handle as for stdout.
On UNIX, if preexec_fn is set to a callable object, this object will be
On POSIX, if preexec_fn is set to a callable object, this object will be
called in the child process just before the child is executed. The use
of preexec_fn is not thread safe, using it in the presence of threads
could lead to a deadlock in the child process before the new executable
is executed.
If close_fds is true, all file descriptors except 0, 1 and 2 will be
closed before the child process is executed.
closed before the child process is executed. The default for close_fds
varies by platform: False on Windows and True on all other platforms
such as POSIX.
if shell is true, the specified command will be executed through the
shell.
......@@ -88,12 +90,12 @@ shell.
If cwd is not None, the current directory will be changed to cwd
before the child is executed.
On UNIX, if restore_signals is True all signals that Python sets to
On POSIX, if restore_signals is True all signals that Python sets to
SIG_IGN are restored to SIG_DFL in the child process before the exec.
Currently this includes the SIGPIPE, SIGXFZ and SIGXFSZ signals. This
parameter does nothing on Windows.
On UNIX, if start_new_session is True, the setsid() system call will be made
On POSIX, if start_new_session is True, the setsid() system call will be made
in the child process prior to executing the command.
If env is not None, it defines the environment variables for the new
......@@ -101,7 +103,7 @@ process.
If universal_newlines is true, the file objects stdout and stderr are
opened as a text files, but lines may be terminated by any of '\n',
the Unix end-of-line convention, '\r', the Macintosh convention or
the Unix end-of-line convention, '\r', the old Macintosh convention or
'\r\n', the Windows convention. All of these external representations
are seen as '\n' by the Python program. Note: This feature is only
available if Python is built with universal newline support (the
......@@ -242,7 +244,7 @@ pid
returncode
The child return code. A None value indicates that the process
hasn't terminated yet. A negative value -N indicates that the
child was terminated by signal N (UNIX only).
child was terminated by signal N (POSIX only).
Replacing older functions with the subprocess module
......@@ -562,7 +564,7 @@ def list2cmdline(seq):
# Various tools for executing commands and looking at their output and status.
#
# NB This only works (and is only relevant) for UNIX.
# NB This only works (and is only relevant) for POSIX.
def getstatusoutput(cmd):
"""Return (status, output) of executing cmd in a shell.
......@@ -602,10 +604,16 @@ def getoutput(cmd):
return getstatusoutput(cmd)[1]
if mswindows:
_PLATFORM_DEFAULT = False
else:
_PLATFORM_DEFAULT = True
class Popen(object):
def __init__(self, args, bufsize=0, executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=None, shell=False,
preexec_fn=None, close_fds=_PLATFORM_DEFAULT, shell=False,
cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0,
restore_signals=True, start_new_session=False,
......@@ -619,15 +627,6 @@ class Popen(object):
if not isinstance(bufsize, int):
raise TypeError("bufsize must be an integer")
if close_fds is None:
# Notification for http://bugs.python.org/issue7213 & issue2320
warnings.warn(
'The close_fds parameter was not specified. Its default'
' behavior will change in a future Python version. '
' Most users should set it to True. Please'
' update your code explicitly set close_fds.',
DeprecationWarning)
if mswindows:
if preexec_fn is not None:
raise ValueError("preexec_fn is not supported on Windows "
......
......@@ -58,22 +58,6 @@ class BaseTestCase(unittest.TestCase):
self.assertEqual(actual, expected, msg)
class DeprecationWarningTests(BaseTestCase):
def testCloseFdsWarning(self):
quick_process = [sys.executable, "-c", "import sys; sys.exit(0)"]
with warnings.catch_warnings(record=True) as warnlist:
warnings.simplefilter("always")
subprocess.call(quick_process, close_fds=True)
self.assertEqual([], warnlist)
subprocess.call(quick_process, close_fds=False)
self.assertEqual([], warnlist)
with self.assertWarns(DeprecationWarning) as wm:
subprocess.Popen(quick_process).wait()
self.assertEqual(1, len(wm.warnings))
self.assertIn('close_fds parameter was not specified',
str(wm.warnings[0]))
class ProcessTestCase(BaseTestCase):
def test_call_seq(self):
......@@ -1250,8 +1234,7 @@ def test_main():
ProcessTestCaseNoPoll,
HelperFunctionTests,
CommandsWithSpaces,
ContextManagerTests,
DeprecationWarningTests)
ContextManagerTests)
support.run_unittest(*unit_tests)
support.reap_children()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment