Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
gh-146076: Fix crash when a ZoneInfo subclass is missing a `_weak_c…
…ache` (GH-146082)

(cherry picked from commit 3b06d68)

Co-authored-by: Stan Ulbrych <[email protected]>
  • Loading branch information
StanFromIreland authored and miss-islington committed Mar 18, 2026
commit 31b2e5065e0ec4397087c7499af8ccaf45985b80
12 changes: 12 additions & 0 deletions Lib/test/test_zoneinfo/test_zoneinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,18 @@ class ZI(self.klass):
"Unexpected instance of int in ZI weak cache for key 'America/Los_Angeles'"
)

def test_deleted_weak_cache(self):
class ZI(self.klass):
pass
delattr(ZI, '_weak_cache')

# These should not segfault
with self.assertRaises(AttributeError):
ZI("UTC")

with self.assertRaises(AttributeError):
ZI.clear_cache()

def test_inconsistent_weak_cache_setdefault(self):
class Cache:
def get(self, key, default=None):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`zoneinfo`: fix crashes when deleting ``_weak_cache`` from a
:class:`zoneinfo.ZoneInfo` subclass.
6 changes: 6 additions & 0 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ zoneinfo_ZoneInfo_impl(PyTypeObject *type, PyObject *key)
}

PyObject *weak_cache = get_weak_cache(state, type);
if (weak_cache == NULL) {
return NULL;
}
instance = PyObject_CallMethod(weak_cache, "get", "O", key, Py_None);
if (instance == NULL) {
Py_DECREF(weak_cache);
Expand Down Expand Up @@ -499,6 +502,9 @@ zoneinfo_ZoneInfo_clear_cache_impl(PyTypeObject *type, PyTypeObject *cls,
{
zoneinfo_state *state = zoneinfo_get_state_by_cls(cls);
PyObject *weak_cache = get_weak_cache(state, type);
if (weak_cache == NULL) {
return NULL;
}

if (only_keys == NULL || only_keys == Py_None) {
PyObject *rv = PyObject_CallMethod(weak_cache, "clear", NULL);
Expand Down
Loading