[Python-checkins] CVS: python/dist/src/Lib/test test_exceptions.py,1.14,1.15

Jeremy Hylton jhylton@users.sourceforge.net
Wed, 26 Sep 2001 13:01:11 -0700


Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv5478

Modified Files:
	test_exceptions.py 
Log Message:
Add tests for new PyErr_NormalizeException() behavior

Add raise_exception() to the _testcapi module.  It isn't a test, but
the C API exists only to support test_exceptions.  raise_exception()
takes two arguments -- an exception class and an integer specifying
how many arguments it should be called with.

test_exceptions uses BadException() to test the interpreter's behavior
when there is a problem instantiating the exception.  test_capi1()
calls it with too many arguments.  test_capi2() causes an exception to
be raised in the Python code of the constructor.


Index: test_exceptions.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_exceptions.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** test_exceptions.py	2001/08/23 03:23:03	1.14
--- test_exceptions.py	2001/09/26 20:01:09	1.15
***************
*** 4,7 ****
--- 4,9 ----
  from types import ClassType
  import warnings
+ import sys, traceback
+ import _testcapi
  
  warnings.filterwarnings("error", "", OverflowWarning, __name__)
***************
*** 161,164 ****
--- 163,199 ----
  try: x = 1/0
  except Exception, e: pass
+ 
+ # test that setting an exception at the C level works even if the
+ # exception object can't be constructed.
+ 
+ class BadException:
+     def __init__(self):
+         raise RuntimeError, "can't instantiate BadException"
+ 
+ def test_capi1():
+     try:
+         _testcapi.raise_exception(BadException, 1)
+     except TypeError, err:
+         exc, err, tb = sys.exc_info()
+         co = tb.tb_frame.f_code
+         assert co.co_name == "test_capi1"
+         assert co.co_filename.endswith('test_exceptions.py')
+     else:
+         print "Expected exception"
+ test_capi1()
+ 
+ def test_capi2():
+     try:
+         _testcapi.raise_exception(BadException, 0)
+     except RuntimeError, err:
+         exc, err, tb = sys.exc_info()
+         co = tb.tb_frame.f_code
+         assert co.co_name == "__init__"
+         assert co.co_filename.endswith('test_exceptions.py')
+         co2 = tb.tb_frame.f_back.f_code
+         assert co2.co_name == "test_capi2"
+     else:
+         print "Expected exception"
+ test_capi2()
  
  unlink(TESTFN)