@@ -1372,17 +1372,17 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose)
13721372 if (verbose ) {
13731373 PySys_WriteStderr ("# restore sys.%s\n" , name );
13741374 }
1375- PyObject * value = _PyDict_GetItemStringWithError (interp -> sysdict ,
1376- orig_name );
1377- if (value == NULL ) {
1378- if (_PyErr_Occurred (tstate )) {
1379- PyErr_WriteUnraisable (NULL );
1380- }
1381- value = Py_None ;
1375+ PyObject * value ;
1376+ if (PyDict_GetItemStringRef (interp -> sysdict , orig_name , & value ) < 0 ) {
1377+ PyErr_WriteUnraisable (NULL );
1378+ value = Py_NewRef (Py_None );
1379+ } else if (value == NULL ) {
1380+ value = Py_NewRef (Py_None );
13821381 }
13831382 if (PyDict_SetItemString (interp -> sysdict , name , value ) < 0 ) {
13841383 PyErr_WriteUnraisable (NULL );
13851384 }
1385+ Py_DECREF (value );
13861386 }
13871387}
13881388
@@ -2207,7 +2207,7 @@ _Py_IsInterpreterFinalizing(PyInterpreterState *interp)
22072207static PyStatus
22082208add_main_module (PyInterpreterState * interp )
22092209{
2210- PyObject * m , * d , * loader , * ann_dict ;
2210+ PyObject * m , * d , * ann_dict ;
22112211 m = PyImport_AddModule ("__main__" );
22122212 if (m == NULL )
22132213 return _PyStatus_ERR ("can't create __main__ module" );
@@ -2220,10 +2220,13 @@ add_main_module(PyInterpreterState *interp)
22202220 }
22212221 Py_DECREF (ann_dict );
22222222
2223- if (_PyDict_GetItemStringWithError (d , "__builtins__" ) == NULL ) {
2224- if (PyErr_Occurred ()) {
2225- return _PyStatus_ERR ("Failed to test __main__.__builtins__" );
2226- }
2223+ PyObject * builtins ;
2224+ if (PyDict_GetItemStringRef (d , "__builtins__" , & builtins ) < 0 ) {
2225+ return _PyStatus_ERR ("Failed to test __main__.__builtins__" );
2226+ }
2227+ int has_buitlins = (builtins != NULL );
2228+ Py_XDECREF (builtins );
2229+ if (!has_buitlins ) {
22272230 PyObject * bimod = PyImport_ImportModule ("builtins" );
22282231 if (bimod == NULL ) {
22292232 return _PyStatus_ERR ("Failed to retrieve builtins module" );
@@ -2239,11 +2242,13 @@ add_main_module(PyInterpreterState *interp)
22392242 * will be set if __main__ gets further initialized later in the startup
22402243 * process.
22412244 */
2242- loader = _PyDict_GetItemStringWithError (d , "__loader__" );
2243- if (loader == NULL || loader == Py_None ) {
2244- if (PyErr_Occurred ()) {
2245- return _PyStatus_ERR ("Failed to test __main__.__loader__" );
2246- }
2245+ PyObject * loader ;
2246+ if (PyDict_GetItemStringRef (d , "__loader__" , & loader ) < 0 ) {
2247+ return _PyStatus_ERR ("Failed to test __main__.__loader__" );
2248+ }
2249+ int has_loader = !(loader == NULL || loader == Py_None );
2250+ Py_XDECREF (loader );
2251+ if (!has_loader ) {
22472252 PyObject * loader = _PyImport_GetImportlibLoader (interp ,
22482253 "BuiltinImporter" );
22492254 if (loader == NULL ) {
0 commit comments