Skip to content

Commit b4a9747

Browse files
authored
GH-103906: Remove immortal refcounting in the interpreter (GH-103909)
1 parent 3a4c44b commit b4a9747

File tree

4 files changed

+476
-516
lines changed

4 files changed

+476
-516
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Don't modify the refcounts of known immortal objects (:const:`True`,
2+
:const:`False`, and :const:`None`) in the main interpreter loop.

Objects/boolobject.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ bool_repr(PyObject *self)
2020

2121
PyObject *PyBool_FromLong(long ok)
2222
{
23-
PyObject *result;
24-
25-
if (ok)
26-
result = Py_True;
27-
else
28-
result = Py_False;
29-
return Py_NewRef(result);
23+
return ok ? Py_True : Py_False;
3024
}
3125

3226
/* We define bool_new to always return either Py_True or Py_False */

Python/bytecodes.c

+14-32
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ dummy_func(
270270
else {
271271
res = Py_False;
272272
}
273-
Py_INCREF(res);
274273
}
275274

276275
inst(UNARY_INVERT, (value -- res)) {
@@ -967,7 +966,7 @@ dummy_func(
967966
if (PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration)) {
968967
value = Py_NewRef(((PyStopIterationObject *)exc_value)->value);
969968
DECREF_INPUTS();
970-
none = Py_NewRef(Py_None);
969+
none = Py_None;
971970
}
972971
else {
973972
_PyErr_SetRaisedException(tstate, Py_NewRef(exc_value));
@@ -1452,7 +1451,7 @@ dummy_func(
14521451
DECREF_INPUTS();
14531452
ERROR_IF(true, error);
14541453
}
1455-
Py_DECREF(none_val);
1454+
assert(Py_IsNone(none_val));
14561455
DECREF_INPUTS();
14571456
}
14581457

@@ -1993,7 +1992,6 @@ dummy_func(
19931992
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
19941993
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
19951994
res = (sign_ish & oparg) ? Py_True : Py_False;
1996-
Py_INCREF(res);
19971995
}
19981996

19991997
// Similar to COMPARE_OP_FLOAT
@@ -2012,7 +2010,6 @@ dummy_func(
20122010
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
20132011
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
20142012
res = (sign_ish & oparg) ? Py_True : Py_False;
2015-
Py_INCREF(res);
20162013
}
20172014

20182015
// Similar to COMPARE_OP_FLOAT, but for ==, != only
@@ -2028,20 +2025,19 @@ dummy_func(
20282025
assert((oparg & 0xf) == COMPARISON_NOT_EQUALS || (oparg & 0xf) == COMPARISON_EQUALS);
20292026
assert(COMPARISON_NOT_EQUALS + 1 == COMPARISON_EQUALS);
20302027
res = ((COMPARISON_NOT_EQUALS + eq) & oparg) ? Py_True : Py_False;
2031-
Py_INCREF(res);
20322028
}
20332029

20342030
inst(IS_OP, (left, right -- b)) {
20352031
int res = Py_Is(left, right) ^ oparg;
20362032
DECREF_INPUTS();
2037-
b = Py_NewRef(res ? Py_True : Py_False);
2033+
b = res ? Py_True : Py_False;
20382034
}
20392035

20402036
inst(CONTAINS_OP, (left, right -- b)) {
20412037
int res = PySequence_Contains(right, left);
20422038
DECREF_INPUTS();
20432039
ERROR_IF(res < 0, error);
2044-
b = Py_NewRef((res^oparg) ? Py_True : Py_False);
2040+
b = (res ^ oparg) ? Py_True : Py_False;
20452041
}
20462042

20472043
inst(CHECK_EG_MATCH, (exc_value, match_type -- rest, match)) {
@@ -2074,7 +2070,7 @@ dummy_func(
20742070

20752071
int res = PyErr_GivenExceptionMatches(left, right);
20762072
DECREF_INPUTS();
2077-
b = Py_NewRef(res ? Py_True : Py_False);
2073+
b = res ? Py_True : Py_False;
20782074
}
20792075

20802076
inst(IMPORT_NAME, (level, fromlist -- res)) {
@@ -2101,14 +2097,10 @@ dummy_func(
21012097
}
21022098

21032099
inst(POP_JUMP_IF_FALSE, (cond -- )) {
2104-
if (Py_IsTrue(cond)) {
2105-
_Py_DECREF_NO_DEALLOC(cond);
2106-
}
2107-
else if (Py_IsFalse(cond)) {
2108-
_Py_DECREF_NO_DEALLOC(cond);
2100+
if (Py_IsFalse(cond)) {
21092101
JUMPBY(oparg);
21102102
}
2111-
else {
2103+
else if (!Py_IsTrue(cond)) {
21122104
int err = PyObject_IsTrue(cond);
21132105
DECREF_INPUTS();
21142106
if (err == 0) {
@@ -2121,14 +2113,10 @@ dummy_func(
21212113
}
21222114

21232115
inst(POP_JUMP_IF_TRUE, (cond -- )) {
2124-
if (Py_IsFalse(cond)) {
2125-
_Py_DECREF_NO_DEALLOC(cond);
2126-
}
2127-
else if (Py_IsTrue(cond)) {
2128-
_Py_DECREF_NO_DEALLOC(cond);
2116+
if (Py_IsTrue(cond)) {
21292117
JUMPBY(oparg);
21302118
}
2131-
else {
2119+
else if (!Py_IsFalse(cond)) {
21322120
int err = PyObject_IsTrue(cond);
21332121
DECREF_INPUTS();
21342122
if (err > 0) {
@@ -2145,14 +2133,10 @@ dummy_func(
21452133
DECREF_INPUTS();
21462134
JUMPBY(oparg);
21472135
}
2148-
else {
2149-
_Py_DECREF_NO_DEALLOC(value);
2150-
}
21512136
}
21522137

21532138
inst(POP_JUMP_IF_NONE, (value -- )) {
21542139
if (Py_IsNone(value)) {
2155-
_Py_DECREF_NO_DEALLOC(value);
21562140
JUMPBY(oparg);
21572141
}
21582142
else {
@@ -2188,19 +2172,19 @@ dummy_func(
21882172
}
21892173
else {
21902174
ERROR_IF(_PyErr_Occurred(tstate), error); // Error!
2191-
attrs = Py_NewRef(Py_None); // Failure!
2175+
attrs = Py_None; // Failure!
21922176
}
21932177
}
21942178

21952179
inst(MATCH_MAPPING, (subject -- subject, res)) {
21962180
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING;
2197-
res = Py_NewRef(match ? Py_True : Py_False);
2181+
res = match ? Py_True : Py_False;
21982182
PREDICT(POP_JUMP_IF_FALSE);
21992183
}
22002184

22012185
inst(MATCH_SEQUENCE, (subject -- subject, res)) {
22022186
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE;
2203-
res = Py_NewRef(match ? Py_True : Py_False);
2187+
res = match ? Py_True : Py_False;
22042188
PREDICT(POP_JUMP_IF_FALSE);
22052189
}
22062190

@@ -2392,7 +2376,7 @@ dummy_func(
23922376
STAT_INC(FOR_ITER, hit);
23932377
_PyInterpreterFrame *gen_frame = (_PyInterpreterFrame *)gen->gi_iframe;
23942378
frame->return_offset = oparg;
2395-
_PyFrame_StackPush(gen_frame, Py_NewRef(Py_None));
2379+
_PyFrame_StackPush(gen_frame, Py_None);
23962380
gen->gi_frame_state = FRAME_EXECUTING;
23972381
gen->gi_exc_state.previous_item = tstate->exc_info;
23982382
tstate->exc_info = &gen->gi_exc_state;
@@ -2499,7 +2483,7 @@ dummy_func(
24992483
prev_exc = exc_info->exc_value;
25002484
}
25012485
else {
2502-
prev_exc = Py_NewRef(Py_None);
2486+
prev_exc = Py_None;
25032487
}
25042488
assert(PyExceptionInstance_Check(new_exc));
25052489
exc_info->exc_value = Py_NewRef(new_exc);
@@ -3393,7 +3377,6 @@ dummy_func(
33933377
_Py_CODEUNIT *here = next_instr-1;
33943378
int offset;
33953379
if (Py_IsNone(value)) {
3396-
_Py_DECREF_NO_DEALLOC(value);
33973380
offset = oparg;
33983381
}
33993382
else {
@@ -3408,7 +3391,6 @@ dummy_func(
34083391
_Py_CODEUNIT *here = next_instr-1;
34093392
int offset;
34103393
if (Py_IsNone(value)) {
3411-
_Py_DECREF_NO_DEALLOC(value);
34123394
offset = 0;
34133395
}
34143396
else {

0 commit comments

Comments
 (0)