Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Getting capsule pointer from module instead of PyCapsule_Import. #18286

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mypyc/lib-rt/module_shim.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ PyInit_{modname}(void)
{{
PyObject *tmp;
if (!(tmp = PyImport_ImportModule("{libname}"))) return NULL;
PyObject *capsule = PyObject_GetAttrString(tmp, "init_{full_modname}");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for null return. Otherwise we may decref a null pointer below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it. Thanks.

Py_DECREF(tmp);
void *init_func = PyCapsule_Import("{libname}.init_{full_modname}", 0);
void *init_func = PyCapsule_GetPointer(capsule, "{libname}.init_{full_modname}");
Py_DECREF(capsule);
if (!init_func) {{
return NULL;
}}
Expand Down
19 changes: 19 additions & 0 deletions mypyc/test-data/commandline.test
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,22 @@ def i(arg: Foo) -> None:

[file test.py]
names = (str(v) for v in [1, 2, 3]) # W: Treating generator comprehension as list

[case testSubPackage]
# cmd: pkg/sub/foo.py
from pkg.sub import foo

[file pkg/__init__.py]

[file pkg/sub/__init__.py]
print("importing...")
from . import foo
print("done")

[file pkg/sub/foo.py]
print("imported foo")

[out]
importing...
imported foo
done
Loading