Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
050acaed
Kaydet (Commit)
050acaed
authored
Eyl 07, 2016
tarafından
Steve Dower
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #6135: Adds encoding and errors parameters to subprocess
üst
173a1f3d
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
81 additions
and
50 deletions
+81
-50
subprocess.rst
Doc/library/subprocess.rst
+0
-0
3.6.rst
Doc/whatsnew/3.6.rst
+3
-0
subprocess.py
Lib/subprocess.py
+40
-37
test_subprocess.py
Lib/test/test_subprocess.py
+36
-13
NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/subprocess.rst
Dosyayı görüntüle @
050acaed
This diff is collapsed.
Click to expand it.
Doc/whatsnew/3.6.rst
Dosyayı görüntüle @
050acaed
...
@@ -589,6 +589,9 @@ proc: ...``) or call explicitly the :meth:`~subprocess.Popen.wait` method to
...
@@ -589,6 +589,9 @@ proc: ...``) or call explicitly the :meth:`~subprocess.Popen.wait` method to
read the exit status of the child process (Contributed by Victor Stinner in
read the exit status of the child process (Contributed by Victor Stinner in
:issue:`26741`).
:issue:`26741`).
The :class:`subprocess.Popen` constructor and all functions that pass arguments
through to it now accept *encoding* and *errors* arguments. Specifying either
of these will enable text mode for the *stdin*, *stdout* and *stderr* streams.
telnetlib
telnetlib
---------
---------
...
...
Lib/subprocess.py
Dosyayı görüntüle @
050acaed
...
@@ -30,7 +30,8 @@ class Popen(args, bufsize=-1, executable=None,
...
@@ -30,7 +30,8 @@ class Popen(args, bufsize=-1, executable=None,
preexec_fn=None, close_fds=True, shell=False,
preexec_fn=None, close_fds=True, shell=False,
cwd=None, env=None, universal_newlines=False,
cwd=None, env=None, universal_newlines=False,
startupinfo=None, creationflags=0,
startupinfo=None, creationflags=0,
restore_signals=True, start_new_session=False, pass_fds=()):
restore_signals=True, start_new_session=False, pass_fds=(),
*, encoding=None, errors=None):
Arguments are:
Arguments are:
...
@@ -104,20 +105,13 @@ in the child process prior to executing the command.
...
@@ -104,20 +105,13 @@ in the child process prior to executing the command.
If env is not None, it defines the environment variables for the new
If env is not None, it defines the environment variables for the new
process.
process.
If universal_newlines is False, the file objects stdin, stdout and stderr
If encoding or errors are specified or universal_newlines is True, the file
are opened as binary files, and no line ending conversion is done.
objects stdout and stderr are opened in text mode. See io.TextIOWrapper for
the interpretation of these parameters are used.
If universal_newlines is True, the file objects stdout and stderr are
If no encoding is specified and universal_newlines is False, the file
opened as a text file, but lines may be terminated by any of '\n',
objects stdin, stdout and stderr are opened as binary files, and no
the Unix end-of-line convention, '\r', the old Macintosh convention or
line ending conversion is done.
'\r\n', the Windows convention. All of these external representations
are seen as '\n' by the Python program. Also, the newlines attribute
of the file objects stdout, stdin and stderr are not updated by the
communicate() method.
In either case, the process being communicated with should start up
expecting to receive bytes on its standard input and decode them with
the same encoding they are sent in.
The startupinfo and creationflags, if given, will be passed to the
The startupinfo and creationflags, if given, will be passed to the
underlying CreateProcess() function. They can specify things such as
underlying CreateProcess() function. They can specify things such as
...
@@ -234,11 +228,8 @@ communicate(input=None)
...
@@ -234,11 +228,8 @@ communicate(input=None)
and stderr, until end-of-file is reached. Wait for process to
and stderr, until end-of-file is reached. Wait for process to
terminate. The optional input argument should be data to be
terminate. The optional input argument should be data to be
sent to the child process, or None, if no data should be sent to
sent to the child process, or None, if no data should be sent to
the child. If the Popen instance was constructed with universal_newlines
the child. If the Popen instance was constructed in text mode, the
set to True, the input argument should be a string and will be encoded
input argument should be a string. Otherwise, it should be bytes.
using the preferred system encoding (see locale.getpreferredencoding);
if universal_newlines is False, the input argument should be a
byte string.
communicate() returns a tuple (stdout, stderr).
communicate() returns a tuple (stdout, stderr).
...
@@ -808,8 +799,8 @@ def getstatusoutput(cmd):
...
@@ -808,8 +799,8 @@ def getstatusoutput(cmd):
""" Return (status, output) of executing cmd in a shell.
""" Return (status, output) of executing cmd in a shell.
Execute the string 'cmd' in a shell with 'check_output' and
Execute the string 'cmd' in a shell with 'check_output' and
return a 2-tuple (status, output).
Universal newlines mode is used,
return a 2-tuple (status, output).
The locale encoding is used
meaning that the result with be decoded to a string
.
to decode the output and process newlines
.
A trailing newline is stripped from the output.
A trailing newline is stripped from the output.
The exit status for the command can be interpreted
The exit status for the command can be interpreted
...
@@ -859,7 +850,7 @@ class Popen(object):
...
@@ -859,7 +850,7 @@ class Popen(object):
shell
=
False
,
cwd
=
None
,
env
=
None
,
universal_newlines
=
False
,
shell
=
False
,
cwd
=
None
,
env
=
None
,
universal_newlines
=
False
,
startupinfo
=
None
,
creationflags
=
0
,
startupinfo
=
None
,
creationflags
=
0
,
restore_signals
=
True
,
start_new_session
=
False
,
restore_signals
=
True
,
start_new_session
=
False
,
pass_fds
=
()):
pass_fds
=
()
,
*
,
encoding
=
None
,
errors
=
None
):
"""Create new Popen instance."""
"""Create new Popen instance."""
_cleanup
()
_cleanup
()
# Held while anything is calling waitpid before returncode has been
# Held while anything is calling waitpid before returncode has been
...
@@ -912,6 +903,8 @@ class Popen(object):
...
@@ -912,6 +903,8 @@ class Popen(object):
self
.
pid
=
None
self
.
pid
=
None
self
.
returncode
=
None
self
.
returncode
=
None
self
.
universal_newlines
=
universal_newlines
self
.
universal_newlines
=
universal_newlines
self
.
encoding
=
encoding
self
.
errors
=
errors
# Input and output objects. The general principle is like
# Input and output objects. The general principle is like
# this:
# this:
...
@@ -944,22 +937,28 @@ class Popen(object):
...
@@ -944,22 +937,28 @@ class Popen(object):
if
errread
!=
-
1
:
if
errread
!=
-
1
:
errread
=
msvcrt
.
open_osfhandle
(
errread
.
Detach
(),
0
)
errread
=
msvcrt
.
open_osfhandle
(
errread
.
Detach
(),
0
)
text_mode
=
encoding
or
errors
or
universal_newlines
self
.
_closed_child_pipe_fds
=
False
try
:
if
p2cwrite
!=
-
1
:
if
p2cwrite
!=
-
1
:
self
.
stdin
=
io
.
open
(
p2cwrite
,
'wb'
,
bufsize
)
self
.
stdin
=
io
.
open
(
p2cwrite
,
'wb'
,
bufsize
)
if
universal_newlines
:
if
text_mode
:
self
.
stdin
=
io
.
TextIOWrapper
(
self
.
stdin
,
write_through
=
True
,
self
.
stdin
=
io
.
TextIOWrapper
(
self
.
stdin
,
write_through
=
True
,
line_buffering
=
(
bufsize
==
1
))
line_buffering
=
(
bufsize
==
1
),
encoding
=
encoding
,
errors
=
errors
)
if
c2pread
!=
-
1
:
if
c2pread
!=
-
1
:
self
.
stdout
=
io
.
open
(
c2pread
,
'rb'
,
bufsize
)
self
.
stdout
=
io
.
open
(
c2pread
,
'rb'
,
bufsize
)
if
universal_newlines
:
if
text_mode
:
self
.
stdout
=
io
.
TextIOWrapper
(
self
.
stdout
)
self
.
stdout
=
io
.
TextIOWrapper
(
self
.
stdout
,
encoding
=
encoding
,
errors
=
errors
)
if
errread
!=
-
1
:
if
errread
!=
-
1
:
self
.
stderr
=
io
.
open
(
errread
,
'rb'
,
bufsize
)
self
.
stderr
=
io
.
open
(
errread
,
'rb'
,
bufsize
)
if
universal_newlines
:
if
text_mode
:
self
.
stderr
=
io
.
TextIOWrapper
(
self
.
stderr
)
self
.
stderr
=
io
.
TextIOWrapper
(
self
.
stderr
,
encoding
=
encoding
,
errors
=
errors
)
self
.
_closed_child_pipe_fds
=
False
try
:
self
.
_execute_child
(
args
,
executable
,
preexec_fn
,
close_fds
,
self
.
_execute_child
(
args
,
executable
,
preexec_fn
,
close_fds
,
pass_fds
,
cwd
,
env
,
pass_fds
,
cwd
,
env
,
startupinfo
,
creationflags
,
shell
,
startupinfo
,
creationflags
,
shell
,
...
@@ -993,8 +992,8 @@ class Popen(object):
...
@@ -993,8 +992,8 @@ class Popen(object):
raise
raise
def
_translate_newlines
(
self
,
data
,
encoding
):
def
_translate_newlines
(
self
,
data
,
encoding
,
errors
):
data
=
data
.
decode
(
encoding
)
data
=
data
.
decode
(
encoding
,
errors
)
return
data
.
replace
(
"
\r\n
"
,
"
\n
"
)
.
replace
(
"
\r
"
,
"
\n
"
)
return
data
.
replace
(
"
\r\n
"
,
"
\n
"
)
.
replace
(
"
\r
"
,
"
\n
"
)
def
__enter__
(
self
):
def
__enter__
(
self
):
...
@@ -1779,13 +1778,15 @@ class Popen(object):
...
@@ -1779,13 +1778,15 @@ class Popen(object):
# Translate newlines, if requested.
# Translate newlines, if requested.
# This also turns bytes into strings.
# This also turns bytes into strings.
if
self
.
universal_newlines
:
if
self
.
encoding
or
self
.
errors
or
self
.
universal_newlines
:
if
stdout
is
not
None
:
if
stdout
is
not
None
:
stdout
=
self
.
_translate_newlines
(
stdout
,
stdout
=
self
.
_translate_newlines
(
stdout
,
self
.
stdout
.
encoding
)
self
.
stdout
.
encoding
,
self
.
stdout
.
errors
)
if
stderr
is
not
None
:
if
stderr
is
not
None
:
stderr
=
self
.
_translate_newlines
(
stderr
,
stderr
=
self
.
_translate_newlines
(
stderr
,
self
.
stderr
.
encoding
)
self
.
stderr
.
encoding
,
self
.
stderr
.
errors
)
return
(
stdout
,
stderr
)
return
(
stdout
,
stderr
)
...
@@ -1797,8 +1798,10 @@ class Popen(object):
...
@@ -1797,8 +1798,10 @@ class Popen(object):
if
self
.
stdin
and
self
.
_input
is
None
:
if
self
.
stdin
and
self
.
_input
is
None
:
self
.
_input_offset
=
0
self
.
_input_offset
=
0
self
.
_input
=
input
self
.
_input
=
input
if
self
.
universal_newlines
and
input
is
not
None
:
if
input
is
not
None
and
(
self
.
_input
=
self
.
_input
.
encode
(
self
.
stdin
.
encoding
)
self
.
encoding
or
self
.
errors
or
self
.
universal_newlines
):
self
.
_input
=
self
.
_input
.
encode
(
self
.
stdin
.
encoding
,
self
.
stdin
.
errors
)
def
send_signal
(
self
,
sig
):
def
send_signal
(
self
,
sig
):
...
...
Lib/test/test_subprocess.py
Dosyayı görüntüle @
050acaed
...
@@ -894,31 +894,42 @@ class ProcessTestCase(BaseTestCase):
...
@@ -894,31 +894,42 @@ class ProcessTestCase(BaseTestCase):
#
#
# UTF-16 and UTF-32-BE are sufficient to check both with BOM and
# UTF-16 and UTF-32-BE are sufficient to check both with BOM and
# without, and UTF-16 and UTF-32.
# without, and UTF-16 and UTF-32.
import
_bootlocale
for
encoding
in
[
'utf-16'
,
'utf-32-be'
]:
for
encoding
in
[
'utf-16'
,
'utf-32-be'
]:
old_getpreferredencoding
=
_bootlocale
.
getpreferredencoding
# Indirectly via io.TextIOWrapper, Popen() defaults to
# locale.getpreferredencoding(False) and earlier in Python 3.2 to
# locale.getpreferredencoding().
def
getpreferredencoding
(
do_setlocale
=
True
):
return
encoding
code
=
(
"import sys; "
code
=
(
"import sys; "
r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('
%
s'))"
%
r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('
%
s'))"
%
encoding
)
encoding
)
args
=
[
sys
.
executable
,
'-c'
,
code
]
args
=
[
sys
.
executable
,
'-c'
,
code
]
try
:
_bootlocale
.
getpreferredencoding
=
getpreferredencoding
# We set stdin to be non-None because, as of this writing,
# We set stdin to be non-None because, as of this writing,
# a different code path is used when the number of pipes is
# a different code path is used when the number of pipes is
# zero or one.
# zero or one.
popen
=
subprocess
.
Popen
(
args
,
universal_newlines
=
True
,
popen
=
subprocess
.
Popen
(
args
,
stdin
=
subprocess
.
PIPE
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
)
stdout
=
subprocess
.
PIPE
,
encoding
=
encoding
)
stdout
,
stderr
=
popen
.
communicate
(
input
=
''
)
stdout
,
stderr
=
popen
.
communicate
(
input
=
''
)
finally
:
_bootlocale
.
getpreferredencoding
=
old_getpreferredencoding
self
.
assertEqual
(
stdout
,
'1
\n
2
\n
3
\n
4'
)
self
.
assertEqual
(
stdout
,
'1
\n
2
\n
3
\n
4'
)
def
test_communicate_errors
(
self
):
for
errors
,
expected
in
[
(
'ignore'
,
''
),
(
'replace'
,
'
\ufffd\ufffd
'
),
(
'surrogateescape'
,
'
\udc80\udc80
'
),
(
'backslashreplace'
,
'
\\
x80
\\
x80'
),
]:
code
=
(
"import sys; "
r"sys.stdout.buffer.write(b'[\x80\x80]')"
)
args
=
[
sys
.
executable
,
'-c'
,
code
]
# We set stdin to be non-None because, as of this writing,
# a different code path is used when the number of pipes is
# zero or one.
popen
=
subprocess
.
Popen
(
args
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
,
encoding
=
'utf-8'
,
errors
=
errors
)
stdout
,
stderr
=
popen
.
communicate
(
input
=
''
)
self
.
assertEqual
(
stdout
,
'[{}]'
.
format
(
expected
))
def
test_no_leaking
(
self
):
def
test_no_leaking
(
self
):
# Make sure we leak no resources
# Make sure we leak no resources
if
not
mswindows
:
if
not
mswindows
:
...
@@ -2539,6 +2550,18 @@ class Win32ProcessTestCase(BaseTestCase):
...
@@ -2539,6 +2550,18 @@ class Win32ProcessTestCase(BaseTestCase):
with
p
:
with
p
:
self
.
assertIn
(
b
"physalis"
,
p
.
stdout
.
read
())
self
.
assertIn
(
b
"physalis"
,
p
.
stdout
.
read
())
def
test_shell_encodings
(
self
):
# Run command through the shell (string)
for
enc
in
[
'ansi'
,
'oem'
]:
newenv
=
os
.
environ
.
copy
()
newenv
[
"FRUIT"
]
=
"physalis"
p
=
subprocess
.
Popen
(
"set"
,
shell
=
1
,
stdout
=
subprocess
.
PIPE
,
env
=
newenv
,
encoding
=
enc
)
with
p
:
self
.
assertIn
(
"physalis"
,
p
.
stdout
.
read
(),
enc
)
def
test_call_string
(
self
):
def
test_call_string
(
self
):
# call() function with string argument on Windows
# call() function with string argument on Windows
rc
=
subprocess
.
call
(
sys
.
executable
+
rc
=
subprocess
.
call
(
sys
.
executable
+
...
...
Misc/NEWS
Dosyayı görüntüle @
050acaed
...
@@ -237,6 +237,8 @@ Build
...
@@ -237,6 +237,8 @@ Build
Windows
Windows
-------
-------
-
Issue
#
6135
:
Adds
encoding
and
errors
parameters
to
subprocess
.
-
Issue
#
27959
:
Adds
oem
encoding
,
alias
ansi
to
mbcs
,
move
aliasmbcs
to
-
Issue
#
27959
:
Adds
oem
encoding
,
alias
ansi
to
mbcs
,
move
aliasmbcs
to
codec
lookup
.
codec
lookup
.
...
...
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