Skip to content

Commit

Permalink
Add hash constants
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Mar 9, 2024
1 parent 52486a9 commit 2358582
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

* 2024-03-09: Add hash constants:

* ``PyHASH_BITS``
* ``PyHASH_IMAG``
* ``PyHASH_INF``
* ``PyHASH_MODULUS``

* 2024-02-20: Add PyTime API:

* ``PyTime_t`` type
Expand Down
9 changes: 9 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,15 @@ static inline int PyTime_PerfCounter(PyTime_t *result)

#endif

// gh-111389 added hash constants to Python 3.13.0a5.
// These constants were first added as private to Python 3.4.0b1.
#if !defined(PyHASH_BITS) && PY_VERSION_HEX >= 0x030400B1
# define PyHASH_BITS _PyHASH_BITS
# define PyHASH_MODULUS _PyHASH_MODULUS
# define PyHASH_INF _PyHASH_INF
# define PyHASH_IMAG _PyHASH_IMAG
#endif


#ifdef __cplusplus
}
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,18 @@ test_hash(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
assert(Py_HashPointer(ptr1) == (Py_hash_t)ptr1);
#endif

#if PY_VERSION_HEX >= 0x030400B1
// Just check that constants are available
size_t bits = PyHASH_BITS;
assert(bits >= 8);
size_t mod = PyHASH_MODULUS;
assert(mod >= 7);
size_t inf = PyHASH_INF;
assert(inf != 0);
size_t imag = PyHASH_IMAG;
assert(imag != 0);
#endif

Py_RETURN_NONE;
}

Expand Down

0 comments on commit 2358582

Please sign in to comment.