MCPcopy Create free account
hub / github.com/boostorg/cobalt / NB_MODULE

Function NB_MODULE

example/python.cpp:312–366  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

310
311
312NB_MODULE(boost_cobalt_example_python, m)
313{
314 namespace execution = asio::execution;
315 m.def("__rethrow_exception", &std::rethrow_exception);
316
317 py::class_<std::unique_ptr<asio::execution_context>>(m, "__asio__execution_context");
318
319 // use some inlined python to get a future
320 py::object locals = py::dict();
321 // language=python
322 PyRun_String(
323 R"(def __await_impl(self):
324 import asyncio
325 lp = asyncio.get_event_loop()
326 ft = lp.create_future()
327 self.initiate(lp, ft)
328 res = yield from ft
329 return res)", Py_single_input, PyEval_GetGlobals(), locals.ptr());
330 py::object await_impl = locals["__await_impl"];
331
332 // language=python
333 PyRun_String(
334 R"(async def __aiter_impl(self):
335 while not self.done:
336 yield await self
337)", Py_single_input, PyEval_GetGlobals(), locals.ptr());
338
339 py::object aiter_impl = locals["__aiter_impl"];
340
341 py::class_<py_coroutine> ct(m, "__cobalt_coroutine");
342 ct.def("initiate", &py_coroutine::initiate)
343 .def_prop_ro("done", &py_coroutine::done);
344 setattr(ct, "__await__", await_impl);
345 setattr(ct, "__aiter__", aiter_impl);
346
347 m.def("test_generator",
348 []() -> py_coroutine
349 {
350 BOOST_COBALT_FOR(auto v, test_generator())
351 co_yield v;
352 co_return py::none();
353 });
354
355 m.def("test_promise",
356 []() -> py_coroutine
357 {
358 co_return co_await test_promise();
359 });
360 m.def("test_py_promise",
361 [](py::object obj) -> py_coroutine
362 {
363 co_await await_py_coroutine(std::move(obj));
364 co_return py::none();
365 });
366}
367

Callers

nothing calls this directly

Calls 2

test_promiseFunction · 0.85
await_py_coroutineFunction · 0.85

Tested by

no test coverage detected