Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Include/cpython/tupleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

typedef struct {
PyObject_VAR_HEAD
/* Cached hash. Initially set to -1. */
Py_uhash_t ob_hash;
/* ob_item contains space for 'ob_size' elements.
Items must normally not be NULL, except during construction when
the tuple is not yet visible outside the function that builds it. */
Expand Down
15 changes: 15 additions & 0 deletions Include/internal/pycore_tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_object.h" // _PyObject_GC_IS_TRACKED
#include "pycore_structs.h" // _PyStackRef

extern void _PyTuple_MaybeUntrack(PyObject *);
Expand All @@ -32,6 +33,20 @@ typedef struct {
PyTupleObject *it_seq; /* Set to NULL when iterator is exhausted */
} _PyTupleIterObject;

#define _PyTuple_RESET_HASH_CACHE(op) (_PyTuple_CAST(op)->ob_hash = -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this would be more safer.

Suggested change
#define _PyTuple_RESET_HASH_CACHE(op) (_PyTuple_CAST(op)->ob_hash = -1)
#define _PyTuple_RESET_HASH_CACHE(op) \
do { \
if (op != NULL) {\
_PyTuple_CAST(op)->ob_hash = -1); \
}\
} while (0)


/*
bpo-42536: If reusing a tuple object, this should be called to re-track it
with the garbage collector and reset its hash cache. */
static inline void
_PyTuple_Recycle(PyObject *op)
{
_PyTuple_RESET_HASH_CACHE(op);
if (!_PyObject_GC_IS_TRACKED(op)) {
_PyObject_GC_TRACK(op);
}
}

#ifdef __cplusplus
}
#endif
Expand Down
Loading
Loading