| 1 |
# Simple support library for our run tests. |
| 2 |
|
| 3 |
from contextlib import contextmanager |
| 4 |
from collections.abc import Iterator |
| 5 |
import math |
| 6 |
import sys |
| 7 |
from typing import ( |
| 8 |
Any, Iterator, TypeVar, Generator, Optional, List, Tuple, Sequence, |
| 9 |
Union, Callable, Awaitable, Generic |
| 10 |
) |
| 11 |
from typing import Final |
| 12 |
|
| 13 |
FLOAT_MAGIC: Final = -113.0 |
| 14 |
|
| 15 |
# Various different float values |
| 16 |
float_vals = [ |
| 17 |
float(n) * 0.25 for n in range(-10, 10) |
| 18 |
] + [ |
| 19 |
-0.0, |
| 20 |
1.0/3.0, |
| 21 |
math.sqrt(2.0), |
| 22 |
1.23e200, |
| 23 |
-2.34e200, |
| 24 |
5.43e-100, |
| 25 |
-6.532e-200, |
| 26 |
float('inf'), |
| 27 |
-float('inf'), |
| 28 |
float('nan'), |
| 29 |
FLOAT_MAGIC, |
| 30 |
math.pi, |
| 31 |
2.0 * math.pi, |
| 32 |
math.pi / 2.0, |
| 33 |
-math.pi / 2.0, |
| 34 |
-1.7976931348623158e+308, # Smallest finite value |
| 35 |
-2.2250738585072014e-308, # Closest to zero negative normal value |
| 36 |
-7.5491e-312, # Arbitrary negative subnormal value |
| 37 |
-5e-324, # Closest to zero negative subnormal value |
| 38 |
1.7976931348623158e+308, # Largest finite value |
| 39 |
2.2250738585072014e-308, # Closest to zero positive normal value |
| 40 |
-6.3492e-312, # Arbitrary positive subnormal value |
| 41 |
5e-324, # Closest to zero positive subnormal value |
| 42 |
] |
| 43 |
|
| 44 |
@contextmanager |
| 45 |
def assertRaises(typ: type, msg: str = '') -> Iterator[None]: |
| 46 |
try: |
| 47 |
yield |
| 48 |
except BaseException as e: |
| 49 |
assert type(e) is typ, f"{e!r} is not a {typ.__name__}" |
| 50 |
assert msg in str(e), f'Message "{e}" does not match "{msg}"' |
| 51 |
else: |
| 52 |
assert False, f"Expected {typ.__name__} but got no exception" |
| 53 |
|
| 54 |
def assertDomainError() -> Any: |
| 55 |
return assertRaises(ValueError, "math domain error") |
| 56 |
|
| 57 |
def assertMathRangeError() -> Any: |
| 58 |
return assertRaises(OverflowError, "math range error") |
| 59 |
|
| 60 |
T = TypeVar('T') |
| 61 |
U = TypeVar('U') |
| 62 |
V = TypeVar('V') |
| 63 |
|
| 64 |
def run_generator(gen: Generator[T, V, U], |
| 65 |
inputs: Optional[List[V]] = None, |
| 66 |
p: bool = False) -> Tuple[Sequence[T], Union[U, str]]: |
| 67 |
res: List[T] = [] |
| 68 |
i = -1 |
| 69 |
while True: |
| 70 |
try: |
| 71 |
if i >= 0 and inputs: |
| 72 |
# ... fixtures don't have send |
| 73 |
val = gen.send(inputs[i]) # type: ignore |
| 74 |
elif not hasattr(gen, '__next__'): # type: ignore |
| 75 |
val = gen.send(None) # type: ignore |
| 76 |
else: |
| 77 |
val = next(gen) |
| 78 |
except StopIteration as e: |
| 79 |
return (tuple(res), e.value) |
| 80 |
except Exception as e: |
| 81 |
return (tuple(res), str(e)) |
| 82 |
if p: |
| 83 |
print(val) |
| 84 |
res.append(val) |
| 85 |
i += 1 |
| 86 |
|
| 87 |
F = TypeVar('F', bound=Callable) |
| 88 |
|
| 89 |
|
| 90 |
class async_val(Awaitable[V], Generic[T, V]): |
| 91 |
def __init__(self, val: T) -> None: |
| 92 |
self.val = val |
| 93 |
|
| 94 |
def __await__(self) -> Generator[T, V, V]: |
| 95 |
z = yield self.val |
| 96 |
return z |
| 97 |
|
| 98 |
|
| 99 |
# Wrap a mypyc-generated function in a real python function, to allow it to be |
| 100 |
# stuck into classes and the like. |
| 101 |
def make_python_function(f: F) -> F: |
| 102 |
def g(*args: Any, **kwargs: Any) -> Any: |
| 103 |
return f(*args, **kwargs) |
| 104 |
return g # type: ignore |
| 105 |
|
| 106 |
|
| 107 |
def is_gil_disabled() -> bool: |
| 108 |
return hasattr(sys, "_is_gil_enabled") and not sys._is_gil_enabled() |
| 109 |
|
| 110 |
|
| 111 |
def is_64_bit_platform() -> bool: |
| 112 |
return getattr(sys, "maxsize") > 2**32 |