10000 gh-101758: Add a Test For Single-Phase Init Modules in Multiple Interpreters by ericsnowcurrently · Pull Request #101920 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-101758: Add a Test For Single-Phase Init Modules in Multiple Interpreters #101920

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

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
Prev Previous commit
Next Next commit
Add _PyImport_ClearExtension().
  • Loading branch information
ericsnowcurrently committed Feb 15, 2023
commit 0f9a66358840129cd4f2afc44972f2d4dbc96586
3 changes: 3 additions & 0 deletions Include/internal/pycore_import.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ PyAPI_DATA(const struct _frozen *) _PyImport_FrozenStdlib;
PyAPI_DATA(const struct _frozen *) _PyImport_FrozenTest;
extern const struct _module_alias * _PyImport_FrozenAliases;

// for testing
PyAPI_FUNC(int) _PyImport_ClearExtension(PyObject *name, PyObject *filename);

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,20 @@ get_interp_settings(PyObject *self, PyObject *args)
}


static PyObject *
clear_extension(PyObject *self, PyObject *args)
{
PyObject *name = NULL, *filename = NULL;
if (!PyArg_ParseTuple(args, "OO:clear_extension", &name, &filename)) {
return NULL;
}
if (_PyImport_ClearExtension(name, filename) < 0) {
return NULL;
}
Py_RETURN_NONE;
}


static PyMethodDef module_functions[] = {
{"get_configs", get_configs, METH_NOARGS},
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
Expand All @@ -692,6 +706,7 @@ static PyMethodDef module_functions[] = {
_TESTINTERNALCAPI_COMPILER_CODEGEN_METHODDEF
_TESTINTERNALCAPI_OPTIMIZE_CFG_METHODDEF
{"get_interp_settings", get_interp_settings, METH_VARARGS, NULL},
{"clear_extension", clear_extension, METH_VARARGS, NULL},
{NULL, NULL} /* sentinel */
};

Expand Down
76 changes: 74 additions & 2 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,28 @@ exec_builtin_or_dynamic(PyObject *mod) {
}


static int clear_singlephase_extension(PyInterpreterState *interp,
PyObject *name, PyObject *filename);

// Currently, this is only used for testing.
// (See _testinternalcapi.clear_extension().)
int
_PyImport_ClearExtension(PyObject *name, PyObject *filename)
{
PyInterpreterState *interp = _PyInterpreterState_GET();

/* Clearing a module's C globals is up to the module. */
if (clear_singlephase_extension(interp, name, filename) < 0) {
return -1;
}

// In the future we'll probably also make sure the extension's
// fi 10000 le handle (and DL handle) is closed (requires saving it).

return 0;
}


/*******************/

#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
Expand Down Expand Up @@ -766,8 +788,30 @@ _extensions_cache_set(PyObject *filename, PyObject *name, PyModuleDef *def)
return 0;
}

static int
_extensions_cache_delete(PyObject *filename, PyObject *name)
{
PyObject *extensions = EXTENSIONS;
if (extensions == NULL) {
return 0;
}
PyObject *key = PyTuple_Pack(2, filename, name);
if (key == NULL) {
return -1;
}
if (PyDict_DelItem(extensions, key) < 0) {
if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
Py_DECREF(key);
return -1;
}
PyErr_Clear();
}
Py_DECREF(key);
return 0;
}

static void
_extensions_cache_clear(void)
_extensions_cache_clear_all(void)
{
Py_CLEAR(EXTENSIONS);
}
Expand Down Expand Up @@ -890,6 +934,34 @@ import_find_extension(PyThreadState *tstate, PyObject *name,
return mod;
}

static int
clear_singlephase_extension(PyInterpreterState *interp,
PyObject *name, PyObject *filename)
{
PyModuleDef *def = _extensions_cache_get(filename, name);
if (def == NULL) {
if (PyErr_Occurred()) {
return -1;
}
return 0;
}

/* Clear data set when the module was initially loaded. */
def->m_base.m_init = NULL;
Py_CLEAR(def->m_base.m_copy);
// We leave m_index alone since there's no reason to reset it.

/* Clear the PyState_*Module() cache entry. */
if (_modules_by_index_check(interp, def->m_base.m_index) == NULL) {
if (_modules_by_index_clear(interp, def) < 0) {
return -1;
}
}

/* Clear the cached module def. */
return _extensions_cache_delete(filename, name);
}


/*******************/
/* builtin modules */
Expand Down Expand Up @@ -2633,7 +2705,7 @@ void
_PyImport_Fini(void)
{
/* Destroy the database used by _PyImport_{Fixup,Find}Extension */
_extensions_cache_clear();
_extensions_cache_clear_all();
if (import_lock != NULL) {
PyThread_free_lock(import_lock);
import_lock = NULL;
Expand Down
0