[Python-checkins] r66013 - in python/trunk: Lib/test/test_py3kwarn.py Misc/NEWS Objects/classobject.c Objects/typeobject.c

benjamin.peterson python-checkins at python.org
Sun Aug 24 20:10:21 CEST 2008


Author: benjamin.peterson
Date: Sun Aug 24 20:10:20 2008
New Revision: 66013

Log:
generate py3k warnings on __getslice__, __delslice__, and __setslice__

Reviewer: Brett Cannon


Modified:
   python/trunk/Lib/test/test_py3kwarn.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/classobject.c
   python/trunk/Objects/typeobject.c

Modified: python/trunk/Lib/test/test_py3kwarn.py
==============================================================================
--- python/trunk/Lib/test/test_py3kwarn.py	(original)
+++ python/trunk/Lib/test/test_py3kwarn.py	Sun Aug 24 20:10:20 2008
@@ -175,6 +175,28 @@
             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:

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sun Aug 24 20:10:20 2008
@@ -26,6 +26,9 @@
 - 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
 -------
 

Modified: python/trunk/Objects/classobject.c
==============================================================================
--- python/trunk/Objects/classobject.c	(original)
+++ python/trunk/Objects/classobject.c	Sun Aug 24 20:10:20 2008
@@ -1174,8 +1174,15 @@
 		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 @@
 
 			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 @@
 
 			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);

Modified: python/trunk/Objects/typeobject.c
==============================================================================
--- python/trunk/Objects/typeobject.c	(original)
+++ python/trunk/Objects/typeobject.c	Sun Aug 24 20:10:20 2008
@@ -4897,7 +4897,17 @@
 	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 @@
 {
 	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);


More information about the Python-checkins mailing list