Add support for __await__, __aiter__, and __anext__ protocols (#1842)

This commit is contained in:
Jeremy Maitin-Shepard
2019-07-18 00:02:35 -07:00
committed by Wenzel Jakob
parent 9b3fb05326
commit a3f4a0e8ab
5 changed files with 65 additions and 0 deletions

26
tests/test_async.cpp Normal file
View File

@@ -0,0 +1,26 @@
/*
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__")();
});
}