| 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") |
| (...skipping 376 matching lines...) Show 10 above Show 10 below | |
| 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_gmail.com
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 | |
|
Jeffrey Yasskin
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 |