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
3a4586a9
Kaydet (Commit)
3a4586a9
authored
Kas 08, 2013
tarafından
Charles-François Natali
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #18923: Update subprocess to use the new selectors module.
üst
2ce6c44a
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
169 deletions
+71
-169
subprocess.py
Lib/subprocess.py
+65
-165
test_subprocess.py
Lib/test/test_subprocess.py
+6
-4
No files found.
Lib/subprocess.py
Dosyayı görüntüle @
3a4586a9
...
...
@@ -404,15 +404,23 @@ if mswindows:
hStdError
=
None
wShowWindow
=
0
else
:
import
select
_has_poll
=
hasattr
(
select
,
'poll'
)
import
_posixsubprocess
import
select
import
selectors
# When select or poll has indicated that the file is writable,
# we can write up to _PIPE_BUF bytes without risk of blocking.
# POSIX defines PIPE_BUF as >= 512.
_PIPE_BUF
=
getattr
(
select
,
'PIPE_BUF'
,
512
)
# poll/select have the advantage of not requiring any extra file
# descriptor, contrarily to epoll/kqueue (also, they require a single
# syscall).
if
hasattr
(
selectors
,
'PollSelector'
):
_PopenSelector
=
selectors
.
PollSelector
else
:
_PopenSelector
=
selectors
.
SelectSelector
__all__
=
[
"Popen"
,
"PIPE"
,
"STDOUT"
,
"call"
,
"check_call"
,
"getstatusoutput"
,
"getoutput"
,
"check_output"
,
"CalledProcessError"
,
"DEVNULL"
]
...
...
@@ -1530,206 +1538,98 @@ class Popen(object):
if
not
input
:
self
.
stdin
.
close
()
if
_has_poll
:
stdout
,
stderr
=
self
.
_communicate_with_poll
(
input
,
endtime
,
orig_timeout
)
else
:
stdout
,
stderr
=
self
.
_communicate_with_select
(
input
,
endtime
,
orig_timeout
)
self
.
wait
(
timeout
=
self
.
_remaining_time
(
endtime
))
# All data exchanged. Translate lists into strings.
if
stdout
is
not
None
:
stdout
=
b
''
.
join
(
stdout
)
if
stderr
is
not
None
:
stderr
=
b
''
.
join
(
stderr
)
# Translate newlines, if requested.
# This also turns bytes into strings.
if
self
.
universal_newlines
:
if
stdout
is
not
None
:
stdout
=
self
.
_translate_newlines
(
stdout
,
self
.
stdout
.
encoding
)
if
stderr
is
not
None
:
stderr
=
self
.
_translate_newlines
(
stderr
,
self
.
stderr
.
encoding
)
return
(
stdout
,
stderr
)
def
_save_input
(
self
,
input
):
# This method is called from the _communicate_with_*() methods
# so that if we time out while communicating, we can continue
# sending input if we retry.
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
)
def
_communicate_with_poll
(
self
,
input
,
endtime
,
orig_timeout
):
stdout
=
None
# Return
stderr
=
None
# Return
if
not
self
.
_communication_started
:
self
.
_fd2file
=
{}
poller
=
select
.
poll
()
def
register_and_append
(
file_obj
,
eventmask
):
poller
.
register
(
file_obj
.
fileno
(),
eventmask
)
self
.
_fd2file
[
file_obj
.
fileno
()]
=
file_obj
def
close_unregister_and_remove
(
fd
):
poller
.
unregister
(
fd
)
self
.
_fd2file
[
fd
]
.
close
()
self
.
_fd2file
.
pop
(
fd
)
if
self
.
stdin
and
input
:
register_and_append
(
self
.
stdin
,
select
.
POLLOUT
)
stdout
=
None
stderr
=
None
# Only create this mapping if we haven't already.
if
not
self
.
_communication_started
:
self
.
_f
d
2output
=
{}
self
.
_f
ileobj
2output
=
{}
if
self
.
stdout
:
self
.
_f
d2output
[
self
.
stdout
.
fileno
()
]
=
[]
self
.
_f
ileobj2output
[
self
.
stdout
]
=
[]
if
self
.
stderr
:
self
.
_f
d2output
[
self
.
stderr
.
fileno
()
]
=
[]
self
.
_f
ileobj2output
[
self
.
stderr
]
=
[]
select_POLLIN_POLLPRI
=
select
.
POLLIN
|
select
.
POLLPRI
if
self
.
stdout
:
register_and_append
(
self
.
stdout
,
select_POLLIN_POLLPRI
)
stdout
=
self
.
_fd2output
[
self
.
stdout
.
fileno
()]
stdout
=
self
.
_fileobj2output
[
self
.
stdout
]
if
self
.
stderr
:
register_and_append
(
self
.
stderr
,
select_POLLIN_POLLPRI
)
stderr
=
self
.
_fd2output
[
self
.
stderr
.
fileno
()]
stderr
=
self
.
_fileobj2output
[
self
.
stderr
]
self
.
_save_input
(
input
)
while
self
.
_fd2file
:
timeout
=
self
.
_remaining_time
(
endtime
)
if
timeout
is
not
None
and
timeout
<
0
:
raise
TimeoutExpired
(
self
.
args
,
orig_timeout
)
try
:
ready
=
poller
.
poll
(
timeout
)
except
OSError
as
e
:
if
e
.
args
[
0
]
==
errno
.
EINTR
:
continue
raise
self
.
_check_timeout
(
endtime
,
orig_timeout
)
# XXX Rewrite these to use non-blocking I/O on the
# file objects; they are no longer using C stdio!
for
fd
,
mode
in
ready
:
if
mode
&
select
.
POLLOUT
:
chunk
=
self
.
_input
[
self
.
_input_offset
:
self
.
_input_offset
+
_PIPE_BUF
]
try
:
self
.
_input_offset
+=
os
.
write
(
fd
,
chunk
)
except
OSError
as
e
:
if
e
.
errno
==
errno
.
EPIPE
:
close_unregister_and_remove
(
fd
)
else
:
raise
else
:
if
self
.
_input_offset
>=
len
(
self
.
_input
):
close_unregister_and_remove
(
fd
)
elif
mode
&
select_POLLIN_POLLPRI
:
data
=
os
.
read
(
fd
,
4096
)
if
not
data
:
close_unregister_and_remove
(
fd
)
self
.
_fd2output
[
fd
]
.
append
(
data
)
else
:
# Ignore hang up or errors.
close_unregister_and_remove
(
fd
)
return
(
stdout
,
stderr
)
def
_communicate_with_select
(
self
,
input
,
endtime
,
orig_timeout
):
if
not
self
.
_communication_started
:
self
.
_read_set
=
[]
self
.
_write_set
=
[]
with
_PopenSelector
()
as
selector
:
if
self
.
stdin
and
input
:
sel
f
.
_write_set
.
append
(
self
.
stdin
)
sel
ector
.
register
(
self
.
stdin
,
selectors
.
EVENT_WRITE
)
if
self
.
stdout
:
sel
f
.
_read_set
.
append
(
self
.
stdout
)
sel
ector
.
register
(
self
.
stdout
,
selectors
.
EVENT_READ
)
if
self
.
stderr
:
self
.
_read_set
.
append
(
self
.
stderr
)
self
.
_save_input
(
input
)
selector
.
register
(
self
.
stderr
,
selectors
.
EVENT_READ
)
stdout
=
None
# Return
stderr
=
None
# Return
if
self
.
stdout
:
if
not
self
.
_communication_started
:
self
.
_stdout_buff
=
[]
stdout
=
self
.
_stdout_buff
if
self
.
stderr
:
if
not
self
.
_communication_started
:
self
.
_stderr_buff
=
[]
stderr
=
self
.
_stderr_buff
while
self
.
_read_set
or
self
.
_write_set
:
while
selector
.
get_map
():
timeout
=
self
.
_remaining_time
(
endtime
)
if
timeout
is
not
None
and
timeout
<
0
:
raise
TimeoutExpired
(
self
.
args
,
orig_timeout
)
try
:
(
rlist
,
wlist
,
xlist
)
=
\
select
.
select
(
self
.
_read_set
,
self
.
_write_set
,
[],
timeout
)
except
OSError
as
e
:
if
e
.
args
[
0
]
==
errno
.
EINTR
:
continue
raise
# According to the docs, returning three empty lists indicates
# that the timeout expired.
if
not
(
rlist
or
wlist
or
xlist
):
raise
TimeoutExpired
(
self
.
args
,
orig_timeout
)
# We also check what time it is ourselves for good measure.
ready
=
selector
.
select
(
timeout
)
self
.
_check_timeout
(
endtime
,
orig_timeout
)
# XXX Rewrite these to use non-blocking I/O on th
e
# file
objects; they are no longer using C stdio!
# XXX Rewrite these to use non-blocking I/O on the fil
e
#
objects; they are no longer using C stdio!
if
self
.
stdin
in
wlist
:
for
key
,
events
in
ready
:
if
key
.
fileobj
is
self
.
stdin
:
chunk
=
self
.
_input
[
self
.
_input_offset
:
self
.
_input_offset
+
_PIPE_BUF
]
try
:
bytes_written
=
os
.
write
(
self
.
stdin
.
fileno
()
,
chunk
)
self
.
_input_offset
+=
os
.
write
(
key
.
fd
,
chunk
)
except
OSError
as
e
:
if
e
.
errno
==
errno
.
EPIPE
:
self
.
stdin
.
close
(
)
self
.
_write_set
.
remove
(
self
.
stdin
)
selector
.
unregister
(
key
.
fileobj
)
key
.
fileobj
.
close
(
)
else
:
raise
else
:
self
.
_input_offset
+=
bytes_written
if
self
.
_input_offset
>=
len
(
self
.
_input
):
self
.
stdin
.
close
()
self
.
_write_set
.
remove
(
self
.
stdin
)
if
self
.
stdout
in
rlist
:
data
=
os
.
read
(
self
.
stdout
.
fileno
(),
1024
)
selector
.
unregister
(
key
.
fileobj
)
key
.
fileobj
.
close
()
elif
key
.
fileobj
in
(
self
.
stdout
,
self
.
stderr
):
data
=
os
.
read
(
key
.
fd
,
4096
)
if
not
data
:
self
.
stdout
.
close
(
)
self
.
_read_set
.
remove
(
self
.
stdout
)
stdout
.
append
(
data
)
selector
.
unregister
(
key
.
fileobj
)
key
.
fileobj
.
close
(
)
self
.
_fileobj2output
[
key
.
fileobj
]
.
append
(
data
)
if
self
.
stderr
in
rlist
:
data
=
os
.
read
(
self
.
stderr
.
fileno
(),
1024
)
if
not
data
:
self
.
stderr
.
close
()
self
.
_read_set
.
remove
(
self
.
stderr
)
stderr
.
append
(
data
)
self
.
wait
(
timeout
=
self
.
_remaining_time
(
endtime
))
# All data exchanged. Translate lists into strings.
if
stdout
is
not
None
:
stdout
=
b
''
.
join
(
stdout
)
if
stderr
is
not
None
:
stderr
=
b
''
.
join
(
stderr
)
# Translate newlines, if requested.
# This also turns bytes into strings.
if
self
.
universal_newlines
:
if
stdout
is
not
None
:
stdout
=
self
.
_translate_newlines
(
stdout
,
self
.
stdout
.
encoding
)
if
stderr
is
not
None
:
stderr
=
self
.
_translate_newlines
(
stderr
,
self
.
stderr
.
encoding
)
return
(
stdout
,
stderr
)
def
_save_input
(
self
,
input
):
# This method is called from the _communicate_with_*() methods
# so that if we time out while communicating, we can continue
# sending input if we retry.
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
)
def
send_signal
(
self
,
sig
):
"""Send a signal to the process
"""
...
...
Lib/test/test_subprocess.py
Dosyayı görüntüle @
3a4586a9
...
...
@@ -11,6 +11,7 @@ import errno
import
tempfile
import
time
import
re
import
selectors
import
sysconfig
import
warnings
import
select
...
...
@@ -2179,15 +2180,16 @@ class CommandTests(unittest.TestCase):
os
.
rmdir
(
dir
)
@unittest.skipUnless
(
getattr
(
subprocess
,
'_has_poll'
,
False
),
"
poll system call not supported
"
)
@unittest.skipUnless
(
hasattr
(
selectors
,
'PollSelector'
),
"
Test needs selectors.PollSelector
"
)
class
ProcessTestCaseNoPoll
(
ProcessTestCase
):
def
setUp
(
self
):
subprocess
.
_has_poll
=
False
self
.
orig_selector
=
subprocess
.
_PopenSelector
subprocess
.
_PopenSelector
=
selectors
.
SelectSelector
ProcessTestCase
.
setUp
(
self
)
def
tearDown
(
self
):
subprocess
.
_
has_poll
=
True
subprocess
.
_
PopenSelector
=
self
.
orig_selector
ProcessTestCase
.
tearDown
(
self
)
...
...
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