Skip to content

Commit e374a40

Browse files
authored
bpo-43901: Fix refleaks in test_module (GH-25754)
1 parent 7dcf0f6 commit e374a40

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-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: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
845845

846846
if ((dict == NULL) || !PyDict_Check(dict)) {
847847
PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
848+
Py_XDECREF(dict);
848849
return NULL;
849850
}
850851

@@ -876,25 +877,31 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
876877
static int
877878
module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
878879
{
880+
int ret = -1;
879881
PyObject *dict = _PyObject_GetAttrId((PyObject *)m, &PyId___dict__);
880882

881883
if ((dict == NULL) || !PyDict_Check(dict)) {
882884
PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
883-
return -1;
885+
goto exit;
884886
}
885887

886888
if (value != NULL) {
887889
/* set */
888-
return _PyDict_SetItemId(dict, &PyId___annotations__, value);
890+
ret = _PyDict_SetItemId(dict, &PyId___annotations__, value);
891+
goto exit;
889892
}
890893

891894
/* delete */
892895
if (!_PyDict_ContainsId(dict, &PyId___annotations__)) {
893896
PyErr_Format(PyExc_AttributeError, "__annotations__");
894-
return -1;
897+
goto exit;
895898
}
896899

897-
return _PyDict_DelItemId(dict, &PyId___annotations__);
900+
ret = _PyDict_DelItemId(dict, &PyId___annotations__);
901+
902+
exit:
903+
Py_XDECREF(dict);
904+
return ret;
898905
}
899906

900907

0 commit comments

Comments
 (0)