Skip to content
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

[WIP] Add @box(slice) #6938

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions numba/core/boxing.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,15 @@ def unbox_slice(typ, obj, c):
return NativeValue(sli._getvalue(), is_error=c.builder.not_(ok))


@box(types.SliceType)
def box_slice(typ, val, c):
sli = c.context.make_helper(c.builder, typ, val)
start = c.box(types.int64, sli.start)
stop = c.box(types.int64, sli.stop)
step = c.box(types.int64, sli.step)
return c.pyapi.slice_new(start, stop, step)


@unbox(types.StringLiteral)
def unbox_string_literal(typ, obj, c):
# A string literal is a dummy value
Expand Down
5 changes: 5 additions & 0 deletions numba/core/pythonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,11 @@ def slice_as_ints(self, obj):
stop = self.builder.load(pstop)
step = self.builder.load(pstep)
return cgutils.is_null(self.builder, res), start, stop, step

def slice_new(self, start, stop, step):
fnty = Type.function(self.pyobj, [self.pyobj, self.pyobj, self.pyobj])
fn = self._get_function(fnty, name="PySlice_New")
return self.builder.call(fn, (start, stop, step))

#
# List and sequence APIs
Expand Down
34 changes: 34 additions & 0 deletions numba/tests/test_slices.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def slice_constructor(*args):
sl = slice(*args)
return sl.start, sl.stop, sl.step

def slice_return(*args):
return slice(*args)

def slice_construct_and_use(args, l):
sl = slice(*args)
return l[sl]
Expand Down Expand Up @@ -63,6 +66,37 @@ def check(a, b, c, d, e, f):
# Some member is neither integer nor None
with self.assertRaises(TypeError):
cfunc(slice(1.5, 1, 1))

def test_slice_return(self):
pyfunc = slice_return
cfunc = jit(nopython=True)(pyfunc)

# Positive steps
cases = (
# (None, None, None),
(None, None, 12),
# (None, 9, None),
# (None, 9, 12),
# (None, -11, None),
# (None, -11, 12),
# (42, None, None),
# (42, None, 12),
# (42, 9, None),
# (42, 9, 12),
# (42, -11, None),
# (42, -11, 12),
# (-1, None, None),
# (-1, None, 12),
# (-1, 9, None),
# (-1, 9, 12),
# (-1, -11, None),
(-1, -11, 12),
)
for start, stop, step in cases:
expected = pyfunc(start, stop, step)
result = cfunc(start, stop, step)
print(result)
# self.assertEqual(expected, result)

def test_slice_constructor(self):
"""
Expand Down