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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
91 additions
and
60 deletions
+91
-60
subprocess.rst
Doc/library/subprocess.rst
+0
-0
3.6.rst
Doc/whatsnew/3.6.rst
+3
-0
subprocess.py
Lib/subprocess.py
+45
-42
test_subprocess.py
Lib/test/test_subprocess.py
+41
-18
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
read the exit status of the child process (Contributed by Victor Stinner in
: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
---------
...
...
Lib/subprocess.py
Dosyayı görüntüle @
050acaed
...
...
@@ -30,7 +30,8 @@ class Popen(args, bufsize=-1, executable=None,
preexec_fn=None, close_fds=True, shell=False,
cwd=None, env=None, universal_newlines=False,
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:
...
...
@@ -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
process.
If universal_newlines is False, the file objects stdin, stdout and stderr
are opened as binary files, and no line ending conversion is done.
If encoding or errors are specified or universal_newlines is True, the file
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
opened as a text file, but lines may be terminated by any of '\n',
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. 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.
If no encoding is specified and universal_newlines is False, the file
objects stdin, stdout and stderr are opened as binary files, and no
line ending conversion is done.
The startupinfo and creationflags, if given, will be passed to the
underlying CreateProcess() function. They can specify things such as
...
...
@@ -234,11 +228,8 @@ communicate(input=None)
and stderr, until end-of-file is reached. Wait for process to
terminate. The optional input argument should be data to be
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
set to True, the input argument should be a string and will be encoded
using the preferred system encoding (see locale.getpreferredencoding);
if universal_newlines is False, the input argument should be a
byte string.
the child. If the Popen instance was constructed in text mode, the
input argument should be a string. Otherwise, it should be bytes.
communicate() returns a tuple (stdout, stderr).
...
...
@@ -808,8 +799,8 @@ def getstatusoutput(cmd):
""" Return (status, output) of executing cmd in a shell.
Execute the string 'cmd' in a shell with 'check_output' and
return a 2-tuple (status, output).
Universal newlines mode is used,
meaning that the result with be decoded to a string
.
return a 2-tuple (status, output).
The locale encoding is used
to decode the output and process newlines
.
A trailing newline is stripped from the output.
The exit status for the command can be interpreted
...
...
@@ -859,7 +850,7 @@ class Popen(object):
shell
=
False
,
cwd
=
None
,
env
=
None
,
universal_newlines
=
False
,
startupinfo
=
None
,
creationflags
=
0
,
restore_signals
=
True
,
start_new_session
=
False
,
pass_fds
=
()):
pass_fds
=
()
,
*
,
encoding
=
None
,
errors
=
None
):
"""Create new Popen instance."""
_cleanup
()
# Held while anything is calling waitpid before returncode has been
...
...
@@ -912,6 +903,8 @@ class Popen(object):
self
.
pid
=
None
self
.
returncode
=
None
self
.
universal_newlines
=
universal_newlines
self
.
encoding
=
encoding
self
.
errors
=
errors
# Input and output objects. The general principle is like
# this:
...
...
@@ -944,22 +937,28 @@ class Popen(object):
if
errread
!=
-
1
:
errread
=
msvcrt
.
open_osfhandle
(
errread
.
Detach
(),
0
)
if
p2cwrite
!=
-
1
:
self
.
stdin
=
io
.
open
(
p2cwrite
,
'wb'
,
bufsize
)
if
universal_newlines
:
self
.
stdin
=
io
.
TextIOWrapper
(
self
.
stdin
,
write_through
=
True
,
line_buffering
=
(
bufsize
==
1
))
if
c2pread
!=
-
1
:
self
.
stdout
=
io
.
open
(
c2pread
,
'rb'
,
bufsize
)
if
universal_newlines
:
self
.
stdout
=
io
.
TextIOWrapper
(
self
.
stdout
)
if
errread
!=
-
1
:
self
.
stderr
=
io
.
open
(
errread
,
'rb'
,
bufsize
)
if
universal_newlines
:
self
.
stderr
=
io
.
TextIOWrapper
(
self
.
stderr
)
text_mode
=
encoding
or
errors
or
universal_newlines
self
.
_closed_child_pipe_fds
=
False
try
:
if
p2cwrite
!=
-
1
:
self
.
stdin
=
io
.
open
(
p2cwrite
,
'wb'
,
bufsize
)
if
text_mode
:
self
.
stdin
=
io
.
TextIOWrapper
(
self
.
stdin
,
write_through
=
True
,
line_buffering
=
(
bufsize
==
1
),
encoding
=
encoding
,
errors
=
errors
)
if
c2pread
!=
-
1
:
self
.
stdout
=
io
.
open
(
c2pread
,
'rb'
,
bufsize
)
if
text_mode
:
self
.
stdout
=
io
.
TextIOWrapper
(
self
.
stdout
,
encoding
=
encoding
,
errors
=
errors
)
if
errread
!=
-
1
:
self
.
stderr
=
io
.
open
(
errread
,
'rb'
,
bufsize
)
if
text_mode
:
self
.
stderr
=
io
.
TextIOWrapper
(
self
.
stderr
,
encoding
=
encoding
,
errors
=
errors
)
self
.
_execute_child
(
args
,
executable
,
preexec_fn
,
close_fds
,
pass_fds
,
cwd
,
env
,
startupinfo
,
creationflags
,
shell
,
...
...
@@ -993,8 +992,8 @@ class Popen(object):
raise
def
_translate_newlines
(
self
,
data
,
encoding
):
data
=
data
.
decode
(
encoding
)
def
_translate_newlines
(
self
,
data
,
encoding
,
errors
):
data
=
data
.
decode
(
encoding
,
errors
)
return
data
.
replace
(
"
\r\n
"
,
"
\n
"
)
.
replace
(
"
\r
"
,
"
\n
"
)
def
__enter__
(
self
):
...
...
@@ -1779,13 +1778,15 @@ class Popen(object):
# Translate newlines, if requested.
# 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
:
stdout
=
self
.
_translate_newlines
(
stdout
,
self
.
stdout
.
encoding
)
self
.
stdout
.
encoding
,
self
.
stdout
.
errors
)
if
stderr
is
not
None
:
stderr
=
self
.
_translate_newlines
(
stderr
,
self
.
stderr
.
encoding
)
self
.
stderr
.
encoding
,
self
.
stderr
.
errors
)
return
(
stdout
,
stderr
)
...
...
@@ -1797,8 +1798,10 @@ class Popen(object):
if
self
.
stdin
and
self
.
_input
is
None
:
self
.
_input_offset
=
0
self
.
_input
=
input
if
self
.
universal_newlines
and
input
is
not
None
:
self
.
_input
=
self
.
_input
.
encode
(
self
.
stdin
.
encoding
)
if
input
is
not
None
and
(
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
):
...
...
Lib/test/test_subprocess.py
Dosyayı görüntüle @
050acaed
...
...
@@ -894,31 +894,42 @@ class ProcessTestCase(BaseTestCase):
#
# UTF-16 and UTF-32-BE are sufficient to check both with BOM and
# without, and UTF-16 and UTF-32.
import
_bootlocale
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; "
r"sys.stdout.buffer.write('1\r\n2\r3\n4'.encode('
%
s'))"
%
encoding
)
args
=
[
sys
.
executable
,
'-c'
,
code
]
try
:
_bootlocale
.
getpreferredencoding
=
getpreferredencoding
# 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
,
universal_newlines
=
True
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
)
stdout
,
stderr
=
popen
.
communicate
(
input
=
''
)
finally
:
_bootlocale
.
getpreferredencoding
=
old_getpreferredencoding
# 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
=
encoding
)
stdout
,
stderr
=
popen
.
communicate
(
input
=
''
)
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
):
# Make sure we leak no resources
if
not
mswindows
:
...
...
@@ -2539,6 +2550,18 @@ class Win32ProcessTestCase(BaseTestCase):
with
p
:
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
):
# call() function with string argument on Windows
rc
=
subprocess
.
call
(
sys
.
executable
+
...
...
Misc/NEWS
Dosyayı görüntüle @
050acaed
...
...
@@ -237,6 +237,8 @@ Build
Windows
-------
-
Issue
#
6135
:
Adds
encoding
and
errors
parameters
to
subprocess
.
-
Issue
#
27959
:
Adds
oem
encoding
,
alias
ansi
to
mbcs
,
move
aliasmbcs
to
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