Skip to content

Commit

Permalink
pythongh-98831: Use DECREF_INPUTS() more (python#102409)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum authored and warsaw committed Apr 11, 2023
1 parent 2e4ec93 commit 9cf44ab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 57 deletions.
80 changes: 31 additions & 49 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ dummy_func(
if (!_PyErr_Occurred(tstate)) {
_PyErr_SetKeyError(sub);
}
Py_DECREF(dict);
Py_DECREF(sub);
DECREF_INPUTS();
ERROR_IF(true, error);
}
Py_INCREF(res); // Do this before DECREF'ing dict, sub
Expand Down Expand Up @@ -420,7 +419,7 @@ dummy_func(

inst(SET_ADD, (set, unused[oparg-1], v -- set, unused[oparg-1])) {
int err = PySet_Add(set, v);
Py_DECREF(v);
DECREF_INPUTS();
ERROR_IF(err, error);
PREDICT(JUMP_BACKWARD);
}
Expand Down Expand Up @@ -899,7 +898,7 @@ dummy_func(
#endif /* ENABLE_SPECIALIZATION */
PyObject **top = stack_pointer + oparg - 1;
int res = unpack_iterable(tstate, seq, oparg, -1, top);
Py_DECREF(seq);
DECREF_INPUTS();
ERROR_IF(res == 0, error);
}

Expand All @@ -910,7 +909,7 @@ dummy_func(
STAT_INC(UNPACK_SEQUENCE, hit);
values[0] = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
values[1] = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
Py_DECREF(seq);
DECREF_INPUTS();
}

inst(UNPACK_SEQUENCE_TUPLE, (unused/1, seq -- values[oparg])) {
Expand All @@ -921,7 +920,7 @@ dummy_func(
for (int i = oparg; --i >= 0; ) {
*values++ = Py_NewRef(items[i]);
}
Py_DECREF(seq);
DECREF_INPUTS();
}

inst(UNPACK_SEQUENCE_LIST, (unused/1, seq -- values[oparg])) {
Expand All @@ -932,14 +931,14 @@ dummy_func(
for (int i = oparg; --i >= 0; ) {
*values++ = Py_NewRef(items[i]);
}
Py_DECREF(seq);
DECREF_INPUTS();
}

inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
PyObject **top = stack_pointer + totalargs - 1;
int res = unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top);
Py_DECREF(seq);
DECREF_INPUTS();
ERROR_IF(res == 0, error);
}

Expand Down Expand Up @@ -967,22 +966,21 @@ dummy_func(
#endif /* ENABLE_SPECIALIZATION */
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
int err = PyObject_SetAttr(owner, name, v);
Py_DECREF(v);
Py_DECREF(owner);
DECREF_INPUTS();
ERROR_IF(err, error);
}

inst(DELETE_ATTR, (owner --)) {
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
int err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
Py_DECREF(owner);
DECREF_INPUTS();
ERROR_IF(err, error);
}

inst(STORE_GLOBAL, (v --)) {
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
int err = PyDict_SetItem(GLOBALS(), name, v);
Py_DECREF(v);
DECREF_INPUTS();
ERROR_IF(err, error);
}

Expand Down Expand Up @@ -1249,9 +1247,7 @@ dummy_func(

inst(BUILD_STRING, (pieces[oparg] -- str)) {
str = _PyUnicode_JoinArray(&_Py_STR(empty), pieces, oparg);
for (int i = 0; i < oparg; i++) {
Py_DECREF(pieces[i]);
}
DECREF_INPUTS();
ERROR_IF(str == NULL, error);
}

Expand Down Expand Up @@ -1314,10 +1310,7 @@ dummy_func(
if (map == NULL)
goto error;

for (int i = 0; i < oparg; i++) {
Py_DECREF(values[i*2]);
Py_DECREF(values[i*2+1]);
}
DECREF_INPUTS();
ERROR_IF(map == NULL, error);
}

Expand Down Expand Up @@ -1373,10 +1366,7 @@ dummy_func(
map = _PyDict_FromItems(
&PyTuple_GET_ITEM(keys, 0), 1,
values, 1, oparg);
Py_DECREF(keys);
for (int i = 0; i < oparg; i++) {
Py_DECREF(values[i]);
}
DECREF_INPUTS();
ERROR_IF(map == NULL, error);
}

Expand Down Expand Up @@ -1464,7 +1454,7 @@ dummy_func(
NULL | meth | arg1 | ... | argN
*/
Py_DECREF(owner);
DECREF_INPUTS();
ERROR_IF(meth == NULL, error);
res2 = NULL;
res = meth;
Expand All @@ -1473,7 +1463,7 @@ dummy_func(
else {
/* Classic, pushes one value. */
res = PyObject_GetAttr(owner, name);
Py_DECREF(owner);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}
}
Expand All @@ -1492,7 +1482,7 @@ dummy_func(
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(res);
res2 = NULL;
Py_DECREF(owner);
DECREF_INPUTS();
}

inst(LOAD_ATTR_MODULE, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
Expand All @@ -1509,7 +1499,7 @@ dummy_func(
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(res);
res2 = NULL;
Py_DECREF(owner);
DECREF_INPUTS();
}

inst(LOAD_ATTR_WITH_HINT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
Expand Down Expand Up @@ -1540,7 +1530,7 @@ dummy_func(
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(res);
res2 = NULL;
Py_DECREF(owner);
DECREF_INPUTS();
}

inst(LOAD_ATTR_SLOT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
Expand All @@ -1554,7 +1544,7 @@ dummy_func(
STAT_INC(LOAD_ATTR, hit);
Py_INCREF(res);
res2 = NULL;
Py_DECREF(owner);
DECREF_INPUTS();
}

inst(LOAD_ATTR_CLASS, (unused/1, type_version/2, unused/2, descr/4, cls -- res2 if (oparg & 1), res)) {
Expand All @@ -1570,7 +1560,7 @@ dummy_func(
res = descr;
assert(res != NULL);
Py_INCREF(res);
Py_DECREF(cls);
DECREF_INPUTS();
}

inst(LOAD_ATTR_PROPERTY, (unused/1, type_version/2, func_version/2, fget/4, owner -- unused if (oparg & 1), unused)) {
Expand Down Expand Up @@ -1707,8 +1697,7 @@ dummy_func(
STAT_INC(COMPARE_OP, deferred);
assert((oparg >> 4) <= Py_GE);
res = PyObject_RichCompare(left, right, oparg>>4);
Py_DECREF(left);
Py_DECREF(right);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}

Expand All @@ -1734,8 +1723,7 @@ dummy_func(
#endif /* ENABLE_SPECIALIZATION */
assert((oparg >> 4) <= Py_GE);
PyObject *cond = PyObject_RichCompare(left, right, oparg>>4);
Py_DECREF(left);
Py_DECREF(right);
DECREF_INPUTS();
ERROR_IF(cond == NULL, error);
assert(next_instr[1].op.code == POP_JUMP_IF_FALSE ||
next_instr[1].op.code == POP_JUMP_IF_TRUE);
Expand Down Expand Up @@ -1885,7 +1873,7 @@ dummy_func(
}
else {
int err = PyObject_IsTrue(cond);
Py_DECREF(cond);
DECREF_INPUTS();
if (err == 0) {
JUMPBY(oparg);
}
Expand All @@ -1905,7 +1893,7 @@ dummy_func(
}
else {
int err = PyObject_IsTrue(cond);
Py_DECREF(cond);
DECREF_INPUTS();
if (err > 0) {
JUMPBY(oparg);
}
Expand All @@ -1917,7 +1905,7 @@ dummy_func(

inst(POP_JUMP_IF_NOT_NONE, (value -- )) {
if (!Py_IsNone(value)) {
Py_DECREF(value);
DECREF_INPUTS();
JUMPBY(oparg);
}
else {
Expand All @@ -1931,7 +1919,7 @@ dummy_func(
JUMPBY(oparg);
}
else {
Py_DECREF(value);
DECREF_INPUTS();
}
}

Expand Down Expand Up @@ -2065,7 +2053,7 @@ dummy_func(
if (iter == NULL) {
goto error;
}
Py_DECREF(iterable);
DECREF_INPUTS();
}
PREDICT(LOAD_CONST);
}
Expand Down Expand Up @@ -2940,9 +2928,7 @@ dummy_func(
assert(PyTuple_CheckExact(callargs));

result = do_call_core(tstate, func, callargs, kwargs, cframe.use_tracing);
Py_DECREF(func);
Py_DECREF(callargs);
Py_XDECREF(kwargs);
DECREF_INPUTS();

assert(PEEK(3 + (oparg & 1)) == NULL);
ERROR_IF(result == NULL, error);
Expand Down Expand Up @@ -3009,9 +2995,7 @@ dummy_func(

inst(BUILD_SLICE, (start, stop, step if (oparg == 3) -- slice)) {
slice = PySlice_New(start, stop, step);
Py_DECREF(start);
Py_DECREF(stop);
Py_XDECREF(step);
DECREF_INPUTS();
ERROR_IF(slice == NULL, error);
}

Expand Down Expand Up @@ -3057,8 +3041,7 @@ dummy_func(
} else {
/* Actually call format(). */
result = PyObject_Format(value, fmt_spec);
Py_DECREF(value);
Py_XDECREF(fmt_spec);
DECREF_INPUTS();
ERROR_IF(result == NULL, error);
}
}
Expand All @@ -3084,8 +3067,7 @@ dummy_func(
assert((unsigned)oparg < Py_ARRAY_LENGTH(binary_ops));
assert(binary_ops[oparg]);
res = binary_ops[oparg](lhs, rhs);
Py_DECREF(lhs);
Py_DECREF(rhs);
DECREF_INPUTS();
ERROR_IF(res == NULL, error);
}

Expand Down
15 changes: 7 additions & 8 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9cf44ab

Please sign in to comment.