[pypy-svn] r76083 - in pypy/branch/fast-forward/pypy/module/struct: . test

benjamin at codespeak.net benjamin at codespeak.net
Fri Jul 9 20:17:56 CEST 2010


Author: benjamin
Date: Fri Jul  9 20:17:54 2010
New Revision: 76083

Modified:
   pypy/branch/fast-forward/pypy/module/struct/formatiterator.py
   pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py
Log:
warnings about type coerion

Why it took me all morning to get this to work is a mystery.


Modified: pypy/branch/fast-forward/pypy/module/struct/formatiterator.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/struct/formatiterator.py	(original)
+++ pypy/branch/fast-forward/pypy/module/struct/formatiterator.py	Fri Jul  9 20:17:54 2010
@@ -76,7 +76,12 @@
             if not e.match(space, space.w_TypeError):
                 raise e
             if not space.is_true(space.isinstance(w_obj, space.w_float)):
+                # CPython silliness. Have a warning, and then raise the error.
+                space.warn("integer argument expected, got non-integer",
+                           space.w_DeprecationWarning)
                 raise e
+            space.warn("struct: integer argument expected, got float",
+                       space.w_DeprecationWarning)
             return space.int(w_obj)   # wrapped float -> wrapped int or long
 
     else:

Modified: pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py	(original)
+++ pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py	Fri Jul  9 20:17:54 2010
@@ -55,6 +55,17 @@
         assert calcsize('=bQ3i') == 1 + 8 + 3*4
 
 
+    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
+
+
     def test_pack_standard_little(self):
         """
         Check packing with the '<' format specifier.



More information about the Pypy-commit mailing list