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
0baa72f4
Commit
0baa72f4
authored
Sep 11, 2018
by
Andrew Svetlov
Committed by
GitHub
Sep 11, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-34622: Extract asyncio exceptions into a separate module (GH-9141)
parent
7c7605ff
Changes
18
Show whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
148 additions
and
110 deletions
+148
-110
Lib/asyncio/__init__.py
Lib/asyncio/__init__.py
+2
-0
Lib/asyncio/base_events.py
Lib/asyncio/base_events.py
+6
-5
Lib/asyncio/base_futures.py
Lib/asyncio/base_futures.py
+0
-6
Lib/asyncio/events.py
Lib/asyncio/events.py
+2
-9
Lib/asyncio/exceptions.py
Lib/asyncio/exceptions.py
+60
-0
Lib/asyncio/futures.py
Lib/asyncio/futures.py
+21
-12
Lib/asyncio/locks.py
Lib/asyncio/locks.py
+4
-3
Lib/asyncio/proactor_events.py
Lib/asyncio/proactor_events.py
+6
-5
Lib/asyncio/streams.py
Lib/asyncio/streams.py
+8
-40
Lib/asyncio/tasks.py
Lib/asyncio/tasks.py
+13
-12
Lib/asyncio/unix_events.py
Lib/asyncio/unix_events.py
+5
-4
Lib/asyncio/windows_events.py
Lib/asyncio/windows_events.py
+3
-2
Lib/test/test_asyncio/test_base_events.py
Lib/test/test_asyncio/test_base_events.py
+2
-2
Lib/test/test_asyncio/test_events.py
Lib/test/test_asyncio/test_events.py
+1
-1
Lib/test/test_asyncio/test_proactor_events.py
Lib/test/test_asyncio/test_proactor_events.py
+3
-3
Lib/test/test_asyncio/test_unix_events.py
Lib/test/test_asyncio/test_unix_events.py
+6
-6
Misc/NEWS.d/next/Library/2018-09-10-13-04-40.bpo-34622.tpv_rN.rst
...S.d/next/Library/2018-09-10-13-04-40.bpo-34622.tpv_rN.rst
+4
-0
Modules/_asynciomodule.c
Modules/_asynciomodule.c
+2
-0
No files found.
Lib/asyncio/__init__.py
View file @
0baa72f4
...
...
@@ -8,6 +8,7 @@ import sys
from
.base_events
import
*
from
.coroutines
import
*
from
.events
import
*
from
.exceptions
import
*
from
.futures
import
*
from
.locks
import
*
from
.protocols
import
*
...
...
@@ -25,6 +26,7 @@ from .tasks import _all_tasks_compat # NoQA
__all__
=
(
base_events
.
__all__
+
coroutines
.
__all__
+
events
.
__all__
+
exceptions
.
__all__
+
futures
.
__all__
+
locks
.
__all__
+
protocols
.
__all__
+
...
...
Lib/asyncio/base_events.py
View file @
0baa72f4
...
...
@@ -37,6 +37,7 @@ except ImportError: # pragma: no cover
from
.
import
constants
from
.
import
coroutines
from
.
import
events
from
.
import
exceptions
from
.
import
futures
from
.
import
protocols
from
.
import
sslproto
...
...
@@ -327,7 +328,7 @@ class Server(events.AbstractServer):
try
:
await
self
.
_serving_forever_fut
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
try
:
self
.
close
()
await
self
.
wait_closed
()
...
...
@@ -800,7 +801,7 @@ class BaseEventLoop(events.AbstractEventLoop):
try
:
return
await
self
.
_sock_sendfile_native
(
sock
,
file
,
offset
,
count
)
except
e
vent
s
.
SendfileNotAvailableError
as
exc
:
except
e
xception
s
.
SendfileNotAvailableError
as
exc
:
if
not
fallback
:
raise
return
await
self
.
_sock_sendfile_fallback
(
sock
,
file
,
...
...
@@ -809,7 +810,7 @@ class BaseEventLoop(events.AbstractEventLoop):
async
def
_sock_sendfile_native
(
self
,
sock
,
file
,
offset
,
count
):
# NB: sendfile syscall is not supported for SSL sockets and
# non-mmap files even if sendfile is supported by OS
raise
e
vent
s
.
SendfileNotAvailableError
(
raise
e
xception
s
.
SendfileNotAvailableError
(
f"syscall sendfile is not available for socket
{
sock
!
r
}
"
"and file {file!r} combination"
)
...
...
@@ -1053,7 +1054,7 @@ class BaseEventLoop(events.AbstractEventLoop):
try
:
return
await
self
.
_sendfile_native
(
transport
,
file
,
offset
,
count
)
except
e
vent
s
.
SendfileNotAvailableError
as
exc
:
except
e
xception
s
.
SendfileNotAvailableError
as
exc
:
if
not
fallback
:
raise
...
...
@@ -1066,7 +1067,7 @@ class BaseEventLoop(events.AbstractEventLoop):
offset
,
count
)
async
def
_sendfile_native
(
self
,
transp
,
file
,
offset
,
count
):
raise
e
vent
s
.
SendfileNotAvailableError
(
raise
e
xception
s
.
SendfileNotAvailableError
(
"sendfile syscall is not supported"
)
async
def
_sendfile_fallback
(
self
,
transp
,
file
,
offset
,
count
):
...
...
Lib/asyncio/base_futures.py
View file @
0baa72f4
__all__
=
()
import
concurrent.futures
import
reprlib
from
.
import
format_helpers
CancelledError
=
concurrent
.
futures
.
CancelledError
TimeoutError
=
concurrent
.
futures
.
TimeoutError
InvalidStateError
=
concurrent
.
futures
.
InvalidStateError
# States for Future.
_PENDING
=
'PENDING'
_CANCELLED
=
'CANCELLED'
...
...
Lib/asyncio/events.py
View file @
0baa72f4
...
...
@@ -3,7 +3,7 @@
__all__
=
(
'AbstractEventLoopPolicy'
,
'AbstractEventLoop'
,
'AbstractServer'
,
'Handle'
,
'TimerHandle'
,
'SendfileNotAvailableError'
,
'Handle'
,
'TimerHandle'
,
'get_event_loop_policy'
,
'set_event_loop_policy'
,
'get_event_loop'
,
'set_event_loop'
,
'new_event_loop'
,
'get_child_watcher'
,
'set_child_watcher'
,
...
...
@@ -19,14 +19,7 @@ import sys
import
threading
from
.
import
format_helpers
class
SendfileNotAvailableError
(
RuntimeError
):
"""Sendfile syscall is not available.
Raised if OS does not support sendfile syscall for given socket or
file type.
"""
from
.
import
exceptions
class
Handle
:
...
...
Lib/asyncio/exceptions.py
0 → 100644
View file @
0baa72f4
"""asyncio exceptions."""
__all__
=
(
'CancelledError'
,
'InvalidStateError'
,
'TimeoutError'
,
'IncompleteReadError'
,
'LimitOverrunError'
,
'SendfileNotAvailableError'
)
import
concurrent.futures
from
.
import
base_futures
class
CancelledError
(
concurrent
.
futures
.
CancelledError
):
"""The Future or Task was cancelled."""
class
TimeoutError
(
concurrent
.
futures
.
TimeoutError
):
"""The operation exceeded the given deadline."""
class
InvalidStateError
(
concurrent
.
futures
.
InvalidStateError
):
"""The operation is not allowed in this state."""
class
SendfileNotAvailableError
(
RuntimeError
):
"""Sendfile syscall is not available.
Raised if OS does not support sendfile syscall for given socket or
file type.
"""
class
IncompleteReadError
(
EOFError
):
"""
Incomplete read error. Attributes:
- partial: read bytes string before the end of stream was reached
- expected: total number of expected bytes (or None if unknown)
"""
def
__init__
(
self
,
partial
,
expected
):
super
().
__init__
(
f'
{
len
(
partial
)
}
bytes read on a total of '
f'
{
expected
!
r
}
expected bytes'
)
self
.
partial
=
partial
self
.
expected
=
expected
def
__reduce__
(
self
):
return
type
(
self
),
(
self
.
partial
,
self
.
expected
)
class
LimitOverrunError
(
Exception
):
"""Reached the buffer limit while looking for a separator.
Attributes:
- consumed: total number of to be consumed bytes.
"""
def
__init__
(
self
,
message
,
consumed
):
super
().
__init__
(
message
)
self
.
consumed
=
consumed
def
__reduce__
(
self
):
return
type
(
self
),
(
self
.
args
[
0
],
self
.
consumed
)
Lib/asyncio/futures.py
View file @
0baa72f4
"""A Future class similar to the one in PEP 3148."""
__all__
=
(
'CancelledError'
,
'TimeoutError'
,
'InvalidStateError'
,
'Future'
,
'wrap_future'
,
'isfuture'
,
)
...
...
@@ -12,12 +11,10 @@ import sys
from
.
import
base_futures
from
.
import
events
from
.
import
exceptions
from
.
import
format_helpers
CancelledError
=
base_futures
.
CancelledError
InvalidStateError
=
base_futures
.
InvalidStateError
TimeoutError
=
base_futures
.
TimeoutError
isfuture
=
base_futures
.
isfuture
...
...
@@ -170,9 +167,9 @@ class Future:
the future is done and has an exception set, this exception is raised.
"""
if
self
.
_state
==
_CANCELLED
:
raise
CancelledError
raise
exceptions
.
CancelledError
if
self
.
_state
!=
_FINISHED
:
raise
InvalidStateError
(
'Result is not ready.'
)
raise
exceptions
.
InvalidStateError
(
'Result is not ready.'
)
self
.
__log_traceback
=
False
if
self
.
_exception
is
not
None
:
raise
self
.
_exception
...
...
@@ -187,9 +184,9 @@ class Future:
InvalidStateError.
"""
if
self
.
_state
==
_CANCELLED
:
raise
CancelledError
raise
exceptions
.
CancelledError
if
self
.
_state
!=
_FINISHED
:
raise
InvalidStateError
(
'Exception is not set.'
)
raise
exceptions
.
InvalidStateError
(
'Exception is not set.'
)
self
.
__log_traceback
=
False
return
self
.
_exception
...
...
@@ -231,7 +228,7 @@ class Future:
InvalidStateError.
"""
if
self
.
_state
!=
_PENDING
:
raise
InvalidStateError
(
'{}: {!r}'
.
format
(
self
.
_state
,
self
)
)
raise
exceptions
.
InvalidStateError
(
f'
{
self
.
_state
}
:
{
self
!
r
}
'
)
self
.
_result
=
result
self
.
_state
=
_FINISHED
self
.
__schedule_callbacks
()
...
...
@@ -243,7 +240,7 @@ class Future:
InvalidStateError.
"""
if
self
.
_state
!=
_PENDING
:
raise
InvalidStateError
(
'{}: {!r}'
.
format
(
self
.
_state
,
self
)
)
raise
exceptions
.
InvalidStateError
(
f'
{
self
.
_state
}
:
{
self
!
r
}
'
)
if
isinstance
(
exception
,
type
):
exception
=
exception
()
if
type
(
exception
)
is
StopIteration
:
...
...
@@ -288,6 +285,18 @@ def _set_result_unless_cancelled(fut, result):
fut
.
set_result
(
result
)
def
_convert_future_exc
(
exc
):
exc_class
=
type
(
exc
)
if
exc_class
is
concurrent
.
futures
.
CancelledError
:
return
exceptions
.
CancelledError
(
*
exc
.
args
)
elif
exc_class
is
concurrent
.
futures
.
TimeoutError
:
return
exceptions
.
TimeoutError
(
*
exc
.
args
)
elif
exc_class
is
concurrent
.
futures
.
InvalidStateError
:
return
exceptions
.
InvalidStateError
(
*
exc
.
args
)
else
:
return
exc
def
_set_concurrent_future_state
(
concurrent
,
source
):
"""Copy state from a future to a concurrent.futures.Future."""
assert
source
.
done
()
...
...
@@ -297,7 +306,7 @@ def _set_concurrent_future_state(concurrent, source):
return
exception
=
source
.
exception
()
if
exception
is
not
None
:
concurrent
.
set_exception
(
exception
)
concurrent
.
set_exception
(
_convert_future_exc
(
exception
)
)
else
:
result
=
source
.
result
()
concurrent
.
set_result
(
result
)
...
...
@@ -317,7 +326,7 @@ def _copy_future_state(source, dest):
else
:
exception
=
source
.
exception
()
if
exception
is
not
None
:
dest
.
set_exception
(
exception
)
dest
.
set_exception
(
_convert_future_exc
(
exception
)
)
else
:
result
=
source
.
result
()
dest
.
set_result
(
result
)
...
...
Lib/asyncio/locks.py
View file @
0baa72f4
...
...
@@ -7,6 +7,7 @@ import warnings
from
.
import
events
from
.
import
futures
from
.
import
exceptions
from
.coroutines
import
coroutine
...
...
@@ -192,7 +193,7 @@ class Lock(_ContextManagerMixin):
await
fut
finally
:
self
.
_waiters
.
remove
(
fut
)
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
if
not
self
.
_locked
:
self
.
_wake_up_first
()
raise
...
...
@@ -363,11 +364,11 @@ class Condition(_ContextManagerMixin):
try
:
await
self
.
acquire
()
break
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
cancelled
=
True
if
cancelled
:
raise
future
s
.
CancelledError
raise
exception
s
.
CancelledError
async
def
wait_for
(
self
,
predicate
):
"""Wait until a predicate becomes true.
...
...
Lib/asyncio/proactor_events.py
View file @
0baa72f4
...
...
@@ -15,6 +15,7 @@ from . import base_events
from
.
import
constants
from
.
import
events
from
.
import
futures
from
.
import
exceptions
from
.
import
protocols
from
.
import
sslproto
from
.
import
transports
...
...
@@ -282,7 +283,7 @@ class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
self
.
_force_close
(
exc
)
except
OSError
as
exc
:
self
.
_fatal_error
(
exc
,
'Fatal read error on pipe transport'
)
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
if
not
self
.
_closing
:
raise
else
:
...
...
@@ -555,11 +556,11 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
try
:
fileno
=
file
.
fileno
()
except
(
AttributeError
,
io
.
UnsupportedOperation
)
as
err
:
raise
e
vent
s
.
SendfileNotAvailableError
(
"not a regular file"
)
raise
e
xception
s
.
SendfileNotAvailableError
(
"not a regular file"
)
try
:
fsize
=
os
.
fstat
(
fileno
).
st_size
except
OSError
as
err
:
raise
e
vent
s
.
SendfileNotAvailableError
(
"not a regular file"
)
raise
e
xception
s
.
SendfileNotAvailableError
(
"not a regular file"
)
blocksize
=
count
if
count
else
fsize
if
not
blocksize
:
return
0
# empty file
...
...
@@ -615,7 +616,7 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
if
f
is
not
None
:
f
.
result
()
# may raise
f
=
self
.
_proactor
.
recv
(
self
.
_ssock
,
4096
)
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
# _close_self_pipe() has been called, stop waiting for data
return
except
Exception
as
exc
:
...
...
@@ -666,7 +667,7 @@ class BaseProactorEventLoop(base_events.BaseEventLoop):
elif
self
.
_debug
:
logger
.
debug
(
"Accept failed on socket %r"
,
sock
,
exc_info
=
True
)
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
sock
.
close
()
else
:
self
.
_accept_futures
[
sock
.
fileno
()]
=
f
...
...
Lib/asyncio/streams.py
View file @
0baa72f4
__all__
=
(
'StreamReader'
,
'StreamWriter'
,
'StreamReaderProtocol'
,
'open_connection'
,
'start_server'
,
'IncompleteReadError'
,
'LimitOverrunError'
,
)
'open_connection'
,
'start_server'
)
import
socket
...
...
@@ -11,6 +9,7 @@ if hasattr(socket, 'AF_UNIX'):
from
.
import
coroutines
from
.
import
events
from
.
import
exceptions
from
.
import
protocols
from
.log
import
logger
from
.tasks
import
sleep
...
...
@@ -19,37 +18,6 @@ from .tasks import sleep
_DEFAULT_LIMIT
=
2
**
16
# 64 KiB
class
IncompleteReadError
(
EOFError
):
"""
Incomplete read error. Attributes:
- partial: read bytes string before the end of stream was reached
- expected: total number of expected bytes (or None if unknown)
"""
def
__init__
(
self
,
partial
,
expected
):
super
().
__init__
(
f'
{
len
(
partial
)
}
bytes read on a total of '
f'
{
expected
!
r
}
expected bytes'
)
self
.
partial
=
partial
self
.
expected
=
expected
def
__reduce__
(
self
):
return
type
(
self
),
(
self
.
partial
,
self
.
expected
)
class
LimitOverrunError
(
Exception
):
"""Reached the buffer limit while looking for a separator.
Attributes:
- consumed: total number of to be consumed bytes.
"""
def
__init__
(
self
,
message
,
consumed
):
super
().
__init__
(
message
)
self
.
consumed
=
consumed
def
__reduce__
(
self
):
return
type
(
self
),
(
self
.
args
[
0
],
self
.
consumed
)
async
def
open_connection
(
host
=
None
,
port
=
None
,
*
,
loop
=
None
,
limit
=
_DEFAULT_LIMIT
,
**
kwds
):
"""A wrapper for create_connection() returning a (reader, writer) pair.
...
...
@@ -494,9 +462,9 @@ class StreamReader:
seplen
=
len
(
sep
)
try
:
line
=
await
self
.
readuntil
(
sep
)
except
IncompleteReadError
as
e
:
except
exceptions
.
IncompleteReadError
as
e
:
return
e
.
partial
except
LimitOverrunError
as
e
:
except
exceptions
.
LimitOverrunError
as
e
:
if
self
.
_buffer
.
startswith
(
sep
,
e
.
consumed
):
del
self
.
_buffer
[:
e
.
consumed
+
seplen
]
else
:
...
...
@@ -571,7 +539,7 @@ class StreamReader:
# see upper comment for explanation.
offset
=
buflen
+
1
-
seplen
if
offset
>
self
.
_limit
:
raise
LimitOverrunError
(
raise
exceptions
.
LimitOverrunError
(
'Separator is not found, and chunk exceed the limit'
,
offset
)
...
...
@@ -582,13 +550,13 @@ class StreamReader:
if
self
.
_eof
:
chunk
=
bytes
(
self
.
_buffer
)
self
.
_buffer
.
clear
()
raise
IncompleteReadError
(
chunk
,
None
)
raise
exceptions
.
IncompleteReadError
(
chunk
,
None
)
# _wait_for_data() will resume reading if stream was paused.
await
self
.
_wait_for_data
(
'readuntil'
)
if
isep
>
self
.
_limit
:
raise
LimitOverrunError
(
raise
exceptions
.
LimitOverrunError
(
'Separator is found, but chunk is longer than limit'
,
isep
)
chunk
=
self
.
_buffer
[:
isep
+
seplen
]
...
...
@@ -674,7 +642,7 @@ class StreamReader:
if
self
.
_eof
:
incomplete
=
bytes
(
self
.
_buffer
)
self
.
_buffer
.
clear
()
raise
IncompleteReadError
(
incomplete
,
n
)
raise
exceptions
.
IncompleteReadError
(
incomplete
,
n
)
await
self
.
_wait_for_data
(
'readexactly'
)
...
...
Lib/asyncio/tasks.py
View file @
0baa72f4
...
...
@@ -21,6 +21,7 @@ import weakref
from
.
import
base_tasks
from
.
import
coroutines
from
.
import
events
from
.
import
exceptions
from
.
import
futures
from
.coroutines
import
coroutine
...
...
@@ -228,11 +229,11 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
def
__step
(
self
,
exc
=
None
):
if
self
.
done
():
raise
future
s
.
InvalidStateError
(
raise
exception
s
.
InvalidStateError
(
f'_step(): already done:
{
self
!
r
}
,
{
exc
!
r
}
'
)
if
self
.
_must_cancel
:
if
not
isinstance
(
exc
,
future
s
.
CancelledError
):
exc
=
future
s
.
CancelledError
()
if
not
isinstance
(
exc
,
exception
s
.
CancelledError
):
exc
=
exception
s
.
CancelledError
()
self
.
_must_cancel
=
False
coro
=
self
.
_coro
self
.
_fut_waiter
=
None
...
...
@@ -250,10 +251,10 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
if
self
.
_must_cancel
:
# Task is cancelled right before coro stops.
self
.
_must_cancel
=
False
super
().
set_exception
(
future
s
.
CancelledError
())
super
().
set_exception
(
exception
s
.
CancelledError
())
else
:
super
().
set_result
(
exc
.
value
)
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
super
().
cancel
()
# I.e., Future.cancel(self).
except
Exception
as
exc
:
super
().
set_exception
(
exc
)
...
...
@@ -419,7 +420,7 @@ async def wait_for(fut, timeout, *, loop=None):
return
fut
.
result
()
fut
.
cancel
()
raise
future
s
.
TimeoutError
()
raise
exception
s
.
TimeoutError
()
waiter
=
loop
.
create_future
()
timeout_handle
=
loop
.
call_later
(
timeout
,
_release_waiter
,
waiter
)
...
...
@@ -432,7 +433,7 @@ async def wait_for(fut, timeout, *, loop=None):
# wait until the future completes or the timeout
try
:
await
waiter
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
fut
.
remove_done_callback
(
cb
)
fut
.
cancel
()
raise
...
...
@@ -445,7 +446,7 @@ async def wait_for(fut, timeout, *, loop=None):
# after wait_for() returns.
# See https://bugs.python.org/issue32751
await
_cancel_and_wait
(
fut
,
loop
=
loop
)
raise
future
s
.
TimeoutError
()
raise
exception
s
.
TimeoutError
()
finally
:
timeout_handle
.
cancel
()
...
...
@@ -554,7 +555,7 @@ def as_completed(fs, *, loop=None, timeout=None):
f
=
await
done
.
get
()
if
f
is
None
:
# Dummy value from _on_timeout().
raise
future
s
.
TimeoutError
raise
exception
s
.
TimeoutError
return
f
.
result
()
# May raise f.exception().
for
f
in
todo
:
...
...
@@ -701,7 +702,7 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
# Check if 'fut' is cancelled first, as
# 'fut.exception()' will *raise* a CancelledError
# instead of returning it.
exc
=
future
s
.
CancelledError
()
exc
=
exception
s
.
CancelledError
()
outer
.
set_exception
(
exc
)
return
else
:
...
...
@@ -720,7 +721,7 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
# Check if 'fut' is cancelled first, as
# 'fut.exception()' will *raise* a CancelledError
# instead of returning it.
res
=
future
s
.
CancelledError
()
res
=
exception
s
.
CancelledError
()
else
:
res
=
fut
.
exception
()
if
res
is
None
:
...
...
@@ -731,7 +732,7 @@ def gather(*coros_or_futures, loop=None, return_exceptions=False):
# If gather is being cancelled we must propagate the
# cancellation regardless of *return_exceptions* argument.
# See issue 32684.
outer
.
set_exception
(
future
s
.
CancelledError
())
outer
.
set_exception
(
exception
s
.
CancelledError
())
else
:
outer
.
set_result
(
results
)
...
...
Lib/asyncio/unix_events.py
View file @
0baa72f4
...
...
@@ -18,6 +18,7 @@ from . import base_subprocess
from
.
import
constants
from
.
import
coroutines
from
.
import
events
from
.
import
exceptions
from
.
import
futures
from
.
import
selector_events
from
.
import
tasks
...
...
@@ -319,16 +320,16 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
try
:
os
.
sendfile
except
AttributeError
as
exc
:
raise
e
vent
s
.
SendfileNotAvailableError
(
raise
e
xception
s
.
SendfileNotAvailableError
(
"os.sendfile() is not available"
)
try
:
fileno
=
file
.
fileno
()
except
(
AttributeError
,
io
.
UnsupportedOperation
)
as
err
:
raise
e
vent
s
.
SendfileNotAvailableError
(
"not a regular file"
)
raise
e
xception
s
.
SendfileNotAvailableError
(
"not a regular file"
)
try
:
fsize
=
os
.
fstat
(
fileno
).
st_size
except
OSError
as
err
:
raise
e
vent
s
.
SendfileNotAvailableError
(
"not a regular file"
)
raise
e
xception
s
.
SendfileNotAvailableError
(
"not a regular file"
)
blocksize
=
count
if
count
else
fsize
if
not
blocksize
:
return
0
# empty file
...
...
@@ -382,7 +383,7 @@ class _UnixSelectorEventLoop(selector_events.BaseSelectorEventLoop):
# one being 'file' is not a regular mmap(2)-like
# file, in which case we'll fall back on using
# plain send().
err
=
e
vent
s
.
SendfileNotAvailableError
(
err
=
e
xception
s
.
SendfileNotAvailableError
(
"os.sendfile call failed"
)
self
.
_sock_sendfile_update_filepos
(
fileno
,
offset
,
total_sent
)
fut
.
set_exception
(
err
)
...
...
Lib/asyncio/windows_events.py
View file @
0baa72f4
...
...
@@ -12,6 +12,7 @@ import weakref
from
.
import
events
from
.
import
base_subprocess
from
.
import
futures
from
.
import
exceptions
from
.
import
proactor_events
from
.
import
selector_events
from
.
import
tasks
...
...
@@ -351,7 +352,7 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop):
elif
self
.
_debug
:
logger
.
warning
(
"Accept pipe failed on pipe %r"
,
pipe
,
exc_info
=
True
)
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
if
pipe
:
pipe
.
close
()
else
:
...
...
@@ -497,7 +498,7 @@ class IocpProactor:
# Coroutine closing the accept socket if the future is cancelled
try
:
await
future
except
future
s
.
CancelledError
:
except
exception
s
.
CancelledError
:
conn
.
close
()
raise
...
...
Lib/test/test_asyncio/test_base_events.py
View file @
0baa72f4
...
...
@@ -1946,7 +1946,7 @@ class BaseLoopSockSendfileTests(test_utils.TestCase):
def
test__sock_sendfile_native_failure
(
self
):
sock
,
proto
=
self
.
prepare
()
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"sendfile is not available"
):
self
.
run_loop
(
self
.
loop
.
_sock_sendfile_native
(
sock
,
self
.
file
,
0
,
None
))
...
...
@@ -1957,7 +1957,7 @@ class BaseLoopSockSendfileTests(test_utils.TestCase):
def
test_sock_sendfile_no_fallback
(
self
):
sock
,
proto
=
self
.
prepare
()
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"sendfile is not available"
):
self
.
run_loop
(
self
.
loop
.
sock_sendfile
(
sock
,
self
.
file
,
fallback
=
False
))
...
...
Lib/test/test_asyncio/test_events.py
View file @
0baa72f4
...
...
@@ -2393,7 +2393,7 @@ class SendfileMixin(SendfileBase):
self
.
loop
.
_sendfile_native
=
sendfile_native
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"not supported"
):
self
.
run_loop
(
self
.
loop
.
sendfile
(
cli_proto
.
transport
,
self
.
file
,
...
...
Lib/test/test_asyncio/test_proactor_events.py
View file @
0baa72f4
...
...
@@ -951,7 +951,7 @@ class ProactorEventLoopUnixSockSendfileTests(test_utils.TestCase):
def
test_sock_sendfile_not_a_file
(
self
):
sock
,
proto
=
self
.
prepare
()
f
=
object
()
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"not a regular file"
):
self
.
run_loop
(
self
.
loop
.
_sock_sendfile_native
(
sock
,
f
,
0
,
None
))
...
...
@@ -960,7 +960,7 @@ class ProactorEventLoopUnixSockSendfileTests(test_utils.TestCase):
def
test_sock_sendfile_iobuffer
(
self
):
sock
,
proto
=
self
.
prepare
()
f
=
io
.
BytesIO
()
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"not a regular file"
):
self
.
run_loop
(
self
.
loop
.
_sock_sendfile_native
(
sock
,
f
,
0
,
None
))
...
...
@@ -970,7 +970,7 @@ class ProactorEventLoopUnixSockSendfileTests(test_utils.TestCase):
sock
,
proto
=
self
.
prepare
()
f
=
mock
.
Mock
()
f
.
fileno
.
return_value
=
-
1
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"not a regular file"
):
self
.
run_loop
(
self
.
loop
.
_sock_sendfile_native
(
sock
,
f
,
0
,
None
))
...
...
Lib/test/test_asyncio/test_unix_events.py
View file @
0baa72f4
...
...
@@ -521,7 +521,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase):
def
test_sock_sendfile_not_available
(
self
):
sock
,
proto
=
self
.
prepare
()
with
mock
.
patch
(
'asyncio.unix_events.os'
,
spec
=
[]):
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"os[.]sendfile[(][)] is not available"
):
self
.
run_loop
(
self
.
loop
.
_sock_sendfile_native
(
sock
,
self
.
file
,
0
,
None
))
...
...
@@ -530,7 +530,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase):
def
test_sock_sendfile_not_a_file
(
self
):
sock
,
proto
=
self
.
prepare
()
f
=
object
()
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"not a regular file"
):
self
.
run_loop
(
self
.
loop
.
_sock_sendfile_native
(
sock
,
f
,
0
,
None
))
...
...
@@ -539,7 +539,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase):
def
test_sock_sendfile_iobuffer
(
self
):
sock
,
proto
=
self
.
prepare
()
f
=
io
.
BytesIO
()
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"not a regular file"
):
self
.
run_loop
(
self
.
loop
.
_sock_sendfile_native
(
sock
,
f
,
0
,
None
))
...
...
@@ -549,7 +549,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase):
sock
,
proto
=
self
.
prepare
()
f
=
mock
.
Mock
()
f
.
fileno
.
return_value
=
-
1
with
self
.
assertRaisesRegex
(
events
.
SendfileNotAvailableError
,
with
self
.
assertRaisesRegex
(
asyncio
.
SendfileNotAvailableError
,
"not a regular file"
):
self
.
run_loop
(
self
.
loop
.
_sock_sendfile_native
(
sock
,
f
,
0
,
None
))
...
...
@@ -605,7 +605,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase):
with
self
.
assertRaises
(
KeyError
):
self
.
loop
.
_selector
.
get_key
(
sock
)
exc
=
fut
.
exception
()
self
.
assertIsInstance
(
exc
,
events
.
SendfileNotAvailableError
)
self
.
assertIsInstance
(
exc
,
asyncio
.
SendfileNotAvailableError
)
self
.
assertEqual
(
0
,
self
.
file
.
tell
())
def
test_sock_sendfile_os_error_next_call
(
self
):
...
...
@@ -630,7 +630,7 @@ class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase):
fileno
=
self
.
file
.
fileno
()
fut
=
self
.
loop
.
create_future
()
err
=
events
.
SendfileNotAvailableError
()
err
=
asyncio
.
SendfileNotAvailableError
()
with
mock
.
patch
(
'os.sendfile'
,
side_effect
=
err
):
self
.
loop
.
_sock_sendfile_native_impl
(
fut
,
sock
.
fileno
(),
sock
,
fileno
,
...
...
Misc/NEWS.d/next/Library/2018-09-10-13-04-40.bpo-34622.tpv_rN.rst
0 → 100644
View file @
0baa72f4
Create a dedicated ``asyncio.CancelledError``, ``asyncio.InvalidStateError``
and ``asyncio.TimeoutError`` exception classes. Inherit them from
corresponding exceptions from ``concurrent.futures`` package. Extract
``asyncio`` exceptions into a separate file.
Modules/_asynciomodule.c
View file @
0baa72f4
...
...
@@ -3306,6 +3306,8 @@ module_init(void)
WITH_MOD
(
"asyncio.base_futures"
)
GET_MOD_ATTR
(
asyncio_future_repr_info_func
,
"_future_repr_info"
)
WITH_MOD
(
"asyncio.exceptions"
)
GET_MOD_ATTR
(
asyncio_InvalidStateError
,
"InvalidStateError"
)
GET_MOD_ATTR
(
asyncio_CancelledError
,
"CancelledError"
)
...
...
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