Skip to content
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

Add Py_HashBuffer() function #112

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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