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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-113024: C API: Add PyObject_GenericHash() function #113025

Merged
merged 5 commits into from
Mar 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions Doc/c-api/hash.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`.
The function cannot fail: it cannot return ``-1``.

.. versionadded:: 3.13

.. c:function:: Py_hash_t PyObject_GenericHash(PyObject *obj)

Generic hashing function that is meant to be put into a type
object's ``tp_hash`` slot.
Its result only depends on the object's identity.

.. impl-detail::
In CPython, it is equivalent to :c:func:`Py_HashPointer`.

.. versionadded:: 3.13
4 changes: 4 additions & 0 deletions Doc/c-api/typeobj.rst
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,10 @@
:c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash`, when the subtype's
:c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash` are both ``NULL``.

**Default:**

:c:data:`PyBaseObject_Type` uses :c:func:`PyObject_GenericHash`.

Check warning on line 888 in Doc/c-api/typeobj.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

c:data reference target not found: PyBaseObject_Type
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a warning: c:data reference target not found: PyBaseObject_Type

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not new. There are 14 unresolved references to PyBaseObject_Type. The preferable solution is to add a target for it, but it is not so simple, and is a different issue anyway.



.. c:member:: ternaryfunc PyTypeObject.tp_call

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1700,6 +1700,10 @@ New Features
* Add :c:func:`Py_HashPointer` function to hash a pointer.
(Contributed by Victor Stinner in :gh:`111545`.)

* Add :c:func:`PyObject_GenericHash` function that implements the default
hashing function of a Python object.
(Contributed by Serhiy Storchaka in :gh:`113024`.)

* Add PyTime C API:

* :c:type:`PyTime_t` type.
Expand Down
1 change: 1 addition & 0 deletions Include/cpython/pyhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ typedef struct {
PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);

PyAPI_FUNC(Py_hash_t) Py_HashPointer(const void *ptr);
PyAPI_FUNC(Py_hash_t) PyObject_GenericHash(PyObject *);
6 changes: 6 additions & 0 deletions Lib/test/test_capi/test_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,12 @@ def test_number_check(self):
self.assertTrue(number_check(0.5))
self.assertFalse(number_check("1 + 1j"))

def test_object_generichash(self):
# Test PyObject_GenericHash()
generichash = _testcapi.object_generichash
for obj in object(), 1, 'string', []:
self.assertEqual(generichash(obj), object.__hash__(obj))
vstinner marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :c:func:`PyObject_GenericHash` function.
2 changes: 1 addition & 1 deletion Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4780,7 +4780,7 @@ _dec_hash(PyDecObject *v)
return -1;
}
else if (mpd_isnan(MPD(v))) {
return _Py_HashPointer(v);
return PyObject_GenericHash((PyObject *)v);
}
else {
return py_hash_inf * mpd_arith_sign(MPD(v));
Expand Down
11 changes: 11 additions & 0 deletions Modules/_testcapi/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ hash_pointer(PyObject *Py_UNUSED(module), PyObject *arg)
}


static PyObject *
object_generichash(PyObject *Py_UNUSED(module), PyObject *arg)
{
NULLABLE(arg);
Py_hash_t hash = PyObject_GenericHash(arg);
Py_BUILD_ASSERT(sizeof(long long) >= sizeof(hash));
return PyLong_FromLongLong(hash);
}


static PyMethodDef test_methods[] = {
{"hash_getfuncdef", hash_getfuncdef, METH_NOARGS},
{"hash_pointer", hash_pointer, METH_O},
{"object_generichash", object_generichash, METH_O},
{NULL},
};

Expand Down
2 changes: 1 addition & 1 deletion Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ static Py_hash_t
method_hash(PyMethodObject *a)
{
Py_hash_t x, y;
x = _Py_HashPointer(a->im_self);
x = PyObject_GenericHash(a->im_self);
y = PyObject_Hash(a->im_func);
if (y == -1)
return -1;
Expand Down
2 changes: 1 addition & 1 deletion Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ wrapper_hash(PyObject *self)
{
wrapperobject *wp = (wrapperobject *)self;
Py_hash_t x, y;
x = _Py_HashPointer(wp->self);
x = PyObject_GenericHash(wp->self);
y = _Py_HashPointer(wp->descr);
x = x ^ y;
if (x == -1)
Expand Down
2 changes: 1 addition & 1 deletion Objects/methodobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ static Py_hash_t
meth_hash(PyCFunctionObject *a)
{
Py_hash_t x, y;
x = _Py_HashPointer(a->m_self);
x = PyObject_GenericHash(a->m_self);
y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
x ^= y;
if (x == -1)
Expand Down
8 changes: 1 addition & 7 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6891,12 +6891,6 @@ PyDoc_STRVAR(object_doc,
"When called, it accepts no arguments and returns a new featureless\n"
"instance that has no instance attributes and cannot be given any.\n");

static Py_hash_t
object_hash(PyObject *obj)
{
return _Py_HashPointer(obj);
}

PyTypeObject PyBaseObject_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"object", /* tp_name */
Expand All @@ -6911,7 +6905,7 @@ PyTypeObject PyBaseObject_Type = {
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
object_hash, /* tp_hash */
PyObject_GenericHash, /* tp_hash */
0, /* tp_call */
object_str, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
Expand Down
2 changes: 1 addition & 1 deletion PC/winreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ PyHKEY_hashFunc(PyObject *ob)
/* Just use the address.
XXX - should we use the handle value?
*/
return _Py_HashPointer(ob);
return PyObject_GenericHash(ob);
}


Expand Down
8 changes: 7 additions & 1 deletion Python/pyhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ _Py_HashDouble(PyObject *inst, double v)
if (Py_IS_INFINITY(v))
return v > 0 ? _PyHASH_INF : -_PyHASH_INF;
else
return _Py_HashPointer(inst);
return PyObject_GenericHash(inst);
}

m = frexp(v, &e);
Expand Down Expand Up @@ -139,6 +139,12 @@ Py_HashPointer(const void *ptr)
return hash;
}

Py_hash_t
PyObject_GenericHash(PyObject *obj)
{
return Py_HashPointer(obj);
}

Py_hash_t
_Py_HashBytes(const void *src, Py_ssize_t len)
{
Expand Down