-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] Add 'range' primitive type #10307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
9532c3c
c45edb6
8c959f1
08616b8
bc76b92
70376d9
94399bd
017711e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3646,3 +3646,69 @@ L0: | |
r2 = r1 >= 0 :: signed | ||
r3 = truncate r1: int32 to builtins.bool | ||
return r3 | ||
|
||
[case testRangeObject] | ||
def range_object() -> None: | ||
r = range(4, 12, 2) | ||
sum = 0 | ||
for i in r: | ||
sum += i | ||
|
||
def range_in_loop() -> None: | ||
sum = 0 | ||
for i in range(4, 12, 2): | ||
sum += i | ||
[out] | ||
def range_object(): | ||
r0, r1, r2, r3, r4, r :: object | ||
sum :: int | ||
r5, r6 :: object | ||
r7, i, r8 :: int | ||
r9 :: bit | ||
L0: | ||
r0 = load_address PyRange_Type | ||
r1 = box(short_int, 8) | ||
r2 = box(short_int, 24) | ||
r3 = box(short_int, 4) | ||
r4 = PyObject_CallFunctionObjArgs(r0, r1, r2, r3, 0) | ||
r = r4 | ||
sum = 0 | ||
r5 = PyObject_GetIter(r) | ||
L1: | ||
r6 = PyIter_Next(r5) | ||
if is_error(r6) goto L4 else goto L2 | ||
L2: | ||
r7 = unbox(int, r6) | ||
i = r7 | ||
r8 = CPyTagged_Add(sum, i) | ||
sum = r8 | ||
L3: | ||
goto L1 | ||
L4: | ||
r9 = CPy_NoErrOccured() | ||
L5: | ||
return 1 | ||
def range_in_loop(): | ||
sum :: int | ||
r0 :: short_int | ||
i :: int | ||
r1 :: bit | ||
r2 :: int | ||
r3 :: short_int | ||
L0: | ||
sum = 0 | ||
r0 = 8 | ||
i = r0 | ||
L1: | ||
r1 = r0 < 24 :: signed | ||
if r1 goto L2 else goto L4 :: bool | ||
L2: | ||
r2 = CPyTagged_Add(sum, i) | ||
sum = r2 | ||
L3: | ||
r3 = r0 + 4 | ||
r0 = r3 | ||
i = r3 | ||
goto L1 | ||
L4: | ||
return 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two cases seem to work correctly. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Test cases for "for" and "while" loops (compile and run) | ||
# Test cases for "range" objects, "for" and "while" loops (compile and run) | ||
|
||
[case testFor] | ||
from typing import List, Tuple | ||
|
@@ -452,3 +452,19 @@ def bar(x: Optional[str]) -> None: | |
[file driver.py] | ||
from native import bar | ||
bar(None) | ||
|
||
[case testRangeObject] | ||
r1 = range(4, 12, 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change this to use a Test also that coercion to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got this error after adding function
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need to change the definition of |
||
for i in r1: | ||
print(i) | ||
try: | ||
r2 = range(4, 12, 0) | ||
except ValueError as e: | ||
assert 'range() arg 3 must not be zero' in str(e) | ||
else: | ||
assert False | ||
[out] | ||
4 | ||
6 | ||
8 | ||
10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There probably is an existing test case for range in a for loop, so this can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot find the irbuild case and I'll keep this.