Skip to content

Commit c99f273

Browse files
committed
Cross-ref SimpleNamespace, snapshot sys.modules, add regression test
- Use :class:`~types.SimpleNamespace` cross-ref in RELEASE.rst - Wrap sys.modules.items() in list() to snapshot against concurrent mutation - Add regression test for unhashable sys.modules entries https://claude.ai/code/session_01PQkqWu11828t7hwzAyR1sZ
1 parent 0854c39 commit c99f273

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

hypothesis-python/RELEASE.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
RELEASE_TYPE: patch
22

33
This patch fixes a crash when :obj:`sys.modules` contains unhashable values,
4-
such as ``types.SimpleNamespace`` objects (:issue:`4660`).
4+
such as :class:`~types.SimpleNamespace` objects (:issue:`4660`).

hypothesis-python/src/hypothesis/internal/conjecture/providers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def _get_local_constants() -> Constants:
312312
# with other threads loading a module before we set _sys_modules_len.
313313
if (sys_modules_len := len(sys.modules)) != _sys_modules_len:
314314
new_modules = []
315-
for name, module in sys.modules.items():
315+
for name, module in list(sys.modules.items()):
316316
try:
317317
seen = module in _seen_modules
318318
except TypeError:

hypothesis-python/tests/conjecture/test_local_constants.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

1111
import math
12+
import sys
13+
from types import SimpleNamespace
1214

1315
import pytest
1416

@@ -92,3 +94,14 @@ def f(n):
9294
pass
9395

9496
f()
97+
98+
99+
def test_unhashable_sys_modules_entry(monkeypatch):
100+
# Regression test for https://github.com/HypothesisWorks/hypothesis/issues/4660
101+
# Some packages (e.g. cog) place unhashable objects like SimpleNamespace
102+
# in sys.modules. This should not crash _get_local_constants.
103+
monkeypatch.setattr(providers, "_sys_modules_len", None)
104+
monkeypatch.setattr(providers, "_seen_modules", set())
105+
monkeypatch.setitem(sys.modules, "_unhashable_test_mod", SimpleNamespace())
106+
107+
providers._get_local_constants()

0 commit comments

Comments
 (0)