Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 5 additions & 3 deletions numba/core/inline_closurecall.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,10 +1068,12 @@ def is_removed(val, removed):
# this doesn't work in objmode as it's effectively untyped
if typed:
len_func_var = ir.Var(scope, mk_unique_var("len_func"), loc)
from numba.cpython.rangeobj import range_iter_len
from numba.cpython.rangeobj import length_of_iterator
stmts.append(_new_definition(func_ir, len_func_var,
ir.Global('range_iter_len', range_iter_len, loc=loc),
loc))
ir.Global('length_of_iterator',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this choice of name, thanks for altering it!

length_of_iterator,
loc=loc),
loc))
size_val = ir.Expr.call(len_func_var, (iter_var,), (), loc=loc)
else:
raise GuardException
Expand Down
27 changes: 24 additions & 3 deletions numba/cpython/rangeobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import llvmlite.llvmpy.core as lc

from numba import prange
from numba.core import types, cgutils
from numba.core import types, cgutils, errors
from numba.cpython.listobj import ListIterInstance
from numba.np.arrayobj import make_array
from numba.core.imputils import (lower_builtin, lower_cast,
Expand Down Expand Up @@ -181,9 +181,10 @@ def range_to_range(context, builder, fromty, toty, val):
return cgutils.make_anonymous_struct(builder, items)

@intrinsic
def range_iter_len(typingctx, val):
def length_of_iterator(typingctx, val):
"""
An implementation of len(range_iter) for internal use.
An implementation of len(iter) for internal use.
Primary use is for array comprehensions (see inline_closurecall).
"""
if isinstance(val, types.RangeIteratorType):
val_type = val.yield_type
Expand Down Expand Up @@ -213,7 +214,27 @@ def codegen(context, builder, sig, args):
# array iterates along the outer dimension
return impl_ret_untracked(context, builder, intp_t, shape[0])
return signature(types.intp, val), codegen
elif isinstance(val, types.UniTupleIter):
def codegen(context, builder, sig, args):
(iterty,) = sig.args
tuplety = iterty.container
intp_t = context.get_value_type(types.intp)
count_const = intp_t(tuplety.count)
return impl_ret_untracked(context, builder, intp_t, count_const)

return signature(types.intp, val), codegen
elif isinstance(val, types.ListTypeIteratorType):
def codegen(context, builder, sig, args):
(value,) = args
intp_t = context.get_value_type(types.intp)
from numba.typed.listobject import ListIterInstance
iterobj = ListIterInstance(context, builder, sig.args[0], value)
return impl_ret_untracked(context, builder, intp_t, iterobj.size)
return signature(types.intp, val), codegen
else:
msg = ('Unsupported iterator found in array comprehension, try '
'preallocating the array and filling manually.')
raise errors.TypingError(msg)
Comment thread
stuartarchibald marked this conversation as resolved.
Resolved

def make_range_attr(index, attribute):
@intrinsic
Expand Down
6 changes: 5 additions & 1 deletion numba/tests/test_comprehension.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy

from numba.core.compiler import compile_isolated
from numba import jit
from numba import jit, typed
from numba.core import types, utils
from numba.core.errors import TypingError, LoweringError
from numba.core.types.functions import _header_lead
Expand Down Expand Up @@ -407,6 +407,10 @@ def array_comp(a):
self.check(array_comp, l)
# with array iterator
self.check(array_comp, np.array(l))
# with tuple iterator (issue #7394)
self.check(array_comp, tuple(l))
# with typed.List iterator (issue #6550)
self.check(array_comp, typed.List(l))

def test_array_comp_with_dtype(self):
def array_comp(n):
Expand Down
6 changes: 3 additions & 3 deletions numba/tests/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from numba.core import types, utils
from numba.tests.support import tag

from numba.cpython.rangeobj import range_iter_len
from numba.cpython.rangeobj import length_of_iterator
def loop1(n):
s = 0
for i in range(n):
Expand Down Expand Up @@ -40,10 +40,10 @@ def range_len2(a, b):
def range_len3(a, b, c):
return len(range(a, b, c))
def range_iter_len1(a):
return range_iter_len(iter(range(a)))
return length_of_iterator(iter(range(a)))

def range_iter_len2(a):
return range_iter_len(iter(a))
return length_of_iterator(iter(a))

def range_attrs(start, stop, step):
r1 = range(start)
Expand Down