Emit CPyInitOnly_* which creates the module object without executing the body. This allows the caller to set up attributes like __file__ and __package__ before the module body runs. Used for same-group native imports.
(self, emitter: Emitter, module_name: str, module_prefix: str)
| 1325 | emitter.emit_line("}") |
| 1326 | |
| 1327 | def emit_init_only_func(self, emitter: Emitter, module_name: str, module_prefix: str) -> None: |
| 1328 | """Emit CPyInitOnly_* which creates the module object without executing the body. |
| 1329 | |
| 1330 | This allows the caller to set up attributes like __file__ and __package__ |
| 1331 | before the module body runs. Used for same-group native imports. |
| 1332 | """ |
| 1333 | init_only_name = f"CPyInitOnly_{exported_name(module_name)}" |
| 1334 | init_only_decl = f"PyObject *{init_only_name}(void)" |
| 1335 | emitter.context.declarations[init_only_name] = HeaderDeclaration(init_only_decl + ";") |
| 1336 | module_static = self.module_internal_static_name(module_name, emitter) |
| 1337 | emitter.emit_lines(init_only_decl, "{") |
| 1338 | emitter.emit_lines( |
| 1339 | f"if ({module_static}) {{", |
| 1340 | f"Py_INCREF({module_static});", |
| 1341 | f"return {module_static};", |
| 1342 | "}", |
| 1343 | ) |
| 1344 | emitter.emit_lines( |
| 1345 | f"{module_static} = PyModule_Create(&{module_prefix}module);", |
| 1346 | f"return {module_static};", |
| 1347 | ) |
| 1348 | emitter.emit_lines("}") |
| 1349 | emitter.emit_line("") |
| 1350 | |
| 1351 | def emit_module_init_func( |
| 1352 | self, emitter: Emitter, module_name: str, module_prefix: str |
no test coverage detected