extended test cases to check for exception __context__ in Py3
authorStefan Behnel <scoder@users.berlios.de>
Sun, 22 Mar 2009 16:55:30 +0000 (17:55 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Sun, 22 Mar 2009 16:55:30 +0000 (17:55 +0100)
tests/run/funcexceptchained.pyx
tests/run/funcexceptreplace.pyx [new file with mode: 0644]

index 78c02c385af252eb4a54423f095703bbbf878de7..27881111cd4f1dbc81f886f5c3cc3a5ed9143378 100644 (file)
@@ -8,7 +8,13 @@ __doc__ = u"""
 ...   except AttributeError:
 ...     print(sys.exc_info()[0] is AttributeError or sys.exc_info()[0])
 ...     try: raise KeyError
-...     except: print(sys.exc_info()[0] is KeyError or sys.exc_info()[0])
+...     except:
+...       print(sys.exc_info()[0] is KeyError or sys.exc_info()[0])
+...       if IS_PY3:
+...         print(isinstance(sys.exc_info()[1].__context__, AttributeError)
+...               or sys.exc_info()[1].__context__)
+...       else:
+...         print(True)
 ...     print((IS_PY3 and sys.exc_info()[0] is AttributeError) or
 ...           (not IS_PY3 and sys.exc_info()[0] is KeyError) or
 ...           sys.exc_info()[0])
@@ -24,6 +30,7 @@ True
 True
 True
 True
+True
 >>> print(sys.exc_info()[0]) # test_py()
 None
 
@@ -32,6 +39,7 @@ True
 True
 True
 True
+True
 >>> print(sys.exc_info()[0]) # test_c()
 None
 
@@ -52,6 +60,7 @@ True
 True
 True
 True
+True
 >>> print(sys.exc_info()[0]) # test_py2()
 None
 
@@ -62,6 +71,7 @@ True
 True
 True
 True
+True
 >>> print(sys.exc_info()[0]) # test_c2()
 None
 """
@@ -76,7 +86,13 @@ def test_c(outer_exc):
     except AttributeError:
         print(sys.exc_info()[0] is AttributeError or sys.exc_info()[0])
         try: raise KeyError
-        except: print(sys.exc_info()[0] is KeyError or sys.exc_info()[0])
+        except:
+            print(sys.exc_info()[0] is KeyError or sys.exc_info()[0])
+            if IS_PY3:
+                print(isinstance(sys.exc_info()[1].__context__, AttributeError)
+                      or sys.exc_info()[1].__context__)
+            else:
+                print(True)
         print(sys.exc_info()[0] is AttributeError or sys.exc_info()[0])
     print(sys.exc_info()[0] is outer_exc or sys.exc_info()[0])
 
diff --git a/tests/run/funcexceptreplace.pyx b/tests/run/funcexceptreplace.pyx
new file mode 100644 (file)
index 0000000..084df4f
--- /dev/null
@@ -0,0 +1,18 @@
+__doc__ = u"""
+>>> try: exc()
+... except IndexError:
+...     if IS_PY3:
+...         print(isinstance(sys.exc_info()[1].__context__, ValueError))
+...     else:
+...         print(True)
+True
+"""
+
+import sys
+IS_PY3 = sys.version_info[0] >= 3
+
+def exc():
+    try:
+        raise ValueError
+    except ValueError:
+        raise IndexError