mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-133778: Fix setting __annotations__
under PEP 563 (#133794)
This commit is contained in:
parent
91b48868a8
commit
4443110c34
3 changed files with 60 additions and 9 deletions
|
@ -2065,19 +2065,46 @@ type_set_annotations(PyObject *tp, PyObject *value, void *Py_UNUSED(closure))
|
|||
return -1;
|
||||
}
|
||||
|
||||
int result;
|
||||
PyObject *dict = PyType_GetDict(type);
|
||||
if (value != NULL) {
|
||||
/* set */
|
||||
result = PyDict_SetItem(dict, &_Py_ID(__annotations_cache__), value);
|
||||
} else {
|
||||
/* delete */
|
||||
result = PyDict_Pop(dict, &_Py_ID(__annotations_cache__), NULL);
|
||||
if (result == 0) {
|
||||
PyErr_SetString(PyExc_AttributeError, "__annotations__");
|
||||
int result = PyDict_ContainsString(dict, "__annotations__");
|
||||
if (result < 0) {
|
||||
Py_DECREF(dict);
|
||||
return -1;
|
||||
}
|
||||
if (result) {
|
||||
// If __annotations__ is currently in the dict, we update it,
|
||||
if (value != NULL) {
|
||||
result = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
|
||||
} else {
|
||||
result = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
|
||||
if (result == 0) {
|
||||
// Somebody else just deleted it?
|
||||
PyErr_SetString(PyExc_AttributeError, "__annotations__");
|
||||
Py_DECREF(dict);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (result < 0) {
|
||||
Py_DECREF(dict);
|
||||
return -1;
|
||||
}
|
||||
// Also clear __annotations_cache__ just in case.
|
||||
result = PyDict_Pop(dict, &_Py_ID(__annotations_cache__), NULL);
|
||||
}
|
||||
else {
|
||||
// Else we update only __annotations_cache__.
|
||||
if (value != NULL) {
|
||||
/* set */
|
||||
result = PyDict_SetItem(dict, &_Py_ID(__annotations_cache__), value);
|
||||
} else {
|
||||
/* delete */
|
||||
result = PyDict_Pop(dict, &_Py_ID(__annotations_cache__), NULL);
|
||||
if (result == 0) {
|
||||
PyErr_SetString(PyExc_AttributeError, "__annotations__");
|
||||
Py_DECREF(dict);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (result < 0) {
|
||||
Py_DECREF(dict);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue