Skip to content

Commit 35982d2

Browse files
committed
[mypyc] Only generate an export table if using separate compilation
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 2e5d7ee commit 35982d2

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
@@ -652,7 +652,8 @@ def generate_c_for_modules(self) -> list[tuple[str, str]]:
652652
decls.emit_lines(*declaration.decl)
653653

654654
if self.group_name:
655-
self.generate_export_table(ext_declarations, emitter)
655+
if self.compiler_options.separate:
656+
self.generate_export_table(ext_declarations, emitter)
656657

657658
self.generate_shared_lib_init(emitter)
658659

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

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

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

0 commit comments

Comments
 (0)