From 3e448c0b5e3abcd179781dd718df2bd2340ddb06 Mon Sep 17 00:00:00 2001 From: Yannick Jadoul Date: Tue, 4 Aug 2020 14:45:55 +0200 Subject: [PATCH] Enable py::ellipsis on Python 2 (#2360) * Enable py::ellipsis on Python 2 * Enable py::ellipsis tests on Python 2 and mention `Ellipsis` in the docs --- docs/advanced/pycpp/numpy.rst | 2 ++ include/pybind11/pytypes.h | 4 ---- tests/test_numpy_array.cpp | 8 +++----- tests/test_numpy_array.py | 1 - 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/docs/advanced/pycpp/numpy.rst b/docs/advanced/pycpp/numpy.rst index f1941392f..3b6a5dfc7 100644 --- a/docs/advanced/pycpp/numpy.rst +++ b/docs/advanced/pycpp/numpy.rst @@ -371,6 +371,8 @@ Ellipsis Python 3 provides a convenient ``...`` ellipsis notation that is often used to slice multidimensional arrays. For instance, the following snippet extracts the middle dimensions of a tensor with the first and last index set to zero. +In Python 2, the syntactic sugar ``...`` is not available, but the singleton +``Ellipsis`` (of type ``ellipsis``) can still be used directly. .. code-block:: python diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 536835f7d..9000668b9 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -736,9 +736,7 @@ inline bool PyIterable_Check(PyObject *obj) { } inline bool PyNone_Check(PyObject *o) { return o == Py_None; } -#if PY_MAJOR_VERSION >= 3 inline bool PyEllipsis_Check(PyObject *o) { return o == Py_Ellipsis; } -#endif inline bool PyUnicode_Check_Permissive(PyObject *o) { return PyUnicode_Check(o) || PYBIND11_BYTES_CHECK(o); } @@ -1020,13 +1018,11 @@ public: none() : object(Py_None, borrowed_t{}) { } }; -#if PY_MAJOR_VERSION >= 3 class ellipsis : public object { public: PYBIND11_OBJECT(ellipsis, object, detail::PyEllipsis_Check) ellipsis() : object(Py_Ellipsis, borrowed_t{}) { } }; -#endif class bool_ : public object { public: diff --git a/tests/test_numpy_array.cpp b/tests/test_numpy_array.cpp index 156a3bfa8..e37beb5a5 100644 --- a/tests/test_numpy_array.cpp +++ b/tests/test_numpy_array.cpp @@ -382,9 +382,7 @@ TEST_SUBMODULE(numpy_array, sm) { return a; }); -#if PY_MAJOR_VERSION >= 3 - sm.def("index_using_ellipsis", [](py::array a) { - return a[py::make_tuple(0, py::ellipsis(), 0)]; - }); -#endif + sm.def("index_using_ellipsis", [](py::array a) { + return a[py::make_tuple(0, py::ellipsis(), 0)]; + }); } diff --git a/tests/test_numpy_array.py b/tests/test_numpy_array.py index 2c977cd62..1b6599dfe 100644 --- a/tests/test_numpy_array.py +++ b/tests/test_numpy_array.py @@ -431,7 +431,6 @@ def test_array_create_and_resize(msg): assert(np.all(a == 42.)) -@pytest.unsupported_on_py2 def test_index_using_ellipsis(): a = m.index_using_ellipsis(np.zeros((5, 6, 7))) assert a.shape == (6,)