MCPcopy Index your code
hub / github.com/python/mypy / emit_module_init_func

Method emit_module_init_func

mypyc/codegen/emitmodule.py:1351–1450  ·  view source on GitHub ↗
(
        self, emitter: Emitter, module_name: str, module_prefix: str
    )

Source from the content-addressed store, hash-verified

1349 emitter.emit_line("")
1350
1351 def emit_module_init_func(
1352 self, emitter: Emitter, module_name: str, module_prefix: str
1353 ) -> None:
1354 if not self.use_shared_lib:
1355 declaration = f"PyMODINIT_FUNC PyInit_{module_name}(void)"
1356 else:
1357 n = f"CPyInit_{exported_name(module_name)}"
1358 declaration = f"PyObject *{n}(void)"
1359 emitter.context.declarations[n] = HeaderDeclaration(declaration + ";")
1360
1361 if self.multi_phase_init:
1362 emitter.emit_lines(declaration, "{")
1363 def_name = f"{module_prefix}module"
1364 emitter.emit_line(f"return PyModuleDef_Init(&{def_name});")
1365 emitter.emit_line("}")
1366 return
1367
1368 exec_func = f"CPyExec_{exported_name(module_name)}"
1369
1370 if self.use_shared_lib:
1371 self.emit_init_only_func(emitter, module_name, module_prefix)
1372
1373 # Emit CPyInit_* / PyInit_* which creates the module and executes the body.
1374 emitter.emit_lines(declaration, "{")
1375 module_static = self.module_internal_static_name(module_name, emitter)
1376
1377 emitter.emit_line("PyObject* modname = NULL;")
1378 emitter.emit_lines(
1379 f"if ({module_static}) {{",
1380 f"Py_INCREF({module_static});",
1381 f"return {module_static};",
1382 "}",
1383 )
1384
1385 emitter.emit_lines(
1386 f"{module_static} = PyModule_Create(&{module_prefix}module);",
1387 f"if (unlikely({module_static} == NULL))",
1388 " goto fail;",
1389 )
1390
1391 emitter.emit_line(f'modname = PyUnicode_FromString("{module_name}");')
1392 emitter.emit_line("if (modname == NULL) CPyError_OutOfMemory();")
1393 emitter.emit_line("int rv = 0;")
1394 if self.group_name:
1395 shared_lib_mod_name = shared_lib_name(self.group_name)
1396 emitter.emit_line("PyObject *mod_dict = PyImport_GetModuleDict();")
1397 emitter.emit_line("PyObject *shared_lib = NULL;")
1398 emitter.emit_line(
1399 f'rv = PyDict_GetItemStringRef(mod_dict, "{shared_lib_mod_name}", &shared_lib);'
1400 )
1401 emitter.emit_line("if (rv < 0) goto fail;")
1402 emitter.emit_line(
1403 'PyObject *shared_lib_file = PyObject_GetAttrString(shared_lib, "__file__");'
1404 )
1405 emitter.emit_line("if (shared_lib_file == NULL) goto fail;")
1406 else:
1407 emitter.emit_line(
1408 f'PyObject *shared_lib_file = PyUnicode_FromString("{module_name + EXT_SUFFIX}");'

Callers 1

generate_module_defMethod · 0.95

Calls 9

emit_init_only_funcMethod · 0.95
exported_nameFunction · 0.90
HeaderDeclarationClass · 0.90
shared_lib_nameFunction · 0.90
intClass · 0.85
emit_linesMethod · 0.45
emit_lineMethod · 0.45
endswithMethod · 0.45

Tested by

no test coverage detected