diff --git a/mypyc/primitives/librt_random_ops.py b/mypyc/primitives/librt_random_ops.py index 6aaee84ecd0d6..bd1a6ea100380 100644 --- a/mypyc/primitives/librt_random_ops.py +++ b/mypyc/primitives/librt_random_ops.py @@ -1,5 +1,5 @@ from mypyc.ir.deps import LIBRT_RANDOM -from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER +from mypyc.ir.ops import ERR_MAGIC, ERR_MAGIC_OVERLAPPING, ERR_NEVER from mypyc.ir.rtypes import float_rprimitive, int64_rprimitive, random_rprimitive from mypyc.primitives.registry import function_op, method_op @@ -29,7 +29,7 @@ arg_types=[random_rprimitive, int64_rprimitive, int64_rprimitive], return_type=int64_rprimitive, c_function_name="LibRTRandom_Random_randint_internal", - error_kind=ERR_MAGIC, + error_kind=ERR_MAGIC_OVERLAPPING, dependencies=[LIBRT_RANDOM], ) @@ -49,7 +49,7 @@ arg_types=[random_rprimitive, int64_rprimitive, int64_rprimitive], return_type=int64_rprimitive, c_function_name="LibRTRandom_Random_randrange2_internal", - error_kind=ERR_MAGIC, + error_kind=ERR_MAGIC_OVERLAPPING, dependencies=[LIBRT_RANDOM], ) @@ -89,7 +89,7 @@ arg_types=[int64_rprimitive, int64_rprimitive], return_type=int64_rprimitive, c_function_name="LibRTRandom_module_randrange2_internal", - error_kind=ERR_MAGIC, + error_kind=ERR_MAGIC_OVERLAPPING, dependencies=[LIBRT_RANDOM], ) @@ -99,6 +99,6 @@ arg_types=[int64_rprimitive, int64_rprimitive], return_type=int64_rprimitive, c_function_name="LibRTRandom_module_randint_internal", - error_kind=ERR_MAGIC, + error_kind=ERR_MAGIC_OVERLAPPING, dependencies=[LIBRT_RANDOM], ) diff --git a/mypyc/test-data/exceptions.test b/mypyc/test-data/exceptions.test index 0bc6c2010f1bc..167b8c2bbf229 100644 --- a/mypyc/test-data/exceptions.test +++ b/mypyc/test-data/exceptions.test @@ -788,3 +788,43 @@ L1: L2: r1 = :: None return r1 + +[case testLibRTRandomOverlappingErrorValue] +from librt.random import Random, randrange +from mypy_extensions import i64 + +def f(r: Random, a: i64, b: i64) -> i64: + return r.randint(a, b) + +def g(n: i64) -> i64: + # Result is never negative, so -113 is unambiguously an error + return randrange(n) +[out] +def f(r, a, b): + r :: librt.random.Random + a, b, r0 :: i64 + r1 :: bit + r2 :: object + r3 :: i64 +L0: + r0 = LibRTRandom_Random_randint_internal(r, a, b) + r1 = r0 == -113 + if r1 goto L2 else goto L1 :: bool +L1: + return r0 +L2: + r2 = PyErr_Occurred() + if not is_error(r2) goto L3 (error at f:5) else goto L1 +L3: + r3 = :: i64 + return r3 +def g(n): + n, r0, r1 :: i64 +L0: + r0 = LibRTRandom_module_randrange1_internal(n) + if is_error(r0) goto L2 (error at g:9) else goto L1 +L1: + return r0 +L2: + r1 = :: i64 + return r1 diff --git a/mypyc/test-data/run-librt-random.test b/mypyc/test-data/run-librt-random.test index 0b34222678018..ff2fc97b15fbb 100644 --- a/mypyc/test-data/run-librt-random.test +++ b/mypyc/test-data/run-librt-random.test @@ -299,6 +299,42 @@ def test_wide_i64_randrange_python_api() -> None: val = randrange_any(lo, hi) assert lo <= val < hi +# +# Results equal to the i64 error value (-113) +# + +def test_randint_overlapping_error_value() -> None: + r = Random(0) + assert r.randint(-113, -113) == -113 + assert method_randint(r, -113, -113) == -113 + assert randint(-113, -113) == -113 + assert module_randint(-113, -113) == -113 + seen = set() + for i in range(1000): + val = r.randint(-114, -112) + assert -114 <= val <= -112 + seen.add(val) + val = randint(-114, -112) + assert -114 <= val <= -112 + seen.add(val) + assert -113 in seen + +def test_randrange_overlapping_error_value() -> None: + r = Random(0) + assert r.randrange(-113, -112) == -113 + assert method_randrange(r, -113, -112) == -113 + assert randrange(-113, -112) == -113 + assert module_randrange(-113, -112) == -113 + seen = set() + for i in range(1000): + val = r.randrange(-114, -111) + assert -114 <= val < -111 + seen.add(val) + val = randrange(-114, -111) + assert -114 <= val < -111 + seen.add(val) + assert -113 in seen + # # Error handling #