mirror of
https://github.com/python/cpython.git
synced 2025-07-10 04:45:36 +00:00
Issue #8188: Introduce a new scheme for computing hashes of numbers
(instances of int, float, complex, decimal.Decimal and fractions.Fraction) that makes it easy to maintain the invariant that hash(x) == hash(y) whenever x and y have equal value.
This commit is contained in:
parent
03721133a6
commit
dc787d2055
14 changed files with 566 additions and 137 deletions
|
@ -570,6 +570,57 @@ sys_setrecursionlimit(PyObject *self, PyObject *args)
|
|||
return Py_None;
|
||||
}
|
||||
|
||||
static PyTypeObject Hash_InfoType;
|
||||
|
||||
PyDoc_STRVAR(hash_info_doc,
|
||||
"hash_info\n\
|
||||
\n\
|
||||
A struct sequence providing parameters used for computing\n\
|
||||
numeric hashes. The attributes are read only.");
|
||||
|
||||
static PyStructSequence_Field hash_info_fields[] = {
|
||||
{"width", "width of the type used for hashing, in bits"},
|
||||
{"modulus", "prime number giving the modulus on which the hash "
|
||||
"function is based"},
|
||||
{"inf", "value to be used for hash of a positive infinity"},
|
||||
{"nan", "value to be used for hash of a nan"},
|
||||
{"imag", "multiplier used for the imaginary part of a complex number"},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
static PyStructSequence_Desc hash_info_desc = {
|
||||
"sys.hash_info",
|
||||
hash_info_doc,
|
||||
hash_info_fields,
|
||||
5,
|
||||
};
|
||||
|
||||
PyObject *
|
||||
get_hash_info(void)
|
||||
{
|
||||
PyObject *hash_info;
|
||||
int field = 0;
|
||||
hash_info = PyStructSequence_New(&Hash_InfoType);
|
||||
if (hash_info == NULL)
|
||||
return NULL;
|
||||
PyStructSequence_SET_ITEM(hash_info, field++,
|
||||
PyLong_FromLong(8*sizeof(long)));
|
||||
PyStructSequence_SET_ITEM(hash_info, field++,
|
||||
PyLong_FromLong(_PyHASH_MODULUS));
|
||||
PyStructSequence_SET_ITEM(hash_info, field++,
|
||||
PyLong_FromLong(_PyHASH_INF));
|
||||
PyStructSequence_SET_ITEM(hash_info, field++,
|
||||
PyLong_FromLong(_PyHASH_NAN));
|
||||
PyStructSequence_SET_ITEM(hash_info, field++,
|
||||
PyLong_FromLong(_PyHASH_IMAG));
|
||||
if (PyErr_Occurred()) {
|
||||
Py_CLEAR(hash_info);
|
||||
return NULL;
|
||||
}
|
||||
return hash_info;
|
||||
}
|
||||
|
||||
|
||||
PyDoc_STRVAR(setrecursionlimit_doc,
|
||||
"setrecursionlimit(n)\n\
|
||||
\n\
|
||||
|
@ -1482,6 +1533,11 @@ _PySys_Init(void)
|
|||
PyFloat_GetInfo());
|
||||
SET_SYS_FROM_STRING("int_info",
|
||||
PyLong_GetInfo());
|
||||
/* initialize hash_info */
|
||||
if (Hash_InfoType.tp_name == 0)
|
||||
PyStructSequence_InitType(&Hash_InfoType, &hash_info_desc);
|
||||
SET_SYS_FROM_STRING("hash_info",
|
||||
get_hash_info());
|
||||
SET_SYS_FROM_STRING("maxunicode",
|
||||
PyLong_FromLong(PyUnicode_GetMax()));
|
||||
SET_SYS_FROM_STRING("builtin_module_names",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue