@@ -45,6 +45,14 @@ hash_getfuncdef(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
4545}
4646
4747
48+ static PyObject *
49+ long_from_hash (Py_hash_t hash )
50+ {
51+ Py_BUILD_ASSERT (sizeof (long long ) >= sizeof (hash ));
52+ return PyLong_FromLongLong (hash );
53+ }
54+
55+
4856static PyObject *
4957hash_pointer (PyObject * Py_UNUSED (module ), PyObject * arg )
5058{
@@ -54,8 +62,21 @@ hash_pointer(PyObject *Py_UNUSED(module), PyObject *arg)
5462 }
5563
5664 Py_hash_t hash = Py_HashPointer (ptr );
57- Py_BUILD_ASSERT (sizeof (long long ) >= sizeof (hash ));
58- return PyLong_FromLongLong (hash );
65+ return long_from_hash (hash );
66+ }
67+
68+
69+ static PyObject *
70+ hash_buffer (PyObject * Py_UNUSED (module ), PyObject * args )
71+ {
72+ char * ptr ;
73+ Py_ssize_t len ;
74+ if (!PyArg_ParseTuple (args , "y#" , & ptr , & len )) {
75+ return NULL ;
76+ }
77+
78+ Py_hash_t hash = Py_HashBuffer (ptr , len );
79+ return long_from_hash (hash );
5980}
6081
6182
@@ -64,14 +85,14 @@ object_generichash(PyObject *Py_UNUSED(module), PyObject *arg)
6485{
6586 NULLABLE (arg );
6687 Py_hash_t hash = PyObject_GenericHash (arg );
67- Py_BUILD_ASSERT (sizeof (long long ) >= sizeof (hash ));
68- return PyLong_FromLongLong (hash );
88+ return long_from_hash (hash );
6989}
7090
7191
7292static PyMethodDef test_methods [] = {
7393 {"hash_getfuncdef" , hash_getfuncdef , METH_NOARGS },
7494 {"hash_pointer" , hash_pointer , METH_O },
95+ {"hash_buffer" , hash_buffer , METH_VARARGS },
7596 {"object_generichash" , object_generichash , METH_O },
7697 {NULL },
7798};
0 commit comments