Skip to content

Commit 37d7c4b

Browse files
committed
Implement _PyTuple_ExactDealloc
1 parent f8b7e4e commit 37d7c4b

File tree

5 files changed

+36
-10
lines changed

5 files changed

+36
-10
lines changed

Include/internal/pycore_tuple.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ extern PyStatus _PyTuple_InitGlobalObjects(PyInterpreterState *);
2121

2222
/* other API */
2323

24+
PyAPI_FUNC(void) _PyTuple_ExactDealloc(PyObject *self);
25+
2426
#define _PyTuple_ITEMS(op) _Py_RVALUE(_PyTuple_CAST(op)->ob_item)
2527

2628
PyAPI_FUNC(PyObject *)_PyTuple_FromStackRefStealOnSuccess(const union _PyStackRef *, Py_ssize_t);

Include/internal/pycore_uop_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/tupleobject.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,35 @@ PyTuple_Pack(Py_ssize_t n, ...)
205205

206206
/* Methods */
207207

208+
void
209+
_PyTuple_ExactDealloc(PyObject *obj)
210+
{
211+
assert(PyTuple_CheckExact(obj));
212+
PyTupleObject *op = _PyTuple_CAST(obj);
213+
214+
if (Py_SIZE(op) == 0 && op == &_Py_SINGLETON(tuple_empty)) {
215+
#ifdef Py_DEBUG
216+
_Py_FatalRefcountError("deallocating the empty tuple singleton");
217+
#else
218+
return;
219+
#endif
220+
}
221+
222+
PyObject_GC_UnTrack(op);
223+
224+
#ifdef Py_DEBUG
225+
Py_ssize_t i = Py_SIZE(op);
226+
while (--i >= 0) {
227+
assert(op->ob_item[i] == NULL);
228+
}
229+
#endif
230+
231+
// This will abort on the empty singleton (if there is one).
232+
if (!maybe_freelist_push(op)) {
233+
Py_TYPE(op)->tp_free((PyObject *)op);
234+
}
235+
}
236+
208237
static void
209238
tuple_dealloc(PyObject *self)
210239
{

Python/bytecodes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
3131
#include "pycore_stackref.h"
3232
#include "pycore_template.h" // _PyTemplate_Build()
33-
#include "pycore_tuple.h" // _PyTuple_ITEMS()
33+
#include "pycore_tuple.h" // _PyTuple_ExactDealloc(), _PyTuple_ITEMS()
3434
#include "pycore_typeobject.h" // _PySuper_Lookup()
3535

3636
#include "pycore_dict.h"
@@ -1682,7 +1682,7 @@ dummy_func(
16821682
*values++ = PyStackRef_FromPyObjectSteal(items[i]);
16831683
items[i] = NULL;
16841684
}
1685-
DECREF_INPUTS();
1685+
PyStackRef_CLOSE_SPECIALIZED(seq, _PyTuple_ExactDealloc);
16861686
}
16871687

16881688
macro(UNPACK_SEQUENCE_LIST) =

Python/executor_cases.c.h

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

0 commit comments

Comments
 (0)