[pypy-commit] pypy stdlib-2.7.9: further fix struct.pack conversions

bdkearns noreply at buildbot.pypy.org
Fri Dec 19 08:17:38 CET 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: stdlib-2.7.9
Changeset: r75035:09d67d2e9a36
Date: 2014-12-19 02:17 -0500
http://bitbucket.org/pypy/pypy/changeset/09d67d2e9a36/

Log:	further fix struct.pack conversions

diff --git a/pypy/module/struct/formatiterator.py b/pypy/module/struct/formatiterator.py
--- a/pypy/module/struct/formatiterator.py
+++ b/pypy/module/struct/formatiterator.py
@@ -65,32 +65,25 @@
             w_index = w_obj
         else:
             w_index = None
-            w_index_method = space.lookup(w_obj, "__index__")
-            if w_index_method is not None:
+            if space.lookup(w_obj, '__index__'):
                 try:
                     w_index = space.index(w_obj)
                 except OperationError, e:
                     if not e.match(space, space.w_TypeError):
                         raise
                     pass
+            if w_index is None and space.lookup(w_obj, '__int__'):
+                if space.isinstance_w(w_obj, space.w_float):
+                    msg = "integer argument expected, got float"
+                else:
+                    msg = "integer argument expected, got non-integer" \
+                          " (implicit conversion using __int__ is deprecated)"
+                space.warn(space.wrap(msg), space.w_DeprecationWarning)
+                w_index = space.int(w_obj)   # wrapped float -> wrapped int or long
             if w_index is None:
-                w_index = self._maybe_float(w_obj)
+                raise StructError("cannot convert argument to integer")
         return getattr(space, meth)(w_index)
 
-    def _maybe_float(self, w_obj):
-        space = self.space
-        if space.isinstance_w(w_obj, space.w_float):
-            msg = "struct: integer argument expected, got float"
-        else:
-            msg = "integer argument expected, got non-integer"
-        space.warn(space.wrap(msg), space.w_DeprecationWarning)
-        try:
-            return space.int(w_obj)   # wrapped float -> wrapped int or long
-        except OperationError as e:
-            if e.match(space, space.w_TypeError):
-                raise StructError("cannot convert argument to integer")
-            raise
-
     def accept_bool_arg(self):
         w_obj = self.accept_obj_arg()
         return self.space.is_true(w_obj)
diff --git a/pypy/module/struct/test/test_struct.py b/pypy/module/struct/test/test_struct.py
--- a/pypy/module/struct/test/test_struct.py
+++ b/pypy/module/struct/test/test_struct.py
@@ -63,12 +63,22 @@
     def test_deprecation_warning(self):
         import warnings
         for code in 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q':
-            with warnings.catch_warnings(record=True) as w:
-                warnings.simplefilter("always")
-                raises(TypeError, self.struct.pack, code, 3j)
-            assert len(w) == 1
-            assert str(w[0].message) == "integer argument expected, got non-integer"
-            assert w[0].category is DeprecationWarning
+            for val in [3., 3j]:
+                with warnings.catch_warnings(record=True) as w:
+                    warnings.simplefilter("always")
+                    if type(val) is float:
+                        self.struct.pack(code, val)
+                    else:
+                        raises(TypeError, self.struct.pack, code, val)
+                assert len(w) == 1
+                if type(val) is float:
+                    assert str(w[0].message) == (
+                        "integer argument expected, got float")
+                else:
+                    assert str(w[0].message) == (
+                        "integer argument expected, got non-integer"
+                        " (implicit conversion using __int__ is deprecated)")
+                assert w[0].category is DeprecationWarning
 
     def test_pack_standard_little(self):
         """


More information about the pypy-commit mailing list