FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-157384: Set package context in PyImport_CreateModuleFromInitfunc() (GH-157387) by miss-islington · Pull Request #159316 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (2) .py  (1) .rst  (2) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 4 additions & 0 deletions Doc/c-api/import.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ Importing Modules
(Custom importers should do this in their
:py:meth:`~importlib.abc.Loader.exec_module` method.)

If the spec's name is dotted and *initfunc* creates the module using only
the last component of the name, the module's :attr:`~module.__name__`
is set to the full name from the spec.

On error, return NULL with an exception set.

.. versionadded:: 3.15
4 changes: 4 additions & 0 deletions Lib/test/test_embed.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ def test_create_module_from_initfunc(self):
"my_test_extension.exec_slot_ran='yes'\n"
"<module 'embedded_ext' (static-extension)>\n"
"embedded_ext.executed='yes'\n"
"<module 'sp_pkg.sp_submod' (static-extension)>\n"
"sp_pkg.sp_submod.__name__='sp_pkg.sp_submod'\n"
"sys.modules[\"sp_pkg.sp_submod\"] is sp_pkg.sp_submod=True\n"
"\"sp_submod\" in sys.modules=False\n"
)

def test_inittab_submodule_multiphase(self):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:c:func:`PyImport_CreateModuleFromInitfunc` now sets the package context
while calling *initfunc*, so that a single-phase init submodule created with
only the last component of its name gets the full name from the spec.
54 changes: 52 additions & 2 deletions Programs/_testembed.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,40 @@ static int test_repeated_init_and_inittab(void)
return 0;
}

// A single-phase init submodule that only knows its short name,
// like pybind11's PYBIND11_MODULE(sp_submod, m) would.
static PyModuleDef cmfi_sp_submod_def = {
PyModuleDef_HEAD_INIT,
.m_name = "sp_submod",
.m_size = -1,
};

static PyObject*
PyInit_cmfi_sp_submod(void)
{
return PyModule_Create(&cmfi_sp_submod_def);
}

// A multi-phase init package for the submodule above.
static PyModuleDef_Slot cmfi_sp_pkg_slots[] = {
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
{0, NULL},
};

static PyModuleDef cmfi_sp_pkg_def = {
PyModuleDef_HEAD_INIT,
.m_name = "sp_pkg",
.m_size = 0,
.m_slots = cmfi_sp_pkg_slots,
};

static PyObject*
PyInit_cmfi_sp_pkg(void)
{
return PyModuleDef_Init(&cmfi_sp_pkg_def);
}

static PyObject*
create_module(PyObject* self, PyObject* spec)
{
Expand All @@ -2420,6 +2454,14 @@ create_module(PyObject* self, PyObject* spec)
Py_DECREF(name);
return PyImport_CreateModuleFromInitfunc(spec, PyInit_embedded_ext);
}
if (PyUnicode_EqualToUTF8(name, "sp_pkg")) {
Py_DECREF(name);
return PyImport_CreateModuleFromInitfunc(spec, PyInit_cmfi_sp_pkg);
}
if (PyUnicode_EqualToUTF8(name, "sp_pkg.sp_submod")) {
Py_DECREF(name);
return PyImport_CreateModuleFromInitfunc(spec, PyInit_cmfi_sp_submod);
}
PyErr_Format(PyExc_LookupError, "static module %R not found", name);
Py_DECREF(name);
return NULL;
Expand Down Expand Up @@ -2467,6 +2509,12 @@ test_create_module_from_initfunc(void)
L"import embedded_ext;"
L"print(embedded_ext);"
L"print(f'{embedded_ext.executed=}');"
// Single-phase submodule whose init only knows its short name
L"import sp_pkg.sp_submod;"
L"print(sp_pkg.sp_submod);"
L"print(f'{sp_pkg.sp_submod.__name__=}');"
L"print(f'{sys.modules[\"sp_pkg.sp_submod\"] is sp_pkg.sp_submod=}');"
L"print(f'{\"sp_submod\" in sys.modules=}');"
};
PyConfig config;
if (PyImport_AppendInittab("create_static_module",
Expand All @@ -2486,8 +2534,10 @@ test_create_module_from_initfunc(void)
" _ORIGIN = \"static-extension\"\n"
" @classmethod\n"
" def find_spec(cls, fullname, path, target=None):\n"
" if fullname in {'my_test_extension', 'embedded_ext'}:\n"
" return spec_from_loader(fullname, cls, origin=cls._ORIGIN)\n"
" if fullname in {'my_test_extension', 'embedded_ext',\n"
" 'sp_pkg', 'sp_pkg.sp_submod'}:\n"
" return spec_from_loader(fullname, cls, origin=cls._ORIGIN,\n"
" is_package=(fullname == 'sp_pkg'))\n"
" return None\n"
" @staticmethod\n"
" def create_module(spec):\n"
Expand Down
13 changes: 13 additions & 0 deletions Python/import.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2512,6 +2512,19 @@ create_builtin(
if (_Py_ext_module_loader_info_init_for_builtin(&info, name) < 0) {
return NULL;
}
if (initfunc != NULL) {
/* An explicitly provided init function (see
* PyImport_CreateModuleFromInitfunc()) may belong to a submodule
* whose single-phase init creates the module using only the last
* component of the name (as e.g. pybind11 does). Set the package
* context so that PyModule_Create() resolves the full name, the
* same as for dynamically loaded extensions. */
info.newcontext = PyUnicode_AsUTF8(info.name);
if (info.newcontext == NULL) {
_Py_ext_module_loader_info_clear(&info);
return NULL;
}
}

struct extensions_cache_value *cached = NULL;
PyObject *mod = import_find_extension(tstate, &info, &cached);
Expand Down
Loading

Back | FazBrowse Home | New Git URL