Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
ba102ec1
Commit
ba102ec1
authored
Mar 16, 2011
by
Ross Lagerwall
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #5870: Add subprocess.DEVNULL constant.
parent
09bb8f46
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
7 deletions
+66
-7
Doc/library/subprocess.rst
Doc/library/subprocess.rst
+17
-6
Lib/subprocess.py
Lib/subprocess.py
+22
-1
Lib/test/test_subprocess.py
Lib/test/test_subprocess.py
+25
-0
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/subprocess.rst
View file @
ba102ec1
...
@@ -125,12 +125,14 @@ This module defines one class called :class:`Popen`:
...
@@ -125,12 +125,14 @@ This module defines one class called :class:`Popen`:
*stdin*, *stdout* and *stderr* specify the executed programs' standard input,
*stdin*, *stdout* and *stderr* specify the executed programs' standard input,
standard output and standard error file handles, respectively. Valid values
standard output and standard error file handles, respectively. Valid values
are :data:`PIPE`, an existing file descriptor (a positive integer), an
are :data:`PIPE`, :data:`DEVNULL`, an existing file descriptor (a positive
existing :term:`file object`, and ``None``. :data:`PIPE` indicates that a
integer), an existing :term:`file object`, and ``None``. :data:`PIPE`
new pipe to the child should be created. With ``None``, no redirection will
indicates that a new pipe to the child should be created. :data:`DEVNULL`
occur; the child's file handles will be inherited from the parent. Additionally,
indicates that the special file :data:`os.devnull` will be used. With ``None``,
*stderr* can be :data:`STDOUT`, which indicates that the stderr data from the
no redirection will occur; the child's file handles will be inherited from
applications should be captured into the same file handle as for stdout.
the parent. Additionally, *stderr* can be :data:`STDOUT`, which indicates
that the stderr data from the applications should be captured into the same
file handle as for stdout.
If *preexec_fn* is set to a callable object, this object will be called in the
If *preexec_fn* is set to a callable object, this object will be called in the
child process just before the child is executed.
child process just before the child is executed.
...
@@ -229,6 +231,15 @@ This module defines one class called :class:`Popen`:
...
@@ -229,6 +231,15 @@ This module defines one class called :class:`Popen`:
Added context manager support.
Added context manager support.
.. data:: DEVNULL
Special value that can be used as the *stdin*, *stdout* or *stderr* argument
to :class:`Popen` and indicates that the special file :data:`os.devnull`
will be used.
.. versionadded:: 3.3
.. data:: PIPE
.. data:: PIPE
Special value that can be used as the *stdin*, *stdout* or *stderr* argument
Special value that can be used as the *stdin*, *stdout* or *stderr* argument
...
...
Lib/subprocess.py
View file @
ba102ec1
...
@@ -431,7 +431,7 @@ else:
...
@@ -431,7 +431,7 @@ else:
return
fds
return
fds
__all__
=
[
"Popen"
,
"PIPE"
,
"STDOUT"
,
"call"
,
"check_call"
,
"getstatusoutput"
,
__all__
=
[
"Popen"
,
"PIPE"
,
"STDOUT"
,
"call"
,
"check_call"
,
"getstatusoutput"
,
"getoutput"
,
"check_output"
,
"CalledProcessError"
]
"getoutput"
,
"check_output"
,
"CalledProcessError"
,
"DEVNULL"
]
if
mswindows
:
if
mswindows
:
from
_subprocess
import
CREATE_NEW_CONSOLE
,
CREATE_NEW_PROCESS_GROUP
from
_subprocess
import
CREATE_NEW_CONSOLE
,
CREATE_NEW_PROCESS_GROUP
...
@@ -456,6 +456,7 @@ def _cleanup():
...
@@ -456,6 +456,7 @@ def _cleanup():
PIPE
=
-
1
PIPE
=
-
1
STDOUT
=
-
2
STDOUT
=
-
2
DEVNULL
=
-
3
def
_eintr_retry_call
(
func
,
*
args
):
def
_eintr_retry_call
(
func
,
*
args
):
...
@@ -800,6 +801,10 @@ class Popen(object):
...
@@ -800,6 +801,10 @@ class Popen(object):
# Child is still running, keep us alive until we can wait on it.
# Child is still running, keep us alive until we can wait on it.
_active
.
append
(
self
)
_active
.
append
(
self
)
def
_get_devnull
(
self
):
if
not
hasattr
(
self
,
'_devnull'
):
self
.
_devnull
=
os
.
open
(
os
.
devnull
,
os
.
O_RDWR
)
return
self
.
_devnull
def
communicate
(
self
,
input
=
None
,
timeout
=
None
):
def
communicate
(
self
,
input
=
None
,
timeout
=
None
):
"""Interact with process: Send data to stdin. Read data from
"""Interact with process: Send data to stdin. Read data from
...
@@ -889,6 +894,8 @@ class Popen(object):
...
@@ -889,6 +894,8 @@ class Popen(object):
p2cread
,
_
=
_subprocess
.
CreatePipe
(
None
,
0
)
p2cread
,
_
=
_subprocess
.
CreatePipe
(
None
,
0
)
elif
stdin
==
PIPE
:
elif
stdin
==
PIPE
:
p2cread
,
p2cwrite
=
_subprocess
.
CreatePipe
(
None
,
0
)
p2cread
,
p2cwrite
=
_subprocess
.
CreatePipe
(
None
,
0
)
elif
stdin
==
DEVNULL
:
p2cread
=
msvcrt
.
get_osfhandle
(
self
.
_get_devnull
())
elif
isinstance
(
stdin
,
int
):
elif
isinstance
(
stdin
,
int
):
p2cread
=
msvcrt
.
get_osfhandle
(
stdin
)
p2cread
=
msvcrt
.
get_osfhandle
(
stdin
)
else
:
else
:
...
@@ -902,6 +909,8 @@ class Popen(object):
...
@@ -902,6 +909,8 @@ class Popen(object):
_
,
c2pwrite
=
_subprocess
.
CreatePipe
(
None
,
0
)
_
,
c2pwrite
=
_subprocess
.
CreatePipe
(
None
,
0
)
elif
stdout
==
PIPE
:
elif
stdout
==
PIPE
:
c2pread
,
c2pwrite
=
_subprocess
.
CreatePipe
(
None
,
0
)
c2pread
,
c2pwrite
=
_subprocess
.
CreatePipe
(
None
,
0
)
elif
stdout
==
DEVNULL
:
c2pwrite
=
msvcrt
.
get_osfhandle
(
self
.
_get_devnull
())
elif
isinstance
(
stdout
,
int
):
elif
isinstance
(
stdout
,
int
):
c2pwrite
=
msvcrt
.
get_osfhandle
(
stdout
)
c2pwrite
=
msvcrt
.
get_osfhandle
(
stdout
)
else
:
else
:
...
@@ -917,6 +926,8 @@ class Popen(object):
...
@@ -917,6 +926,8 @@ class Popen(object):
errread
,
errwrite
=
_subprocess
.
CreatePipe
(
None
,
0
)
errread
,
errwrite
=
_subprocess
.
CreatePipe
(
None
,
0
)
elif
stderr
==
STDOUT
:
elif
stderr
==
STDOUT
:
errwrite
=
c2pwrite
errwrite
=
c2pwrite
elif
stderr
==
DEVNULL
:
errwrite
=
msvcrt
.
get_osfhandle
(
self
.
_get_devnull
())
elif
isinstance
(
stderr
,
int
):
elif
isinstance
(
stderr
,
int
):
errwrite
=
msvcrt
.
get_osfhandle
(
stderr
)
errwrite
=
msvcrt
.
get_osfhandle
(
stderr
)
else
:
else
:
...
@@ -1026,6 +1037,8 @@ class Popen(object):
...
@@ -1026,6 +1037,8 @@ class Popen(object):
c2pwrite
.
Close
()
c2pwrite
.
Close
()
if
errwrite
!=
-
1
:
if
errwrite
!=
-
1
:
errwrite
.
Close
()
errwrite
.
Close
()
if
hasattr
(
self
,
'_devnull'
):
os
.
close
(
self
.
_devnull
)
# Retain the process handle, but close the thread handle
# Retain the process handle, but close the thread handle
self
.
_child_created
=
True
self
.
_child_created
=
True
...
@@ -1159,6 +1172,8 @@ class Popen(object):
...
@@ -1159,6 +1172,8 @@ class Popen(object):
pass
pass
elif
stdin
==
PIPE
:
elif
stdin
==
PIPE
:
p2cread
,
p2cwrite
=
_create_pipe
()
p2cread
,
p2cwrite
=
_create_pipe
()
elif
stdin
==
DEVNULL
:
p2cread
=
self
.
_get_devnull
()
elif
isinstance
(
stdin
,
int
):
elif
isinstance
(
stdin
,
int
):
p2cread
=
stdin
p2cread
=
stdin
else
:
else
:
...
@@ -1169,6 +1184,8 @@ class Popen(object):
...
@@ -1169,6 +1184,8 @@ class Popen(object):
pass
pass
elif
stdout
==
PIPE
:
elif
stdout
==
PIPE
:
c2pread
,
c2pwrite
=
_create_pipe
()
c2pread
,
c2pwrite
=
_create_pipe
()
elif
stdout
==
DEVNULL
:
c2pwrite
=
self
.
_get_devnull
()
elif
isinstance
(
stdout
,
int
):
elif
isinstance
(
stdout
,
int
):
c2pwrite
=
stdout
c2pwrite
=
stdout
else
:
else
:
...
@@ -1181,6 +1198,8 @@ class Popen(object):
...
@@ -1181,6 +1198,8 @@ class Popen(object):
errread
,
errwrite
=
_create_pipe
()
errread
,
errwrite
=
_create_pipe
()
elif
stderr
==
STDOUT
:
elif
stderr
==
STDOUT
:
errwrite
=
c2pwrite
errwrite
=
c2pwrite
elif
stderr
==
DEVNULL
:
errwrite
=
self
.
_get_devnull
()
elif
isinstance
(
stderr
,
int
):
elif
isinstance
(
stderr
,
int
):
errwrite
=
stderr
errwrite
=
stderr
else
:
else
:
...
@@ -1374,6 +1393,8 @@ class Popen(object):
...
@@ -1374,6 +1393,8 @@ class Popen(object):
os
.
close
(
c2pwrite
)
os
.
close
(
c2pwrite
)
if
errwrite
!=
-
1
and
errread
!=
-
1
:
if
errwrite
!=
-
1
and
errread
!=
-
1
:
os
.
close
(
errwrite
)
os
.
close
(
errwrite
)
if
hasattr
(
self
,
'_devnull'
):
os
.
close
(
self
.
_devnull
)
# Wait for exec to fail or succeed; possibly raising an
# Wait for exec to fail or succeed; possibly raising an
# exception (limited in size)
# exception (limited in size)
...
...
Lib/test/test_subprocess.py
View file @
ba102ec1
...
@@ -323,6 +323,31 @@ class ProcessTestCase(BaseTestCase):
...
@@ -323,6 +323,31 @@ class ProcessTestCase(BaseTestCase):
rc
=
subprocess
.
call
([
sys
.
executable
,
"-c"
,
cmd
],
stdout
=
1
)
rc
=
subprocess
.
call
([
sys
.
executable
,
"-c"
,
cmd
],
stdout
=
1
)
self
.
assertEqual
(
rc
,
2
)
self
.
assertEqual
(
rc
,
2
)
def
test_stdout_devnull
(
self
):
p
=
subprocess
.
Popen
([
sys
.
executable
,
"-c"
,
'for i in range(10240):'
'print("x" * 1024)'
],
stdout
=
subprocess
.
DEVNULL
)
p
.
wait
()
self
.
assertEqual
(
p
.
stdout
,
None
)
def
test_stderr_devnull
(
self
):
p
=
subprocess
.
Popen
([
sys
.
executable
,
"-c"
,
'import sys
\
n
'
'for i in range(10240):'
'sys.stderr.write("x" * 1024)'
],
stderr
=
subprocess
.
DEVNULL
)
p
.
wait
()
self
.
assertEqual
(
p
.
stderr
,
None
)
def
test_stdin_devnull
(
self
):
p
=
subprocess
.
Popen
([
sys
.
executable
,
"-c"
,
'import sys;'
'sys.stdin.read(1)'
],
stdin
=
subprocess
.
DEVNULL
)
p
.
wait
()
self
.
assertEqual
(
p
.
stdin
,
None
)
def
test_cwd
(
self
):
def
test_cwd
(
self
):
tmpdir
=
tempfile
.
gettempdir
()
tmpdir
=
tempfile
.
gettempdir
()
# We cannot use os.path.realpath to canonicalize the path,
# We cannot use os.path.realpath to canonicalize the path,
...
...
Misc/NEWS
View file @
ba102ec1
...
@@ -72,6 +72,8 @@ Core and Builtins
...
@@ -72,6 +72,8 @@ Core and Builtins
Library
Library
-------
-------
-
Issue
#
5870
:
Add
`
subprocess
.
DEVNULL
`
constant
.
-
Issue
#
11133
:
fix
two
cases
where
inspect
.
getattr_static
can
trigger
code
-
Issue
#
11133
:
fix
two
cases
where
inspect
.
getattr_static
can
trigger
code
execution
.
Patch
by
Andreas
St
ü
hrk
.
execution
.
Patch
by
Andreas
St
ü
hrk
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment