[pypy-svn] r16552 - pypy/dist/pypy/interpreter

arigo at codespeak.net arigo at codespeak.net
Thu Aug 25 21:08:47 CEST 2005


Author: arigo
Date: Thu Aug 25 21:08:46 2005
New Revision: 16552

Modified:
   pypy/dist/pypy/interpreter/baseobjspace.py
   pypy/dist/pypy/interpreter/pyopcode.py
Log:
In RPython we need a custom exception class to pass a message around.


Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py	Thu Aug 25 21:08:46 2005
@@ -100,6 +100,12 @@
     def _freeze_(self): 
         return True
 
+class UnpackValueError(ValueError):
+    def __init__(self, msg):
+        self.msg = msg
+    def __str__(self):
+        return self.msg
+
 class ObjSpace(object):
     """Base class for the interpreter-level implementations of object spaces.
     http://codespeak.net/pypy/index.cgi?doc/objspace.html"""
@@ -322,7 +328,7 @@
 
     def unpackiterable(self, w_iterable, expected_length=-1):
         """Unpack an iterable object into a real (interpreter-level) list.
-        Raise a real ValueError if the length is wrong."""
+        Raise a real (subclass of) ValueError if the length is wrong."""
         w_iterator = self.iter(w_iterable)
         items = []
         while True:
@@ -333,7 +339,7 @@
                     raise
                 break  # done
             if expected_length != -1 and len(items) == expected_length:
-                raise ValueError, "too many values to unpack"
+                raise UnpackValueError("too many values to unpack")
             items.append(w_item)
         if expected_length != -1 and len(items) < expected_length:
             i = len(items)
@@ -341,7 +347,8 @@
                 plural = ""
             else:
                 plural = "s"
-            raise ValueError, "need more than %d value%s to unpack" % (i, plural)
+            raise UnpackValueError("need more than %d value%s to unpack" %
+                                   (i, plural))
         return items
 
     def unpacktuple(self, w_tuple, expected_length=-1):
@@ -349,8 +356,8 @@
         Only use for bootstrapping or performance reasons."""
         tuple_length = self.int_w(self.len(w_tuple))
         if expected_length != -1 and tuple_length != expected_length:
-            raise ValueError, "got a tuple of length %d instead of %d" % (
-                tuple_length, expected_length)
+            raise UnpackValueError("got a tuple of length %d instead of %d" % (
+                tuple_length, expected_length))
         items = [
             self.getitem(w_tuple, self.wrap(i)) for i in range(tuple_length)]
         return items

Modified: pypy/dist/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/dist/pypy/interpreter/pyopcode.py	(original)
+++ pypy/dist/pypy/interpreter/pyopcode.py	Thu Aug 25 21:08:46 2005
@@ -5,6 +5,7 @@
 """
 
 from pypy.interpreter.baseobjspace import OperationError, BaseWrappable
+from pypy.interpreter.baseobjspace import UnpackValueError
 from pypy.interpreter import gateway, function
 from pypy.interpreter import pyframe, pytraceback
 from pypy.interpreter.miscutils import InitializedClass
@@ -413,8 +414,8 @@
         w_iterable = f.valuestack.pop()
         try:
             items = f.space.unpackiterable(w_iterable, itemcount)
-        except ValueError, e:
-            raise OperationError(f.space.w_ValueError, f.space.wrap(str(e)))
+        except UnpackValueError, e:
+            raise OperationError(f.space.w_ValueError, f.space.wrap(e.msg))
         items.reverse()
         for item in items:
             f.valuestack.push(item)



More information about the Pypy-commit mailing list