|
12 | 12 | import operator as op |
13 | 13 | import warnings |
14 | 14 | import zoneinfo |
15 | | -from calendar import monthrange |
16 | 15 | from functools import cache, partial |
17 | 16 | from importlib import resources |
18 | 17 | from pathlib import Path |
@@ -88,26 +87,55 @@ def datetime_does_not_exist(value): |
88 | 87 | return value != roundtrip |
89 | 88 |
|
90 | 89 |
|
| 90 | +def _num_days_in_month(year, month): |
| 91 | + """Equivalent to ``monthrange(year, month)[1]`` for valid inputs. |
| 92 | +
|
| 93 | + Written using only arithmetic and (in)equality, with no branching, ``and``, |
| 94 | + ``or``, or indexing on ``month``/``year``. This keeps the result symbolic |
| 95 | + under symbolic-execution backends (which would otherwise concretize the year |
| 96 | + and month the moment they hit a branch or a list lookup), while remaining a |
| 97 | + handful of cheap integer ops for the blackbox engine. |
| 98 | + """ |
| 99 | + leap = (year % 4 == 0) * (1 - (year % 100 == 0) * (year % 400 != 0)) |
| 100 | + is_feb = month == 2 |
| 101 | + is_30_day = 1 - (month != 4) * (month != 6) * (month != 9) * (month != 11) |
| 102 | + return 31 - is_30_day - is_feb * (3 - leap) |
| 103 | + |
| 104 | + |
91 | 105 | def draw_capped_multipart( |
92 | 106 | data, min_value, max_value, duration_names=DATENAMES + TIMENAMES |
93 | 107 | ): |
94 | 108 | assert isinstance(min_value, (dt.date, dt.time, dt.datetime)) |
95 | 109 | assert type(min_value) == type(max_value) |
96 | 110 | assert min_value <= max_value |
97 | 111 | result = {} |
98 | | - cap_low, cap_high = True, True |
| 112 | + # ``cap_low``/``cap_high`` are 0/1 integers (not booleans) recording whether |
| 113 | + # every field drawn so far has equalled ``min_value``'s / ``max_value``'s, |
| 114 | + # i.e. whether that bound is still "active" and constrains the next field. |
| 115 | + # We combine them into the bounds with arithmetic rather than branching, so |
| 116 | + # that the *same* code stays fully symbolic under symbolic-execution |
| 117 | + # backends (important for cross-backend database replay) yet compiles down |
| 118 | + # to a few integer ops for the blackbox engine. The draws themselves -- |
| 119 | + # their count, order, bounds, and ``shrink_towards`` -- are byte-for-byte |
| 120 | + # identical to the previous branchy implementation. |
| 121 | + cap_low = cap_high = 1 |
99 | 122 | for name in duration_names: |
100 | | - low = getattr(min_value if cap_low else dt.datetime.min, name) |
101 | | - high = getattr(max_value if cap_high else dt.datetime.max, name) |
102 | | - if name == "day" and not cap_high: |
103 | | - _, high = monthrange(**result) |
| 123 | + natural_low = getattr(dt.datetime.min, name) |
| 124 | + if name == "day": |
| 125 | + natural_high = _num_days_in_month(result["year"], result["month"]) |
| 126 | + else: |
| 127 | + natural_high = getattr(dt.datetime.max, name) |
| 128 | + # low = min_value.<name> if cap_low else natural_low |
| 129 | + # high = max_value.<name> if cap_high else natural_high |
| 130 | + low = natural_low + cap_low * (getattr(min_value, name) - natural_low) |
| 131 | + high = natural_high + cap_high * (getattr(max_value, name) - natural_high) |
104 | 132 | if name == "year": |
105 | 133 | val = data.draw_integer(low, high, shrink_towards=2000) |
106 | 134 | else: |
107 | 135 | val = data.draw_integer(low, high) |
108 | 136 | result[name] = val |
109 | | - cap_low = cap_low and val == low |
110 | | - cap_high = cap_high and val == high |
| 137 | + cap_low = cap_low * (val == low) |
| 138 | + cap_high = cap_high * (val == high) |
111 | 139 | if hasattr(min_value, "fold"): |
112 | 140 | # The `fold` attribute is ignored in comparison of naive datetimes. |
113 | 141 | # In tz-aware datetimes it would require *very* invasive changes to |
|
0 commit comments