[pypy-svn] r46949 - pypy/dist/pypy/module/struct

arigo at codespeak.net arigo at codespeak.net
Thu Sep 27 14:14:27 CEST 2007


Author: arigo
Date: Thu Sep 27 14:14:26 2007
New Revision: 46949

Modified:
   pypy/dist/pypy/module/struct/formatiterator.py
   pypy/dist/pypy/module/struct/interp_struct.py
Log:
Move the "except OverflowError" directly around the ovfcheck(),
as needed for RPython for now.  Also raise struct.error instead of
OverflowError at app-level, which is more in line with CPython.


Modified: pypy/dist/pypy/module/struct/formatiterator.py
==============================================================================
--- pypy/dist/pypy/module/struct/formatiterator.py	(original)
+++ pypy/dist/pypy/module/struct/formatiterator.py	Thu Sep 27 14:14:26 2007
@@ -54,8 +54,13 @@
                     index += 1
                     if not c.isdigit():
                         break
-                    repetitions = ovfcheck(repetitions * 10)
-                    repetitions = ovfcheck(repetitions + (ord(c) - ord('0')))
+                    try:
+                        repetitions = ovfcheck(repetitions * 10)
+                        repetitions = ovfcheck(repetitions + (ord(c) -
+                                                              ord('0')))
+                    except OverflowError:
+                        raise StructError("overflow in item count")
+                assert repetitions >= 0
             else:
                 repetitions = 1
 
@@ -77,16 +82,23 @@
     totalsize = 0
 
     def operate(self, fmtdesc, repetitions):
-        if fmtdesc.size == 1:
-            size = repetitions  # skip the overflow-checked multiplication by 1
-        else:
-            size = ovfcheck(fmtdesc.size * repetitions)
-        self.totalsize = ovfcheck(self.totalsize + size)
+        try:
+            if fmtdesc.size == 1:
+                # skip the overflow-checked multiplication by 1
+                size = repetitions
+            else:
+                size = ovfcheck(fmtdesc.size * repetitions)
+            self.totalsize = ovfcheck(self.totalsize + size)
+        except OverflowError:
+            raise StructError("total struct size too long")
     operate._annspecialcase_ = 'specialize:arg(1)'
 
     def align(self, mask):
         pad = (-self.totalsize) & mask
-        self.totalsize = ovfcheck(self.totalsize + pad)
+        try:
+            self.totalsize = ovfcheck(self.totalsize + pad)
+        except OverflowError:
+            raise StructError("total struct size too long")
 
 
 class PackFormatIterator(FormatIterator):

Modified: pypy/dist/pypy/module/struct/interp_struct.py
==============================================================================
--- pypy/dist/pypy/module/struct/interp_struct.py	(original)
+++ pypy/dist/pypy/module/struct/interp_struct.py	Thu Sep 27 14:14:26 2007
@@ -6,19 +6,12 @@
 from pypy.module.struct.formatiterator import UnpackFormatIterator
 
 
-def overflow(space):
-    return OperationError(space.w_OverflowError,
-                          space.wrap("struct format too large"))
-
-
 def calcsize(space, format):
     fmtiter = CalcSizeFormatIterator()
     try:
         fmtiter.interpret(format)
     except StructError, e:
         raise e.at_applevel(space)
-    except OverflowError:
-        raise overflow(space)
     return space.wrap(fmtiter.totalsize)
 calcsize.unwrap_spec = [ObjSpace, str]
 
@@ -29,8 +22,6 @@
         fmtiter.interpret(format)
     except StructError, e:
         raise e.at_applevel(space)
-    except OverflowError:
-        raise overflow(space)
     result = ''.join(fmtiter.result)
     return space.wrap(result)
 pack.unwrap_spec = [ObjSpace, str, 'args_w']
@@ -42,7 +33,5 @@
         fmtiter.interpret(format)
     except StructError, e:
         raise e.at_applevel(space)
-    except OverflowError:
-        raise overflow(space)
     return space.newtuple(fmtiter.result_w)
 unpack.unwrap_spec = [ObjSpace, str, str]



More information about the Pypy-commit mailing list