[pypy-commit] pypy stdlib-2.7.9: fix test_xdrlib by correcting struct.pack exception types

bdkearns noreply at buildbot.pypy.org
Fri Dec 19 05:27:03 CET 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: stdlib-2.7.9
Changeset: r75033:8ed6988ac4dd
Date: 2014-12-18 23:27 -0500
http://bitbucket.org/pypy/pypy/changeset/8ed6988ac4dd/

Log:	fix test_xdrlib by correcting struct.pack exception types

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
@@ -84,7 +84,12 @@
         else:
             msg = "integer argument expected, got non-integer"
         space.warn(space.wrap(msg), space.w_DeprecationWarning)
-        return space.int(w_obj)   # wrapped float -> wrapped int or long
+        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()
@@ -100,7 +105,12 @@
 
     def accept_float_arg(self):
         w_obj = self.accept_obj_arg()
-        return self.space.float_w(w_obj)
+        try:
+            return self.space.float_w(w_obj)
+        except OperationError as e:
+            if e.match(self.space, self.space.w_TypeError):
+                raise StructError("required argument is not a float")
+            raise
 
 
 class UnpackFormatIterator(FormatIterator):
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
@@ -328,6 +328,12 @@
             raises(error, unpack, "0p", "")   # segfaults on CPython 2.5.2!
         raises(error, pack, "b", 150)   # argument out of range
         # XXX the accepted ranges still differs between PyPy and CPython
+        exc = raises(error, pack, ">d", 'abc')
+        assert str(exc.value) == "required argument is not a float"
+        exc = raises(error, pack, ">l", 'abc')
+        assert str(exc.value) == "cannot convert argument to integer"
+        exc = raises(error, pack, ">H", 'abc')
+        assert str(exc.value) == "cannot convert argument to integer"
 
     def test_overflow_error(self):
         """


More information about the pypy-commit mailing list