Skip to content

Commit f9406ec

Browse files
authored
Modify Numpy seed restriction (#691)
1 parent 22049aa commit f9406ec

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ Changelog
55
Unreleased
66
----------
77

8-
* Removed the random state caching, which would grow without bound, leaking memory in long test runs.
8+
* Remove the random state caching, which would grow without bound, leaking memory in long test runs.
99
The caching was added to slightly speed up re-using the same (final) seed, but since the final seed is now different for each test, it has no effect.
1010

1111
`PR #690 <https://github.com/pytest-dev/pytest-randomly/issues/687>`__.
1212

13+
* Modify Numpy seed restriction, replacing hashing with a modulo operation.
14+
The extra work to hash is unnecessary now that we generate a final seed per test with CRC32.
15+
This change saves ~500ns per test when Numpy is installed.
16+
17+
`PR #691 <https://github.com/pytest-dev/pytest-randomly/issues/691>`__.
18+
1319
4.0.0 (2025-09-10)
1420
------------------
1521

src/pytest_randomly/__init__.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import argparse
4-
import hashlib
54
import random
65
import sys
76
from functools import lru_cache
@@ -160,8 +159,7 @@ def _reseed(config: Config, offset: int = 0) -> int:
160159
baker_random.setstate(random_state)
161160

162161
if have_numpy: # pragma: no branch
163-
numpy_seed = _truncate_seed_for_numpy(seed)
164-
np_random.seed(numpy_seed)
162+
np_random.seed(seed % 2**32)
165163

166164
if entrypoint_reseeds is None:
167165
eps = entry_points(group="pytest_randomly.random_seeder")
@@ -172,15 +170,6 @@ def _reseed(config: Config, offset: int = 0) -> int:
172170
return seed
173171

174172

175-
def _truncate_seed_for_numpy(seed: int) -> int:
176-
seed = abs(seed)
177-
if seed <= 2**32 - 1:
178-
return seed
179-
180-
seed_bytes = seed.to_bytes(seed.bit_length(), "big")
181-
return int.from_bytes(hashlib.sha512(seed_bytes).digest()[: 32 // 8], "big")
182-
183-
184173
def pytest_report_header(config: Config) -> str:
185174
seed = config.getoption("randomly_seed")
186175
_reseed(config)
@@ -189,7 +178,7 @@ def pytest_report_header(config: Config) -> str:
189178

190179
def pytest_runtest_setup(item: Item) -> None:
191180
if item.config.getoption("randomly_reset_seed"):
192-
_reseed(item.config, _crc32(item.nodeid) - 1)
181+
_reseed(item.config, (_crc32(item.nodeid) - 1) % 2**32)
193182

194183

195184
def pytest_runtest_call(item: Item) -> None:
@@ -199,7 +188,7 @@ def pytest_runtest_call(item: Item) -> None:
199188

200189
def pytest_runtest_teardown(item: Item) -> None:
201190
if item.config.getoption("randomly_reset_seed"):
202-
_reseed(item.config, _crc32(item.nodeid) + 1)
191+
_reseed(item.config, (_crc32(item.nodeid) + 1) % 2**32)
203192

204193

205194
@hookimpl(tryfirst=True)

0 commit comments

Comments
 (0)