Skip to content

Commit fc12cea

Browse files
committed
test with None
1 parent eeb09ab commit fc12cea

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

mypyc/irbuild/for_helpers.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
SetExpr,
2323
TupleExpr,
2424
TypeAlias,
25+
Var,
2526
)
2627
from mypyc.ir.ops import (
2728
ERR_NEVER,
@@ -1168,7 +1169,10 @@ def need_cleanup(self) -> bool:
11681169
return True
11691170

11701171
def init(self, index: Lvalue, func: Expression, iterable: Expression) -> None:
1171-
self.filter_func = self.builder.accept(func)
1172+
if isinstance(func, NameExpr) and isinstance(func.node, Var) and func.node.fullname == "builtins.None":
1173+
self.filter_func = None
1174+
else:
1175+
self.filter_func = self.builder.accept(func)
11721176
self.iterable = iterable
11731177
self.index = index
11741178

@@ -1194,8 +1198,11 @@ def begin_body(self) -> None:
11941198
builder = self.builder
11951199
line = self.line
11961200
item = builder.read(builder.get_assignment_target(self.index), line)
1197-
# TODO: implement logic to handle c calls of native functions
1198-
result = builder.py_call(self.filter_func, [item], line)
1201+
if self.filter_func is None:
1202+
result = item
1203+
else:
1204+
# TODO: implement logic to handle c calls of native functions
1205+
result = builder.py_call(self.filter_func, [item], line)
11991206

12001207
# Now, filter: only enter the body if func(item) is truthy
12011208
cont_block, rest_block = BasicBlock(), BasicBlock()

mypyc/test-data/irbuild-basic.test

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3617,3 +3617,47 @@ L5:
36173617
L6:
36183618
L7:
36193619
return s
3620+
3621+
[case testForFilterNone]
3622+
def f(a: list[int]) -> int:
3623+
c = 0
3624+
for x in filter(None, a):
3625+
c += 1
3626+
return 0
3627+
3628+
[out]
3629+
def f(a):
3630+
a :: list
3631+
c :: int
3632+
r0, r1 :: native_int
3633+
r2 :: bit
3634+
r3 :: object
3635+
r4, x :: int
3636+
r5 :: bit
3637+
r6 :: int
3638+
r7 :: native_int
3639+
L0:
3640+
c = 0
3641+
r0 = 0
3642+
L1:
3643+
r1 = var_object_size a
3644+
r2 = r0 < r1 :: signed
3645+
if r2 goto L2 else goto L6 :: bool
3646+
L2:
3647+
r3 = list_get_item_unsafe a, r0
3648+
r4 = unbox(int, r3)
3649+
x = r4
3650+
r5 = x != 0
3651+
if r5 goto L4 else goto L3 :: bool
3652+
L3:
3653+
goto L5
3654+
L4:
3655+
r6 = CPyTagged_Add(c, 2)
3656+
c = r6
3657+
L5:
3658+
r7 = r0 + 1
3659+
r0 = r7
3660+
goto L1
3661+
L6:
3662+
L7:
3663+
return 0

mypyc/test-data/run-loops.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,20 @@ print(f([]))
590590
0
591591
0
592592

593+
[case testRunForFilterNone]
594+
def f(a: list[int]) -> int:
595+
c = 0
596+
for x in filter(None, a):
597+
c += 1
598+
return c
599+
600+
[file driver.py]
601+
from native import f
602+
print(f([0, 1, 2, 3, 4, 5, 6]))
603+
604+
[out]
605+
6
606+
593607
[case testRunForFilterEdgeCases]
594608
def f(a: list[int]) -> int:
595609
s = 0

0 commit comments

Comments
 (0)