Skip to content

Commit 392f2ad

Browse files
authored
pythongh-98831: Use DECREF_INPUTS() more (python#102409)
1 parent 959ea2f commit 392f2ad

File tree

2 files changed

+38
-57
lines changed

2 files changed

+38
-57
lines changed

Python/bytecodes.c

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ dummy_func(
384384
if (!_PyErr_Occurred(tstate)) {
385385
_PyErr_SetKeyError(sub);
386386
}
387-
Py_DECREF(dict);
388-
Py_DECREF(sub);
387+
DECREF_INPUTS();
389388
ERROR_IF(true, error);
390389
}
391390
Py_INCREF(res); // Do this before DECREF'ing dict, sub
@@ -420,7 +419,7 @@ dummy_func(
420419

421420
inst(SET_ADD, (set, unused[oparg-1], v -- set, unused[oparg-1])) {
422421
int err = PySet_Add(set, v);
423-
Py_DECREF(v);
422+
DECREF_INPUTS();
424423
ERROR_IF(err, error);
425424
PREDICT(JUMP_BACKWARD);
426425
}
@@ -899,7 +898,7 @@ dummy_func(
899898
#endif /* ENABLE_SPECIALIZATION */
900899
PyObject **top = stack_pointer + oparg - 1;
901900
int res = unpack_iterable(tstate, seq, oparg, -1, top);
902-
Py_DECREF(seq);
901+
DECREF_INPUTS();
903902
ERROR_IF(res == 0, error);
904903
}
905904

@@ -910,7 +909,7 @@ dummy_func(
910909
STAT_INC(UNPACK_SEQUENCE, hit);
911910
values[0] = Py_NewRef(PyTuple_GET_ITEM(seq, 1));
912911
values[1] = Py_NewRef(PyTuple_GET_ITEM(seq, 0));
913-
Py_DECREF(seq);
912+
DECREF_INPUTS();
914913
}
915914

916915
inst(UNPACK_SEQUENCE_TUPLE, (unused/1, seq -- values[oparg])) {
@@ -921,7 +920,7 @@ dummy_func(
921920
for (int i = oparg; --i >= 0; ) {
922921
*values++ = Py_NewRef(items[i]);
923922
}
924-
Py_DECREF(seq);
923+
DECREF_INPUTS();
925924
}
926925

927926
inst(UNPACK_SEQUENCE_LIST, (unused/1, seq -- values[oparg])) {
@@ -932,14 +931,14 @@ dummy_func(
932931
for (int i = oparg; --i >= 0; ) {
933932
*values++ = Py_NewRef(items[i]);
934933
}
935-
Py_DECREF(seq);
934+
DECREF_INPUTS();
936935
}
937936

938937
inst(UNPACK_EX, (seq -- unused[oparg & 0xFF], unused, unused[oparg >> 8])) {
939938
int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
940939
PyObject **top = stack_pointer + totalargs - 1;
941940
int res = unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8, top);
942-
Py_DECREF(seq);
941+
DECREF_INPUTS();
943942
ERROR_IF(res == 0, error);
944943
}
945944

@@ -967,22 +966,21 @@ dummy_func(
967966
#endif /* ENABLE_SPECIALIZATION */
968967
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
969968
int err = PyObject_SetAttr(owner, name, v);
970-
Py_DECREF(v);
971-
Py_DECREF(owner);
969+
DECREF_INPUTS();
972970
ERROR_IF(err, error);
973971
}
974972

975973
inst(DELETE_ATTR, (owner --)) {
976974
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
977975
int err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
978-
Py_DECREF(owner);
976+
DECREF_INPUTS();
979977
ERROR_IF(err, error);
980978
}
981979

982980
inst(STORE_GLOBAL, (v --)) {
983981
PyObject *name = GETITEM(frame->f_code->co_names, oparg);
984982
int err = PyDict_SetItem(GLOBALS(), name, v);
985-
Py_DECREF(v);
983+
DECREF_INPUTS();
986984
ERROR_IF(err, error);
987985
}
988986

@@ -1249,9 +1247,7 @@ dummy_func(
12491247

12501248
inst(BUILD_STRING, (pieces[oparg] -- str)) {
12511249
str = _PyUnicode_JoinArray(&_Py_STR(empty), pieces, oparg);
1252-
for (int i = 0; i < oparg; i++) {
1253-
Py_DECREF(pieces[i]);
1254-
}
1250+
DECREF_INPUTS();
12551251
ERROR_IF(str == NULL, error);
12561252
}
12571253

@@ -1314,10 +1310,7 @@ dummy_func(
13141310
if (map == NULL)
13151311
goto error;
13161312

1317-
for (int i = 0; i < oparg; i++) {
1318-
Py_DECREF(values[i*2]);
1319-
Py_DECREF(values[i*2+1]);
1320-
}
1313+
DECREF_INPUTS();
13211314
ERROR_IF(map == NULL, error);
13221315
}
13231316

@@ -1373,10 +1366,7 @@ dummy_func(
13731366
map = _PyDict_FromItems(
13741367
&PyTuple_GET_ITEM(keys, 0), 1,
13751368
values, 1, oparg);
1376-
Py_DECREF(keys);
1377-
for (int i = 0; i < oparg; i++) {
1378-
Py_DECREF(values[i]);
1379-
}
1369+
DECREF_INPUTS();
13801370
ERROR_IF(map == NULL, error);
13811371
}
13821372

@@ -1464,7 +1454,7 @@ dummy_func(
14641454
14651455
NULL | meth | arg1 | ... | argN
14661456
*/
1467-
Py_DECREF(owner);
1457+
DECREF_INPUTS();
14681458
ERROR_IF(meth == NULL, error);
14691459
res2 = NULL;
14701460
res = meth;
@@ -1473,7 +1463,7 @@ dummy_func(
14731463
else {
14741464
/* Classic, pushes one value. */
14751465
res = PyObject_GetAttr(owner, name);
1476-
Py_DECREF(owner);
1466+
DECREF_INPUTS();
14771467
ERROR_IF(res == NULL, error);
14781468
}
14791469
}
@@ -1492,7 +1482,7 @@ dummy_func(
14921482
STAT_INC(LOAD_ATTR, hit);
14931483
Py_INCREF(res);
14941484
res2 = NULL;
1495-
Py_DECREF(owner);
1485+
DECREF_INPUTS();
14961486
}
14971487

14981488
inst(LOAD_ATTR_MODULE, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
@@ -1509,7 +1499,7 @@ dummy_func(
15091499
STAT_INC(LOAD_ATTR, hit);
15101500
Py_INCREF(res);
15111501
res2 = NULL;
1512-
Py_DECREF(owner);
1502+
DECREF_INPUTS();
15131503
}
15141504

15151505
inst(LOAD_ATTR_WITH_HINT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
@@ -1540,7 +1530,7 @@ dummy_func(
15401530
STAT_INC(LOAD_ATTR, hit);
15411531
Py_INCREF(res);
15421532
res2 = NULL;
1543-
Py_DECREF(owner);
1533+
DECREF_INPUTS();
15441534
}
15451535

15461536
inst(LOAD_ATTR_SLOT, (unused/1, type_version/2, index/1, unused/5, owner -- res2 if (oparg & 1), res)) {
@@ -1554,7 +1544,7 @@ dummy_func(
15541544
STAT_INC(LOAD_ATTR, hit);
15551545
Py_INCREF(res);
15561546
res2 = NULL;
1557-
Py_DECREF(owner);
1547+
DECREF_INPUTS();
15581548
}
15591549

15601550
inst(LOAD_ATTR_CLASS, (unused/1, type_version/2, unused/2, descr/4, cls -- res2 if (oparg & 1), res)) {
@@ -1570,7 +1560,7 @@ dummy_func(
15701560
res = descr;
15711561
assert(res != NULL);
15721562
Py_INCREF(res);
1573-
Py_DECREF(cls);
1563+
DECREF_INPUTS();
15741564
}
15751565

15761566
inst(LOAD_ATTR_PROPERTY, (unused/1, type_version/2, func_version/2, fget/4, owner -- unused if (oparg & 1), unused)) {
@@ -1707,8 +1697,7 @@ dummy_func(
17071697
STAT_INC(COMPARE_OP, deferred);
17081698
assert((oparg >> 4) <= Py_GE);
17091699
res = PyObject_RichCompare(left, right, oparg>>4);
1710-
Py_DECREF(left);
1711-
Py_DECREF(right);
1700+
DECREF_INPUTS();
17121701
ERROR_IF(res == NULL, error);
17131702
}
17141703

@@ -1734,8 +1723,7 @@ dummy_func(
17341723
#endif /* ENABLE_SPECIALIZATION */
17351724
assert((oparg >> 4) <= Py_GE);
17361725
PyObject *cond = PyObject_RichCompare(left, right, oparg>>4);
1737-
Py_DECREF(left);
1738-
Py_DECREF(right);
1726+
DECREF_INPUTS();
17391727
ERROR_IF(cond == NULL, error);
17401728
assert(next_instr[1].op.code == POP_JUMP_IF_FALSE ||
17411729
next_instr[1].op.code == POP_JUMP_IF_TRUE);
@@ -1885,7 +1873,7 @@ dummy_func(
18851873
}
18861874
else {
18871875
int err = PyObject_IsTrue(cond);
1888-
Py_DECREF(cond);
1876+
DECREF_INPUTS();
18891877
if (err == 0) {
18901878
JUMPBY(oparg);
18911879
}
@@ -1905,7 +1893,7 @@ dummy_func(
19051893
}
19061894
else {
19071895
int err = PyObject_IsTrue(cond);
1908-
Py_DECREF(cond);
1896+
DECREF_INPUTS();
19091897
if (err > 0) {
19101898
JUMPBY(oparg);
19111899
}
@@ -1917,7 +1905,7 @@ dummy_func(
19171905

19181906
inst(POP_JUMP_IF_NOT_NONE, (value -- )) {
19191907
if (!Py_IsNone(value)) {
1920-
Py_DECREF(value);
1908+
DECREF_INPUTS();
19211909
JUMPBY(oparg);
19221910
}
19231911
else {
@@ -1931,7 +1919,7 @@ dummy_func(
19311919
JUMPBY(oparg);
19321920
}
19331921
else {
1934-
Py_DECREF(value);
1922+
DECREF_INPUTS();
19351923
}
19361924
}
19371925

@@ -2065,7 +2053,7 @@ dummy_func(
20652053
if (iter == NULL) {
20662054
goto error;
20672055
}
2068-
Py_DECREF(iterable);
2056+
DECREF_INPUTS();
20692057
}
20702058
PREDICT(LOAD_CONST);
20712059
}
@@ -2940,9 +2928,7 @@ dummy_func(
29402928
assert(PyTuple_CheckExact(callargs));
29412929

29422930
result = do_call_core(tstate, func, callargs, kwargs, cframe.use_tracing);
2943-
Py_DECREF(func);
2944-
Py_DECREF(callargs);
2945-
Py_XDECREF(kwargs);
2931+
DECREF_INPUTS();
29462932

29472933
assert(PEEK(3 + (oparg & 1)) == NULL);
29482934
ERROR_IF(result == NULL, error);
@@ -3009,9 +2995,7 @@ dummy_func(
30092995

30102996
inst(BUILD_SLICE, (start, stop, step if (oparg == 3) -- slice)) {
30112997
slice = PySlice_New(start, stop, step);
3012-
Py_DECREF(start);
3013-
Py_DECREF(stop);
3014-
Py_XDECREF(step);
2998+
DECREF_INPUTS();
30152999
ERROR_IF(slice == NULL, error);
30163000
}
30173001

@@ -3057,8 +3041,7 @@ dummy_func(
30573041
} else {
30583042
/* Actually call format(). */
30593043
result = PyObject_Format(value, fmt_spec);
3060-
Py_DECREF(value);
3061-
Py_XDECREF(fmt_spec);
3044+
DECREF_INPUTS();
30623045
ERROR_IF(result == NULL, error);
30633046
}
30643047
}
@@ -3084,8 +3067,7 @@ dummy_func(
30843067
assert((unsigned)oparg < Py_ARRAY_LENGTH(binary_ops));
30853068
assert(binary_ops[oparg]);
30863069
res = binary_ops[oparg](lhs, rhs);
3087-
Py_DECREF(lhs);
3088-
Py_DECREF(rhs);
3070+
DECREF_INPUTS();
30893071
ERROR_IF(res == NULL, error);
30903072
}
30913073

Python/generated_cases.c.h

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)