def make_timedelta(p: Callable) -> datetime.timedelta: microseconds, seconds, days = p(int), p(int), p(int) # the normalized ranges, per the docs: if not (0 <= microseconds < 1000000 and 0 <= seconds < 3600 * 24 and -999999999 <= days <= 999999999): raise IgnoreAttempt('Invalid timedelta') try: return _timedelta_skip_construct(days, seconds, microseconds) except OverflowError: raise IgnoreAttempt('Invalid timedelta')
def make_date(p: Callable) -> datetime.date: year, month, day = p(int), p(int), p(int) # This condition isn't technically required, but it develops useful # symbolic inequalities before the realization below: if not (1 <= year <= 9999 and 1 <= month <= 12 and 1 <= day <= 31): raise IgnoreAttempt('Invalid date') try: return datetime.date(realize(year), realize(month), realize(day)) except ValueError: raise IgnoreAttempt('Invalid date')
def size(self): totalsize = 1 for size in self.shape: if size < 0: raise IgnoreAttempt('ndarray disallows negative dimensions') totalsize *= size return totalsize
def make_timezone(p: Any) -> datetime.timezone: if p.space.smt_fork(): delta = p(datetime.timedelta) if _min_tz_offset < delta < _max_tz_offset: return datetime.timezone(delta, p(str)) else: raise IgnoreAttempt('Invalid timezone offset') else: return datetime.timezone.utc
def make_timezone(p: Any) -> datetime.timezone: if p.space.smt_fork(): delta = p(datetime.timedelta, "_offset") if _min_tz_offset < delta < _max_tz_offset: return datetime.timezone(delta, realize(p(str, "_name"))) else: raise IgnoreAttempt("Invalid timezone offset") else: return datetime.timezone.utc
def make_timezone(p: Any) -> datetime.timezone: if p.space.smt_fork(desc="use explicit timezone"): delta = p(datetime.timedelta, "_offset") with ResumedTracing(): if _min_tz_offset < delta < _max_tz_offset: return datetime.timezone(delta, realize(p(str, "_name"))) else: raise IgnoreAttempt("Invalid timezone offset") else: return datetime.timezone.utc
def make_date(p: Any) -> datetime.date: year, month, day = p(int), p(int), p(int) try: p.space.defer_assumption( 'Invalid date', lambda: (not _raises_value_error(orig_check_date_fields, (year, month, day)))) return datetime.date(year, month, day) except ValueError: raise IgnoreAttempt('Invalid date')
def __array__(self): if any(size < 0 for size in self.shape): raise IgnoreAttempt('ndarray disallows negative dimensions') concrete_shape = tuple(map(int, self.shape)) concrete_dtype = realize(self.dtype) # For the contents, we just construct it with ones. This makes it much # less complete in terms of finding counterexamples, but is sufficient # for array dimension and type reasoning. If we were more ambitious, # we would rewrite a (slow) implementation of numpy in terms of native # Python types. return np.ones(concrete_shape, concrete_dtype)
def make_time(p: Any) -> datetime.time: hour, minute, sec, usec = p(int), p(int), p(int), p(int) tzinfo = p(datetime.timezone) if p.space.smt_fork() else None try: p.space.defer_assumption( 'Invalid datetime', lambda: (not _raises_value_error(orig_check_time_fields, (hour, minute, sec, usec, 0)))) return datetime.time(hour, minute, sec, usec, tzinfo) except ValueError: raise IgnoreAttempt('Invalid datetime')
def make_time(p: Any) -> datetime.time: hour = p(int, "_hour") minute = p(int, "_minute") sec = p(int, "_sec") usec = p(int, "_usec") tzinfo = p(Optional[datetime.timezone], "_tzinfo") try: p.space.defer_assumption( "Invalid datetime", lambda: (not _raises_value_error(orig_check_time_fields, (hour, minute, sec, usec, 0))), ) return datetime.time(hour, minute, sec, usec, tzinfo) except ValueError: raise IgnoreAttempt("Invalid datetime")
def make_datetime(p: Any) -> datetime.datetime: year, month, day, hour, minute, sec, usec = ( p(int, "_year"), p(int, "_month"), p(int, "_day"), p(int, "_hour"), p(int, "_minute"), p(int, "_sec"), p(int, "usec"), ) tzinfo = p(Optional[datetime.timezone], "_tz") try: p.space.defer_assumption( "Invalid datetime", lambda: (not _raises_value_error(orig_check_date_fields, (year, month, day)) and not _raises_value_error(orig_check_time_fields, (hour, minute, sec, usec, 0))), ) return datetime.datetime(year, month, day, hour, minute, sec, usec, tzinfo) except ValueError: raise IgnoreAttempt("Invalid datetime")