Emit insertion of coroutines into the module dict when the module is initialized. Coroutines are wrapped in CPyFunction objects to enable introspection by functions like inspect.iscoroutinefunction(fn).
(self, emitter: Emitter, module: ModuleIR, globals: str)
| 1198 | emitter.emit_line() |
| 1199 | |
| 1200 | def emit_coroutine_wrappers(self, emitter: Emitter, module: ModuleIR, globals: str) -> None: |
| 1201 | """Emit insertion of coroutines into the module dict when the module is initialized. |
| 1202 | Coroutines are wrapped in CPyFunction objects to enable introspection by functions like |
| 1203 | inspect.iscoroutinefunction(fn). |
| 1204 | """ |
| 1205 | for fn in module.functions: |
| 1206 | if fn.class_name is not None or fn.name == TOP_LEVEL_NAME: |
| 1207 | continue |
| 1208 | if not fn.decl.is_coroutine: |
| 1209 | continue |
| 1210 | |
| 1211 | filepath = self.source_paths[module.fullname] |
| 1212 | error_stmt = " goto fail;" |
| 1213 | name = short_id_from_name(fn.name, fn.decl.shortname, fn.line) |
| 1214 | wrapper_name = emitter.emit_cpyfunction_instance(fn, name, filepath, error_stmt) |
| 1215 | name_obj = f"{wrapper_name}_name" |
| 1216 | emitter.emit_line(f'PyObject *{name_obj} = PyUnicode_FromString("{fn.name}");') |
| 1217 | emitter.emit_line(f"if (unlikely(!{name_obj}))") |
| 1218 | emitter.emit_line(error_stmt) |
| 1219 | emitter.emit_line( |
| 1220 | f"if (PyDict_SetItem({globals}, {name_obj}, (PyObject *){wrapper_name}) < 0)" |
| 1221 | ) |
| 1222 | emitter.emit_line(error_stmt) |
| 1223 | |
| 1224 | def emit_module_exec_func( |
| 1225 | self, emitter: Emitter, module_name: str, module_prefix: str, module: ModuleIR |
no test coverage detected