Skip to content

Commit

Permalink
GH-103906: Remove immortal refcounting in the interpreter (GH-103909)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandtbucher authored May 16, 2023
1 parent 3a4c44b commit b4a9747
Show file tree
Hide file tree
Showing 4 changed files with 476 additions and 516 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Don't modify the refcounts of known immortal objects (:const:`True`,
:const:`False`, and :const:`None`) in the main interpreter loop.
8 changes: 1 addition & 7 deletions Objects/boolobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ bool_repr(PyObject *self)

PyObject *PyBool_FromLong(long ok)
{
PyObject *result;

if (ok)
result = Py_True;
else
result = Py_False;
return Py_NewRef(result);
return ok ? Py_True : Py_False;
}

/* We define bool_new to always return either Py_True or Py_False */
Expand Down
46 changes: 14 additions & 32 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ dummy_func(
else {
res = Py_False;
}
Py_INCREF(res);
}

inst(UNARY_INVERT, (value -- res)) {
Expand Down Expand Up @@ -967,7 +966,7 @@ dummy_func(
if (PyErr_GivenExceptionMatches(exc_value, PyExc_StopIteration)) {
value = Py_NewRef(((PyStopIterationObject *)exc_value)->value);
DECREF_INPUTS();
none = Py_NewRef(Py_None);
none = Py_None;
}
else {
_PyErr_SetRaisedException(tstate, Py_NewRef(exc_value));
Expand Down Expand Up @@ -1452,7 +1451,7 @@ dummy_func(
DECREF_INPUTS();
ERROR_IF(true, error);
}
Py_DECREF(none_val);
assert(Py_IsNone(none_val));
DECREF_INPUTS();
}

Expand Down Expand Up @@ -1993,7 +1992,6 @@ dummy_func(
_Py_DECREF_SPECIALIZED(left, _PyFloat_ExactDealloc);
_Py_DECREF_SPECIALIZED(right, _PyFloat_ExactDealloc);
res = (sign_ish & oparg) ? Py_True : Py_False;
Py_INCREF(res);
}

// Similar to COMPARE_OP_FLOAT
Expand All @@ -2012,7 +2010,6 @@ dummy_func(
_Py_DECREF_SPECIALIZED(left, (destructor)PyObject_Free);
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
res = (sign_ish & oparg) ? Py_True : Py_False;
Py_INCREF(res);
}

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

inst(IS_OP, (left, right -- b)) {
int res = Py_Is(left, right) ^ oparg;
DECREF_INPUTS();
b = Py_NewRef(res ? Py_True : Py_False);
b = res ? Py_True : Py_False;
}

inst(CONTAINS_OP, (left, right -- b)) {
int res = PySequence_Contains(right, left);
DECREF_INPUTS();
ERROR_IF(res < 0, error);
b = Py_NewRef((res^oparg) ? Py_True : Py_False);
b = (res ^ oparg) ? Py_True : Py_False;
}

inst(CHECK_EG_MATCH, (exc_value, match_type -- rest, match)) {
Expand Down Expand Up @@ -2074,7 +2070,7 @@ dummy_func(

int res = PyErr_GivenExceptionMatches(left, right);
DECREF_INPUTS();
b = Py_NewRef(res ? Py_True : Py_False);
b = res ? Py_True : Py_False;
}

inst(IMPORT_NAME, (level, fromlist -- res)) {
Expand All @@ -2101,14 +2097,10 @@ dummy_func(
}

inst(POP_JUMP_IF_FALSE, (cond -- )) {
if (Py_IsTrue(cond)) {
_Py_DECREF_NO_DEALLOC(cond);
}
else if (Py_IsFalse(cond)) {
_Py_DECREF_NO_DEALLOC(cond);
if (Py_IsFalse(cond)) {
JUMPBY(oparg);
}
else {
else if (!Py_IsTrue(cond)) {
int err = PyObject_IsTrue(cond);
DECREF_INPUTS();
if (err == 0) {
Expand All @@ -2121,14 +2113,10 @@ dummy_func(
}

inst(POP_JUMP_IF_TRUE, (cond -- )) {
if (Py_IsFalse(cond)) {
_Py_DECREF_NO_DEALLOC(cond);
}
else if (Py_IsTrue(cond)) {
_Py_DECREF_NO_DEALLOC(cond);
if (Py_IsTrue(cond)) {
JUMPBY(oparg);
}
else {
else if (!Py_IsFalse(cond)) {
int err = PyObject_IsTrue(cond);
DECREF_INPUTS();
if (err > 0) {
Expand All @@ -2145,14 +2133,10 @@ dummy_func(
DECREF_INPUTS();
JUMPBY(oparg);
}
else {
_Py_DECREF_NO_DEALLOC(value);
}
}

inst(POP_JUMP_IF_NONE, (value -- )) {
if (Py_IsNone(value)) {
_Py_DECREF_NO_DEALLOC(value);
JUMPBY(oparg);
}
else {
Expand Down Expand Up @@ -2188,19 +2172,19 @@ dummy_func(
}
else {
ERROR_IF(_PyErr_Occurred(tstate), error); // Error!
attrs = Py_NewRef(Py_None); // Failure!
attrs = Py_None; // Failure!
}
}

inst(MATCH_MAPPING, (subject -- subject, res)) {
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_MAPPING;
res = Py_NewRef(match ? Py_True : Py_False);
res = match ? Py_True : Py_False;
PREDICT(POP_JUMP_IF_FALSE);
}

inst(MATCH_SEQUENCE, (subject -- subject, res)) {
int match = Py_TYPE(subject)->tp_flags & Py_TPFLAGS_SEQUENCE;
res = Py_NewRef(match ? Py_True : Py_False);
res = match ? Py_True : Py_False;
PREDICT(POP_JUMP_IF_FALSE);
}

Expand Down Expand Up @@ -2392,7 +2376,7 @@ dummy_func(
STAT_INC(FOR_ITER, hit);
_PyInterpreterFrame *gen_frame = (_PyInterpreterFrame *)gen->gi_iframe;
frame->return_offset = oparg;
_PyFrame_StackPush(gen_frame, Py_NewRef(Py_None));
_PyFrame_StackPush(gen_frame, Py_None);
gen->gi_frame_state = FRAME_EXECUTING;
gen->gi_exc_state.previous_item = tstate->exc_info;
tstate->exc_info = &gen->gi_exc_state;
Expand Down Expand Up @@ -2499,7 +2483,7 @@ dummy_func(
prev_exc = exc_info->exc_value;
}
else {
prev_exc = Py_NewRef(Py_None);
prev_exc = Py_None;
}
assert(PyExceptionInstance_Check(new_exc));
exc_info->exc_value = Py_NewRef(new_exc);
Expand Down Expand Up @@ -3393,7 +3377,6 @@ dummy_func(
_Py_CODEUNIT *here = next_instr-1;
int offset;
if (Py_IsNone(value)) {
_Py_DECREF_NO_DEALLOC(value);
offset = oparg;
}
else {
Expand All @@ -3408,7 +3391,6 @@ dummy_func(
_Py_CODEUNIT *here = next_instr-1;
int offset;
if (Py_IsNone(value)) {
_Py_DECREF_NO_DEALLOC(value);
offset = 0;
}
else {
Expand Down
Loading

0 comments on commit b4a9747

Please sign in to comment.