generate py3k warnings on __getslice__, __delslice__, and __setslice__

Reviewer: Brett Cannon
This commit is contained in:
Benjamin Peterson 2008-08-24 18:10:20 +00:00
parent a403e4141a
commit 712ee92309
4 changed files with 72 additions and 8 deletions

View File

@ -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:

View File

@ -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
-------

View File

@ -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);

View File

@ -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);