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
3 changes: 2 additions & 1 deletion Lib/test/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
import weakref
from test.support import gc_collect
from test.support import import_helper
from test.support.script_helper import assert_python_ok

import sys
Expand Down Expand Up @@ -334,7 +335,7 @@ def test_annotations_getset_raises(self):
del foo.__annotations__

def test_annotations_are_created_correctly(self):
from test import ann_module4
ann_module4 = import_helper.import_fresh_module('test.ann_module4')
self.assertTrue("__annotations__" in ann_module4.__dict__)
del ann_module4.__annotations__
self.assertFalse("__annotations__" in ann_module4.__dict__)
Expand Down
15 changes: 11 additions & 4 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))

if ((dict == NULL) || !PyDict_Check(dict)) {
PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
Py_XDECREF(dict);
return NULL;
}

Expand Down Expand Up @@ -876,25 +877,31 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
static int
module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
{
int ret = -1;
PyObject *dict = _PyObject_GetAttrId((PyObject *)m, &PyId___dict__);

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

if (value != NULL) {
/* set */
return _PyDict_SetItemId(dict, &PyId___annotations__, value);
ret = _PyDict_SetItemId(dict, &PyId___annotations__, value);
goto exit;
}

/* delete */
if (!_PyDict_ContainsId(dict, &PyId___annotations__)) {
PyErr_Format(PyExc_AttributeError, "__annotations__");
return -1;
goto exit;
}

return _PyDict_DelItemId(dict, &PyId___annotations__);
ret = _PyDict_DelItemId(dict, &PyId___annotations__);

exit:
Py_XDECREF(dict);
return ret;
}


Expand Down