]> granicus.if.org Git - python/commitdiff
Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
authorAlexandre Vassalotti <alexandre@peadrop.com>
Sun, 5 Jul 2009 04:22:40 +0000 (04:22 +0000)
committerAlexandre Vassalotti <alexandre@peadrop.com>
Sun, 5 Jul 2009 04:22:40 +0000 (04:22 +0000)
operator.sequenceIncludes.

Patch contributed by Jeff Balogh (and updated slightly by me).

Lib/test/test_py3kwarn.py
Modules/operator.c

index eb05303c308f5032ba743cb395f41ce777a20b1b..a636a49c889e5ff27aa4ab7cfb6888ff0fa8c2eb 100644 (file)
@@ -279,6 +279,18 @@ class TestPy3KWarnings(unittest.TestCase):
                 def __hash__(self): pass
             self.assertEqual(len(w.warnings), 0)
 
+    def test_operator(self):
+        from operator import isCallable, sequenceIncludes
+
+        expected_ = ("operator.isCallable() is not supported in 3.x. "
+                     "Use hasattr(obj, '__call__').")
+        seq_warn = ("operator.sequenceIncludes() is not supported "
+                    "in 3.x. Use operator.contains().")
+        with check_warnings() as w:
+            self.assertWarning(isCallable(self), w, callable_warn)
+            w.reset()
+            self.assertWarning(sequenceIncludes(range(3), 2), w, seq_warn)
+
 
 class TestStdlibRemovals(unittest.TestCase):
 
index fd98efdbe7d64044989f89f8919b4e377cd1822d..78b7796d69bb18687b80d552a02fbce10e326027 100644 (file)
@@ -65,7 +65,26 @@ used for special class methods; variants without leading and trailing\n\
   if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
   return PyObject_RichCompare(a1,a2,A); }
 
-spami(isCallable       , PyCallable_Check)
+/* Deprecated operators that need warnings. */
+static int
+op_isCallable(PyObject *x)
+{
+       if (PyErr_WarnPy3k("operator.isCallable() is not supported in 3.x. "
+                          "Use hasattr(obj, '__call__').", 1) < 0)
+               return -1;
+       return PyCallable_Check(x);
+}
+
+static int
+op_sequenceIncludes(PyObject *seq, PyObject* ob)
+{
+       if (PyErr_WarnPy3k("operator.sequenceIncludes() is not supported "
+                          "in 3.x. Use operator.contains().", 1) < 0)
+               return -1;
+       return PySequence_Contains(seq, ob);
+}
+
+spami(isCallable       , op_isCallable)
 spami(isNumberType     , PyNumber_Check)
 spami(truth            , PyObject_IsTrue)
 spam2(op_add           , PyNumber_Add)
@@ -104,7 +123,7 @@ spamoi(op_repeat       , PySequence_Repeat)
 spam2(op_iconcat       , PySequence_InPlaceConcat)
 spamoi(op_irepeat      , PySequence_InPlaceRepeat)
 spami2b(op_contains     , PySequence_Contains)
-spami2b(sequenceIncludes, PySequence_Contains)
+spami2b(sequenceIncludes, op_sequenceIncludes)
 spamn2(indexOf         , PySequence_Index)
 spamn2(countOf         , PySequence_Count)
 spami(isMappingType    , PyMapping_Check)