Commits

Anonymous committed a542e12

Added ability to put superclasses and tuples in except: clauses.

Comments (0)

Files changed (2)

pypy/appspace/test/test_exceptcomp.py

+"""Test comparisons of Exceptions in except clauses.
+
+New for PyPy - Could be incorporated into CPython regression tests.
+"""
+
+import unittest
+
+class TestExceptionComp(unittest.TestCase):
+
+    def test_string(self):
+        string = "string"
+        try:
+            raise string
+        except string:
+            pass
+        except:
+            self.fail("Identical string exceptions do not match.") 
+
+    def test_stringfail(self):
+        string1 = "string1"
+        string1_ = "string" + "1"
+        assert string1 is not string1_
+        try:
+            raise string1
+        except "string2":
+            self.fail("Different string exceptions match.") 
+        except string1_:
+            self.fail("Non Identical string exceptions match.")
+        except string1:
+            pass
+        except:
+            self.fail("Unknown value for variable raise.")
+            
+
+    def test_exception(self):
+        try:
+            raise TypeError, "nothing"
+        except TypeError:
+            pass
+        except:
+            self.fail("Identical exceptions do not match.") 
+
+    def test_exceptionfail(self):
+        try:
+            raise TypeError, "nothing"
+        except KeyError:
+            self.fail("Different exceptions match.")
+        except TypeError:
+            pass
+        except:
+            self.fail("Unanticipated value for exception raise.")
+            
+
+    def test_called(self):
+        try:
+            raise SyntaxError("Invalid")
+        except SyntaxError:
+            pass
+        except:
+            self.fail("Instantiated exception does not match parent class.") 
+
+    def test_calledfail(self):
+        try:
+            raise SyntaxError("Invalid")
+        except ZeroDivisionError:
+            self.fail("Instantiated exception matches different parent class.") 
+        except SyntaxError:
+            pass
+        except:
+            self.fail("Unanticpated value for exception raise.")
+            
+        
+    def test_userclass(self):
+        class UserExcept(Exception):
+            pass
+        try:
+            raise UserExcept, "nothing"
+        except UserExcept:
+            pass
+        except:
+            self.fail("User defined class exceptions do not match.") 
+            
+    def test_subclass(self):
+        try:
+            raise KeyError("key")
+        except LookupError:
+            pass
+        except:
+            self.fail("Exception does not match parent class.") 
+
+    def test_deepsubclass(self):
+        try:
+            raise FloatingPointError("1.2r")
+        except Exception:
+            pass
+        except:
+            self.fail("Exception does not match grandparent class.") 
+
+    def test_tuple(self):
+        try:
+            raise ArithmeticError("2+jack")
+        except (ZeroDivisionError, ArithmeticError):
+            pass
+        except:
+            self.fail("Exception does not match self in tuple.") 
+
+    def test_parenttuple(self):
+        try:
+            raise ZeroDivisionError("0")
+        except (StandardError, SystemExit):
+            pass
+        except:
+            self.fail("Exception does not match parent in tuple.") 
+
+    def test_nestedtuples(self):
+        try:
+            raise AssertionError("0")
+        except (SystemExit, (KeyboardInterrupt, AssertionError)):
+            pass
+        except:
+            self.fail("Exception does not match self in nested tuple.") 
+
+if __name__ == "__main__":
+    unittest.main()

pypy/interpreter/baseobjspace.py

 
     def exception_match(self, w_exc_type, w_check_class):
         """Checks if the given exception type matches 'w_check_class'."""
-        # XXX very limited version!
-        return self.is_(w_exc_type, w_check_class)
-
-
+        #Match identical items.
+        w_rv = self.is_(w_exc_type, w_check_class)
+        if self.is_true(w_rv):
+            return w_rv
+        #Match subclasses.
+        try:
+            w_rv = self.issubtype(w_exc_type, w_check_class)
+        except: pass
+        else:
+            if self.is_true(w_rv):
+                return w_rv
+        #Match tuples containing identical or parent classes
+        try:
+            exclst = self.unpackiterable(w_check_class)
+        except:
+            #w_check_class is not iterable
+            return self.w_False
+        #w_check_class is iterable
+        for w_item in exclst:
+            w_rv = self.is_(w_exc_type, w_item)
+            if self.is_true(w_rv):
+                return w_rv
+            try:
+                w_rv = self.issubtype(w_exc_type, w_item)
+            except: pass
+            else:
+                if self.is_true(w_rv):
+                    return w_rv
+        return self.w_False
+            
 ## Table describing the regular part of the interface of object spaces,
 ## namely all methods which only take w_ arguments and return a w_ result
 ## (if any).  XXX Maybe we should say that these methods must be accessed
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.