@@ -522,6 +522,7 @@ PyModule_GetNameObject(PyObject *mod)
522522 }
523523 PyObject * name ;
524524 if (PyDict_GetItemRef (dict , & _Py_ID (__name__ ), & name ) <= 0 ) {
525+ // error or not found
525526 goto error ;
526527 }
527528 if (!PyUnicode_Check (name )) {
@@ -562,6 +563,7 @@ PyModule_GetFilenameObject(PyObject *mod)
562563 }
563564 PyObject * fileobj ;
564565 if (PyDict_GetItemRef (dict , & _Py_ID (__file__ ), & fileobj ) <= 0 ) {
566+ // error or not found
565567 goto error ;
566568 }
567569 if (!PyUnicode_Check (fileobj )) {
@@ -816,28 +818,28 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
816818 PyErr_Clear ();
817819 }
818820 assert (m -> md_dict != NULL );
819- getattr = PyDict_GetItemWithError (m -> md_dict , & _Py_ID (__getattr__ ));
821+ if (PyDict_GetItemRef (m -> md_dict , & _Py_ID (__getattr__ ), & getattr ) < 0 ) {
822+ return NULL ;
823+ }
820824 if (getattr ) {
821825 PyObject * result = PyObject_CallOneArg (getattr , name );
822826 if (result == NULL && suppress == 1 && PyErr_ExceptionMatches (PyExc_AttributeError )) {
823827 // suppress AttributeError
824828 PyErr_Clear ();
825829 }
830+ Py_DECREF (getattr );
826831 return result ;
827832 }
828- if (PyErr_Occurred () ) {
833+ if (PyDict_GetItemRef ( m -> md_dict , & _Py_ID ( __name__ ), & mod_name ) < 0 ) {
829834 return NULL ;
830835 }
831- mod_name = PyDict_GetItemWithError (m -> md_dict , & _Py_ID (__name__ ));
832836 if (mod_name && PyUnicode_Check (mod_name )) {
833- Py_INCREF (mod_name );
834- PyObject * spec = PyDict_GetItemWithError (m -> md_dict , & _Py_ID (__spec__ ));
835- if (spec == NULL && PyErr_Occurred ()) {
837+ PyObject * spec ;
838+ if (PyDict_GetItemRef (m -> md_dict , & _Py_ID (__spec__ ), & spec ) < 0 ) {
836839 Py_DECREF (mod_name );
837840 return NULL ;
838841 }
839842 if (suppress != 1 ) {
840- Py_XINCREF (spec );
841843 if (_PyModuleSpec_IsInitializing (spec )) {
842844 PyErr_Format (PyExc_AttributeError ,
843845 "partially initialized "
@@ -856,14 +858,12 @@ _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
856858 "module '%U' has no attribute '%U'" ,
857859 mod_name , name );
858860 }
859- Py_XDECREF (spec );
860861 }
862+ Py_XDECREF (spec );
861863 Py_DECREF (mod_name );
862864 return NULL ;
863865 }
864- else if (PyErr_Occurred ()) {
865- return NULL ;
866- }
866+ Py_XDECREF (mod_name );
867867 if (suppress != 1 ) {
868868 PyErr_Format (PyExc_AttributeError ,
869869 "module has no attribute '%U'" , name );
@@ -957,11 +957,8 @@ module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
957957 return NULL ;
958958 }
959959
960- PyObject * annotations = PyDict_GetItemWithError (dict , & _Py_ID (__annotations__ ));
961- if (annotations ) {
962- Py_INCREF (annotations );
963- }
964- else if (!PyErr_Occurred ()) {
960+ PyObject * annotations ;
961+ if (PyDict_GetItemRef (dict , & _Py_ID (__annotations__ ), & annotations ) == 0 ) {
965962 annotations = PyDict_New ();
966963 if (annotations ) {
967964 int result = PyDict_SetItem (
0 commit comments