Skip to content

Commit 3187fb9

Browse files
authored
Merge pull request #4868 from Zac-HD/claude/hypothesis-datetime-strategy-ajzai0
Bias datetimes() towards bug-revealing values
2 parents 67e5c04 + a60dc77 commit 3187fb9

12 files changed

Lines changed: 871 additions & 39 deletions

File tree

‎hypothesis/RELEASE.rst‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
RELEASE_TYPE: minor
2+
3+
:func:`~hypothesis.strategies.datetimes` now generates "tricky" datetimes
4+
more often: values on or near daylight-saving and other utc-offset
5+
transitions of the drawn timezone - including imaginary wall times, and
6+
ambiguous ones with each value of ``fold`` - as well as times adjacent to
7+
leap seconds and to well-known datetimes like the millennium and the end of
8+
the signed 32-bit Unix epoch (:issue:`69`).
9+
10+
This release also fixes some rare internal errors, where an error raised
11+
partway through updating an internal cache - for example by a deeply
12+
recursive strategy raising ``RecursionError`` - could leave that
13+
cache corrupted.

‎hypothesis/src/hypothesis/internal/cache.py‎

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,43 @@ def data(self) -> list[Entry[K, V]]:
9191
self._threadlocal.data = []
9292
return self._threadlocal.data
9393

94+
def _repair_if_interrupted(self) -> None:
95+
"""An exception raised partway through a mutation - most plausibly a
96+
RecursionError below a deeply recursive strategy - can leave the heap
97+
property or the key index broken. We note the interruption and
98+
rebuild both here, on the next operation, from the surviving entries
99+
(a sorted list is heap-ordered), rather than attempting a repair at
100+
the moment the stack overflowed.
101+
"""
102+
if getattr(self._threadlocal, "interrupted", False):
103+
self.data.sort(key=lambda e: e.sort_key)
104+
self.keys_to_indices.clear()
105+
for i, e in enumerate(self.data):
106+
self.keys_to_indices[e.key] = i
107+
self._threadlocal.interrupted = False
108+
94109
def __len__(self) -> int:
110+
self._repair_if_interrupted()
95111
assert len(self.keys_to_indices) == len(self.data)
96112
return len(self.data)
97113

98114
def __contains__(self, key: K) -> bool:
115+
self._repair_if_interrupted()
99116
return key in self.keys_to_indices
100117

101118
def __getitem__(self, key: K) -> V:
119+
self._repair_if_interrupted()
102120
i = self.keys_to_indices[key]
103121
result = self.data[i]
104-
self.__entry_was_accessed(i)
122+
try:
123+
self.__entry_was_accessed(i)
124+
except BaseException:
125+
self._threadlocal.interrupted = True
126+
raise
105127
return result.value
106128

107129
def __setitem__(self, key: K, value: V) -> None:
130+
self._repair_if_interrupted()
108131
evicted = None
109132
try:
110133
i = self.keys_to_indices[key]
@@ -122,13 +145,21 @@ def __setitem__(self, key: K, value: V) -> None:
122145
else:
123146
i = len(self.data)
124147
self.data.append(entry)
125-
self.keys_to_indices[key] = i
126-
self.__balance(i)
148+
try:
149+
self.keys_to_indices[key] = i
150+
self.__balance(i)
151+
except BaseException:
152+
self._threadlocal.interrupted = True
153+
raise
127154
else:
128155
entry = self.data[i]
129156
assert entry.key == key
130157
entry.value = value
131-
self.__entry_was_accessed(i)
158+
try:
159+
self.__entry_was_accessed(i)
160+
except BaseException:
161+
self._threadlocal.interrupted = True
162+
raise
132163

133164
if evicted is not None:
134165
if self.data[0] is not entry:
@@ -150,18 +181,27 @@ def pin(self, key: K, value: V) -> None:
150181
entry = self.data[i]
151182
entry.pins += 1
152183
if entry.pins == 1:
153-
self.__balance(i)
184+
try:
185+
self.__balance(i)
186+
except BaseException:
187+
self._threadlocal.interrupted = True
188+
raise
154189

155190
def unpin(self, key: K) -> None:
156191
"""Undo one previous call to ``pin(key)``. The value stays the same.
157192
Once all calls are undone this key may be evicted as normal."""
193+
self._repair_if_interrupted()
158194
i = self.keys_to_indices[key]
159195
entry = self.data[i]
160196
if entry.pins == 0:
161197
raise ValueError(f"Key {key!r} has not been pinned")
162198
entry.pins -= 1
163199
if entry.pins == 0:
164-
self.__balance(i)
200+
try:
201+
self.__balance(i)
202+
except BaseException:
203+
self._threadlocal.interrupted = True
204+
raise
165205

166206
def is_pinned(self, key: K) -> bool:
167207
"""Returns True if the key is currently pinned."""

‎hypothesis/src/hypothesis/internal/conjecture/data.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,9 @@ def draw(
12951295
self._inverting = True
12961296
try:
12971297
inverted = unwrapped._invert(hole.value)
1298-
except CannotInvert:
1298+
except Exception:
1299+
# Usually CannotInvert, but _invert may execute arbitrary user code, eg
1300+
# if a .filter is involved.
12991301
pass
13001302
else:
13011303
self.prefix = (

0 commit comments

Comments
 (0)