[pypy-commit] pypy default: Raise struct.error instead of OverflowError if someone passes a larger-than-machine-size integer to struct.pack().

mjacob noreply at buildbot.pypy.org
Mon Aug 17 14:15:35 CEST 2015


Author: Manuel Jacob <me at manueljacob.de>
Branch: 
Changeset: r79009:06d07916e1be
Date: 2015-08-17 02:22 +0200
http://bitbucket.org/pypy/pypy/changeset/06d07916e1be/

Log:	Raise struct.error instead of OverflowError if someone passes a
	larger-than-machine-size integer to struct.pack().

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
@@ -82,7 +82,13 @@
                 w_index = space.int(w_obj)   # wrapped float -> wrapped int or long
             if w_index is None:
                 raise StructError("cannot convert argument to integer")
-        return getattr(space, meth)(w_index)
+        method = getattr(space, meth)
+        try:
+            return method(w_index)
+        except OperationError as e:
+            if e.match(self.space, self.space.w_OverflowError):
+                raise StructError("argument out of range")
+            raise
 
     def accept_bool_arg(self):
         w_obj = self.accept_obj_arg()
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
@@ -428,6 +428,9 @@
         assert s.unpack(s.pack(42)) == (42,)
         assert s.unpack_from(memoryview(s.pack(42))) == (42,)
 
+    def test_overflow(self):
+        raises(self.struct.error, self.struct.pack, 'i', 1<<65)
+
 
 class AppTestStructBuffer(object):
     spaceconfig = dict(usemodules=['struct', '__pypy__'])


More information about the pypy-commit mailing list