Skip to content

Commit c18eb5a

Browse files
committed
add _PyEval_UnpackIndices
1 parent af01d32 commit c18eb5a

File tree

6 files changed

+98
-81
lines changed

6 files changed

+98
-81
lines changed

Include/cpython/ceval.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ _PyEval_RequestCodeExtraIndex(freefunc f) {
2323

2424
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
2525
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
26+
PyAPI_FUNC(int) _PyEval_UnpackIndices(PyObject *, PyObject *,
27+
Py_ssize_t,
28+
Py_ssize_t *, Py_ssize_t *);
2629

2730

2831
// Trampoline API

Modules/_testinternalcapi/test_cases.c.h

Lines changed: 17 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -878,22 +878,20 @@ dummy_func(
878878
Py_ssize_t len = PyUnicode_CheckExact(container_o)
879879
? PyUnicode_GET_LENGTH(container_o)
880880
: Py_SIZE(container_o);
881-
Py_ssize_t istart = 0, istop = PY_SSIZE_T_MAX;
882-
if (!_PyEval_SliceIndex(start_o, &istart) ||
883-
!_PyEval_SliceIndex(stop_o, &istop)) {
881+
Py_ssize_t istart, istop;
882+
int err = _PyEval_UnpackIndices(start_o, stop_o, len,
883+
&istart, &istop);
884+
if (err == 0) {
884885
res_o = NULL;
885886
}
887+
else if (PyList_CheckExact(container_o)) {
888+
res_o = PyList_GetSlice(container_o, istart, istop);
889+
}
890+
else if (PyTuple_CheckExact(container_o)) {
891+
res_o = PyTuple_GetSlice(container_o, istart, istop);
892+
}
886893
else {
887-
PySlice_AdjustIndices(len, &istart, &istop, 1);
888-
if (PyList_CheckExact(container_o)) {
889-
res_o = PyList_GetSlice(container_o, istart, istop);
890-
}
891-
else if (PyTuple_CheckExact(container_o)) {
892-
res_o = PyTuple_GetSlice(container_o, istart, istop);
893-
}
894-
else {
895-
res_o = PyUnicode_Substring(container_o, istart, istop);
896-
}
894+
res_o = PyUnicode_Substring(container_o, istart, istop);
897895
}
898896
}
899897
else {

Python/ceval.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,6 +2922,26 @@ _PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
29222922
return 1;
29232923
}
29242924

2925+
int
2926+
_PyEval_UnpackIndices(PyObject *start, PyObject *stop,
2927+
Py_ssize_t len,
2928+
Py_ssize_t *istart, Py_ssize_t *istop)
2929+
{
2930+
if (len < 0) {
2931+
return 0;
2932+
}
2933+
*istart = 0;
2934+
*istop = PY_SSIZE_T_MAX;
2935+
if (!_PyEval_SliceIndex(start, istart)) {
2936+
return 0;
2937+
}
2938+
if (!_PyEval_SliceIndex(stop, istop)) {
2939+
return 0;
2940+
}
2941+
PySlice_AdjustIndices(len, istart, istop, 1);
2942+
return 1;
2943+
}
2944+
29252945
PyObject *
29262946
_PyEval_ImportName(PyThreadState *tstate, PyObject *builtins,
29272947
PyObject *globals, PyObject *locals, PyObject *name,

Python/executor_cases.c.h

Lines changed: 30 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 17 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)