Skip to content

Commit e510a7b

Browse files
encukouvstinner
andauthored
Make PyUnstable_Unicode_GET_CACHED_HASH return -1 on PyPy (#157)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 6c77b6b commit e510a7b

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Python 3.15
6868

6969
See `PyUnstable_Unicode_GET_CACHED_HASH() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnstable_Unicode_GET_CACHED_HASH>`__.
7070

71-
Not available on PyPy.
71+
On PyPy, always returns ``-1``.
7272

7373

7474
Python 3.14

pythoncapi_compat.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,11 +2572,14 @@ PyTuple_FromArray(PyObject *const *array, Py_ssize_t size)
25722572
#endif
25732573

25742574

2575-
#if PY_VERSION_HEX < 0x030F00A1 && !defined(PYPY_VERSION)
2575+
#if PY_VERSION_HEX < 0x030F00A1
25762576
static inline Py_hash_t
25772577
PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op)
25782578
{
2579-
#if PY_VERSION_HEX >= 0x03000000
2579+
#ifdef PYPY_VERSION
2580+
(void)op; // unused argument
2581+
return -1;
2582+
#elif PY_VERSION_HEX >= 0x03000000
25802583
return ((PyASCIIObject*)op)->hash;
25812584
#else
25822585
return ((PyUnicodeObject*)op)->hash;

tests/test_pythoncapi_compat_cext.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1611,8 +1611,10 @@ test_unicode(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
16111611
assert(PyErr_ExceptionMatches(PyExc_TypeError));
16121612
PyErr_Clear();
16131613

1614-
#ifndef PYPY_VERSION
16151614
// Test PyUnstable_Unicode_GET_CACHED_HASH()
1615+
#ifdef PYPY_VERSION
1616+
assert(PyUnstable_Unicode_GET_CACHED_HASH(abc) == -1);
1617+
#else
16161618
Py_hash_t hash = PyObject_Hash(abc);
16171619
assert(hash != -1);
16181620
assert(PyUnstable_Unicode_GET_CACHED_HASH(abc) == hash);

0 commit comments

Comments
 (0)