@@ -287,16 +287,11 @@ static PyObject *
287287get_weak_cache (zoneinfo_state * state , PyTypeObject * type )
288288{
289289 if (type == state -> ZoneInfoType ) {
290+ Py_INCREF (state -> ZONEINFO_WEAK_CACHE );
290291 return state -> ZONEINFO_WEAK_CACHE ;
291292 }
292293 else {
293- PyObject * cache =
294- PyObject_GetAttrString ((PyObject * )type , "_weak_cache" );
295- // We are assuming that the type lives at least as long as the function
296- // that calls get_weak_cache, and that it holds a reference to the
297- // cache, so we'll return a "borrowed reference".
298- Py_XDECREF (cache );
299- return cache ;
294+ return PyObject_GetAttrString ((PyObject * )type , "_weak_cache" );
300295 }
301296}
302297
@@ -323,26 +318,30 @@ zoneinfo_ZoneInfo_impl(PyTypeObject *type, PyObject *key)
323318 PyObject * weak_cache = get_weak_cache (state , type );
324319 instance = PyObject_CallMethod (weak_cache , "get" , "O" , key , Py_None );
325320 if (instance == NULL ) {
321+ Py_DECREF (weak_cache );
326322 return NULL ;
327323 }
328324
329325 if (instance == Py_None ) {
330326 Py_DECREF (instance );
331327 PyObject * tmp = zoneinfo_new_instance (state , type , key );
332328 if (tmp == NULL ) {
329+ Py_DECREF (weak_cache );
333330 return NULL ;
334331 }
335332
336333 instance =
337334 PyObject_CallMethod (weak_cache , "setdefault" , "OO" , key , tmp );
338335 Py_DECREF (tmp );
339336 if (instance == NULL ) {
337+ Py_DECREF (weak_cache );
340338 return NULL ;
341339 }
342340 ((PyZoneInfo_ZoneInfo * )instance )-> source = SOURCE_CACHE ;
343341 }
344342
345343 update_strong_cache (state , type , key , instance );
344+ Py_DECREF (weak_cache );
346345 return instance ;
347346}
348347
@@ -504,12 +503,14 @@ zoneinfo_ZoneInfo_clear_cache_impl(PyTypeObject *type, PyTypeObject *cls,
504503 PyObject * item = NULL ;
505504 PyObject * pop = PyUnicode_FromString ("pop" );
506505 if (pop == NULL ) {
506+ Py_DECREF (weak_cache );
507507 return NULL ;
508508 }
509509
510510 PyObject * iter = PyObject_GetIter (only_keys );
511511 if (iter == NULL ) {
512512 Py_DECREF (pop );
513+ Py_DECREF (weak_cache );
513514 return NULL ;
514515 }
515516
@@ -534,6 +535,7 @@ zoneinfo_ZoneInfo_clear_cache_impl(PyTypeObject *type, PyTypeObject *cls,
534535 Py_DECREF (pop );
535536 }
536537
538+ Py_DECREF (weak_cache );
537539 if (PyErr_Occurred ()) {
538540 return NULL ;
539541 }
0 commit comments