diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 830b255a3405dc..1f6c7c48d8f438 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -171,7 +171,7 @@ dummy_func( super(LOAD_CONST__LOAD_FAST) = LOAD_CONST + LOAD_FAST; inst(POP_TOP, (value --)) { - Py_DECREF(value); + DECREF_INPUTS(); } inst(PUSH_NULL, (-- res)) { @@ -182,7 +182,7 @@ dummy_func( inst(UNARY_POSITIVE, (value -- res)) { res = PyNumber_Positive(value); - Py_DECREF(value); + DECREF_INPUTS(); ERROR_IF(res == NULL, error); } @@ -196,7 +196,7 @@ dummy_func( inst(UNARY_NEGATIVE, (value -- res)) { res = PyNumber_Negative(value); - Py_DECREF(value); + DECREF_INPUTS(); ERROR_IF(res == NULL, error); } @@ -210,7 +210,7 @@ dummy_func( inst(UNARY_NOT, (value -- res)) { int err = PyObject_IsTrue(value); - Py_DECREF(value); + DECREF_INPUTS(); ERROR_IF(err < 0, error); if (err == 0) { res = Py_True; @@ -239,7 +239,7 @@ dummy_func( inst(UNARY_INVERT, (value -- res)) { res = PyNumber_Invert(value); - Py_DECREF(value); + DECREF_INPUTS(); ERROR_IF(res == NULL, error); } @@ -401,8 +401,7 @@ dummy_func( STAT_INC(BINARY_SUBSCR, deferred); DECREMENT_ADAPTIVE_COUNTER(cache->counter); res = PyObject_GetItem(container, sub); - Py_DECREF(container); - Py_DECREF(sub); + DECREF_INPUTS(); ERROR_IF(res == NULL, error); } @@ -488,8 +487,7 @@ dummy_func( ERROR_IF(true, error); } Py_INCREF(res); // Do this before DECREF'ing dict, sub - Py_DECREF(dict); - Py_DECREF(sub); + DECREF_INPUTS(); } inst(BINARY_SUBSCR_GETITEM, (unused/1, type_version/2, func_version/1, container, sub -- unused)) { @@ -550,9 +548,7 @@ dummy_func( DECREMENT_ADAPTIVE_COUNTER(cache->counter); /* container[sub] = v */ int err = PyObject_SetItem(container, sub, v); - Py_DECREF(v); - Py_DECREF(container); - Py_DECREF(sub); + DECREF_INPUTS(); ERROR_IF(err, error); } @@ -588,8 +584,7 @@ dummy_func( inst(DELETE_SUBSCR, (container, sub --)) { /* del container[sub] */ int err = PyObject_DelItem(container, sub); - Py_DECREF(container); - Py_DECREF(sub); + DECREF_INPUTS(); ERROR_IF(err, error); } @@ -600,11 +595,11 @@ dummy_func( if (hook == NULL) { _PyErr_SetString(tstate, PyExc_RuntimeError, "lost sys.displayhook"); - Py_DECREF(value); + DECREF_INPUTS(); ERROR_IF(true, error); } res = PyObject_CallOneArg(hook, value); - Py_DECREF(value); + DECREF_INPUTS(); ERROR_IF(res == NULL, error); Py_DECREF(res); } @@ -675,12 +670,12 @@ dummy_func( "'async for' requires an object with " "__aiter__ method, got %.100s", type->tp_name); - Py_DECREF(obj); + DECREF_INPUTS(); ERROR_IF(true, error); } iter = (*getter)(obj); - Py_DECREF(obj); + DECREF_INPUTS(); ERROR_IF(iter == NULL, error); if (Py_TYPE(iter)->tp_as_async == NULL || diff --git a/Tools/cases_generator/generate_cases.py b/Tools/cases_generator/generate_cases.py index 4d635d568e792e..23381055ec23a9 100644 --- a/Tools/cases_generator/generate_cases.py +++ b/Tools/cases_generator/generate_cases.py @@ -209,7 +209,7 @@ def write_body(self, out: Formatter, dedent: int, cache_adjust: int = 0) -> None cache_offset += ceffect.size assert cache_offset == self.cache_offset + cache_adjust - # Write the body, substituting a goto for ERROR_IF() + # Write the body, substituting a goto for ERROR_IF() and other stuff assert dedent <= 0 extra = " " * -dedent for line in self.block_text: @@ -232,6 +232,10 @@ def write_body(self, out: Formatter, dedent: int, cache_adjust: int = 0) -> None ) else: out.write_raw(f"{extra}{space}if ({cond}) goto {label};\n") + elif m := re.match(r"(\s*)DECREF_INPUTS\(\);\s*$", line): + space = m.group(1) + for ieff in self.input_effects: + out.write_raw(f"{extra}{space}Py_DECREF({ieff.name});\n") else: out.write_raw(extra + line)