Skip to content

Commit f445369

Browse files
authored
[mypyc] Getting capsule pointer from module instead of PyCapsule_Import (#18286)
Fixes mypyc/mypyc#999. `PyCapsule_Import` was failing in sub-packages. Since the capsule is an attribute of the module, we can access the capsule from the module instead of importing it.
1 parent 823c0e5 commit f445369

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

mypyc/lib-rt/module_shim.tmpl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ PyInit_{modname}(void)
55
{{
66
PyObject *tmp;
77
if (!(tmp = PyImport_ImportModule("{libname}"))) return NULL;
8+
PyObject *capsule = PyObject_GetAttrString(tmp, "init_{full_modname}");
89
Py_DECREF(tmp);
9-
void *init_func = PyCapsule_Import("{libname}.init_{full_modname}", 0);
10+
if (capsule == NULL) return NULL;
11+
void *init_func = PyCapsule_GetPointer(capsule, "{libname}.init_{full_modname}");
12+
Py_DECREF(capsule);
1013
if (!init_func) {{
1114
return NULL;
1215
}}

mypyc/test-data/commandline.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,22 @@ def i(arg: Foo) -> None:
243243

244244
[file test.py]
245245
names = (str(v) for v in [1, 2, 3]) # W: Treating generator comprehension as list
246+
247+
[case testSubPackage]
248+
# cmd: pkg/sub/foo.py
249+
from pkg.sub import foo
250+
251+
[file pkg/__init__.py]
252+
253+
[file pkg/sub/__init__.py]
254+
print("importing...")
255+
from . import foo
256+
print("done")
257+
258+
[file pkg/sub/foo.py]
259+
print("imported foo")
260+
261+
[out]
262+
importing...
263+
imported foo
264+
done

0 commit comments

Comments
 (0)