Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-105927: PyImport_AddModule() uses _PyWeakref_GET_REF()#106001

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 1 commit intopython:mainfromvstinner:getref_add_module
Jun 22, 2023
Merged
Changes fromall commits
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
22 changes: 19 additions & 3 deletionsPython/import.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -12,6 +12,7 @@
#include "pycore_pymem.h" // _PyMem_SetDefaultAllocator()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_sysmodule.h" // _PySys_Audit()
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
#include "marshal.h" // PyMarshal_ReadObjectFromString()
#include "importdl.h" // _PyImport_DynLoadFiletab
#include "pydtrace.h" // PyDTrace_IMPORT_FIND_LOAD_START_ENABLED()
Expand DownExpand Up@@ -373,15 +374,30 @@ PyImport_AddModuleObject(PyObject *name)
return NULL;
}

// gh-86160: PyImport_AddModuleObject() returns a borrowed reference
// gh-86160: PyImport_AddModuleObject() returns a borrowed reference.
// Create a weak reference to produce a borrowed reference, since it can
// become NULL. sys.modules type can be different than dict and it is not
// guaranteed that it keeps a strong reference to the module. It can be a
// custom mapping with __getitem__() which returns a new object or removes
// returned object, or __setitem__ which does nothing. There is so much
// unknown. With weakref we can be sure that we get either a reference to
// live object or NULL.
//
// Use PyImport_AddModuleRef() to avoid these issues.
PyObject *ref = PyWeakref_NewRef(mod, NULL);
Py_DECREF(mod);
if (ref == NULL) {
return NULL;
}

mod = PyWeakref_GetObject(ref);
mod = _PyWeakref_GET_REF(ref);
Py_DECREF(ref);
Py_XDECREF(mod);

if (mod == NULL && !PyErr_Occurred()) {

Choose a reason for hiding this comment

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

I am not sure that comparing mod with NULL after Py_XDECREF does not have an undefined behavior. AFAIK, you can do nothing with a pointer to released memory, not even compare it with NULL.

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

See comments of my PR#105998: IMO most of this code is useless and can be removed.

PyErr_SetString(PyExc_RuntimeError,
"sys.modules does not hold a strong reference "
"to the module");
}
return mod; /* borrowed reference */
}

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp