[pypy-svn] r5129 - pypy/trunk/src/pypy/interpreter

arigo at codespeak.net arigo at codespeak.net
Wed Jun 16 19:20:24 CEST 2004


Author: arigo
Date: Wed Jun 16 19:20:14 2004
New Revision: 5129

Modified:
   pypy/trunk/src/pypy/interpreter/baseobjspace.py
   pypy/trunk/src/pypy/interpreter/error.py
   pypy/trunk/src/pypy/interpreter/pyopcode.py
Log:
Try to allow raising any object as an exception.
The syntax 'raise x' is largely ambiguous, though.
There are some sanity checks.  The syntax 'raise type,x'
is not ambiguous, so I guess it should be preferred.



Modified: pypy/trunk/src/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/trunk/src/pypy/interpreter/baseobjspace.py	Wed Jun 16 19:20:14 2004
@@ -166,7 +166,12 @@
             except OperationError:
                 # Assume that this is a TypeError: w_item not a type,
                 # and assume that w_item is then actually a tuple.
-                exclst = self.unpackiterable(w_item)
+                try:
+                    exclst = self.unpackiterable(w_item)
+                except OperationError:
+                    # hum, maybe it is not a tuple after all, and w_exc_type
+                    # was not a type at all (string exceptions).  Give up.
+                    continue
                 check_list.extend(exclst)
         return False
 

Modified: pypy/trunk/src/pypy/interpreter/error.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/error.py	(original)
+++ pypy/trunk/src/pypy/interpreter/error.py	Wed Jun 16 19:20:14 2004
@@ -104,11 +104,17 @@
             exc_value    = self.w_value
         else:
             w = space.wrap
-            exc_typename  = space.unwrap(
-                space.getattr(self.w_type, w('__name__')))
-            exc_value = space.unwrap(space.str(self.w_value))
+            if space.is_true(space.is_(space.type(self.w_type), space.w_str)):
+                exc_typename = space.unwrap(self.w_type)
+            else:
+                exc_typename = space.unwrap(
+                    space.getattr(self.w_type, w('__name__')))
+            if self.w_value == space.w_None:
+                exc_value = None
+            else:
+                exc_value = space.unwrap(space.str(self.w_value))
             print >> file, '(application-level)',
-        if exc_value is None:
+        if not exc_value:
             print >> file, exc_typename
         else:
             print >> file, exc_typename+':', exc_value

Modified: pypy/trunk/src/pypy/interpreter/pyopcode.py
==============================================================================
--- pypy/trunk/src/pypy/interpreter/pyopcode.py	(original)
+++ pypy/trunk/src/pypy/interpreter/pyopcode.py	Wed Jun 16 19:20:14 2004
@@ -853,29 +853,37 @@
         #     which is not the case in CPython, but well
         import sys
         etype, value, traceback = sys.exc_info()
+    #XXX re-enable the following check
     #if not isinstance(traceback, (types.NoneType, types.TracebackType)):
     #    raise TypeError, "raise: arg 3 must be traceback or None"
     while isinstance(etype, tuple):
         etype = etype[0]
-    if type(etype) is str:
-        # XXX warn
-        pass
-    elif isinstance(etype, Exception):
+    if isinstance(etype, type):
+        if isinstance(value, etype):
+            # raise Type, Instance: everything is fine
+            pass
+        elif value is None:
+            # raise Type: we assume we have to instantiate Type
+            value = etype()
+        else:
+            # raise Type, X: assume X is the constructor argument
+            value = etype(value)
+    elif type(etype) is str:
+        # XXX warn -- deprecated
+        if value is not None and type(value) is not str:
+            raise TypeError("string exceptions can only have a string value")
+    else:
+        # raise X: we assume that X is an already-built instance
         if value is not None:
             raise TypeError("instance exception may not have a separate value")
         value = etype
         etype = value.__class__
-    elif isinstance(etype, type) and issubclass(etype, Exception):
-        if not isinstance(value,etype):
-            if value is None:
-                value = ()
-            elif not isinstance(value, tuple):
-                value = (value,)
-            value = etype(*value)
-    else:
-        raise TypeError("exceptions must be instances or subclasses of "
-                        "Exception or strings (deprecated), not %s" %
-                        (type(etype).__name__,))
+        # for the sake of language consistency we should not allow
+        # things like 'raise 1', but it's probably fine (i.e.
+        # not ambiguous) to allow them in the explicit form 'raise int, 1'
+        if not hasattr(value, '__dict__') and not hasattr(value, '__slots__'):
+            raise TypeError("raising built-in objects can be ambiguous, "
+                            "use 'raise type, value' instead")
     return etype, value, traceback
 
 def app_find_metaclass(bases, namespace, globals):



More information about the Pypy-commit mailing list