1
1
#include "Python.h"
2
- #include "pycore_dict.h" // _PyDict_GetItemWithError()
3
2
#include "pycore_interp.h" // PyInterpreterState.warnings
4
3
#include "pycore_long.h" // _PyLong_GetZero()
5
4
#include "pycore_pyerrors.h" // _PyErr_Occurred()
8
7
#include "pycore_sysmodule.h" // _PySys_GetAttr()
9
8
#include "pycore_traceback.h" // _Py_DisplaySourceLine()
10
9
10
+ #include <stdbool.h>
11
+
11
12
#include "clinic/_warnings.c.h"
12
13
13
14
#define MODULE_NAME "_warnings"
@@ -397,7 +398,7 @@ static int
397
398
already_warned (PyInterpreterState * interp , PyObject * registry , PyObject * key ,
398
399
int should_set )
399
400
{
400
- PyObject * version_obj , * already_warned ;
401
+ PyObject * already_warned ;
401
402
402
403
if (key == NULL )
403
404
return -1 ;
@@ -406,14 +407,17 @@ already_warned(PyInterpreterState *interp, PyObject *registry, PyObject *key,
406
407
if (st == NULL ) {
407
408
return -1 ;
408
409
}
409
- version_obj = _PyDict_GetItemWithError (registry , & _Py_ID (version ));
410
- if (version_obj == NULL
410
+ PyObject * version_obj ;
411
+ if (PyDict_GetItemRef (registry , & _Py_ID (version ), & version_obj ) < 0 ) {
412
+ return -1 ;
413
+ }
414
+ bool should_update_version = (
415
+ version_obj == NULL
411
416
|| !PyLong_CheckExact (version_obj )
412
- || PyLong_AsLong (version_obj ) != st -> filters_version )
413
- {
414
- if (PyErr_Occurred ()) {
415
- return -1 ;
416
- }
417
+ || PyLong_AsLong (version_obj ) != st -> filters_version
418
+ );
419
+ Py_XDECREF (version_obj );
420
+ if (should_update_version ) {
417
421
PyDict_Clear (registry );
418
422
version_obj = PyLong_FromLong (st -> filters_version );
419
423
if (version_obj == NULL )
@@ -911,13 +915,12 @@ setup_context(Py_ssize_t stack_level,
911
915
/* Setup registry. */
912
916
assert (globals != NULL );
913
917
assert (PyDict_Check (globals ));
914
- * registry = _PyDict_GetItemWithError (globals , & _Py_ID (__warningregistry__ ));
918
+ int rc = PyDict_GetItemRef (globals , & _Py_ID (__warningregistry__ ),
919
+ registry );
920
+ if (rc < 0 ) {
921
+ goto handle_error ;
922
+ }
915
923
if (* registry == NULL ) {
916
- int rc ;
917
-
918
- if (_PyErr_Occurred (tstate )) {
919
- goto handle_error ;
920
- }
921
924
* registry = PyDict_New ();
922
925
if (* registry == NULL )
923
926
goto handle_error ;
@@ -926,21 +929,21 @@ setup_context(Py_ssize_t stack_level,
926
929
if (rc < 0 )
927
930
goto handle_error ;
928
931
}
929
- else
930
- Py_INCREF (* registry );
931
932
932
933
/* Setup module. */
933
- * module = _PyDict_GetItemWithError (globals , & _Py_ID (__name__ ));
934
- if (* module == Py_None || (* module != NULL && PyUnicode_Check (* module ))) {
935
- Py_INCREF (* module );
936
- }
937
- else if (_PyErr_Occurred (tstate )) {
934
+ rc = PyDict_GetItemRef (globals , & _Py_ID (__name__ ), module );
935
+ if (rc < 0 ) {
938
936
goto handle_error ;
939
937
}
940
- else {
941
- * module = PyUnicode_FromString ("<string>" );
942
- if (* module == NULL )
943
- goto handle_error ;
938
+ if (rc > 0 ) {
939
+ if (Py_IsNone (* module ) || PyUnicode_Check (* module )) {
940
+ return 1 ;
941
+ }
942
+ Py_DECREF (* module );
943
+ }
944
+ * module = PyUnicode_FromString ("<string>" );
945
+ if (* module == NULL ) {
946
+ goto handle_error ;
944
947
}
945
948
946
949
return 1 ;
@@ -1063,12 +1066,12 @@ get_source_line(PyInterpreterState *interp, PyObject *module_globals, int lineno
1063
1066
return NULL ;
1064
1067
}
1065
1068
1066
- module_name = _PyDict_GetItemWithError (module_globals , & _Py_ID (__name__ ));
1067
- if (!module_name ) {
1069
+ int rc = PyDict_GetItemRef (module_globals , & _Py_ID (__name__ ),
1070
+ & module_name );
1071
+ if (rc < 0 || rc == 0 ) {
1068
1072
Py_DECREF (loader );
1069
1073
return NULL ;
1070
1074
}
1071
- Py_INCREF (module_name );
1072
1075
1073
1076
/* Make sure the loader implements the optional get_source() method. */
1074
1077
(void )PyObject_GetOptionalAttr (loader , & _Py_ID (get_source ), & get_source );
0 commit comments