Skip to content

Commit

Permalink
bpo-41568: Fix refleaks in zoneinfo subclasses (pythonGH-21907)
Browse files Browse the repository at this point in the history
* Fix refleak in C module __init_subclass__

This was leaking a reference to the weak cache dictionary for every
ZoneInfo subclass created.

* Fix refleak in ZoneInfo subclass's clear_cache

The previous version of the code accidentally cleared the global
ZONEINFO_STRONG_CACHE variable (and inducing `ZoneInfo` to create a new
strong cache) on calls to a subclass's `clear_cache()`. This would not
affect guaranteed behavior, but it's still not the right thing to do
(and it caused reference leaks).
  • Loading branch information
pganssle authored and shihai1991 committed Aug 20, 2020
1 parent 2ee8174 commit 65f9968
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ zoneinfo_clear_cache(PyObject *cls, PyObject *args, PyObject *kwargs)
}

clear_strong_cache(type);
ZONEINFO_STRONG_CACHE = NULL;
}
else {
PyObject *item = NULL;
Expand Down Expand Up @@ -2471,6 +2470,7 @@ clear_strong_cache(const PyTypeObject *const type)
}

strong_cache_free(ZONEINFO_STRONG_CACHE);
ZONEINFO_STRONG_CACHE = NULL;
}

static PyObject *
Expand Down Expand Up @@ -2525,6 +2525,7 @@ zoneinfo_init_subclass(PyTypeObject *cls, PyObject *args, PyObject **kwargs)
}

PyObject_SetAttrString((PyObject *)cls, "_weak_cache", weak_cache);
Py_DECREF(weak_cache);
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -2616,8 +2617,7 @@ module_free()
Py_CLEAR(ZONEINFO_WEAK_CACHE);
}

strong_cache_free(ZONEINFO_STRONG_CACHE);
ZONEINFO_STRONG_CACHE = NULL;
clear_strong_cache(&PyZoneInfo_ZoneInfoType);
}

static int
Expand Down

0 comments on commit 65f9968

Please sign in to comment.