Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.3k
gh-119127: Fix _functools.Placeholder singleton#124601
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -26,7 +26,7 @@ typedef struct _functools_state { | ||||||
/* this object is used delimit args and keywords in the cache keys */ | ||||||
PyObject *kwd_mark; | ||||||
PyTypeObject *placeholder_type; | ||||||
PyObject *placeholder; // strong reference (singleton) | ||||||
PyTypeObject *partial_type; | ||||||
PyTypeObject *keyobject_type; | ||||||
PyTypeObject *lru_list_elem_type; | ||||||
@@ -76,13 +76,12 @@ static PyMethodDef placeholder_methods[] = { | ||||||
}; | ||||||
static void | ||||||
placeholder_dealloc(PyObject*self) | ||||||
{ | ||||||
PyObject_GC_UnTrack(self); | ||||||
PyTypeObject *tp = Py_TYPE(self); | ||||||
tp->tp_free((PyObject*)self); | ||||||
Member 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. Suggested change
nit: there's no need to casting it to | ||||||
Py_DECREF(tp); | ||||||
} | ||||||
static PyObject * | ||||||
@@ -93,10 +92,26 @@ placeholder_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) | ||||||
return NULL; | ||||||
} | ||||||
_functools_state *state = get_functools_state_by_type(type); | ||||||
if (state->placeholder != NULL) { | ||||||
return Py_NewRef(state->placeholder); | ||||||
} | ||||||
PyObject *placeholder = PyType_GenericNew(type, NULL, NULL); | ||||||
if (placeholder == NULL) { | ||||||
return NULL; | ||||||
} | ||||||
if (state->placeholder == NULL) { | ||||||
state->placeholder =Py_NewRef(placeholder); | ||||||
} | ||||||
return placeholder; | ||||||
} | ||||||
static int | ||||||
placeholder_traverse(PyObject *self, visitproc visit, void *arg) | ||||||
{ | ||||||
Py_VISIT(Py_TYPE(self)); | ||||||
return 0; | ||||||
} | ||||||
static PyType_Slot placeholder_type_slots[] = { | ||||||
@@ -105,13 +120,14 @@ static PyType_Slot placeholder_type_slots[] = { | ||||||
{Py_tp_doc, (void *)placeholder_doc}, | ||||||
{Py_tp_methods, placeholder_methods}, | ||||||
{Py_tp_new, placeholder_new}, | ||||||
{Py_tp_traverse, placeholder_traverse}, | ||||||
{0, 0} | ||||||
}; | ||||||
static PyType_Spec placeholder_type_spec = { | ||||||
.name = "functools._PlaceholderType", | ||||||
.basicsize = sizeof(placeholderobject), | ||||||
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC, | ||||||
.slots = placeholder_type_slots | ||||||
}; | ||||||
@@ -1717,13 +1733,17 @@ _functools_exec(PyObject *module) | ||||||
if (PyModule_AddType(module, state->placeholder_type) < 0) { | ||||||
return -1; | ||||||
} | ||||||
PyObject *placeholder = PyObject_CallNoArgs((PyObject *)state->placeholder_type); | ||||||
if (placeholder == NULL) { | ||||||
return -1; | ||||||
} | ||||||
if (PyModule_AddObjectRef(module, "Placeholder", placeholder) < 0) { | ||||||
Py_DECREF(placeholder); | ||||||
return -1; | ||||||
} | ||||||
Py_DECREF(placeholder); | ||||||
state->partial_type = (PyTypeObject *)PyType_FromModuleAndSpec(module, | ||||||
&partial_type_spec, NULL); | ||||||
if (state->partial_type == NULL) { | ||||||
@@ -1769,6 +1789,7 @@ _functools_traverse(PyObject *module, visitproc visit, void *arg) | ||||||
_functools_state *state = get_functools_state(module); | ||||||
Py_VISIT(state->kwd_mark); | ||||||
Py_VISIT(state->placeholder_type); | ||||||
Py_VISIT(state->placeholder); | ||||||
Py_VISIT(state->partial_type); | ||||||
Py_VISIT(state->keyobject_type); | ||||||
Py_VISIT(state->lru_list_elem_type); | ||||||
@@ -1781,6 +1802,7 @@ _functools_clear(PyObject *module) | ||||||
_functools_state *state = get_functools_state(module); | ||||||
Py_CLEAR(state->kwd_mark); | ||||||
Py_CLEAR(state->placeholder_type); | ||||||
Py_CLEAR(state->placeholder); | ||||||
Py_CLEAR(state->partial_type); | ||||||
Py_CLEAR(state->keyobject_type); | ||||||
Py_CLEAR(state->lru_list_elem_type); | ||||||
Uh oh!
There was an error while loading.Please reload this page.