[pypy-commit] pypy py3.5: Add two cases of DeprecationWarnings

arigo pypy.commits at gmail.com
Mon Oct 17 09:23:21 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r87842:84f779655e85
Date: 2016-10-17 15:22 +0200
http://bitbucket.org/pypy/pypy/changeset/84f779655e85/

Log:	Add two cases of DeprecationWarnings

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -289,7 +289,15 @@
             self._typed_unwrap_error(space, "integer")
         w_result = space.get_and_call_function(w_impl, self)
 
+        if space.is_w(space.type(w_result), space.w_int):
+            return w_result
         if space.isinstance_w(w_result, space.w_int):
+            tp = space.type(w_result).name.decode('utf-8')
+            space.warn(space.wrap(
+                "__int__ returned non-int (type %s).  "
+                "The ability to return an instance of a strict subclass of int "
+                "is deprecated, and may be removed in a future version of "
+                "Python." % (tp,)), space.w_DeprecationWarning)
             return w_result
         raise oefmt(space.w_TypeError,
                     "__int__ returned non-int (type '%T')", w_result)
diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -480,7 +480,15 @@
                         w_obj)
         w_result = space.get_and_call_function(w_impl, w_obj)
 
+        if space.is_w(space.type(w_result), space.w_int):
+            return w_result
         if space.isinstance_w(w_result, space.w_int):
+            tp = space.type(w_result).name.decode('utf-8')
+            space.warn(space.wrap(
+                "__index__ returned non-int (type %s).  "
+                "The ability to return an instance of a strict subclass of int "
+                "is deprecated, and may be removed in a future version of "
+                "Python." % (tp,)), space.w_DeprecationWarning)
             return w_result
         raise oefmt(space.w_TypeError,
                     "__index__ returned non-int (type %T)", w_result)
diff --git a/pypy/objspace/std/test/test_intobject.py b/pypy/objspace/std/test/test_intobject.py
--- a/pypy/objspace/std/test/test_intobject.py
+++ b/pypy/objspace/std/test/test_intobject.py
@@ -586,6 +586,22 @@
     def test_floor(self):
         assert 8 .__floor__() == 8
 
+    def test_deprecation_warning_1(self):
+        import warnings, _operator
+        class BadInt:
+            def __int__(self):
+                return True
+            def __index__(self):
+                return False
+        bad = BadInt()
+        with warnings.catch_warnings(record=True) as log:
+            warnings.simplefilter("always", DeprecationWarning)
+            n = int(bad)
+            m = _operator.index(bad)
+        assert n is True
+        assert m is False
+        assert len(log) == 2
+
 
 class AppTestIntShortcut(AppTestInt):
     spaceconfig = {"objspace.std.intshortcut": True}


More information about the pypy-commit mailing list