mirror of
https://github.com/gentoo-mirror/gentoo.git
synced 2026-11-23 23:27:12 -08:00
dev-python/immutables: Backport hash fixes for 32-bit platforms
Closes: https://bugs.gentoo.org/801634 Signed-off-by: Michał Górny <mgorny@gentoo.org>
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
From fa355239e70411179c70b16ed4ff7113d8008dad Mon Sep 17 00:00:00 2001
|
||||
From: Elvis Pranskevichus <elvis@edgedb.com>
|
||||
Date: Wed, 4 Aug 2021 19:25:44 -0700
|
||||
Subject: [PATCH] Fix test_none_collisions on 32-bit systems (#69)
|
||||
|
||||
There are two issues at play here:
|
||||
|
||||
1. Python version of `map_hash` unnecessarily performs hash truncation
|
||||
even if the hash is already 32-bit wide, which potentially converts
|
||||
it from signed int to unsigned long.
|
||||
|
||||
2. The `test_none_collisions` test generates a collision node with
|
||||
hash greater than 2^32.
|
||||
|
||||
Both of these are problematic on 32-bit systems, where `sizeof(Py_hash_t)`
|
||||
is 4, and so anything that doesn't fit into `Py_hash_t` gets bit-mangled,
|
||||
breaking the `hash(x) != x` invariance that the test relies upon.
|
||||
|
||||
Fixes: #53
|
||||
Fixes: #50
|
||||
---
|
||||
.github/workflows/tests.yml | 10 +++++++++-
|
||||
immutables/map.py | 5 ++++-
|
||||
tests/test_none_keys.py | 14 +++++++++-----
|
||||
3 files changed, 22 insertions(+), 7 deletions(-)
|
||||
|
||||
diff --git a/immutables/map.py b/immutables/map.py
|
||||
index 2c1ffa91..0ad28588 100644
|
||||
--- a/immutables/map.py
|
||||
+++ b/immutables/map.py
|
||||
@@ -19,7 +19,10 @@
|
||||
|
||||
def map_hash(o):
|
||||
x = hash(o)
|
||||
- return (x & 0xffffffff) ^ ((x >> 32) & 0xffffffff)
|
||||
+ if sys.hash_info.width > 32:
|
||||
+ return (x & 0xffffffff) ^ ((x >> 32) & 0xffffffff)
|
||||
+ else:
|
||||
+ return x
|
||||
|
||||
|
||||
def map_mask(hash, shift):
|
||||
diff --git a/tests/test_none_keys.py b/tests/test_none_keys.py
|
||||
index 8c0bb379..26d4220b 100644
|
||||
--- a/tests/test_none_keys.py
|
||||
+++ b/tests/test_none_keys.py
|
||||
@@ -1,3 +1,4 @@
|
||||
+import ctypes
|
||||
import unittest
|
||||
|
||||
from immutables.map import map_hash, map_mask, Map as PyMap
|
||||
@@ -6,16 +7,19 @@
|
||||
|
||||
none_hash = map_hash(None)
|
||||
assert(none_hash != 1)
|
||||
-assert((none_hash >> 32) == 0)
|
||||
+assert(none_hash.bit_length() <= 32)
|
||||
|
||||
-not_collision = 0xffffffff & (~none_hash)
|
||||
+none_hash_u = ctypes.c_size_t(none_hash).value
|
||||
+not_collision = 0xffffffff & (~none_hash_u)
|
||||
|
||||
mask = 0x7ffffffff
|
||||
-none_collisions = [none_hash & (mask >> shift)
|
||||
+none_collisions = [none_hash_u & (mask >> shift)
|
||||
for shift in reversed(range(0, 32, 5))]
|
||||
assert(len(none_collisions) == 7)
|
||||
-none_collisions = [h | (not_collision & (mask << shift))
|
||||
- for shift, h in zip(range(5, 37, 5), none_collisions)]
|
||||
+none_collisions = [
|
||||
+ ctypes.c_ssize_t(h | (not_collision & (mask << shift))).value
|
||||
+ for shift, h in zip(range(5, 37, 5), none_collisions)
|
||||
+]
|
||||
|
||||
|
||||
class NoneCollision(HashKey):
|
||||
@@ -0,0 +1,22 @@
|
||||
# Copyright 2019-2021 Gentoo Authors
|
||||
# Distributed under the terms of the GNU General Public License v2
|
||||
|
||||
EAPI=7
|
||||
|
||||
PYTHON_COMPAT=( python3_{8..10} )
|
||||
inherit distutils-r1
|
||||
|
||||
DESCRIPTION="A high-performance immutable mapping type for Python"
|
||||
HOMEPAGE="https://github.com/MagicStack/immutables"
|
||||
SRC_URI="https://github.com/MagicStack/${PN}/archive/v${PV}.tar.gz -> ${P}.tar.gz"
|
||||
|
||||
LICENSE="Apache-2.0"
|
||||
SLOT="0"
|
||||
KEYWORDS="~amd64 ~arm ~arm64 ~ppc ~ppc64 ~sparc ~x86"
|
||||
|
||||
PATCHES=(
|
||||
# https://github.com/MagicStack/immutables/commit/fa355239e70411179c70b16ed4ff7113d8008dad
|
||||
"${FILESDIR}"/${P}-32bit-hash.patch
|
||||
)
|
||||
|
||||
distutils_enable_tests pytest
|
||||
Reference in New Issue
Block a user