GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-111545: Add Py_HashDouble() function by vstinner · Pull Request #112449 · python/cpython

gh-111545: Add Py_HashDouble() function #112449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vstinner wants to merge 6 commits into python:main from vstinner:hash_double3

Uh oh!

There was an error while loading. Please reload this page.

Closed

gh-111545: Add Py_HashDouble() function #112449

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
6 commits
Select commit Hold shift + click to select a range
250c3e1
gh-111545: Add Py_HashDouble() function
vstinner Nov 14, 2023
dd19138
Update
vstinner Dec 12, 2023
44d3bd9
Update Doc/c-api/hash.rst
vstinner Dec 12, 2023
6df0183
Update Lib/test/test_capi/test_hash.py
vstinner Dec 12, 2023
62ecc19
Update Python/pyhash.c
vstinner Dec 12, 2023
95a8f09
Suggest how to replace private _Py_HashDouble()
vstinner Dec 12, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

25 changes: 25 additions & 0 deletions 25 Doc/c-api/hash.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ PyHash API

See also the :c:member:`PyTypeObject.tp_hash` member.

Types
^^^^^

.. c:type:: Py_hash_t

Hash value type: signed integer.

.. versionadded:: 3.2


.. c:type:: Py_uhash_t

Hash value type: unsigned integer.
Expand Down Expand Up @@ -41,6 +45,27 @@ See also the :c:member:`PyTypeObject.tp_hash` member.
.. versionadded:: 3.4


Functions
^^^^^^^^^

.. c:function:: int Py_HashDouble(double value, Py_hash_t *result)

Hash a C double number.

* Set *\*result* to the hash value and return ``1`` on success.
* Set *\*result* to ``0`` and return ``0`` if the hash value cannot be
calculated. For example, if *value* is not-a-number (NaN).

*result* must not be ``NULL``.

.. note::
Only rely on the function return value to distinguish the "not-a-number"
case. *\*result* can be ``0`` if *value* is finite. For example,
``Py_HashDouble(0.0, &result)`` sets *\*result* to 0.
Comment thread
vstinner marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


Comment thread
vstinner marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

.. versionadded:: 3.13


.. c:function:: PyHash_FuncDef* PyHash_GetFuncDef(void)

Get the hash function definition.
Expand Down
15 changes: 15 additions & 0 deletions 15 Doc/whatsnew/3.13.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,21 @@ New Features
* Add :c:func:`Py_HashPointer` function to hash a pointer.
(Contributed by Victor Stinner in :gh:`111545`.)

* Add :c:func:`Py_HashDouble` function to hash a C double number. Existing code
using the private ``_Py_HashDouble()`` function can be updated to::

Py_hash_t
hash_double(PyObject *obj, double value)
{
Py_hash_t hash;
if (Py_HashDouble(value, &hash) == 0) {
hash = Py_HashPointer(obj);
}
return hash;
}

(Contributed by Victor Stinner in :gh:`111545`.)


Porting to Python 3.13
----------------------
Expand Down
1 change: 1 addition & 0 deletions 1 Include/cpython/pyhash.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ typedef struct {
PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);

PyAPI_FUNC(Py_hash_t) Py_HashPointer(const void *ptr);
PyAPI_FUNC(int) Py_HashDouble(double value, Py_hash_t *result);
44 changes: 44 additions & 0 deletions 44 Lib/test/test_capi/test_hash.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import sys
import unittest
from test.support import import_helper
Expand Down Expand Up @@ -77,3 +78,46 @@ def python_hash_pointer(x):
# Py_HashPointer((void*)(uintptr_t)-1) doesn't return -1 but -2
VOID_P_MAX = -1 & (2 ** (8 * SIZEOF_VOID_P) - 1)
self.assertEqual(hash_pointer(VOID_P_MAX), -2)

def test_hash_double(self):
# Test Py_HashDouble()
hash_double = _testcapi.hash_double

def check_number(value, expected):
self.assertEqual(hash_double(value), (1, expected))

# test some integers
integers = [
*range(1, 30),
2**30 - 1,
2 ** 233,
int(sys.float_info.max),
]
for x in integers:
with self.subTest(x=x):
check_number(float(x), hash(x))
check_number(float(-x), hash(-x))

# test positive and negative zeros
check_number(float(0.0), 0)
check_number(float(-0.0), 0)

# test +inf and -inf
inf = float("inf")
check_number(inf, sys.hash_info.inf)
check_number(-inf, -sys.hash_info.inf)

# special float values: compare with Python hash() function
special_values = (
math.nextafter(0.0, 1.0), # smallest positive subnormal number
sys.float_info.min, # smallest positive normal number
sys.float_info.epsilon,
sys.float_info.max, # largest positive finite number
)
for x in special_values:
with self.subTest(x=x):
check_number(x, hash(x))
check_number(-x, hash(-x))

# test not-a-number (NaN)
self.assertEqual(hash_double(float('nan')), (0, 0))
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/C API/2023-12-06-15-32-30.gh-issue-111545.kSOygi.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :c:func:`Py_HashDouble` function to hash a C double number. Patch by
Victor Stinner.
29 changes: 27 additions & 2 deletions 29 Modules/_testcapi/hash.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
#include "parts.h"
#include "util.h"


static PyObject *
long_from_hash(Py_hash_t hash)
{
assert(hash != -1);

Py_BUILD_ASSERT(sizeof(long long) >= sizeof(hash));
return PyLong_FromLongLong(hash);
}


static PyObject *
hash_getfuncdef(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
Expand Down Expand Up @@ -54,14 +65,28 @@ hash_pointer(PyObject *Py_UNUSED(module), PyObject *arg)
}

Py_hash_t hash = Py_HashPointer(ptr);
Py_BUILD_ASSERT(sizeof(long long) >= sizeof(hash));
return PyLong_FromLongLong(hash);
return long_from_hash(hash);
}


static PyObject *
hash_double(PyObject *Py_UNUSED(module), PyObject *args)
{
double value;
if (!PyArg_ParseTuple(args, "d", &value)) {
return NULL;
}

Py_hash_t hash;
int res = Py_HashDouble(value, &hash);
return Py_BuildValue("iN", res, long_from_hash(hash));
}


static PyMethodDef test_methods[] = {
{"hash_getfuncdef", hash_getfuncdef, METH_NOARGS},
{"hash_pointer", hash_pointer, METH_O},
{"hash_double", hash_double, METH_VARARGS},
{NULL},
};

Expand Down
32 changes: 25 additions & 7 deletions 32 Python/pyhash.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,23 @@ static Py_ssize_t hashstats[Py_HASH_STATS_MAX + 1] = {0};

*/

Py_hash_t
_Py_HashDouble(PyObject *inst, double v)
int
Py_HashDouble(double v, Py_hash_t *result)
{
int e, sign;
double m;
Py_uhash_t x, y;

if (!Py_IS_FINITE(v)) {
if (Py_IS_INFINITY(v))
return v > 0 ? _PyHASH_INF : -_PyHASH_INF;
else
return _Py_HashPointer(inst);
if (Py_IS_INFINITY(v)) {
*result = (v > 0 ? _PyHASH_INF : -_PyHASH_INF);
return 1;
}
else {
assert(Py_IS_NAN(v));
*result = 0;
return 0;
}
}

m = frexp(v, &e);
Expand Down Expand Up @@ -126,7 +131,20 @@ _Py_HashDouble(PyObject *inst, double v)
x = x * sign;
if (x == (Py_uhash_t)-1)
x = (Py_uhash_t)-2;
return (Py_hash_t)x;
*result = (Py_hash_t)x;
return 1;
}

Py_hash_t
_Py_HashDouble(PyObject *obj, double value)
{
assert(obj != NULL);

Py_hash_t hash;
if (Py_HashDouble(value, &hash) == 0) {
hash = Py_HashPointer(obj);
}
return hash;
}

Py_hash_t
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.