Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
a3f4a0e8
Commit
a3f4a0e8
authored
Jul 18, 2019
by
Jeremy Maitin-Shepard
Committed by
Wenzel Jakob
Jul 18, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for __await__, __aiter__, and __anext__ protocols (#1842)
parent
9b3fb053
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
65 additions
and
0 deletions
+65
-0
include/pybind11/detail/class.h
+3
-0
tests/CMakeLists.txt
+8
-0
tests/conftest.py
+5
-0
tests/test_async.cpp
+26
-0
tests/test_async.py
+23
-0
No files found.
include/pybind11/detail/class.h
View file @
a3f4a0e8
...
@@ -586,6 +586,9 @@ inline PyObject* make_new_python_type(const type_record &rec) {
...
@@ -586,6 +586,9 @@ inline PyObject* make_new_python_type(const type_record &rec) {
type
->
tp_as_number
=
&
heap_type
->
as_number
;
type
->
tp_as_number
=
&
heap_type
->
as_number
;
type
->
tp_as_sequence
=
&
heap_type
->
as_sequence
;
type
->
tp_as_sequence
=
&
heap_type
->
as_sequence
;
type
->
tp_as_mapping
=
&
heap_type
->
as_mapping
;
type
->
tp_as_mapping
=
&
heap_type
->
as_mapping
;
#if PY_VERSION_HEX >= 0x03050000
type
->
tp_as_async
=
&
heap_type
->
as_async
;
#endif
/* Flags */
/* Flags */
type
->
tp_flags
|=
Py_TPFLAGS_DEFAULT
|
Py_TPFLAGS_BASETYPE
|
Py_TPFLAGS_HEAPTYPE
;
type
->
tp_flags
|=
Py_TPFLAGS_DEFAULT
|
Py_TPFLAGS_BASETYPE
|
Py_TPFLAGS_HEAPTYPE
;
...
...
tests/CMakeLists.txt
View file @
a3f4a0e8
...
@@ -26,6 +26,7 @@ endif()
...
@@ -26,6 +26,7 @@ endif()
# Full set of test files (you can override these; see below)
# Full set of test files (you can override these; see below)
set
(
PYBIND11_TEST_FILES
set
(
PYBIND11_TEST_FILES
test_async.cpp
test_buffers.cpp
test_buffers.cpp
test_builtin_casters.cpp
test_builtin_casters.cpp
test_call_policies.cpp
test_call_policies.cpp
...
@@ -71,6 +72,13 @@ if (PYBIND11_TEST_OVERRIDE)
...
@@ -71,6 +72,13 @@ if (PYBIND11_TEST_OVERRIDE)
set
(
PYBIND11_TEST_FILES
${
PYBIND11_TEST_OVERRIDE
}
)
set
(
PYBIND11_TEST_FILES
${
PYBIND11_TEST_OVERRIDE
}
)
endif
()
endif
()
# Skip test_async for Python < 3.5
list
(
FIND PYBIND11_TEST_FILES test_async.cpp PYBIND11_TEST_FILES_ASYNC_I
)
if
((
PYBIND11_TEST_FILES_ASYNC_I GREATER -1
)
AND
(
"
${
PYTHON_VERSION_MAJOR
}
.
${
PYTHON_VERSION_MINOR
}
"
VERSION_LESS 3.5
))
message
(
STATUS
"Skipping test_async because Python version
${
PYTHON_VERSION_MAJOR
}
.
${
PYTHON_VERSION_MINOR
}
< 3.5"
)
list
(
REMOVE_AT PYBIND11_TEST_FILES
${
PYBIND11_TEST_FILES_ASYNC_I
}
)
endif
()
string
(
REPLACE
".cpp"
".py"
PYBIND11_PYTEST_FILES
"
${
PYBIND11_TEST_FILES
}
"
)
string
(
REPLACE
".cpp"
".py"
PYBIND11_PYTEST_FILES
"
${
PYBIND11_TEST_FILES
}
"
)
# Contains the set of test files that require pybind11_cross_module_tests to be
# Contains the set of test files that require pybind11_cross_module_tests to be
...
...
tests/conftest.py
View file @
a3f4a0e8
...
@@ -17,6 +17,11 @@ _unicode_marker = re.compile(r'u(\'[^\']*\')')
...
@@ -17,6 +17,11 @@ _unicode_marker = re.compile(r'u(\'[^\']*\')')
_long_marker
=
re
.
compile
(
r'([0-9])L'
)
_long_marker
=
re
.
compile
(
r'([0-9])L'
)
_hexadecimal
=
re
.
compile
(
r'0x[0-9a-fA-F]+'
)
_hexadecimal
=
re
.
compile
(
r'0x[0-9a-fA-F]+'
)
# test_async.py requires support for async and await
collect_ignore
=
[]
if
sys
.
version_info
[:
2
]
<
(
3
,
5
):
collect_ignore
.
append
(
"test_async.py"
)
def
_strip_and_dedent
(
s
):
def
_strip_and_dedent
(
s
):
"""For triple-quote strings"""
"""For triple-quote strings"""
...
...
tests/test_async.cpp
0 → 100644
View file @
a3f4a0e8
/*
tests/test_async.cpp -- __await__ support
Copyright (c) 2019 Google Inc.
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/
#include "pybind11_tests.h"
TEST_SUBMODULE
(
async_module
,
m
)
{
struct
DoesNotSupportAsync
{};
py
::
class_
<
DoesNotSupportAsync
>
(
m
,
"DoesNotSupportAsync"
)
.
def
(
py
::
init
<>
());
struct
SupportsAsync
{};
py
::
class_
<
SupportsAsync
>
(
m
,
"SupportsAsync"
)
.
def
(
py
::
init
<>
())
.
def
(
"__await__"
,
[](
const
SupportsAsync
&
self
)
->
py
::
object
{
static_cast
<
void
>
(
self
);
py
::
object
loop
=
py
::
module
::
import
(
"asyncio.events"
).
attr
(
"get_event_loop"
)();
py
::
object
f
=
loop
.
attr
(
"create_future"
)();
f
.
attr
(
"set_result"
)(
5
);
return
f
.
attr
(
"__await__"
)();
});
}
tests/test_async.py
0 → 100644
View file @
a3f4a0e8
import
asyncio
import
pytest
from
pybind11_tests
import
async_module
as
m
@pytest.fixture
def
event_loop
():
loop
=
asyncio
.
new_event_loop
()
yield
loop
loop
.
close
()
async
def
get_await_result
(
x
):
return
await
x
def
test_await
(
event_loop
):
assert
5
==
event_loop
.
run_until_complete
(
get_await_result
(
m
.
SupportsAsync
()))
def
test_await_missing
(
event_loop
):
with
pytest
.
raises
(
TypeError
):
event_loop
.
run_until_complete
(
get_await_result
(
m
.
DoesNotSupportAsync
()))
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