Skip to content

Add PyUnstable_Unicode_GET_CACHED_HASH #134891

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
encukou opened this issue May 29, 2025 · 0 comments
Closed

Add PyUnstable_Unicode_GET_CACHED_HASH #134891

encukou opened this issue May 29, 2025 · 0 comments
Assignees
Labels
topic-C-API type-feature A feature request or enhancement

Comments

@encukou
Copy link
Member

encukou commented May 29, 2025

Feature or enhancement

Proposal:

I'd like to add a quick way to maybe get a str hash, so that advanced users don't need to reach into the undocumented-but-public PyASCIIObject->hash.

Py_hash_t PyUnstable_Unicode_GET_CACHED_HASH(PyObject *str)

If the hash of str, as returned by PyObject_Hash, has been cached and is immediately available, return it.
Otherwise, return -1 without setting an exception.

If str is not a string (that is, if PyUnicode_Check(obj) is false), the behavior is undefined.

This function never fails with an exception.

Note that there are no guarantees on when a object's hash is cached, and the (non-)existence of a cached hash does not imply that the string has any other properties.

Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/82543

Linked PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-C-API type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant