Skip to content

Commit 3a2b788

Browse files
authored
[mypyc] Only generate an export table if using separate compilation (#19521)
When not using separate compilation, the export table is not used. Also, there's actually no simple and safe way to use it without separate compilation, since we'd have to first ensure that the structure of the export table is compatible, as otherwise the order of fields or the types of the fields could be incompatible and cause segfaults and other fun stuff.
1 parent 0c1f104 commit 3a2b788

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

mypyc/codegen/emitmodule.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,8 @@ def generate_c_for_modules(self) -> list[tuple[str, str]]:
651651
decls.emit_lines(*declaration.decl)
652652

653653
if self.group_name:
654-
self.generate_export_table(ext_declarations, emitter)
654+
if self.compiler_options.separate:
655+
self.generate_export_table(ext_declarations, emitter)
655656

656657
self.generate_shared_lib_init(emitter)
657658

@@ -808,20 +809,21 @@ def generate_shared_lib_init(self, emitter: Emitter) -> None:
808809
"",
809810
)
810811

811-
emitter.emit_lines(
812-
'capsule = PyCapsule_New(&exports, "{}.exports", NULL);'.format(
813-
shared_lib_name(self.group_name)
814-
),
815-
"if (!capsule) {",
816-
"goto fail;",
817-
"}",
818-
'res = PyObject_SetAttrString(module, "exports", capsule);',
819-
"Py_DECREF(capsule);",
820-
"if (res < 0) {",
821-
"goto fail;",
822-
"}",
823-
"",
824-
)
812+
if self.compiler_options.separate:
813+
emitter.emit_lines(
814+
'capsule = PyCapsule_New(&exports, "{}.exports", NULL);'.format(
815+
shared_lib_name(self.group_name)
816+
),
817+
"if (!capsule) {",
818+
"goto fail;",
819+
"}",
820+
'res = PyObject_SetAttrString(module, "exports", capsule);',
821+
"Py_DECREF(capsule);",
822+
"if (res < 0) {",
823+
"goto fail;",
824+
"}",
825+
"",
826+
)
825827

826828
for mod in self.modules:
827829
name = exported_name(mod)

0 commit comments

Comments
 (0)