From 1afb00435b1e82f5635cc06033747aa0a4f98bc5 Mon Sep 17 00:00:00 2001 From: Bryn Lloyd <12702862+dyollb@users.noreply.github.com> Date: Wed, 16 Apr 2025 09:31:56 +0200 Subject: [PATCH 1/3] Change behavior of default py::slice --- include/pybind11/pytypes.h | 3 ++- tests/test_pytypes.cpp | 2 ++ tests/test_pytypes.py | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index db07a03139..32b3d2575d 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1942,13 +1942,14 @@ class weakref : public object { 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 start, std::optional stop, std::optional step) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index cbbf42178b..5104f167e5 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -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", [](py::list a) { return a[py::slice()]; }); + // See #2361 m.def("issue2361_str_implicit_copy_none", []() { py::str is_this_none = py::none(); diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index b20b098834..56ccc82cf0 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -604,6 +604,7 @@ def test_number_protocol(): def test_list_slicing(): li = list(range(100)) assert li[::2] == m.test_list_slicing(li) + assert li[::] == m.test_list_slicing_default(li) def test_issue2361(): From bf6df40ca52b35d274bda4523a6b8426a5fa72af Mon Sep 17 00:00:00 2001 From: Bryn Lloyd <12702862+dyollb@users.noreply.github.com> Date: Wed, 16 Apr 2025 10:48:21 +0200 Subject: [PATCH 2/3] make clang-tidy happy --- tests/test_pytypes.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index 5104f167e5..9d7c955d8b 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -671,7 +671,7 @@ 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", [](py::list a) { return a[py::slice()]; }); + m.def("test_list_slicing_default", [](const py::list &a) { return a[py::slice()]; }); // See #2361 m.def("issue2361_str_implicit_copy_none", []() { From f14eba9e3244ce22dc0bae676701c9310809f794 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Wed, 16 Apr 2025 09:59:21 -0400 Subject: [PATCH 3/3] Update tests/test_pytypes.py --- tests/test_pytypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 56ccc82cf0..d1044d995d 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -603,7 +603,7 @@ 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)