Skip to content

Commit

Permalink
Add Py_HashBuffer() function (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner authored Oct 9, 2024
1 parent fba4977 commit 3f1c06d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
33 changes: 32 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Supported Python versions:
* Python 3.6 - 3.14
* PyPy 2.7 and PyPy 3.6 - 3.10

Python 2.7 and Python 3.4 are no longer officially supported since GitHub
Python 2.7 and Python 3.5 are no longer officially supported since GitHub
Actions doesn't support them anymore: only best effort support is provided.

C++03 and C++11 are supported on Python 3.6 and newer.
Expand All @@ -37,6 +37,10 @@ Python 3.14

See `PyBytes_Join() documentation <https://docs.python.org/dev/c-api/bytes.html#c.PyBytes_Join>`__.

.. c:function:: Py_hash_t Py_HashBuffer(const void *ptr, Py_ssize_t len)

See `Py_HashBuffer() documentation <https://docs.python.org/dev/c-api/hash.html#c.Py_HashBuffer>`__.

.. c:function:: int PyUnicode_Equal(PyObject *str1, PyObject *str2)

See `PyUnicode_Equal() documentation <https://docs.python.org/dev/c-api/unicode.html#c.PyUnicode_Equal>`__.
Expand Down Expand Up @@ -83,7 +87,34 @@ Python 3.14

Not supported:

* ``PyConfig_Get()``
* ``PyConfig_GetInt()``
* ``PyConfig_Names()``
* ``PyConfig_Set()``
* ``PyInitConfig_AddModule()``
* ``PyInitConfig_Create()``
* ``PyInitConfig_Free()``
* ``PyInitConfig_FreeStrList()``
* ``PyInitConfig_GetError()``
* ``PyInitConfig_GetExitCode()``
* ``PyInitConfig_GetInt()``
* ``PyInitConfig_GetStr()``
* ``PyInitConfig_GetStrList()``
* ``PyInitConfig_HasOption()``
* ``PyInitConfig_SetInt()``
* ``PyInitConfig_SetStr()``
* ``PyInitConfig_SetStrList()``
* ``PyLong_AsInt32()``
* ``PyLong_AsInt64()``
* ``PyLong_AsUInt32()``
* ``PyLong_AsUInt64()``
* ``PyLong_FromInt32()``
* ``PyLong_FromInt64()``
* ``PyLong_FromUInt32()``
* ``PyLong_FromUInt64()``
* ``PyType_GetBaseByToken()``
* ``PyUnicodeWriter_DecodeUTF8Stateful()``
* ``Py_InitializeFromInitConfig()``


Python 3.13
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog

* ``PyBytes_Join()``
* ``PyUnicode_Equal()``
* ``Py_HashBuffer()``

* 2024-07-18: Add functions:

Expand Down
21 changes: 21 additions & 0 deletions pythoncapi_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,27 @@ static inline PyObject* PyBytes_Join(PyObject *sep, PyObject *iterable)
#endif


#if PY_VERSION_HEX < 0x030E00A0
static inline Py_hash_t Py_HashBuffer(const void *ptr, Py_ssize_t len)
{
#if PY_VERSION_HEX >= 0x03000000 && !defined(PYPY_VERSION)
extern Py_hash_t _Py_HashBytes(const void *src, Py_ssize_t len);

return _Py_HashBytes(ptr, len);
#else
Py_hash_t hash;
PyObject *bytes = PyBytes_FromStringAndSize((const char*)ptr, len);
if (bytes == NULL) {
return -1;
}
hash = PyObject_Hash(bytes);
Py_DECREF(bytes);
return hash;
#endif
}
#endif


#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions tests/test_pythoncapi_compat_cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,20 @@ test_hash(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
assert(imag != 0);
#endif

// Test Py_HashBuffer()
{
PyObject *abc = PyBytes_FromString("abc");
if (abc == NULL) {
return NULL;
}
Py_hash_t hash = Py_HashBuffer(PyBytes_AS_STRING(abc),
PyBytes_GET_SIZE(abc));
Py_hash_t hash2 = PyObject_Hash(abc);
assert(hash == hash2);

Py_DECREF(abc);
}

Py_RETURN_NONE;
}

Expand Down Expand Up @@ -1884,6 +1898,7 @@ test_unicodewriter_format(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
static PyObject *
test_bytes(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
// Test PyBytes_Join()
PyObject *abc = PyBytes_FromString("a b c");
if (abc == NULL) {
return NULL;
Expand Down

0 comments on commit 3f1c06d

Please sign in to comment.