test_async.cpp 864 B

1234567891011121314151617181920212223242526
  1. /*
  2. tests/test_async.cpp -- __await__ support
  3. Copyright (c) 2019 Google Inc.
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. TEST_SUBMODULE(async_module, m) {
  9. struct DoesNotSupportAsync {};
  10. py::class_<DoesNotSupportAsync>(m, "DoesNotSupportAsync")
  11. .def(py::init<>());
  12. struct SupportsAsync {};
  13. py::class_<SupportsAsync>(m, "SupportsAsync")
  14. .def(py::init<>())
  15. .def("__await__", [](const SupportsAsync& self) -> py::object {
  16. static_cast<void>(self);
  17. py::object loop = py::module_::import("asyncio.events").attr("get_event_loop")();
  18. py::object f = loop.attr("create_future")();
  19. f.attr("set_result")(5);
  20. return f.attr("__await__")();
  21. });
  22. }