Skip to content

Commit 81d41b2

Browse files
committed
pythongh-103906: Avoid refcount changes for True, False, None
1 parent a3a5b4b commit 81d41b2

File tree

2 files changed

+207
-213
lines changed

2 files changed

+207
-213
lines changed

Python/bytecodes.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ dummy_func(
957957
if (PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration)) {
958958
value = Py_NewRef(((PyStopIterationObject *)exc_value)->value);
959959
DECREF_INPUTS();
960-
none = Py_NewRef(Py_None);
960+
none = Py_None;
961961
}
962962
else {
963963
_PyErr_SetRaisedException(tstate, Py_NewRef(exc_value));
@@ -1911,7 +1911,6 @@ dummy_func(
19111911
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
19121912
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
19131913
res = (sign_ish & oparg) ? Py_True : Py_False;
1914-
Py_INCREF(res);
19151914
}
19161915

19171916
// Similar to COMPARE_OP_FLOAT
@@ -1930,7 +1929,6 @@ dummy_func(
19301929
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
19311930
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
19321931
res = (sign_ish & oparg) ? Py_True : Py_False;
1933-
Py_INCREF(res);
19341932
}
19351933

19361934
// Similar to COMPARE_OP_FLOAT, but for ==, != only
@@ -1946,20 +1944,19 @@ dummy_func(
19461944
assert((oparg & 0xf) == COMPARISON_NOT_EQUALS || (oparg & 0xf) == COMPARISON_EQUALS);
19471945
assert(COMPARISON_NOT_EQUALS + 1 == COMPARISON_EQUALS);
19481946
res = ((COMPARISON_NOT_EQUALS + eq) & oparg) ? Py_True : Py_False;
1949-
Py_INCREF(res);
19501947
}
19511948

19521949
inst(IS_OP, (left, right -- b)) {
19531950
int res = Py_Is(left, right) ^ oparg;
19541951
DECREF_INPUTS();
1955-
b = Py_NewRef(res ? Py_True : Py_False);
1952+
b = res ? Py_True : Py_False;
19561953
}
19571954

19581955
inst(CONTAINS_OP, (left, right -- b)) {
19591956
int res = PySequence_Contains(right, left);
19601957
DECREF_INPUTS();
19611958
ERROR_IF(res < 0, error);
1962-
b = Py_NewRef((res^oparg) ? Py_True : Py_False);
1959+
b = (res^oparg) ? Py_True : Py_False;
19631960
}
19641961

19651962
inst(CHECK_EG_MATCH, (exc_value, match_type -- rest, match)) {
@@ -1992,7 +1989,7 @@ dummy_func(
19921989

19931990
int res = PyErr_GivenExceptionMatches(left, right);
19941991
DECREF_INPUTS();
1995-
b = Py_NewRef(res ? Py_True : Py_False);
1992+
b = res ? Py_True : Py_False;
19961993
}
19971994

19981995
inst(IMPORT_NAME, (level, fromlist -- res)) {
@@ -2106,19 +2103,19 @@ dummy_func(
21062103
}
21072104
else {
21082105
ERROR_IF(_PyErr_Occurred(tstate), error); // Error!
2109-
attrs = Py_NewRef(Py_None); // Failure!
2106+
attrs = Py_None; // Failure!
21102107
}
21112108
}
21122109

21132110
inst(MATCH_MAPPING, (subject -- subject, res)) {
21142111
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING;
2115-
res = Py_NewRef(match ? Py_True : Py_False);
2112+
res = match ? Py_True : Py_False;
21162113
PREDICT(POP_JUMP_IF_FALSE);
21172114
}
21182115

21192116
inst(MATCH_SEQUENCE, (subject -- subject, res)) {
21202117
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE;
2121-
res = Py_NewRef(match ? Py_True : Py_False);
2118+
res = match ? Py_True : Py_False;
21222119
PREDICT(POP_JUMP_IF_FALSE);
21232120
}
21242121

@@ -2309,7 +2306,7 @@ dummy_func(
23092306
STAT_INC(FOR_ITER, hit);
23102307
_PyInterpreterFrame *gen_frame = (_PyInterpreterFrame *)gen->gi_iframe;
23112308
frame->return_offset = oparg;
2312-
_PyFrame_StackPush(gen_frame, Py_NewRef(Py_None));
2309+
_PyFrame_StackPush(gen_frame, Py_None);
23132310
gen->gi_frame_state = FRAME_EXECUTING;
23142311
gen->gi_exc_state.previous_item = tstate->exc_info;
23152312
tstate->exc_info = &gen->gi_exc_state;
@@ -2416,7 +2413,7 @@ dummy_func(
24162413
prev_exc = exc_info->exc_value;
24172414
}
24182415
else {
2419-
prev_exc = Py_NewRef(Py_None);
2416+
prev_exc = Py_None;
24202417
}
24212418
assert(PyExceptionInstance_Check(new_exc));
24222419
exc_info->exc_value = Py_NewRef(new_exc);

0 commit comments

Comments
 (0)