Skip to content

Commit

Permalink
pythongh-103906: Avoid refcount changes for True, False, None
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Apr 26, 2023
1 parent a3a5b4b commit 81d41b2
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 213 deletions.
21 changes: 9 additions & 12 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,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 @@ -1911,7 +1911,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 @@ -1930,7 +1929,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 @@ -1946,20 +1944,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 @@ -1992,7 +1989,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 Down Expand Up @@ -2106,19 +2103,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 @@ -2309,7 +2306,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 @@ -2416,7 +2413,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
Loading

0 comments on commit 81d41b2

Please sign in to comment.