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
11161056
Commit
11161056
authored
May 11, 2015
by
Yury Selivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sync asyncio changes from the main repo.
parent
62b39bd5
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
94 additions
and
16 deletions
+94
-16
Lib/asyncio/base_events.py
Lib/asyncio/base_events.py
+33
-11
Lib/asyncio/events.py
Lib/asyncio/events.py
+9
-1
Lib/test/test_asyncio/test_base_events.py
Lib/test/test_asyncio/test_base_events.py
+43
-2
Lib/test/test_asyncio/test_tasks.py
Lib/test/test_asyncio/test_tasks.py
+7
-2
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Lib/asyncio/base_events.py
View file @
11161056
...
...
@@ -197,6 +197,7 @@ class BaseEventLoop(events.AbstractEventLoop):
# exceed this duration in seconds, the slow callback/task is logged.
self
.
slow_callback_duration
=
0.1
self
.
_current_handle
=
None
self
.
_task_factory
=
None
def
__repr__
(
self
):
return
(
'<%s running=%s closed=%s debug=%s>'
...
...
@@ -209,11 +210,32 @@ class BaseEventLoop(events.AbstractEventLoop):
Return a task object.
"""
self
.
_check_closed
()
task
=
tasks
.
Task
(
coro
,
loop
=
self
)
if
task
.
_source_traceback
:
del
task
.
_source_traceback
[
-
1
]
if
self
.
_task_factory
is
None
:
task
=
tasks
.
Task
(
coro
,
loop
=
self
)
if
task
.
_source_traceback
:
del
task
.
_source_traceback
[
-
1
]
else
:
task
=
self
.
_task_factory
(
self
,
coro
)
return
task
def
set_task_factory
(
self
,
factory
):
"""Set a task factory that will be used by loop.create_task().
If factory is None the default task factory will be set.
If factory is a callable, it should have a signature matching
'(loop, coro)', where 'loop' will be a reference to the active
event loop, 'coro' will be a coroutine object. The callable
must return a Future.
"""
if
factory
is
not
None
and
not
callable
(
factory
):
raise
TypeError
(
'task factory must be a callable or None'
)
self
.
_task_factory
=
factory
def
get_task_factory
(
self
):
"""Return a task factory, or None if the default one is in use."""
return
self
.
_task_factory
def
_make_socket_transport
(
self
,
sock
,
protocol
,
waiter
=
None
,
*
,
extra
=
None
,
server
=
None
):
"""Create socket transport."""
...
...
@@ -465,25 +487,25 @@ class BaseEventLoop(events.AbstractEventLoop):
self
.
_write_to_self
()
return
handle
def
run_in_executor
(
self
,
executor
,
callback
,
*
args
):
if
(
coroutines
.
iscoroutine
(
callback
)
or
coroutines
.
iscoroutinefunction
(
callback
)):
def
run_in_executor
(
self
,
executor
,
func
,
*
args
):
if
(
coroutines
.
iscoroutine
(
func
)
or
coroutines
.
iscoroutinefunction
(
func
)):
raise
TypeError
(
"coroutines cannot be used with run_in_executor()"
)
self
.
_check_closed
()
if
isinstance
(
callback
,
events
.
Handle
):
if
isinstance
(
func
,
events
.
Handle
):
assert
not
args
assert
not
isinstance
(
callback
,
events
.
TimerHandle
)
if
callback
.
_cancelled
:
assert
not
isinstance
(
func
,
events
.
TimerHandle
)
if
func
.
_cancelled
:
f
=
futures
.
Future
(
loop
=
self
)
f
.
set_result
(
None
)
return
f
callback
,
args
=
callback
.
_callback
,
callback
.
_args
func
,
args
=
func
.
_callback
,
func
.
_args
if
executor
is
None
:
executor
=
self
.
_default_executor
if
executor
is
None
:
executor
=
concurrent
.
futures
.
ThreadPoolExecutor
(
_MAX_WORKERS
)
self
.
_default_executor
=
executor
return
futures
.
wrap_future
(
executor
.
submit
(
callback
,
*
args
),
loop
=
self
)
return
futures
.
wrap_future
(
executor
.
submit
(
func
,
*
args
),
loop
=
self
)
def
set_default_executor
(
self
,
executor
):
self
.
_default_executor
=
executor
...
...
Lib/asyncio/events.py
View file @
11161056
...
...
@@ -277,7 +277,7 @@ class AbstractEventLoop:
def
call_soon_threadsafe
(
self
,
callback
,
*
args
):
raise
NotImplementedError
def
run_in_executor
(
self
,
executor
,
callback
,
*
args
):
def
run_in_executor
(
self
,
executor
,
func
,
*
args
):
raise
NotImplementedError
def
set_default_executor
(
self
,
executor
):
...
...
@@ -438,6 +438,14 @@ class AbstractEventLoop:
def
remove_signal_handler
(
self
,
sig
):
raise
NotImplementedError
# Task factory.
def
set_task_factory
(
self
,
factory
):
raise
NotImplementedError
def
get_task_factory
(
self
):
raise
NotImplementedError
# Error handlers.
def
set_exception_handler
(
self
,
handler
):
...
...
Lib/test/test_asyncio/test_base_events.py
View file @
11161056
...
...
@@ -16,10 +16,15 @@ from asyncio import constants
from
asyncio
import
test_utils
try
:
from
test
import
support
from
test.script_helper
import
assert_python_ok
except
ImportError
:
from
asyncio
import
test_support
as
support
from
asyncio.test_support
import
assert_python_ok
try
:
from
test.support.script_helper
import
assert_python_ok
except
ImportError
:
try
:
from
test.script_helper
import
assert_python_ok
except
ImportError
:
from
asyncio.test_support
import
assert_python_ok
MOCK_ANY
=
mock
.
ANY
...
...
@@ -623,6 +628,42 @@ class BaseEventLoopTests(test_utils.TestCase):
self
.
assertIs
(
type
(
_context
[
'context'
][
'exception'
]),
ZeroDivisionError
)
def
test_set_task_factory_invalid
(
self
):
with
self
.
assertRaisesRegex
(
TypeError
,
'task factory must be a callable or None'
):
self
.
loop
.
set_task_factory
(
1
)
self
.
assertIsNone
(
self
.
loop
.
get_task_factory
())
def
test_set_task_factory
(
self
):
self
.
loop
.
_process_events
=
mock
.
Mock
()
class
MyTask
(
asyncio
.
Task
):
pass
@
asyncio
.
coroutine
def
coro
():
pass
factory
=
lambda
loop
,
coro
:
MyTask
(
coro
,
loop
=
loop
)
self
.
assertIsNone
(
self
.
loop
.
get_task_factory
())
self
.
loop
.
set_task_factory
(
factory
)
self
.
assertIs
(
self
.
loop
.
get_task_factory
(),
factory
)
task
=
self
.
loop
.
create_task
(
coro
())
self
.
assertTrue
(
isinstance
(
task
,
MyTask
))
self
.
loop
.
run_until_complete
(
task
)
self
.
loop
.
set_task_factory
(
None
)
self
.
assertIsNone
(
self
.
loop
.
get_task_factory
())
task
=
self
.
loop
.
create_task
(
coro
())
self
.
assertTrue
(
isinstance
(
task
,
asyncio
.
Task
))
self
.
assertFalse
(
isinstance
(
task
,
MyTask
))
self
.
loop
.
run_until_complete
(
task
)
def
test_env_var_debug
(
self
):
code
=
'
\
n
'
.
join
((
'import asyncio'
,
...
...
Lib/test/test_asyncio/test_tasks.py
View file @
11161056
...
...
@@ -15,10 +15,15 @@ from asyncio import coroutines
from
asyncio
import
test_utils
try
:
from
test
import
support
from
test.script_helper
import
assert_python_ok
except
ImportError
:
from
asyncio
import
test_support
as
support
from
asyncio.test_support
import
assert_python_ok
try
:
from
test.support.script_helper
import
assert_python_ok
except
ImportError
:
try
:
from
test.script_helper
import
assert_python_ok
except
ImportError
:
from
asyncio.test_support
import
assert_python_ok
PY34
=
(
sys
.
version_info
>=
(
3
,
4
))
...
...
Misc/NEWS
View file @
11161056
...
...
@@ -42,6 +42,8 @@ Core and Builtins
- Issue #21354: PyCFunction_New function is exposed by python DLL again.
- asyncio: New event loop APIs: set_task_factory() and get_task_factory()
Library
-------
...
...
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