Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ jobs:
matrix:
python-version: [3.6, 3.7, 3.8, 3.9, 3.10.0-beta.4]
os: [windows-latest, ubuntu-latest, macos-latest]
arch: [x64, x86]
exclude:
# 32-bit Python is only available on Windows
- os: ubuntu-latest
arch: x86
- os: macos-latest
arch: x86

steps:
- uses: actions/checkout@v2
Expand All @@ -38,11 +45,12 @@ jobs:
if: steps.release.outputs.version == 0
with:
python-version: ${{ matrix.python-version }}
architecture: ${{ matrix.arch }}

- name: Test
if: steps.release.outputs.version == 0
run: |
pip install -e .[test]
pip install --verbose -e .[test]
flake8 immutables/ tests/
mypy immutables/
python -m pytest -v
Expand Down
5 changes: 4 additions & 1 deletion immutables/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 9 additions & 5 deletions tests/test_none_keys.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ctypes
import unittest

from immutables.map import map_hash, map_mask, Map as PyMap
Expand All @@ -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):
Expand Down