-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add Modules/_testcapi/hash.c and Lib/test/test_capi/test_hash.py.
- Loading branch information
Showing
9 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
.. highlight:: c | ||
|
||
PyHash API | ||
---------- | ||
|
||
See also the :c:member:`PyTypeObject.tp_hash` member. | ||
|
||
.. c:type:: Py_hash_t | ||
Hash value type: signed integer. | ||
|
||
.. versionadded:: 3.2 | ||
|
||
.. c:type:: Py_uhash_t | ||
Hash value type: unsigned integer. | ||
|
||
.. versionadded:: 3.2 | ||
|
||
|
||
.. c:type:: PyHash_FuncDef | ||
Hash function definition used by :c:func:`PyHash_GetFuncDef`. | ||
|
||
.. c::member:: Py_hash_t (*const hash)(const void *, Py_ssize_t) | ||
Hash function. | ||
|
||
.. c:member:: const char *name | ||
Hash function name (UTF-8 encoded string). | ||
|
||
.. c:member:: const int hash_bits | ||
Internal size of the hash value in bits. | ||
|
||
.. c:member:: const int seed_bits | ||
Size of seed input in bits. | ||
|
||
.. versionadded:: 3.4 | ||
|
||
|
||
.. c:function:: PyHash_FuncDef* PyHash_GetFuncDef(void) | ||
Get the hash function definition. | ||
.. versionadded:: 3.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import sys | ||
import unittest | ||
from test.support import import_helper | ||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
|
||
SIZEOF_PY_HASH_T = _testcapi.SIZEOF_VOID_P | ||
|
||
|
||
class CAPITest(unittest.TestCase): | ||
def test_hash_getfuncdef(self): | ||
# Test PyHash_GetFuncDef() | ||
hash_getfuncdef = _testcapi.hash_getfuncdef | ||
func_def = hash_getfuncdef() | ||
|
||
match func_def.name: | ||
case "fnv": | ||
self.assertEqual(func_def.hash_bits, 8 * SIZEOF_PY_HASH_T) | ||
self.assertEqual(func_def.seed_bits, 16 * SIZEOF_PY_HASH_T) | ||
case "siphash13": | ||
self.assertEqual(func_def.hash_bits, 64) | ||
self.assertEqual(func_def.seed_bits, 128) | ||
case "siphash24": | ||
self.assertEqual(func_def.hash_bits, 64) | ||
self.assertEqual(func_def.seed_bits, 128) | ||
case _: | ||
self.fail(f"unknown function name: {func_def.name!r}") | ||
|
||
# compare with sys.hash_info | ||
hash_info = sys.hash_info | ||
self.assertEqual(func_def.name, hash_info.algorithm) | ||
self.assertEqual(func_def.hash_bits, hash_info.hash_bits) | ||
self.assertEqual(func_def.seed_bits, hash_info.seed_bits) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "parts.h" | ||
#include "util.h" | ||
|
||
static PyObject * | ||
hash_getfuncdef(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args)) | ||
{ | ||
// bind PyHash_GetFuncDef() | ||
PyHash_FuncDef *def = PyHash_GetFuncDef(); | ||
|
||
PyObject *types = PyImport_ImportModule("types"); | ||
if (types == NULL) { | ||
return NULL; | ||
} | ||
|
||
PyObject *result = PyObject_CallMethod(types, "SimpleNamespace", NULL); | ||
Py_DECREF(types); | ||
if (result == NULL) { | ||
return NULL; | ||
} | ||
|
||
// ignore PyHash_FuncDef.hash | ||
|
||
PyObject *value = PyUnicode_FromString(def->name); | ||
int res = PyObject_SetAttrString(result, "name", value); | ||
Py_DECREF(value); | ||
if (res < 0) { | ||
return NULL; | ||
} | ||
|
||
value = PyLong_FromLong(def->hash_bits); | ||
res = PyObject_SetAttrString(result, "hash_bits", value); | ||
Py_DECREF(value); | ||
if (res < 0) { | ||
return NULL; | ||
} | ||
|
||
value = PyLong_FromLong(def->seed_bits); | ||
res = PyObject_SetAttrString(result, "seed_bits", value); | ||
Py_DECREF(value); | ||
if (res < 0) { | ||
return NULL; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
static PyMethodDef test_methods[] = { | ||
{"hash_getfuncdef", hash_getfuncdef, METH_NOARGS}, | ||
{NULL}, | ||
}; | ||
|
||
int | ||
_PyTestCapi_Init_Hash(PyObject *m) | ||
{ | ||
return PyModule_AddFunctions(m, test_methods); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters