-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
base: main
Are you sure you want to change the base?
Conversation
Include/cpython/unicodeobject.h
Outdated
| PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op) { | ||
| return _PyASCIIObject_CAST(op)->hash; | ||
| } |
There was a problem hiding this comment.
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:
cpython/Objects/unicodeobject.c
Lines 170 to 175 in d963436
| static inline Py_hash_t PyUnicode_HASH(PyObject *op) | |
| { | |
| assert(_PyUnicode_CHECK(op)); | |
| return FT_ATOMIC_LOAD_SSIZE_RELAXED(_PyASCIIObject_CAST(op)->hash); | |
| } | |
Modules/_testcapi/unicode.c
Outdated
| static PyObject* | ||
| unicode_GET_CACHED_HASH(PyObject *self, PyObject *arg) | ||
| { | ||
| return PyLong_FromLong((long)PyUnstable_Unicode_GET_CACHED_HASH(arg)); |
There was a problem hiding this comment.
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.
Co-authored-by: Colin Marquardt <cmarqu42@gmail.com>
PyUnstable_Unicode_GET_CACHED_HASH聽#134891馃摎 Documentation preview 馃摎: https://cpython-previews--134892.org.readthedocs.build/