(
cl: ClassIR, coroutine_setup_name: str, module_name: str, emitter: Emitter
)
| 1306 | |
| 1307 | |
| 1308 | def generate_coroutine_setup( |
| 1309 | cl: ClassIR, coroutine_setup_name: str, module_name: str, emitter: Emitter |
| 1310 | ) -> None: |
| 1311 | emitter.emit_line("static bool") |
| 1312 | emitter.emit_line(f"{NATIVE_PREFIX}{coroutine_setup_name}(PyObject *type)") |
| 1313 | emitter.emit_line("{") |
| 1314 | |
| 1315 | error_stmt = " return 2;" |
| 1316 | |
| 1317 | def emit_instance(fn: FuncIR, fn_name: str) -> str: |
| 1318 | filepath = emitter.filepath or "" |
| 1319 | return emitter.emit_cpyfunction_instance(fn, fn_name, filepath, error_stmt) |
| 1320 | |
| 1321 | def success() -> None: |
| 1322 | emitter.emit_line("return 1;") |
| 1323 | emitter.emit_line("}") |
| 1324 | |
| 1325 | if cl.coroutine_name: |
| 1326 | # Callable class generated for a coroutine. It stores its function wrapper as an attribute. |
| 1327 | wrapper_name = emit_instance(cl.methods["__call__"], cl.coroutine_name) |
| 1328 | struct_name = cl.struct_name(emitter.names) |
| 1329 | attr = emitter.attr(CPYFUNCTION_NAME) |
| 1330 | emitter.emit_line(f"(({struct_name} *)type)->{attr} = {wrapper_name};") |
| 1331 | return success() |
| 1332 | |
| 1333 | if not any(fn.decl.is_coroutine for fn in cl.methods.values()): |
| 1334 | return success() |
| 1335 | |
| 1336 | emitter.emit_line("PyTypeObject *tp = (PyTypeObject *)type;") |
| 1337 | |
| 1338 | for fn in cl.methods.values(): |
| 1339 | if not fn.decl.is_coroutine: |
| 1340 | continue |
| 1341 | |
| 1342 | name = short_id_from_name(fn.name, fn.decl.shortname, fn.line) |
| 1343 | wrapper_name = emit_instance(fn, name) |
| 1344 | name_obj = f"{wrapper_name}_name" |
| 1345 | emitter.emit_line(f'PyObject *{name_obj} = PyUnicode_FromString("{fn.name}");') |
| 1346 | emitter.emit_line(f"if (unlikely(!{name_obj}))") |
| 1347 | emitter.emit_line(error_stmt) |
| 1348 | emitter.emit_line(f"if (PyDict_SetItem(tp->tp_dict, {name_obj}, {wrapper_name}) < 0)") |
| 1349 | emitter.emit_line(error_stmt) |
| 1350 | |
| 1351 | return success() |
no test coverage detected
searching dependent graphs…