[pypy-svn] r73981 - in pypy/trunk/pypy: interpreter interpreter/test module/_stackless module/termios objspace/std

fijal at codespeak.net fijal at codespeak.net
Thu Apr 22 18:18:24 CEST 2010


Author: fijal
Date: Thu Apr 22 18:18:21 2010
New Revision: 73981

Modified:
   pypy/trunk/pypy/interpreter/baseobjspace.py
   pypy/trunk/pypy/interpreter/pyopcode.py
   pypy/trunk/pypy/interpreter/test/test_objspace.py
   pypy/trunk/pypy/module/_stackless/interp_coroutine.py
   pypy/trunk/pypy/module/termios/interp_termios.py
   pypy/trunk/pypy/objspace/std/objspace.py
Log:
Kill UnpackValueError. Instead raise OperationError directly. Each caller
so far either reraised it as OperationError or forgot and crashed the interpreter


Modified: pypy/trunk/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/trunk/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/trunk/pypy/interpreter/baseobjspace.py	Thu Apr 22 18:18:21 2010
@@ -209,12 +209,6 @@
     def ready(self, result):
         pass
 
-class UnpackValueError(ValueError):
-    def __init__(self, msg):
-        self.msg = msg
-    def __str__(self):
-        return self.msg
-
 class DescrMismatch(Exception):
     pass
 
@@ -723,7 +717,8 @@
                     raise
                 break  # done
             if expected_length != -1 and len(items) == expected_length:
-                raise UnpackValueError("too many values to unpack")
+                raise OperationError(space.w_ValueError,
+                                     space.wrap("too many values to unpack"))
             items.append(w_item)
         if expected_length != -1 and len(items) < expected_length:
             i = len(items)
@@ -731,8 +726,9 @@
                 plural = ""
             else:
                 plural = "s"
-            raise UnpackValueError("need more than %d value%s to unpack" %
-                                   (i, plural))
+            raise OperationError(space.w_ValueError,
+                      space.wrap("need more than %d value%s to unpack" %
+                                 (i, plural)))
         return items
 
     def fixedview(self, w_iterable, expected_length=-1):

Modified: pypy/trunk/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/trunk/pypy/interpreter/pyopcode.py	(original)
+++ pypy/trunk/pypy/interpreter/pyopcode.py	Thu Apr 22 18:18:21 2010
@@ -6,7 +6,7 @@
 
 import sys
 from pypy.interpreter.error import OperationError, operationerrfmt
-from pypy.interpreter.baseobjspace import UnpackValueError, Wrappable
+from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter import gateway, function, eval, pyframe, pytraceback
 from pypy.interpreter.pycode import PyCode
 from pypy.tool.sourcetools import func_with_new_name
@@ -638,11 +638,7 @@
 
     def UNPACK_SEQUENCE(self, itemcount, next_instr):
         w_iterable = self.popvalue()
-        try:
-            items = self.space.fixedview(w_iterable, itemcount)
-        except UnpackValueError, e:
-            w_msg = self.space.wrap(e.msg)
-            raise OperationError(self.space.w_ValueError, w_msg)
+        items = self.space.fixedview(w_iterable, itemcount)
         self.pushrevvalues(itemcount, items)
 
     def STORE_ATTR(self, nameindex, next_instr):

Modified: pypy/trunk/pypy/interpreter/test/test_objspace.py
==============================================================================
--- pypy/trunk/pypy/interpreter/test/test_objspace.py	(original)
+++ pypy/trunk/pypy/interpreter/test/test_objspace.py	Thu Apr 22 18:18:21 2010
@@ -61,31 +61,40 @@
         assert self.space.newbool(1) == self.space.w_True
 
     def test_unpackiterable(self):
-        w = self.space.wrap
+        space = self.space
+        w = space.wrap
         l = [w(1), w(2), w(3), w(4)]
-        w_l = self.space.newlist(l)
-        assert self.space.unpackiterable(w_l) == l
-        assert self.space.unpackiterable(w_l, 4) == l
-        raises(ValueError, self.space.unpackiterable, w_l, 3)
-        raises(ValueError, self.space.unpackiterable, w_l, 5)
+        w_l = space.newlist(l)
+        assert space.unpackiterable(w_l) == l
+        assert space.unpackiterable(w_l, 4) == l
+        err = raises(OperationError, space.unpackiterable, w_l, 3)
+        assert err.value.match(space, space.w_ValueError)
+        err = raises(OperationError, space.unpackiterable, w_l, 5)
+        assert err.value.match(space, space.w_ValueError)
 
     def test_fixedview(self):
-        w = self.space.wrap
+        space = self.space
+        w = space.wrap
         l = [w(1), w(2), w(3), w(4)]
-        w_l = self.space.newtuple(l)
-        assert self.space.fixedview(w_l) == l
-        assert self.space.fixedview(w_l, 4) == l
-        raises(ValueError, self.space.fixedview, w_l, 3)
-        raises(ValueError, self.space.fixedview, w_l, 5)
+        w_l = space.newtuple(l)
+        assert space.fixedview(w_l) == l
+        assert space.fixedview(w_l, 4) == l
+        err = raises(OperationError, space.fixedview, w_l, 3)
+        assert err.value.match(space, space.w_ValueError)
+        err = raises(OperationError, space.fixedview, w_l, 5)
+        assert err.value.match(space, space.w_ValueError)
 
     def test_listview(self):
-        w = self.space.wrap
+        space = self.space
+        w = space.wrap
         l = [w(1), w(2), w(3), w(4)]
-        w_l = self.space.newtuple(l)
-        assert self.space.listview(w_l) == l
-        assert self.space.listview(w_l, 4) == l
-        raises(ValueError, self.space.listview, w_l, 3)
-        raises(ValueError, self.space.listview, w_l, 5)
+        w_l = space.newtuple(l)
+        assert space.listview(w_l) == l
+        assert space.listview(w_l, 4) == l
+        err = raises(OperationError, space.listview, w_l, 3)
+        assert err.value.match(space, space.w_ValueError)
+        err = raises(OperationError, space.listview, w_l, 5)
+        assert err.value.match(space, space.w_ValueError)
 
     def test_exception_match(self):
         assert self.space.exception_match(self.space.w_ValueError,

Modified: pypy/trunk/pypy/module/_stackless/interp_coroutine.py
==============================================================================
--- pypy/trunk/pypy/module/_stackless/interp_coroutine.py	(original)
+++ pypy/trunk/pypy/module/_stackless/interp_coroutine.py	Thu Apr 22 18:18:21 2010
@@ -15,7 +15,7 @@
 experience to decide where to set the limits.
 """
 
-from pypy.interpreter.baseobjspace import Wrappable, UnpackValueError
+from pypy.interpreter.baseobjspace import Wrappable
 from pypy.interpreter.argument import Arguments
 from pypy.interpreter.typedef import GetSetProperty, TypeDef
 from pypy.interpreter.typedef import interp_attrproperty, interp_attrproperty_w
@@ -173,11 +173,8 @@
         return nt([w_new_inst, nt(tup_base), nt(tup_state)])
 
     def descr__setstate__(self, space, w_args):
-        try:
-            w_flags, w_state, w_thunk, w_parent = space.unpackiterable(w_args,
-                                                             expected_length=4)
-        except UnpackValueError, e:
-            raise OperationError(space.w_ValueError, space.wrap(e.msg))
+        w_flags, w_state, w_thunk, w_parent = space.unpackiterable(w_args,
+                                                        expected_length=4)
         self.flags = space.int_w(w_flags)
         if space.is_w(w_parent, space.w_None):
             w_parent = self.w_getmain(space)
@@ -188,11 +185,8 @@
         if space.is_w(w_thunk, space.w_None):
             self.thunk = None
         else:
-            try:
-                w_func, w_args, w_kwds = space.unpackiterable(w_thunk,
-                                                             expected_length=3)
-            except UnpackValueError, e:
-                raise OperationError(space.w_ValueError, space.wrap(e.msg))
+            w_func, w_args, w_kwds = space.unpackiterable(w_thunk,
+                                                          expected_length=3)
             args = Arguments.frompacked(space, w_args, w_kwds)
             self.bind(_AppThunk(space, self.costate, w_func, args))
 

Modified: pypy/trunk/pypy/module/termios/interp_termios.py
==============================================================================
--- pypy/trunk/pypy/module/termios/interp_termios.py	(original)
+++ pypy/trunk/pypy/module/termios/interp_termios.py	Thu Apr 22 18:18:21 2010
@@ -28,14 +28,8 @@
     return OperationError(w_exception_class, w_exception)
 
 def tcsetattr(space, fd, when, w_attributes):
-    from pypy.interpreter.baseobjspace import UnpackValueError
-    try:
-        w_iflag, w_oflag, w_cflag, w_lflag, w_ispeed, w_ospeed, w_cc = \
-                 space.unpackiterable(w_attributes, expected_length=7)
-    except UnpackValueError, e:
-        raise OperationError(
-            space.w_TypeError,
-            space.wrap("tcsetattr, arg 3: must be 7 element list"))
+    w_iflag, w_oflag, w_cflag, w_lflag, w_ispeed, w_ospeed, w_cc = \
+             space.unpackiterable(w_attributes, expected_length=7)
     w_builtin = space.getbuiltinmodule('__builtin__')
     cc = []
     for w_c in space.unpackiterable(w_cc):

Modified: pypy/trunk/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/trunk/pypy/objspace/std/objspace.py	(original)
+++ pypy/trunk/pypy/objspace/std/objspace.py	Thu Apr 22 18:18:21 2010
@@ -1,7 +1,7 @@
 import __builtin__
 import types
 from pypy.interpreter import pyframe, function, special
-from pypy.interpreter.baseobjspace import ObjSpace, Wrappable, UnpackValueError
+from pypy.interpreter.baseobjspace import ObjSpace, Wrappable
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.typedef import get_unique_interplevel_subclass
 from pypy.objspace.std import (builtinshortcut, stdtypedef, frame, model,
@@ -332,6 +332,11 @@
     # have different return type. First one is a resizable list, second
     # one is not
 
+    def _wrap_expected_length(self, expected, got):
+        return OperationError(self.w_ValueError,
+                self.wrap("Expected length %d, got %d" % (expected, got)))
+        
+
     def unpackiterable(self, w_obj, expected_length=-1):
         if isinstance(w_obj, W_TupleObject):
             t = w_obj.wrappeditems[:]
@@ -340,7 +345,7 @@
         else:
             return ObjSpace.unpackiterable(self, w_obj, expected_length)
         if expected_length != -1 and len(t) != expected_length:
-            raise UnpackValueError("Expected length %d, got %d" % (expected_length, len(t)))
+            raise self._wrap_expected_length(expected_length, len(t))
         return t
 
     def fixedview(self, w_obj, expected_length=-1):
@@ -353,7 +358,7 @@
         else:
             return ObjSpace.fixedview(self, w_obj, expected_length)
         if expected_length != -1 and len(t) != expected_length:
-            raise UnpackValueError("Expected length %d, got %d" % (expected_length, len(t)))
+            raise self._wrap_expected_length(expected_length, len(t))
         return t
 
     def listview(self, w_obj, expected_length=-1):
@@ -364,7 +369,7 @@
         else:
             return ObjSpace.listview(self, w_obj, expected_length)
         if expected_length != -1 and len(t) != expected_length:
-            raise UnpackValueError("Expected length %d, got %d" % (expected_length, len(t)))
+            raise self._wrap_expected_length(expected_length, len(t))
         return t
 
     def sliceindices(self, w_slice, w_length):



More information about the Pypy-commit mailing list