Skip to content

bpo-43132: Fix incorrect handling of PyObject_RichCompareBool() in _zoneinfo #24450

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
Changes from all 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
23 changes: 17 additions & 6 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ is_leap_year(int year);
static size_t
_bisect(const int64_t value, const int64_t *arr, size_t size);

static void
static int
eject_from_strong_cache(const PyTypeObject *const type, PyObject *key);
static void
clear_strong_cache(const PyTypeObject *const type);
Expand Down Expand Up @@ -266,7 +266,7 @@ zoneinfo_new(PyTypeObject *type, PyObject *args, PyObject *kw)
}

PyObject *instance = zone_from_strong_cache(type, key);
if (instance != NULL) {
if (instance != NULL || PyErr_Occurred()) {
return instance;
}

Expand Down Expand Up @@ -429,7 +429,10 @@ zoneinfo_clear_cache(PyObject *cls, PyObject *args, PyObject *kwargs)

while ((item = PyIter_Next(iter))) {
// Remove from strong cache
eject_from_strong_cache(type, item);
if (eject_from_strong_cache(type, item) < 0) {
Py_DECREF(item);
break;
}

// Remove from weak cache
PyObject *tmp = PyObject_CallMethodObjArgs(weak_cache, pop, item,
Expand Down Expand Up @@ -2342,7 +2345,11 @@ find_in_strong_cache(const StrongCacheNode *const root, PyObject *const key)
{
const StrongCacheNode *node = root;
while (node != NULL) {
if (PyObject_RichCompareBool(key, node->key, Py_EQ)) {
int rv = PyObject_RichCompareBool(key, node->key, Py_EQ);
if (rv < 0) {
return NULL;
}
if (rv) {
return (StrongCacheNode *)node;
}

Expand All @@ -2356,11 +2363,11 @@ find_in_strong_cache(const StrongCacheNode *const root, PyObject *const key)
*
* This function is used to enable the per-key functionality in clear_cache.
*/
static void
static int
eject_from_strong_cache(const PyTypeObject *const type, PyObject *key)
{
if (type != &PyZoneInfo_ZoneInfoType) {
return;
return 0;
}

StrongCacheNode *node = find_in_strong_cache(ZONEINFO_STRONG_CACHE, key);
Expand All @@ -2369,6 +2376,10 @@ eject_from_strong_cache(const PyTypeObject *const type, PyObject *key)

strong_cache_node_free(node);
}
else if (PyErr_Occurred()) {
return -1;
}
return 0;
}

/* Moves a node to the front of the LRU cache.
Expand Down