Skip to content

gh-134891: Add PyUnstable_Unicode_GET_CACHED_HASH #134892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

encukou
Copy link
Member

@encukou encukou commented May 29, 2025

Comment on lines 305 to 307
PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op) {
return _PyASCIIObject_CAST(op)->hash;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Hash can be computed and assigned concurrently so to avoid a data race in free-threaded build:

#ifdef Py_GIL_DISABLED
    return _Py_atomic_load_ssize_relaxed(&_PyASCIIObject_CAST(op)->hash);
#else
    return _PyASCIIObject_CAST(op)->hash;
#endif

For example, in unicodeobject.c:

static inline Py_hash_t PyUnicode_HASH(PyObject *op)
{
assert(_PyUnicode_CHECK(op));
return FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyASCIIObject_CAST(op)->hash);
}

static PyObject*
unicode_GET_CACHED_HASH(PyObject *self, PyObject *arg)
{
return PyLong_FromLong((long)PyUnstable_Unicode_GET_CACHED_HASH(arg));
Copy link
Contributor

Choose a reason for hiding this comment

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

Use PyLong_FromSSize_t. Py_hash_t is an alias for Py_ssize_t, but it might be a different size than long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants