Skip to content

Commit 2e343fc

Browse files
authored
gh-99300: Use Py_NewRef() in Python/ceval.c (#99318)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in Python/ceval.c and related files.
1 parent 231d83b commit 2e343fc

3 files changed

Lines changed: 68 additions & 128 deletions

File tree

Python/bytecodes.c

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,9 @@ dummy_func(
941941
PUSH(value);
942942
DISPATCH();
943943
}
944-
Py_INCREF(exc_value);
945944
PyObject *exc_type = Py_NewRef(Py_TYPE(exc_value));
946945
PyObject *exc_traceback = PyException_GetTraceback(exc_value);
947-
_PyErr_Restore(tstate, exc_type, exc_value, exc_traceback);
946+
_PyErr_Restore(tstate, exc_type, Py_NewRef(exc_value), exc_traceback);
948947
goto exception_unwind;
949948
}
950949

@@ -982,8 +981,8 @@ dummy_func(
982981
}
983982
assert(PyExceptionInstance_Check(error));
984983
SET_TOP(error);
985-
PyException_SetCause(error, exc);
986-
Py_INCREF(exc);
984+
PyException_SetCause(error, Py_NewRef(exc));
985+
// Steal exc reference, rather than Py_NewRef+Py_DECREF
987986
PyException_SetContext(error, exc);
988987
Py_DECREF(message);
989988
}
@@ -994,8 +993,7 @@ dummy_func(
994993
// stack effect: ( -- __0)
995994
inst(LOAD_ASSERTION_ERROR) {
996995
PyObject *value = PyExc_AssertionError;
997-
Py_INCREF(value);
998-
PUSH(value);
996+
PUSH(Py_NewRef(value));
999997
}
1000998

1001999
// stack effect: ( -- __0)
@@ -1351,8 +1349,7 @@ dummy_func(
13511349
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
13521350
STAT_INC(LOAD_GLOBAL, hit);
13531351
STACK_GROW(push_null+1);
1354-
Py_INCREF(res);
1355-
SET_TOP(res);
1352+
SET_TOP(Py_NewRef(res));
13561353
}
13571354

13581355
// error: LOAD_GLOBAL has irregular stack effect
@@ -1376,8 +1373,7 @@ dummy_func(
13761373
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
13771374
STAT_INC(LOAD_GLOBAL, hit);
13781375
STACK_GROW(push_null+1);
1379-
Py_INCREF(res);
1380-
SET_TOP(res);
1376+
SET_TOP(Py_NewRef(res));
13811377
}
13821378

13831379
// stack effect: ( -- )
@@ -1459,8 +1455,7 @@ dummy_func(
14591455
format_exc_unbound(tstate, frame->f_code, oparg);
14601456
goto error;
14611457
}
1462-
Py_INCREF(value);
1463-
PUSH(value);
1458+
PUSH(Py_NewRef(value));
14641459
}
14651460

14661461
// stack effect: (__0 -- )
@@ -1482,8 +1477,7 @@ dummy_func(
14821477
assert(oparg == co->co_nfreevars);
14831478
for (int i = 0; i < oparg; ++i) {
14841479
PyObject *o = PyTuple_GET_ITEM(closure, i);
1485-
Py_INCREF(o);
1486-
frame->localsplus[offset + i] = o;
1480+
frame->localsplus[offset + i] = Py_NewRef(o);
14871481
}
14881482
}
14891483

@@ -1987,9 +1981,8 @@ dummy_func(
19871981
SET_TOP(NULL);
19881982
int shrink_stack = !(oparg & 1);
19891983
STACK_SHRINK(shrink_stack);
1990-
Py_INCREF(name);
19911984
new_frame->localsplus[0] = owner;
1992-
new_frame->localsplus[1] = name;
1985+
new_frame->localsplus[1] = Py_NewRef(name);
19931986
for (int i = 2; i < code->co_nlocalsplus; i++) {
19941987
new_frame->localsplus[i] = NULL;
19951988
}
@@ -2233,8 +2226,7 @@ dummy_func(
22332226
PyObject *left = TOP();
22342227
int res = Py_Is(left, right) ^ oparg;
22352228
PyObject *b = res ? Py_True : Py_False;
2236-
Py_INCREF(b);
2237-
SET_TOP(b);
2229+
SET_TOP(Py_NewRef(b));
22382230
Py_DECREF(left);
22392231
Py_DECREF(right);
22402232
}
@@ -2250,8 +2242,7 @@ dummy_func(
22502242
goto error;
22512243
}
22522244
PyObject *b = (res^oparg) ? Py_True : Py_False;
2253-
Py_INCREF(b);
2254-
PUSH(b);
2245+
PUSH(Py_NewRef(b));
22552246
}
22562247

22572248
// stack effect: ( -- )
@@ -2532,8 +2523,7 @@ dummy_func(
25322523
}
25332524
else {
25342525
// Failure!
2535-
Py_INCREF(Py_None);
2536-
SET_TOP(Py_None);
2526+
SET_TOP(Py_NewRef(Py_None));
25372527
}
25382528
Py_DECREF(subject);
25392529
}
@@ -2543,8 +2533,7 @@ dummy_func(
25432533
PyObject *subject = TOP();
25442534
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING;
25452535
PyObject *res = match ? Py_True : Py_False;
2546-
Py_INCREF(res);
2547-
PUSH(res);
2536+
PUSH(Py_NewRef(res));
25482537
PREDICT(POP_JUMP_IF_FALSE);
25492538
}
25502539

@@ -2553,8 +2542,7 @@ dummy_func(
25532542
PyObject *subject = TOP();
25542543
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE;
25552544
PyObject *res = match ? Py_True : Py_False;
2556-
Py_INCREF(res);
2557-
PUSH(res);
2545+
PUSH(Py_NewRef(res));
25582546
PREDICT(POP_JUMP_IF_FALSE);
25592547
}
25602548

@@ -2656,8 +2644,7 @@ dummy_func(
26562644
if (seq) {
26572645
if (it->it_index < PyList_GET_SIZE(seq)) {
26582646
PyObject *next = PyList_GET_ITEM(seq, it->it_index++);
2659-
Py_INCREF(next);
2660-
PUSH(next);
2647+
PUSH(Py_NewRef(next));
26612648
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER);
26622649
DISPATCH();
26632650
}
@@ -2704,8 +2691,7 @@ dummy_func(
27042691
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg);
27052692
assert(_Py_OPCODE(*next_instr) == END_FOR);
27062693
frame->prev_instr = next_instr - 1;
2707-
Py_INCREF(Py_None);
2708-
_PyFrame_StackPush(gen_frame, Py_None);
2694+
_PyFrame_StackPush(gen_frame, Py_NewRef(Py_None));
27092695
gen->gi_frame_state = FRAME_EXECUTING;
27102696
gen->gi_exc_state.previous_item = tstate->exc_info;
27112697
tstate->exc_info = &gen->gi_exc_state;
@@ -2826,12 +2812,10 @@ dummy_func(
28262812
SET_TOP(exc_info->exc_value);
28272813
}
28282814
else {
2829-
Py_INCREF(Py_None);
2830-
SET_TOP(Py_None);
2815+
SET_TOP(Py_NewRef(Py_None));
28312816
}
28322817

2833-
Py_INCREF(value);
2834-
PUSH(value);
2818+
PUSH(Py_NewRef(value));
28352819
assert(PyExceptionInstance_Check(value));
28362820
exc_info->exc_value = value;
28372821

@@ -2857,8 +2841,7 @@ dummy_func(
28572841
PyObject *res = read_obj(cache->descr);
28582842
assert(res != NULL);
28592843
assert(_PyType_HasFeature(Py_TYPE(res), Py_TPFLAGS_METHOD_DESCRIPTOR));
2860-
Py_INCREF(res);
2861-
SET_TOP(res);
2844+
SET_TOP(Py_NewRef(res));
28622845
PUSH(self);
28632846
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_ATTR);
28642847
}
@@ -2885,8 +2868,7 @@ dummy_func(
28852868
PyObject *res = read_obj(cache->descr);
28862869
assert(res != NULL);
28872870
assert(_PyType_HasFeature(Py_TYPE(res), Py_TPFLAGS_METHOD_DESCRIPTOR));
2888-
Py_INCREF(res);
2889-
SET_TOP(res);
2871+
SET_TOP(Py_NewRef(res));
28902872
PUSH(self);
28912873
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_ATTR);
28922874
}
@@ -2904,8 +2886,7 @@ dummy_func(
29042886
PyObject *res = read_obj(cache->descr);
29052887
assert(res != NULL);
29062888
assert(_PyType_HasFeature(Py_TYPE(res), Py_TPFLAGS_METHOD_DESCRIPTOR));
2907-
Py_INCREF(res);
2908-
SET_TOP(res);
2889+
SET_TOP(Py_NewRef(res));
29092890
PUSH(self);
29102891
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_ATTR);
29112892
}
@@ -2927,8 +2908,7 @@ dummy_func(
29272908
PyObject *res = read_obj(cache->descr);
29282909
assert(res != NULL);
29292910
assert(_PyType_HasFeature(Py_TYPE(res), Py_TPFLAGS_METHOD_DESCRIPTOR));
2930-
Py_INCREF(res);
2931-
SET_TOP(res);
2911+
SET_TOP(Py_NewRef(res));
29322912
PUSH(self);
29332913
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_ATTR);
29342914
}
@@ -2939,12 +2919,10 @@ dummy_func(
29392919
PyObject *function = PEEK(oparg + 1);
29402920
DEOPT_IF(Py_TYPE(function) != &PyMethod_Type, CALL);
29412921
STAT_INC(CALL, hit);
2942-
PyObject *meth = ((PyMethodObject *)function)->im_func;
29432922
PyObject *self = ((PyMethodObject *)function)->im_self;
2944-
Py_INCREF(meth);
2945-
Py_INCREF(self);
2946-
PEEK(oparg + 1) = self;
2947-
PEEK(oparg + 2) = meth;
2923+
PEEK(oparg + 1) = Py_NewRef(self);
2924+
PyObject *meth = ((PyMethodObject *)function)->im_func;
2925+
PEEK(oparg + 2) = Py_NewRef(meth);
29482926
Py_DECREF(function);
29492927
GO_TO_INSTRUCTION(CALL_PY_EXACT_ARGS);
29502928
}
@@ -2974,12 +2952,10 @@ dummy_func(
29742952
is_meth = is_method(stack_pointer, oparg);
29752953
PyObject *function = PEEK(oparg + 1);
29762954
if (!is_meth && Py_TYPE(function) == &PyMethod_Type) {
2977-
PyObject *meth = ((PyMethodObject *)function)->im_func;
29782955
PyObject *self = ((PyMethodObject *)function)->im_self;
2979-
Py_INCREF(meth);
2980-
Py_INCREF(self);
2981-
PEEK(oparg+1) = self;
2982-
PEEK(oparg+2) = meth;
2956+
PEEK(oparg+1) = Py_NewRef(self);
2957+
PyObject *meth = ((PyMethodObject *)function)->im_func;
2958+
PEEK(oparg+2) = Py_NewRef(meth);
29832959
Py_DECREF(function);
29842960
is_meth = 1;
29852961
}
@@ -3102,8 +3078,7 @@ dummy_func(
31023078
for (int i = argcount; i < code->co_argcount; i++) {
31033079
PyObject *def = PyTuple_GET_ITEM(func->func_defaults,
31043080
i - minargs);
3105-
Py_INCREF(def);
3106-
new_frame->localsplus[i] = def;
3081+
new_frame->localsplus[i] = Py_NewRef(def);
31073082
}
31083083
for (int i = code->co_argcount; i < code->co_nlocalsplus; i++) {
31093084
new_frame->localsplus[i] = NULL;
@@ -3734,8 +3709,7 @@ dummy_func(
37343709
inst(COPY) {
37353710
assert(oparg != 0);
37363711
PyObject *peek = PEEK(oparg);
3737-
Py_INCREF(peek);
3738-
PUSH(peek);
3712+
PUSH(Py_NewRef(peek));
37393713
}
37403714

37413715
// stack effect: (__0 -- )

Python/ceval.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,7 @@ match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
391391
Py_DECREF(value);
392392
Py_DECREF(values);
393393
// Return None:
394-
Py_INCREF(Py_None);
395-
values = Py_None;
394+
values = Py_NewRef(Py_None);
396395
goto done;
397396
}
398397
PyTuple_SET_ITEM(values, i, value);
@@ -1883,8 +1882,7 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
18831882
for (; i < defcount; i++) {
18841883
if (localsplus[m+i] == NULL) {
18851884
PyObject *def = defs[i];
1886-
Py_INCREF(def);
1887-
localsplus[m+i] = def;
1885+
localsplus[m+i] = Py_NewRef(def);
18881886
}
18891887
}
18901888
}
@@ -1900,8 +1898,7 @@ initialize_locals(PyThreadState *tstate, PyFunctionObject *func,
19001898
if (func->func_kwdefaults != NULL) {
19011899
PyObject *def = PyDict_GetItemWithError(func->func_kwdefaults, varname);
19021900
if (def) {
1903-
Py_INCREF(def);
1904-
localsplus[i] = def;
1901+
localsplus[i] = Py_NewRef(def);
19051902
continue;
19061903
}
19071904
else if (_PyErr_Occurred(tstate)) {
@@ -2092,15 +2089,13 @@ PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
20922089
newargs[i] = args[i];
20932090
}
20942091
for (int i = 0; i < kwcount; i++) {
2095-
Py_INCREF(kws[2*i]);
2096-
PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
2092+
PyTuple_SET_ITEM(kwnames, i, Py_NewRef(kws[2*i]));
20972093
newargs[argcount+i] = kws[2*i+1];
20982094
}
20992095
allargs = newargs;
21002096
}
21012097
for (int i = 0; i < kwcount; i++) {
2102-
Py_INCREF(kws[2*i]);
2103-
PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
2098+
PyTuple_SET_ITEM(kwnames, i, Py_NewRef(kws[2*i]));
21042099
}
21052100
PyFrameConstructor constr = {
21062101
.fc_globals = globals,
@@ -2395,8 +2390,7 @@ call_exc_trace(Py_tracefunc func, PyObject *self,
23952390
int err;
23962391
_PyErr_Fetch(tstate, &type, &value, &orig_traceback);
23972392
if (value == NULL) {
2398-
value = Py_None;
2399-
Py_INCREF(value);
2393+
value = Py_NewRef(Py_None);
24002394
}
24012395
_PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
24022396
traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
@@ -2699,8 +2693,7 @@ _PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
26992693
return -1;
27002694
}
27012695

2702-
Py_XINCREF(firstiter);
2703-
Py_XSETREF(tstate->async_gen_firstiter, firstiter);
2696+
Py_XSETREF(tstate->async_gen_firstiter, Py_XNewRef(firstiter));
27042697
return 0;
27052698
}
27062699

@@ -2720,8 +2713,7 @@ _PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
27202713
return -1;
27212714
}
27222715

2723-
Py_XINCREF(finalizer);
2724-
Py_XSETREF(tstate->async_gen_finalizer, finalizer);
2716+
Py_XSETREF(tstate->async_gen_finalizer, Py_XNewRef(finalizer));
27252717
return 0;
27262718
}
27272719

0 commit comments

Comments
 (0)