PyHash API¶
另请参阅 PyTypeObject.tp_hash 成员和 数字类型的哈希运算。
-
type Py_hash_t¶
哈希值类型:有符号整数。
Added in version 3.2.
-
type Py_uhash_t¶
哈希值类型:无符号整数。
Added in version 3.2.
-
Py_HASH_ALGORITHM¶
一个指明用于
str,bytes和memoryview的哈希算法的数字值。算法名称由
sys.hash_info.algorithm对外公开。Added in version 3.4.
-
Py_HASH_FNV¶
-
Py_HASH_SIPHASH24¶
-
Py_HASH_SIPHASH13¶
-
Py_HASH_EXTERNAL¶
Numerical values to compare to
Py_HASH_ALGORITHMto determine which algorithm is used for hashing. The built-in hash algorithm can be configured via the configure--with-hash-algorithmoption.Py_HASH_EXTERNALhas value0and indicates a hash implementation supplied by the application embedding Python; seePyHash_Func.Added in version 3.4: Add
Py_HASH_FNV,Py_HASH_SIPHASH24, andPy_HASH_EXTERNAL.Added in version 3.11: 增加
Py_HASH_SIPHASH13。
-
Py_HASH_CUTOFF¶
长度在
[1, Py_HASH_CUTOFF)范围内的缓冲区将使用 DJBX33A 而不是由Py_HASH_ALGORITHM所指定的哈希算法。Py_HASH_CUTOFF值为 0 将禁用优化。Py_HASH_CUTOFF必须为小于等于 7 的非负数。
32 位平台应当使用比 64 位平台更小的截断值,因为在 32 位平台上更容易创建碰撞字符串。截断值在 64 位平台上为 7 而在 32 位平台上为 5 应当能够提供足够的安全边界。
这对应于
sys.hash_info.cutoff常量。Added in version 3.4.
-
PyHASH_MODULUS¶
梅森素数
P = 2**n -1,用于数字哈希方案。这对应于
sys.hash_info.modulus常量。Added in version 3.13.
-
PyHASH_BITS¶
PyHASH_MODULUS中P的指数n。Added in version 3.13.
-
PyHASH_MULTIPLIER¶
用于字符串和多种其他哈希算法的质数乘数。
Added in version 3.13.
-
PyHASH_INF¶
针对正无穷大返回的哈希值。
这对应于
sys.hash_info.inf常量。Added in version 3.13.
-
PyHASH_IMAG¶
用于复数虚部的乘数。
这对应于
sys.hash_info.imag常量。Added in version 3.13.
-
type PyHash_FuncDef¶
PyHash_GetFuncDef()使用的哈希函数定义。-
Py_hash_t (*const hash)(const void*, Py_ssize_t)¶
哈希函数。
-
const char *name¶
哈希函数名称(UTF-8 编码的字符串)。
这对应于
sys.hash_info.algorithm常量。
-
const int hash_bits¶
以比特位表示的哈希值内部大小。
这对应于
sys.hash_info.hash_bits常量。
-
const int seed_bits¶
以比特位表示的输入种子值大小。
这对应于
sys.hash_info.seed_bits常量。
Added in version 3.4.
-
Py_hash_t (*const hash)(const void*, Py_ssize_t)¶
-
PyHash_FuncDef PyHash_Func¶
When Python is built with
Py_HASH_ALGORITHMset toPy_HASH_EXTERNAL, the application embedding Python must define this variable to supply a hash implementation instead of using one of Python's built-in hash algorithms.Added in version 3.4.
从 3.15 版起已弃用,将在 3.19 版中移除: Use one of the built-in hash algorithms instead.
-
PyHash_FuncDef *PyHash_GetFuncDef(void)¶
获取哈希函数定义。
参见
PEP 456 "安全且可互换的哈希算法"。
Added in version 3.4.
-
Py_hash_t Py_HashPointer(const void *ptr)¶
对指针值执行哈希运算:将指针值作为整数来处理(在内部将其转换为
uintptr_t类型)。指针不会被解引用。此函数不会失败:它不可能返回
-1。Added in version 3.13.
-
Py_hash_t Py_HashBuffer(const void *ptr, Py_ssize_t len)¶
- 属于 稳定 ABI 自 3.16 版起.
计算并返回从地址 ptr 开始的 len 字节缓冲区的哈希值。哈希保证与
bytes、memoryview和其他实现 缓冲区协议 的内置对象的哈希匹配。使用此函数为不可变对象实现哈希,这些对象的
tp_richcompare函数会与另一个对象的缓冲区进行比较。len 必须大于或等于
0。这个函数总是能够成功执行。
Added in version 3.14.
-
Py_hash_t PyObject_GenericHash(PyObject *obj)¶
旨在放入类型对象的
tp_hash槽位的通用哈希函数。其结果仅取决于对象的标识。在 CPython 中,它等价于
Py_HashPointer()。Added in version 3.13.