@@ -2372,11 +2372,11 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
2372
2372
if (nhooks < 0 )
2373
2373
return NULL ; /* Shouldn't happen */
2374
2374
2375
- importer = PyDict_GetItemWithError ( path_importer_cache , p );
2376
- if ( importer != NULL || _PyErr_Occurred ( tstate )) {
2377
- return Py_XNewRef ( importer ) ;
2375
+ if ( PyDict_GetItemRef ( path_importer_cache , p , & importer ) != 0 ) {
2376
+ // found or error
2377
+ return importer ;
2378
2378
}
2379
-
2379
+ // not found
2380
2380
/* set path_importer_cache[p] to None to avoid recursion */
2381
2381
if (PyDict_SetItem (path_importer_cache , p , Py_None ) != 0 )
2382
2382
return NULL ;
@@ -2565,7 +2565,7 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
2565
2565
{
2566
2566
PyObject * abs_name ;
2567
2567
PyObject * package = NULL ;
2568
- PyObject * spec ;
2568
+ PyObject * spec = NULL ;
2569
2569
Py_ssize_t last_dot ;
2570
2570
PyObject * base ;
2571
2571
int level_up ;
@@ -2578,20 +2578,18 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
2578
2578
_PyErr_SetString (tstate , PyExc_TypeError , "globals must be a dict" );
2579
2579
goto error ;
2580
2580
}
2581
- package = PyDict_GetItemWithError (globals , & _Py_ID (__package__ ));
2581
+ if (PyDict_GetItemRef (globals , & _Py_ID (__package__ ), & package ) < 0 ) {
2582
+ goto error ;
2583
+ }
2582
2584
if (package == Py_None ) {
2585
+ Py_DECREF (package );
2583
2586
package = NULL ;
2584
2587
}
2585
- else if (package == NULL && _PyErr_Occurred (tstate )) {
2586
- goto error ;
2587
- }
2588
- spec = PyDict_GetItemWithError (globals , & _Py_ID (__spec__ ));
2589
- if (spec == NULL && _PyErr_Occurred (tstate )) {
2588
+ if (PyDict_GetItemRef (globals , & _Py_ID (__spec__ ), & spec ) < 0 ) {
2590
2589
goto error ;
2591
2590
}
2592
2591
2593
2592
if (package != NULL ) {
2594
- Py_INCREF (package );
2595
2593
if (!PyUnicode_Check (package )) {
2596
2594
_PyErr_SetString (tstate , PyExc_TypeError ,
2597
2595
"package must be a string" );
@@ -2635,16 +2633,15 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
2635
2633
goto error ;
2636
2634
}
2637
2635
2638
- package = PyDict_GetItemWithError (globals , & _Py_ID (__name__ ));
2636
+ if (PyDict_GetItemRef (globals , & _Py_ID (__name__ ), & package ) < 0 ) {
2637
+ goto error ;
2638
+ }
2639
2639
if (package == NULL ) {
2640
- if (!_PyErr_Occurred (tstate )) {
2641
- _PyErr_SetString (tstate , PyExc_KeyError ,
2642
- "'__name__' not in globals" );
2643
- }
2640
+ _PyErr_SetString (tstate , PyExc_KeyError ,
2641
+ "'__name__' not in globals" );
2644
2642
goto error ;
2645
2643
}
2646
2644
2647
- Py_INCREF (package );
2648
2645
if (!PyUnicode_Check (package )) {
2649
2646
_PyErr_SetString (tstate , PyExc_TypeError ,
2650
2647
"__name__ must be a string" );
@@ -2692,6 +2689,7 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
2692
2689
}
2693
2690
}
2694
2691
2692
+ Py_XDECREF (spec );
2695
2693
base = PyUnicode_Substring (package , 0 , last_dot );
2696
2694
Py_DECREF (package );
2697
2695
if (base == NULL || PyUnicode_GET_LENGTH (name ) == 0 ) {
@@ -2708,6 +2706,7 @@ resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level
2708
2706
"with no known parent package" );
2709
2707
2710
2708
error :
2709
+ Py_XDECREF (spec );
2711
2710
Py_XDECREF (package );
2712
2711
return NULL ;
2713
2712
}
0 commit comments