-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
[mypyc] Set dunder attrs when adding module to sys.modules #21275
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ | |
| ) | ||
| from mypyc.codegen.literals import Literals | ||
| from mypyc.common import ( | ||
| EXT_SUFFIX, | ||
| IS_FREE_THREADED, | ||
| MODULE_PREFIX, | ||
| PREFIX, | ||
|
|
@@ -1286,11 +1287,39 @@ def emit_module_init_func( | |
| f"if (unlikely({module_static} == NULL))", | ||
| " goto fail;", | ||
| ) | ||
|
|
||
| emitter.emit_line(f'modname = PyUnicode_FromString("{module_name}");') | ||
| emitter.emit_line("if (modname == NULL) CPyError_OutOfMemory();") | ||
| if self.group_name: | ||
| shared_lib_mod_name = shared_lib_name(self.group_name) | ||
| emitter.emit_line("PyObject *mod_dict = PyImport_GetModuleDict();") | ||
| emitter.emit_line( | ||
| f'PyObject *shared_lib = PyDict_GetItemString(mod_dict, "{shared_lib_mod_name}");' | ||
| ) | ||
| emitter.emit_line("if (shared_lib == NULL) goto fail;") | ||
| emitter.emit_line( | ||
| 'PyObject *shared_lib_file = PyObject_GetAttrString(shared_lib, "__file__");' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use PyDict_GetItemStringRef instead, as this returns a borrowed reference, and we decref it below. |
||
| ) | ||
| emitter.emit_line("if (shared_lib_file == NULL) goto fail;") | ||
| else: | ||
| emitter.emit_line( | ||
| f'PyObject *shared_lib_file = PyUnicode_FromString("{module_name + EXT_SUFFIX}");' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No NULL check for |
||
| ) | ||
| emitter.emit_line(f'PyObject *ext_suffix = PyUnicode_FromString("{EXT_SUFFIX}");') | ||
| emitter.emit_line("if (ext_suffix == NULL) CPyError_OutOfMemory();") | ||
| is_pkg = int(self.source_paths[module_name].endswith("__init__.py")) | ||
| emitter.emit_line(f"Py_ssize_t is_pkg = {is_pkg};") | ||
|
|
||
| emitter.emit_line( | ||
| f"int rv = CPyImport_SetDunderAttrs({module_static}, modname, shared_lib_file, ext_suffix, is_pkg);" | ||
| ) | ||
| emitter.emit_line("Py_DECREF(ext_suffix);") | ||
| emitter.emit_line("Py_DECREF(shared_lib_file);") | ||
| emitter.emit_line("if (rv < 0) goto fail;") | ||
|
|
||
| # Register in sys.modules early so that circular imports via | ||
| # CPyImport_ImportNative can detect that this module is already | ||
| # being initialized and avoid re-executing the module body. | ||
| emitter.emit_line(f'modname = PyUnicode_FromString("{module_name}");') | ||
| emitter.emit_line("if (modname == NULL) CPyError_OutOfMemory();") | ||
| emitter.emit_line( | ||
| f"if (PyObject_SetItem(PyImport_GetModuleDict(), modname, {module_static}) < 0)" | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyDict_GetItemStringRefwould be a bit more correct in free-threading builds.