summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-11-15 02:41:29 (GMT)
committerGitHub <noreply@github.com>2023-11-15 02:41:29 (GMT)
commit55f3cce821f8f18ddb485aa07bdf0190c358d081 (patch)
treee3a5984dba95733646cc19a14d236c33548ccd28 /Modules/_testcapi
parente0f512797596282bff63260f8102592aad37cdf1 (diff)
downloadcpython-55f3cce821f8f18ddb485aa07bdf0190c358d081.zip
cpython-55f3cce821f8f18ddb485aa07bdf0190c358d081.tar.gz
cpython-55f3cce821f8f18ddb485aa07bdf0190c358d081.tar.bz2
gh-111545: Test PyHash_GetFuncDef() function (#112098)
Add Modules/_testcapi/hash.c and Lib/test/test_capi/test_hash.py.
Diffstat (limited to 'Modules/_testcapi')
-rw-r--r--Modules/_testcapi/hash.c56
-rw-r--r--Modules/_testcapi/parts.h1
2 files changed, 57 insertions, 0 deletions
diff --git a/Modules/_testcapi/hash.c b/Modules/_testcapi/hash.c
new file mode 100644
index 0000000..d0b8127
--- /dev/null
+++ b/Modules/_testcapi/hash.c
@@ -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);
+}
diff --git a/Modules/_testcapi/parts.h b/Modules/_testcapi/parts.h
index 9af414e..29817ed 100644
--- a/Modules/_testcapi/parts.h
+++ b/Modules/_testcapi/parts.h
@@ -58,6 +58,7 @@ int _PyTestCapi_Init_Codec(PyObject *module);
int _PyTestCapi_Init_Immortal(PyObject *module);
int _PyTestCapi_Init_GC(PyObject *module);
int _PyTestCapi_Init_Sys(PyObject *module);
+int _PyTestCapi_Init_Hash(PyObject *module);
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
int _PyTestCapi_Init_HeaptypeRelative(PyObject *module);