Skip to content

Commit

Permalink
Merge pull request #481 from DuToitSpies/add_newslice
Browse files Browse the repository at this point in the history
Added HPySlice_New
  • Loading branch information
DuToitSpies authored Jun 7, 2024
2 parents 176724c + 7fdbca1 commit 79fb330
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/api-reference/function-index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ HPy Core API Function Index
* :c:func:`HPyLong_FromUInt32_t`
* :c:func:`HPyLong_FromUInt64_t`
* :c:func:`HPyNumber_Check`
* :c:func:`HPySlice_New`
* :c:func:`HPySlice_Unpack`
* :c:func:`HPyTracker_Add`
* :c:func:`HPyTracker_Close`
Expand Down
1 change: 1 addition & 0 deletions docs/porting-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ with the code for the :term:`CPython ABI` mode, so it is guaranteed to be correc
`PySequence_GetSlice <https://docs.python.org/3/c-api/sequence.html#c.PySequence_GetSlice>`_ :c:func:`HPy_GetSlice`
`PySequence_SetSlice <https://docs.python.org/3/c-api/sequence.html#c.PySequence_SetSlice>`_ :c:func:`HPy_SetSlice`
`PySlice_AdjustIndices <https://docs.python.org/3/c-api/slice.html#c.PySlice_AdjustIndices>`_ :c:func:`HPySlice_AdjustIndices`
`PySlice_New <https://docs.python.org/3/c-api/slice.html#c.PySlice_New>`_ :c:func:`HPySlice_New`
`PySlice_Unpack <https://docs.python.org/3/c-api/slice.html#c.PySlice_Unpack>`_ :c:func:`HPySlice_Unpack`
`PyTuple_Check <https://docs.python.org/3/c-api/tuple.html#c.PyTuple_Check>`_ :c:func:`HPyTuple_Check`
`PyType_IsSubtype <https://docs.python.org/3/c-api/type.html#c.PyType_IsSubtype>`_ :c:func:`HPyType_IsSubtype`
Expand Down
2 changes: 2 additions & 0 deletions hpy/debug/src/autogen_debug_ctx_init.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions hpy/debug/src/autogen_debug_wrappers.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions hpy/devel/include/hpy/cpython/autogen_api_impl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hpy/devel/include/hpy/universal/autogen_ctx.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions hpy/devel/include/hpy/universal/autogen_trampolines.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions hpy/tools/autogen/public_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,26 @@ HPy HPyTuple_FromArray(HPyContext *ctx, const HPy items[], HPy_ssize_t n);

/* sliceobject.h */

/**
* Creates a new empty Python slice object.
*
* :param ctx:
* The execution context.
*
* :param start:
* A handle to an object to be used as the slice start value.
* :param end:
* A handle to an object to be used as the slice end value.
* :param step:
* A handle to an object to be used as the slice step value.
*
* :returns:
* A handle to the new and empty Python slice object or ``HPy_NULL`` in case
* of an error.
*/
HPy_ID(272)
HPy HPySlice_New(HPyContext *ctx, HPy start, HPy stop, HPy step);

/**
* Extract the start, stop and step data members from a slice object as C
* integers.
Expand Down Expand Up @@ -987,6 +1007,7 @@ HPy HPyTuple_FromArray(HPyContext *ctx, const HPy items[], HPy_ssize_t n);
* :returns:
* ``-1`` on error, ``0`` on success
*/

HPy_ID(259)
int HPySlice_Unpack(HPyContext *ctx, HPy slice, HPy_ssize_t *start, HPy_ssize_t *stop, HPy_ssize_t *step);

Expand Down
6 changes: 4 additions & 2 deletions hpy/trace/src/autogen_trace_ctx_init.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions hpy/trace/src/autogen_trace_func_table.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions hpy/trace/src/autogen_trace_wrappers.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions hpy/universal/src/autogen_ctx_def.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions hpy/universal/src/autogen_ctx_impl.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions test/test_hpyslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,31 @@ def test_adjust_indices(self):
assert mod.f(10, 9, 0, -3) == (3, 9, 0, -3)
assert mod.f(10, -1, -10, -3) == (3, 9, 0, -3)
assert mod.f(10, 5, 5, -3) == (0, 5, 5, -3)

def test_new(self):
mod = self.make_module("""
HPyDef_METH(f, "f", HPyFunc_VARARGS)
static HPy f_impl(HPyContext *ctx, HPy self, const HPy *args, size_t nargs)
{
HPy start, stop, step;
if (nargs != 3) {
HPyErr_SetString(ctx, ctx->h_TypeError,
"expected exactly 3 arguments");
return HPy_NULL;
}
if (HPyArg_Parse(ctx, NULL, args, nargs, "OOO",
&start, &stop, &step) < 0) {
return HPy_NULL;
}
return HPySlice_New(ctx, start, stop, step);
}
@EXPORT(f)
@INIT
""")

assert mod.f(0, 10, 1) == slice(0, 10, 1)
assert mod.f(None, 10, 1) == slice(None, 10, 1)
assert mod.f(1, None, 1) == slice(1, None, 1)
assert mod.f(0, 10, None) == slice(0, 10, None)

0 comments on commit 79fb330

Please sign in to comment.