Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions mypyc/primitives/librt_random_ops.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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],
)

Expand All @@ -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],
)

Expand Down Expand Up @@ -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],
)

Expand All @@ -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],
)
40 changes: 40 additions & 0 deletions mypyc/test-data/exceptions.test
Original file line number Diff line number Diff line change
Expand Up @@ -788,3 +788,43 @@ L1:
L2:
r1 = <error> :: 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 = <error> :: 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 = <error> :: i64
return r1
36 changes: 36 additions & 0 deletions mypyc/test-data/run-librt-random.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
Loading