@@ -88,13 +88,10 @@ def datetime_does_not_exist(value):
8888
8989
9090def _num_days_in_month (year , month ):
91- """Equivalent to ``monthrange(year, month)[1]`` for valid inputs.
91+ """Branchless equivalent of ``monthrange(year, month)[1]`` for valid inputs.
9292
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.
93+ Written using only arithmetic and (in)equality, with no branching or indexing.
94+ This avoids concretizing the input or adding more path constraints than necessary.
9895 """
9996 leap = (year % 4 == 0 ) * (1 - (year % 100 == 0 ) * (year % 400 != 0 ))
10097 is_feb = month == 2
@@ -108,25 +105,27 @@ def draw_capped_multipart(
108105 assert isinstance (min_value , (dt .date , dt .time , dt .datetime ))
109106 assert type (min_value ) == type (max_value )
110107 assert min_value <= max_value
108+
109+ # cap_{low, high} records whether every field drawn so far has equalled
110+ # ``min_value``'s / ``max_value``'s, i.e. whether that bound is still "active" and
111+ # constrains the next field.
112+ #
113+ # cap_{low, high} are conceptually booleans. We define them as integers and interpret
114+ # boolean operations on them as multiplication, so that we don't concretize or
115+ # branch under symbolic backends. See
116+ # https://github.com/HypothesisWorks/hypothesis/issues/4759.
117+ cap_low = 1
118+ cap_high = 1
111119 result = {}
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
122120 for name in duration_names :
123121 natural_low = getattr (dt .datetime .min , name )
124122 if name == "day" :
125123 natural_high = _num_days_in_month (result ["year" ], result ["month" ])
126124 else :
127125 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
126+ # equivalent to:
127+ # low = min_value.<name> if cap_low else natural_low
128+ # high = max_value.<name> if cap_high else natural_high
130129 low = natural_low + cap_low * (getattr (min_value , name ) - natural_low )
131130 high = natural_high + cap_high * (getattr (max_value , name ) - natural_high )
132131 if name == "year" :
0 commit comments