« no previous file | Python/ceval.c » ('j') | View unified diff | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Hide Comments ('s')
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
| OLD | NEW |
|---|---|
| 1 # Python test set -- part 5, built-in exceptions | 1 # Python test set -- part 5, built-in exceptions |
| 2 | 2 |
| 3 import os | 3 import os |
| 4 import sys | 4 import sys |
| 5 import unittest | 5 import unittest |
| 6 import pickle | 6 import pickle |
| 7 import weakref | |
| 7 | 8 |
| 8 from test.test_support import TESTFN, unlink, run_unittest | 9 from test.test_support import TESTFN, unlink, run_unittest |
| 9 | 10 |
| 10 # XXX This is not really enough, each *operation* should be tested! | 11 # XXX This is not really enough, each *operation* should be tested! |
| 11 | 12 |
| 12 class ExceptionTests(unittest.TestCase): | 13 class ExceptionTests(unittest.TestCase): |
| 13 | 14 |
| 14 def raise_catch(self, exc, excname): | 15 def raise_catch(self, exc, excname): |
| 15 try: | 16 try: |
| 16 raise exc("spam") | 17 raise exc("spam") |
| 17 except exc as err: | 18 except exc as err: |
| 18 buf1 = str(err) | 19 buf1 = str(err) |
| 19 try: | 20 try: |
| 20 raise exc("spam") | 21 raise exc("spam") |
| 21 except exc as err: | 22 except exc as err: |
| 22 buf2 = str(err) | 23 buf2 = str(err) |
| 23 self.assertEquals(buf1, buf2) | 24 self.assertEquals(buf1, buf2) |
| 24 self.assertEquals(exc.__name__, excname) | 25 self.assertEquals(exc.__name__, excname) |
| 25 | 26 |
| 26 def testRaising(self): | 27 def testRaising(self): |
| 27 self.raise_catch(AttributeError, "AttributeError") | 28 self.raise_catch(AttributeError, "AttributeError") |
| 28 self.assertRaises(AttributeError, getattr, sys, "undefined_attribute") | 29 self.assertRaises(AttributeError, getattr, sys, "undefined_attribute") |
| 29 | 30 |
| 30 self.raise_catch(EOFError, "EOFError") | 31 self.raise_catch(EOFError, "EOFError") |
| 31 fp = open(TESTFN, 'w') | 32 fp = open(TESTFN, 'w') |
| 32 fp.close() | 33 fp.close() |
| 33 fp = open(TESTFN, 'r') | 34 fp = open(TESTFN, 'r') |
| 34 savestdin = sys.stdin | 35 savestdin = sys.stdin |
| 35 try: | 36 try: |
| 36 try: | 37 try: |
| 37 import marshal | 38 import marshal |
| 38 marshal.loads('') | 39 marshal.loads('') |
| 39 except EOFError: | 40 except EOFError: |
| 40 pass | 41 pass |
| 41 finally: | 42 finally: |
| 42 sys.stdin = savestdin | 43 sys.stdin = savestdin |
| 43 fp.close() | 44 fp.close() |
| 44 unlink(TESTFN) | 45 unlink(TESTFN) |
| 45 | 46 |
| 46 self.raise_catch(IOError, "IOError") | 47 self.raise_catch(IOError, "IOError") |
| 47 self.assertRaises(IOError, open, 'this file does not exist', 'r') | 48 self.assertRaises(IOError, open, 'this file does not exist', 'r') |
| 48 | 49 |
| 49 self.raise_catch(ImportError, "ImportError") | 50 self.raise_catch(ImportError, "ImportError") |
| 50 self.assertRaises(ImportError, __import__, "undefined_module") | 51 self.assertRaises(ImportError, __import__, "undefined_module") |
| 51 | 52 |
| 52 self.raise_catch(IndexError, "IndexError") | 53 self.raise_catch(IndexError, "IndexError") |
| 53 x = [] | 54 x = [] |
| 54 self.assertRaises(IndexError, x.__getitem__, 10) | 55 self.assertRaises(IndexError, x.__getitem__, 10) |
| 55 | 56 |
| 56 self.raise_catch(KeyError, "KeyError") | 57 self.raise_catch(KeyError, "KeyError") |
| (...skipping 296 matching lines...) | |
| 353 | 354 |
| 354 def testChainingAttrs(self): | 355 def testChainingAttrs(self): |
| 355 e = Exception() | 356 e = Exception() |
| 356 self.assertEqual(e.__context__, None) | 357 self.assertEqual(e.__context__, None) |
| 357 self.assertEqual(e.__cause__, None) | 358 self.assertEqual(e.__cause__, None) |
| 358 | 359 |
| 359 e = TypeError() | 360 e = TypeError() |
| 360 self.assertEqual(e.__context__, None) | 361 self.assertEqual(e.__context__, None) |
| 361 self.assertEqual(e.__cause__, None) | 362 self.assertEqual(e.__cause__, None) |
| 362 | 363 |
| 363 class MyException(EnvironmentError): | 364 class MyException(EnvironmentError): |
| 364 pass | 365 pass |
| 365 | 366 |
| 366 e = MyException() | 367 e = MyException() |
| 367 self.assertEqual(e.__context__, None) | 368 self.assertEqual(e.__context__, None) |
| 368 self.assertEqual(e.__cause__, None) | 369 self.assertEqual(e.__cause__, None) |
| 369 | 370 |
| 370 def testKeywordArgs(self): | 371 def testKeywordArgs(self): |
| 371 # test that builtin exception don't take keyword args, | 372 # test that builtin exception don't take keyword args, |
| 372 # but user-defined subclasses can if they want | 373 # but user-defined subclasses can if they want |
| 373 self.assertRaises(TypeError, BaseException, a=1) | 374 self.assertRaises(TypeError, BaseException, a=1) |
| 374 | 375 |
| 375 class DerivedException(BaseException): | 376 class DerivedException(BaseException): |
| 376 def __init__(self, fancy_arg): | 377 def __init__(self, fancy_arg): |
| 377 BaseException.__init__(self) | 378 BaseException.__init__(self) |
| 378 self.fancy_arg = fancy_arg | 379 self.fancy_arg = fancy_arg |
| 379 | 380 |
| 380 x = DerivedException(fancy_arg=42) | 381 x = DerivedException(fancy_arg=42) |
| 381 self.assertEquals(x.fancy_arg, 42) | 382 self.assertEquals(x.fancy_arg, 42) |
| 382 | 383 |
| 383 def testInfiniteRecursion(self): | 384 def testInfiniteRecursion(self): |
| 384 def f(): | 385 def f(): |
| 385 return f() | 386 return f() |
| 386 self.assertRaises(RuntimeError, f) | 387 self.assertRaises(RuntimeError, f) |
| 387 | 388 |
| 388 def g(): | 389 def g(): |
| 389 try: | 390 try: |
| 390 return g() | 391 return g() |
| 391 except ValueError: | 392 except ValueError: |
| 392 return -1 | 393 return -1 |
| 393 self.assertRaises(RuntimeError, g) | 394 self.assertRaises(RuntimeError, g) |
| 394 | 395 |
| 395 def testUnicodeStrUsage(self): | 396 def testUnicodeStrUsage(self): |
| 396 # Make sure both instances and classes have a str and unicode | 397 # Make sure both instances and classes have a str and unicode |
| 397 # representation. | 398 # representation. |
| 398 self.failUnless(str(Exception)) | 399 self.failUnless(str(Exception)) |
| 399 self.failUnless(str(Exception)) | 400 self.failUnless(str(Exception)) |
| 400 self.failUnless(str(Exception('a'))) | 401 self.failUnless(str(Exception('a'))) |
| 401 self.failUnless(str(Exception('a'))) | 402 self.failUnless(str(Exception('a'))) |
| 402 | 403 |
| 403 def testExceptionCleanup(self): | 404 def testExceptionCleanupNames(self): |
|
108414
2008/05/02 23:19:14
This is a test.
| |
| 404 # Make sure "except V as N" exceptions are cleaned up properly | 405 # Make sure the local variable bound to the exception instance by |
| 406 # an "except" statement is only visible inside the except block. | |
| 405 | 407 |
| 406 try: | 408 try: |
| 407 raise Exception() | 409 raise Exception() |
| 408 except Exception as e: | 410 except Exception as e: |
| 409 self.failUnless(e) | 411 self.failUnless(e) |
| 410 del e | 412 del e |
| 411 self.failIf('e' in locals()) | 413 self.failIf('e' in locals()) |
|
jhylton
2008/05/06 12:59:48
This test isn't too obvious. The del e seems like
antoine.pitrou
2008/05/06 15:19:41
On 2008/05/06 12:59:48, jhylton wrote:
> This test
| |
| 414 | |
| 415 def testExceptionCleanupState(self): | |
| 416 # Make sure exception state is cleaned up as soon as the except | |
| 417 # block is left. See #2507 | |
| 418 | |
| 419 class MyException(Exception): | |
| 420 def __init__(self, obj): | |
| 421 self.obj = obj | |
| 422 class MyObj: | |
| 423 pass | |
| 424 | |
| 425 def inner_raising_func(): | |
| 426 # Create some references in exception value and traceback | |
| 427 local_ref = obj | |
| 428 raise MyException(obj) | |
|
jhylton
2008/05/06 12:59:48
Are both references necessary? Will the test fail
antoine.pitrou
2008/05/06 15:19:41
On 2008/05/06 12:59:48, jhylton wrote:
> Are both
| |
| 429 | |
| 430 obj = MyObj() | |
| 431 wr = weakref.ref(obj) | |
| 432 try: | |
| 433 inner_raising_func() | |
| 434 except MyException as e: | |
| 435 pass | |
| 436 obj = None | |
|
jyasskin
2008/05/03 19:20:06
Comment that this line exists to kill the referenc
| |
| 437 obj = wr() | |
| 438 self.failUnless(obj is None, "%s" % obj) | |
| 412 | 439 |
| 413 | 440 |
| 414 def test_main(): | 441 def test_main(): |
| 415 run_unittest(ExceptionTests) | 442 run_unittest(ExceptionTests) |
| 416 | 443 |
| 417 if __name__ == '__main__': | 444 if __name__ == '__main__': |
| 418 unittest.main() | 445 unittest.main() |
| OLD | NEW |
« no previous file | Python/ceval.c » ('j') | View unified diff | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')