Skip to content

Commit 1afb004

Browse files
dyollbhenryiii
authored andcommitted
Change behavior of default py::slice
1 parent 5c49858 commit 1afb004

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

include/pybind11/pytypes.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1942,13 +1942,14 @@ class weakref : public object {
19421942

19431943
class slice : public object {
19441944
public:
1945-
PYBIND11_OBJECT_DEFAULT(slice, object, PySlice_Check)
1945+
PYBIND11_OBJECT(slice, object, PySlice_Check)
19461946
slice(handle start, handle stop, handle step)
19471947
: object(PySlice_New(start.ptr(), stop.ptr(), step.ptr()), stolen_t{}) {
19481948
if (!m_ptr) {
19491949
pybind11_fail("Could not allocate slice object!");
19501950
}
19511951
}
1952+
slice() : slice(none(), none(), none()) {}
19521953

19531954
#ifdef PYBIND11_HAS_OPTIONAL
19541955
slice(std::optional<ssize_t> start, std::optional<ssize_t> stop, std::optional<ssize_t> step)

tests/test_pytypes.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,8 @@ TEST_SUBMODULE(pytypes, m) {
671671

672672
m.def("test_list_slicing", [](const py::list &a) { return a[py::slice(0, -1, 2)]; });
673673

674+
m.def("test_list_slicing_default", [](py::list a) { return a[py::slice()]; });
675+
674676
// See #2361
675677
m.def("issue2361_str_implicit_copy_none", []() {
676678
py::str is_this_none = py::none();

tests/test_pytypes.py

+1
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ def test_number_protocol():
604604
def test_list_slicing():
605605
li = list(range(100))
606606
assert li[::2] == m.test_list_slicing(li)
607+
assert li[::] == m.test_list_slicing_default(li)
607608

608609

609610
def test_issue2361():

0 commit comments

Comments
 (0)