mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-19 14:29:11 +00:00
fix: provide useful behavior of default py::slice (#5620)
* Change behavior of default py::slice * make clang-tidy happy * Update tests/test_pytypes.py --------- Co-authored-by: Bryn Lloyd <12702862+dyollb@users.noreply.github.com> Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
This commit is contained in:
@@ -1942,13 +1942,14 @@ private:
|
||||
|
||||
class slice : public object {
|
||||
public:
|
||||
PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check)
|
||||
PYBIND11_OBJECT(slice, object, PySlice_Check)
|
||||
slice(handle start, handle stop, handle step)
|
||||
: object(PySlice_New(start.ptr(), stop.ptr(), step.ptr()), stolen_t{}) {
|
||||
if (!m_ptr) {
|
||||
pybind11_fail("Could not allocate slice object!");
|
||||
}
|
||||
}
|
||||
slice() : slice(none(), none(), none()) {}
|
||||
|
||||
#ifdef PYBIND11_HAS_OPTIONAL
|
||||
slice(std::optional<ssize_t> start, std::optional<ssize_t> stop, std::optional<ssize_t> step)
|
||||
|
||||
@@ -671,6 +671,8 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
|
||||
m.def("test_list_slicing", [](const py::list &a) { return a[py::slice(0, -1, 2)]; });
|
||||
|
||||
m.def("test_list_slicing_default", [](const py::list &a) { return a[py::slice()]; });
|
||||
|
||||
// See #2361
|
||||
m.def("issue2361_str_implicit_copy_none", []() {
|
||||
py::str is_this_none = py::none();
|
||||
|
||||
@@ -603,7 +603,8 @@ def test_number_protocol():
|
||||
|
||||
def test_list_slicing():
|
||||
li = list(range(100))
|
||||
assert li[::2] == m.test_list_slicing(li)
|
||||
assert li[0:-1:2] == m.test_list_slicing(li)
|
||||
assert li[::] == m.test_list_slicing_default(li)
|
||||
|
||||
|
||||
def test_issue2361():
|
||||
|
||||
Reference in New Issue
Block a user