diff --git a/Lib/test/test_py3kwarn.py b/Lib/test/test_py3kwarn.py index 274e1471676..1b487fe53ad 100644 --- a/Lib/test/test_py3kwarn.py +++ b/Lib/test/test_py3kwarn.py @@ -175,6 +175,28 @@ class TestPy3KWarnings(unittest.TestCase): with catch_warning() as w: self.assertWarning(set(), w, expected) + def test_slice_methods(self): + class Spam(object): + def __getslice__(self, i, j): pass + def __setslice__(self, i, j, what): pass + def __delslice__(self, i, j): pass + class Egg: + def __getslice__(self, i, h): pass + def __setslice__(self, i, j, what): pass + def __delslice__(self, i, j): pass + + expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__" + + for obj in (Spam(), Egg()): + with catch_warning() as w: + self.assertWarning(obj[1:2], w, expected.format('get')) + w.reset() + del obj[3:4] + self.assertWarning(None, w, expected.format('del')) + w.reset() + obj[4:5] = "eggs" + self.assertWarning(None, w, expected.format('set')) + def test_tuple_parameter_unpacking(self): expected = "tuple parameter unpacking has been removed in 3.x" with catch_warning() as w: diff --git a/Misc/NEWS b/Misc/NEWS index 9c07eeea2a7..ff754dfbb0b 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -26,6 +26,9 @@ Core and Builtins - Silenced another compiler warning about a used but not defined function 'stringlib_contains_obj'. +- Added warnings on the use of ``__getslice__``, ``__setslice__``, or + ``__delslice__``. + Library ------- diff --git a/Objects/classobject.c b/Objects/classobject.c index caf6b3e7b0d..7d3d0480b51 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -1174,8 +1174,15 @@ instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j) if (func == NULL) return NULL; arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j)); - } else + } + else { + if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; " + "use __getitem__", 1) < 0) { + Py_DECREF(func); + return NULL; + } arg = Py_BuildValue("(nn)", i, j); + } if (arg == NULL) { Py_DECREF(func); @@ -1257,8 +1264,15 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j)); - } else + } + else { + if (PyErr_WarnPy3k("in 3.x, __delslice__ has been " + "removed; use __delitem__", 1) < 0) { + Py_DECREF(func); + return -1; + } arg = Py_BuildValue("(nn)", i, j); + } } else { if (setslicestr == NULL) { @@ -1284,8 +1298,15 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject arg = Py_BuildValue("(NO)", _PySlice_FromIndices(i, j), value); - } else + } + else { + if (PyErr_WarnPy3k("in 3.x, __setslice__ has been " + "removed; use __setitem__", 1) < 0) { + Py_DECREF(func); + return -1; + } arg = Py_BuildValue("(nnO)", i, j, value); + } } if (arg == NULL) { Py_DECREF(func); diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 42974f8fcca..02b50128cb1 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -4897,7 +4897,17 @@ slot_sq_item(PyObject *self, Py_ssize_t i) return NULL; } -SLOT2(slot_sq_slice, "__getslice__", Py_ssize_t, Py_ssize_t, "nn") +static PyObject* +slot_sq_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j) +{ + static PyObject *getslice_str; + + if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; " + "use __getitem__", 1) < 0) + return NULL; + return call_method(self, "__getslice__", &getslice_str, + "nn", i, j); +} static int slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) @@ -4922,13 +4932,21 @@ slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value) { PyObject *res; static PyObject *delslice_str, *setslice_str; - - if (value == NULL) + + if (value == NULL) { + if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; " + "use __delitem__", 1) < 0) + return -1; res = call_method(self, "__delslice__", &delslice_str, "(nn)", i, j); - else + } + else { + if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; " + "use __setitem__", 1) < 0) + return -1; res = call_method(self, "__setslice__", &setslice_str, - "(nnO)", i, j, value); + "(nnO)", i, j, value); + } if (res == NULL) return -1; Py_DECREF(res);