[pypy-dev]
[issue16] try: except: fails to catch exceptions in non trivial cases
Rocco Moretti
pypy-issues at codespeak.net
Sat Apr 12 23:52:44 MEST 2003
New submission from Rocco Moretti <roccomoretti at netscape.net>:
The current exceptionmatch() function in baseobjectspace.py
only handles the trivial case (exception to be matched is
identical to the one raised). The associated subversion diff
should allow parent classes and tuples to be allowed in the
except: clause.
----------
files: except_diff.txt
messages: 32
nosy: rocco
priority: bug
status: testing
title: try: except: fails to catch exceptions in non trivial cases
__________________________________________________
PyPython issue tracker <pypy-issues at codespeak.net>
http://codespeak.net/issues/pypy/issue16
__________________________________________________
-------------- next part --------------
Index: interpreter/baseobjspace.py
===================================================================
--- interpreter/baseobjspace.py (revision 364)
+++ interpreter/baseobjspace.py (working copy)
@@ -96,10 +96,36 @@
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
Index: appspace/test/test_exceptcomp.py
===================================================================
--- appspace/test/test_exceptcomp.py (working copy)
+++ appspace/test/test_exceptcomp.py (working copy)
@@ -0,0 +1,124 @@
+"""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()
More information about the pypy-dev
mailing list