Skip to content

Commit d1554e0

Browse files
committed
bpo-43901: Fix refleaks in test_module
1 parent 9746cda commit d1554e0

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

Lib/test/test_module.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33
import weakref
44
from test.support import gc_collect
5+
from test.support import import_helper
56
from test.support.script_helper import assert_python_ok
67

78
import sys
@@ -334,7 +335,7 @@ def test_annotations_getset_raises(self):
334335
del foo.__annotations__
335336

336337
def test_annotations_are_created_correctly(self):
337-
from test import ann_module4
338+
ann_module4 = import_helper.import_fresh_module('test.ann_module4')
338339
self.assertTrue("__annotations__" in ann_module4.__dict__)
339340
del ann_module4.__annotations__
340341
self.assertFalse("__annotations__" in ann_module4.__dict__)

Objects/moduleobject.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -876,25 +876,31 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
876876
static int
877877
module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
878878
{
879+
int ret = -1;
879880
PyObject *dict = _PyObject_GetAttrId((PyObject *)m, &PyId___dict__);
880881

881882
if ((dict == NULL) || !PyDict_Check(dict)) {
882883
PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
883-
return -1;
884+
goto exit;
884885
}
885886

886887
if (value != NULL) {
887888
/* set */
888-
return _PyDict_SetItemId(dict, &PyId___annotations__, value);
889+
ret = _PyDict_SetItemId(dict, &PyId___annotations__, value);
890+
goto exit;
889891
}
890892

891893
/* delete */
892894
if (!_PyDict_ContainsId(dict, &PyId___annotations__)) {
893895
PyErr_Format(PyExc_AttributeError, "__annotations__");
894-
return -1;
896+
goto exit;
895897
}
896898

897-
return _PyDict_DelItemId(dict, &PyId___annotations__);
899+
ret = _PyDict_DelItemId(dict, &PyId___annotations__);
900+
901+
exit:
902+
Py_XDECREF(dict);
903+
return ret;
898904
}
899905

900906

0 commit comments

Comments
 (0)