[Python-checkins] peps: Get the C API straight

christian.heimes python-checkins at python.org
Sun Oct 6 15:56:10 CEST 2013


http://hg.python.org/peps/rev/9c0744c9e394
changeset:   5171:9c0744c9e394
user:        Christian Heimes <christian at cheimes.de>
date:        Sun Oct 06 15:56:00 2013 +0200
summary:
  Get the C API straight
Python already has sys.hash_info

files:
  pep-0456.txt |  51 ++++++++++++++++++++++++++-------------
  1 files changed, 34 insertions(+), 17 deletions(-)


diff --git a/pep-0456.txt b/pep-0456.txt
--- a/pep-0456.txt
+++ b/pep-0456.txt
@@ -310,31 +310,43 @@
 
 function prototype::
 
-    typedef Py_hash_t (*PyHash_Func)(const void *, Py_ssize_t);
+    typedef Py_hash_t (*PyHash_Func_t)(const void *, Py_ssize_t);
 
 
-hash function table
--------------------
+hash function selection
+-----------------------
 
 type definition::
 
+    #define PY_HASH_SIPHASH24 0x53495024
+    #define PY_HASH_FNV 0x464E56
+
+    #ifndef PY_HASH_ALGORITHM
+    #if defined(PY_UINT64_T) && defined(PY_UINT32_T)
+    #define PY_HASH_ALGORITHM PY_HASH_SIPHASH24
+    #else
+    #define PY_HASH_ALGORITHM PY_HASH_FNV
+    #endif /* uint64_t && uint32_t */
+    #endif /* PY_HASH_ALGORITHM */
+
     typedef struct {
-        PyHash_Func hash;      /* function pointer */
+        PyHash_Func_t hash;    /* function pointer */
         char *name;            /* name of the hash algorithm and variant */
         int hash_bits;         /* internal size of hash value */
         int seed_bits;         /* size of seed input */
-    } _PyHash_FuncDef;
+    } PyHash_FuncDef;
 
-    PyAPI_DATA(_PyHash_FuncDef *) _PyHash_Func;
+    PyAPI_DATA(PyHash_FuncDef) PyHash_Func;
 
 Implementation::
 
-    #ifndef PY_HASH_FUNC
-    #ifdef PY_UINT64_T
-    _PyHash_Func = {siphash24, "sip24", 64, 128}
-    #else
-    _PyHash_Func = {fnv, "fnv", 8 * sizeof(Py_hash_t), 16 * sizeof(Py_hash_t)}
+    #if PY_HASH_ALGORITHM == PY_HASH_FNV
+    PyHash_FuncDef PyHash_Func = {fnv, "fnv", 8 * sizeof(Py_hash_t),
+                                  16 * sizeof(Py_hash_t)};
     #endif
+
+    #if PY_HASH_ALGORITHM == PY_HASH_SIPHASH24
+    PyHash_FuncDef PyHash_Func = {siphash24, "siphash24", 64, 128};
     #endif
 
 TODO: select hash algorithm with autoconf variable
@@ -346,14 +358,19 @@
 sys module
 ----------
 
-The sys module grows a new struct member with information about the select
-algorithm as well as all available algorithms.
+The sys module already has a hash_info struct sequence. More fields are added
+to the object to reflect the active hash algorithm and its properties.
 
 ::
 
-    sys.hash_info(algorithm='siphash24',
+    sys.hash_info(width=64,
+                  modulus=2305843009213693951,
+                  inf=314159,
+                  nan=0,
+                  imag=1000003,
+                  # new fields:
+                  algorithm='siphash24',
                   hash_bits=64,
-                  hash_output=64,        # 8 * sizeof(Py_hash_t)
                   seed_bits=128)
 
 
@@ -411,8 +428,8 @@
 
     if (PyUnicode_READY(u) == -1)
         return -1;
-    x = _PyHash_Func->hash(PyUnicode_DATA(u),
-                           PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u));
+    x = PyHash_Func.hash(PyUnicode_DATA(u),
+                         PyUnicode_GET_LENGTH(u) * PyUnicode_KIND(u));
 
 
 generic_hash (Modules/_datetimemodule.c)

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list