Skip to content

Commit

Permalink
gh-98831: Use DECREF_INPUTS() more (#102409)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum authored and Fidget-Spinner committed Mar 27, 2023
1 parent 75153c1 commit b99132d
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 @@ -457,8 +457,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 @@ -493,7 +492,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 @@ -972,7 +971,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 @@ -983,7 +982,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 @@ -994,7 +993,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 @@ -1005,14 +1004,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 @@ -1040,22 +1039,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 @@ -1322,9 +1320,7 @@ dummy_func(

inst(BUILD_STRING, (pieces[oparg] -- str: PyUnicode_Type)) {
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 @@ -1387,10 +1383,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 @@ -1446,10 +1439,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 @@ -1537,7 +1527,7 @@ dummy_func(
NULL | meth | arg1 | ... | argN
*/
Py_DECREF(owner);
DECREF_INPUTS();
ERROR_IF(meth == NULL, error);
res2 = NULL;
res = meth;
Expand All @@ -1546,7 +1536,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 @@ -1565,7 +1555,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 @@ -1582,7 +1572,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 @@ -1613,7 +1603,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 @@ -1627,7 +1617,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 @@ -1643,7 +1633,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 @@ -1780,8 +1770,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 @@ -1807,8 +1796,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 @@ -1963,7 +1951,7 @@ dummy_func(
}
else {
int err = PyObject_IsTrue(cond);
Py_DECREF(cond);
DECREF_INPUTS();
if (err == 0) {
JUMPBY(oparg);
}
Expand Down Expand Up @@ -2004,7 +1992,7 @@ dummy_func(
}
else {
int err = PyObject_IsTrue(cond);
Py_DECREF(cond);
DECREF_INPUTS();
if (err > 0) {
JUMPBY(oparg);
}
Expand Down Expand Up @@ -2038,7 +2026,7 @@ dummy_func(

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

Expand Down Expand Up @@ -2270,7 +2258,7 @@ dummy_func(
if (iter == NULL) {
goto error;
}
Py_DECREF(iterable);
DECREF_INPUTS();
}
PREDICT(LOAD_CONST);
}
Expand Down Expand Up @@ -3168,9 +3156,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 @@ -3237,9 +3223,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 @@ -3285,8 +3269,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 @@ -3312,8 +3295,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 b99132d

Please sign in to comment.