mirror of
https://github.com/python/cpython.git
synced 2026-01-22 23:28:43 +00:00
[3.14] gh-137422: Fix race condition in PyImport_AddModuleRef (gh-141822) (gh-141830)
(cherry picked from commit 2d50dd242e)
Co-authored-by: Sam Gross <colesbury@gmail.com>
This commit is contained in:
parent
af586d8d26
commit
5bca7f4d7a
3 changed files with 68 additions and 6 deletions
|
|
@ -3,6 +3,7 @@
|
|||
#include "Python.h"
|
||||
#include "pycore_audit.h" // _PySys_Audit()
|
||||
#include "pycore_ceval.h"
|
||||
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
|
||||
#include "pycore_hashtable.h" // _Py_hashtable_new_full()
|
||||
#include "pycore_import.h" // _PyImport_BootstrapImp()
|
||||
#include "pycore_initconfig.h" // _PyStatus_OK()
|
||||
|
|
@ -309,13 +310,8 @@ PyImport_GetModule(PyObject *name)
|
|||
if not, create a new one and insert it in the modules dictionary. */
|
||||
|
||||
static PyObject *
|
||||
import_add_module(PyThreadState *tstate, PyObject *name)
|
||||
import_add_module_lock_held(PyObject *modules, PyObject *name)
|
||||
{
|
||||
PyObject *modules = get_modules_dict(tstate, false);
|
||||
if (modules == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *m;
|
||||
if (PyMapping_GetOptionalItem(modules, name, &m) < 0) {
|
||||
return NULL;
|
||||
|
|
@ -335,6 +331,21 @@ import_add_module(PyThreadState *tstate, PyObject *name)
|
|||
return m;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
import_add_module(PyThreadState *tstate, PyObject *name)
|
||||
{
|
||||
PyObject *modules = get_modules_dict(tstate, false);
|
||||
if (modules == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject *m;
|
||||
Py_BEGIN_CRITICAL_SECTION(modules);
|
||||
m = import_add_module_lock_held(modules, name);
|
||||
Py_END_CRITICAL_SECTION();
|
||||
return m;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PyImport_AddModuleRef(const char *name)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue