annotate Lib/test/test_exceptions.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 4966907d3661
children a580cf4ab940 e67b3a9bd2dc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Python test set -- part 5, built-in exceptions
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3 import os
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
4 import sys
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
5 import unittest
39543
9bc0bc0ad1bf Quite a few fixes to make the library and test suite more robust when
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
6 import pickle
46725
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
7 import weakref
72163
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
8 import errno
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
9
59442
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
10 from test.support import (TESTFN, unlink, run_unittest, captured_output,
62598
f816b991e5b3 Merged revisions 82330 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59440
diff changeset
11 gc_collect, cpython_only)
19437
5fec1c9a21ea Turn OverflowWarning into an error locally, in order to make the
Guido van Rossum <guido@python.org>
parents: 19239
diff changeset
12
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # XXX This is not really enough, each *operation* should be tested!
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
15 class ExceptionTests(unittest.TestCase):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
16
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
17 def raise_catch(self, exc, excname):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
18 try:
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
19 raise exc("spam")
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
20 except exc as err:
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
21 buf1 = str(err)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
22 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
23 raise exc("spam")
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
24 except exc as err:
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
25 buf2 = str(err)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
26 self.assertEqual(buf1, buf2)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
27 self.assertEqual(exc.__name__, excname)
12969
e6b407ba4715 add minimal test of exception use. verify that each exception can be
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12758
diff changeset
28
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
29 def testRaising(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
30 self.raise_catch(AttributeError, "AttributeError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
31 self.assertRaises(AttributeError, getattr, sys, "undefined_attribute")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
33 self.raise_catch(EOFError, "EOFError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
34 fp = open(TESTFN, 'w')
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
35 fp.close()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
36 fp = open(TESTFN, 'r')
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
37 savestdin = sys.stdin
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
38 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
39 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
40 import marshal
75374
4966907d3661 Issue #14177: marshal.loads() now raises TypeError when given an unicode string.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72163
diff changeset
41 marshal.loads(b'')
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
42 except EOFError:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
43 pass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
44 finally:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
45 sys.stdin = savestdin
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
46 fp.close()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
47 unlink(TESTFN)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
49 self.raise_catch(IOError, "IOError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
50 self.assertRaises(IOError, open, 'this file does not exist', 'r')
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
51
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
52 self.raise_catch(ImportError, "ImportError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
53 self.assertRaises(ImportError, __import__, "undefined_module")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
55 self.raise_catch(IndexError, "IndexError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
56 x = []
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
57 self.assertRaises(IndexError, x.__getitem__, 10)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
59 self.raise_catch(KeyError, "KeyError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
60 x = {}
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
61 self.assertRaises(KeyError, x.__getitem__, 'key')
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
62
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
63 self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
65 self.raise_catch(MemoryError, "MemoryError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
66
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
67 self.raise_catch(NameError, "NameError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
68 try: x = undefined_variable
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
69 except NameError: pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
71 self.raise_catch(OverflowError, "OverflowError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
72 x = 1
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
73 for dummy in range(128):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
74 x += x # this simply shouldn't blow up
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
76 self.raise_catch(RuntimeError, "RuntimeError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
78 self.raise_catch(SyntaxError, "SyntaxError")
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39543
diff changeset
79 try: exec('/\n')
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
80 except SyntaxError: pass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
81
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
82 self.raise_catch(IndentationError, "IndentationError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
84 self.raise_catch(TabError, "TabError")
47464
a65619ad59f2 Remove meaning of -ttt, but still accept -t option on cmdline for compatibility.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
85 try: compile("try:\n\t1/0\n \t1/0\nfinally:\n pass\n",
a65619ad59f2 Remove meaning of -ttt, but still accept -t option on cmdline for compatibility.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
86 '<string>', 'exec')
a65619ad59f2 Remove meaning of -ttt, but still accept -t option on cmdline for compatibility.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
87 except TabError: pass
a65619ad59f2 Remove meaning of -ttt, but still accept -t option on cmdline for compatibility.
Georg Brandl <georg@python.org>
parents: 47463
diff changeset
88 else: self.fail("TabError not raised")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
90 self.raise_catch(SystemError, "SystemError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
91
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
92 self.raise_catch(SystemExit, "SystemExit")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
93 self.assertRaises(SystemExit, sys.exit, 0)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
95 self.raise_catch(TypeError, "TypeError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
96 try: [] + ()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
97 except TypeError: pass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
98
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
99 self.raise_catch(ValueError, "ValueError")
42297
058abf878ad4 Make the test of chr() overflow pass.
Guido van Rossum <guido@python.org>
parents: 41992
diff changeset
100 self.assertRaises(ValueError, chr, 17<<16)
14472
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
101
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
102 self.raise_catch(ZeroDivisionError, "ZeroDivisionError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
103 try: x = 1/0
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
104 except ZeroDivisionError: pass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
105
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
106 self.raise_catch(Exception, "Exception")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
107 try: x = 1/0
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
108 except Exception as e: pass
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
109
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
110 def testSyntaxErrorMessage(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
111 # make sure the right exception message is raised for each of
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
112 # these code fragments
14472
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
113
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
114 def ckmsg(src, msg):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
115 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
116 compile(src, '<fragment>', 'exec')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
117 except SyntaxError as e:
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
118 if e.msg != msg:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
119 self.fail("expected %s, got %s" % (msg, e.msg))
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
120 else:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
121 self.fail("failed to get expected SyntaxError")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
122
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
123 s = '''while 1:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
124 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
125 pass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
126 finally:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
127 continue'''
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
128
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
129 if not sys.platform.startswith('java'):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
130 ckmsg(s, "'continue' not supported inside 'finally' clause")
14472
f6b86e2dc55b Add test cases to make sure we get the right SyntaxError message for
Fred Drake <fdrake@acm.org>
parents: 14342
diff changeset
131
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
132 s = '''if 1:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
133 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
134 continue
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
135 except:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
136 pass'''
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
137
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
138 ckmsg(s, "'continue' not properly in loop")
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
139 ckmsg("continue\n", "'continue' not properly in loop")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
62598
f816b991e5b3 Merged revisions 82330 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59440
diff changeset
141 @cpython_only
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
142 def testSettingException(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
143 # test that setting an exception at the C level works even if the
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
144 # exception object can't be constructed.
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
146 class BadException(Exception):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
147 def __init__(self_):
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
148 raise RuntimeError("can't instantiate BadException")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
150 class InvalidException:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
151 pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
153 def test_capi1():
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
154 import _testcapi
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
155 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
156 _testcapi.raise_exception(BadException, 1)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
157 except TypeError as err:
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
158 exc, err, tb = sys.exc_info()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
159 co = tb.tb_frame.f_code
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
160 self.assertEqual(co.co_name, "test_capi1")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
161 self.assertTrue(co.co_filename.endswith('test_exceptions.py'))
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
162 else:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
163 self.fail("Expected exception")
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
164
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
165 def test_capi2():
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
166 import _testcapi
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
167 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
168 _testcapi.raise_exception(BadException, 0)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
169 except RuntimeError as err:
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
170 exc, err, tb = sys.exc_info()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
171 co = tb.tb_frame.f_code
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
172 self.assertEqual(co.co_name, "__init__")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
173 self.assertTrue(co.co_filename.endswith('test_exceptions.py'))
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
174 co2 = tb.tb_frame.f_back.f_code
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
175 self.assertEqual(co2.co_name, "test_capi2")
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
176 else:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
177 self.fail("Expected exception")
21309
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
178
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
179 def test_capi3():
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
180 import _testcapi
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
181 self.assertRaises(SystemError, _testcapi.raise_exception,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
182 InvalidException, 1)
37871
a47117fa3ca8 Fix (and add test for) missing check for BaseException subclasses in the C
Thomas Wouters <thomas@python.org>
parents: 37045
diff changeset
183
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
184 if not sys.platform.startswith('java'):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
185 test_capi1()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
186 test_capi2()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
187 test_capi3()
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
188
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
189 def test_WindowsError(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
190 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
191 WindowsError
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
192 except NameError:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
193 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
194 else:
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
195 self.assertEqual(str(WindowsError(1001)), "1001")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
196 self.assertEqual(str(WindowsError(1001, "message")),
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
197 "[Error 1001] message")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
198 self.assertEqual(WindowsError(1001, "message").errno, 22)
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
199 self.assertEqual(WindowsError(1001, "message").winerror, 1001)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
200
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
201 def testAttributes(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
202 # test that exception attributes are happy
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37871
diff changeset
203
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
204 exceptionList = [
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
205 (BaseException, (), {'args' : ()}),
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
206 (BaseException, (1, ), {'args' : (1,)}),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
207 (BaseException, ('foo',),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
208 {'args' : ('foo',)}),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
209 (BaseException, ('foo', 1),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
210 {'args' : ('foo', 1)}),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
211 (SystemExit, ('foo',),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
212 {'args' : ('foo',), 'code' : 'foo'}),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
213 (IOError, ('foo',),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
214 {'args' : ('foo',), 'filename' : None,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
215 'errno' : None, 'strerror' : None}),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
216 (IOError, ('foo', 'bar'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
217 {'args' : ('foo', 'bar'), 'filename' : None,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
218 'errno' : 'foo', 'strerror' : 'bar'}),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
219 (IOError, ('foo', 'bar', 'baz'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
220 {'args' : ('foo', 'bar'), 'filename' : 'baz',
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
221 'errno' : 'foo', 'strerror' : 'bar'}),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
222 (IOError, ('foo', 'bar', 'baz', 'quux'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
223 {'args' : ('foo', 'bar', 'baz', 'quux')}),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
224 (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
225 {'args' : ('errnoStr', 'strErrorStr'),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
226 'strerror' : 'strErrorStr', 'errno' : 'errnoStr',
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
227 'filename' : 'filenameStr'}),
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
228 (EnvironmentError, (1, 'strErrorStr', 'filenameStr'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
229 {'args' : (1, 'strErrorStr'), 'errno' : 1,
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
230 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
231 (SyntaxError, (), {'msg' : None, 'text' : None,
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
232 'filename' : None, 'lineno' : None, 'offset' : None,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
233 'print_file_and_line' : None}),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
234 (SyntaxError, ('msgStr',),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
235 {'args' : ('msgStr',), 'text' : None,
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
236 'print_file_and_line' : None, 'msg' : 'msgStr',
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
237 'filename' : None, 'lineno' : None, 'offset' : None}),
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
238 (SyntaxError, ('msgStr', ('filenameStr', 'linenoStr', 'offsetStr',
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
239 'textStr')),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
240 {'offset' : 'offsetStr', 'text' : 'textStr',
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
241 'args' : ('msgStr', ('filenameStr', 'linenoStr',
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
242 'offsetStr', 'textStr')),
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
243 'print_file_and_line' : None, 'msg' : 'msgStr',
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
244 'filename' : 'filenameStr', 'lineno' : 'linenoStr'}),
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
245 (SyntaxError, ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
246 'textStr', 'print_file_and_lineStr'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
247 {'text' : None,
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
248 'args' : ('msgStr', 'filenameStr', 'linenoStr', 'offsetStr',
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
249 'textStr', 'print_file_and_lineStr'),
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
250 'print_file_and_line' : None, 'msg' : 'msgStr',
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
251 'filename' : None, 'lineno' : None, 'offset' : None}),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
252 (UnicodeError, (), {'args' : (),}),
41853
86a9d5f43307 Fix test_exceptions.py: There were still str8 objects
Walter Dörwald <walter@livinglogic.de>
parents: 41749
diff changeset
253 (UnicodeEncodeError, ('ascii', 'a', 0, 1,
86a9d5f43307 Fix test_exceptions.py: There were still str8 objects
Walter Dörwald <walter@livinglogic.de>
parents: 41749
diff changeset
254 'ordinal not in range'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
255 {'args' : ('ascii', 'a', 0, 1,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
256 'ordinal not in range'),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
257 'encoding' : 'ascii', 'object' : 'a',
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
258 'start' : 0, 'reason' : 'ordinal not in range'}),
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
259 (UnicodeDecodeError, ('ascii', bytearray(b'\xff'), 0, 1,
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43102
diff changeset
260 'ordinal not in range'),
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
261 {'args' : ('ascii', bytearray(b'\xff'), 0, 1,
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43102
diff changeset
262 'ordinal not in range'),
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43102
diff changeset
263 'encoding' : 'ascii', 'object' : b'\xff',
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43102
diff changeset
264 'start' : 0, 'reason' : 'ordinal not in range'}),
41853
86a9d5f43307 Fix test_exceptions.py: There were still str8 objects
Walter Dörwald <walter@livinglogic.de>
parents: 41749
diff changeset
265 (UnicodeDecodeError, ('ascii', b'\xff', 0, 1,
86a9d5f43307 Fix test_exceptions.py: There were still str8 objects
Walter Dörwald <walter@livinglogic.de>
parents: 41749
diff changeset
266 'ordinal not in range'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
267 {'args' : ('ascii', b'\xff', 0, 1,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
268 'ordinal not in range'),
41646
6a016f3e0051 Got test_exceptions.py working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
269 'encoding' : 'ascii', 'object' : b'\xff',
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
270 'start' : 0, 'reason' : 'ordinal not in range'}),
41853
86a9d5f43307 Fix test_exceptions.py: There were still str8 objects
Walter Dörwald <walter@livinglogic.de>
parents: 41749
diff changeset
271 (UnicodeTranslateError, ("\u3042", 0, 1, "ouch"),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
272 {'args' : ('\u3042', 0, 1, 'ouch'),
41575
beacf40b5960 Rip out all the u"..." literals and calls to unicode().
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
273 'object' : '\u3042', 'reason' : 'ouch',
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
274 'start' : 0, 'end' : 1}),
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37871
diff changeset
275 ]
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
276 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
277 exceptionList.append(
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
278 (WindowsError, (1, 'strErrorStr', 'filenameStr'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
279 {'args' : (1, 'strErrorStr'),
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
280 'strerror' : 'strErrorStr', 'winerror' : 1,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
281 'errno' : 22, 'filename' : 'filenameStr'})
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
282 )
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
283 except NameError:
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
284 pass
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37871
diff changeset
285
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
286 for exc, args, expected in exceptionList:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
287 try:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
288 e = exc(*args)
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
289 except:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43102
diff changeset
290 print("\nexc=%r, args=%r" % (exc, args), file=sys.stderr)
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
291 raise
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
292 else:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
293 # Verify module name
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
294 self.assertEqual(type(e).__module__, 'builtins')
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
295 # Verify no ref leaks in Exc_str()
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
296 s = str(e)
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
297 for checkArgName in expected:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
298 value = getattr(e, checkArgName)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
299 self.assertEqual(repr(value),
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
300 repr(expected[checkArgName]),
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
301 '%r.%s == %r, expected %r' % (
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
302 e, checkArgName,
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
303 value, expected[checkArgName]))
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
304
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
305 # test for pickling support
42322
3e33215c66ed Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents: 42297
diff changeset
306 for p in [pickle]:
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
307 for protocol in range(p.HIGHEST_PROTOCOL + 1):
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
308 s = p.dumps(e, protocol)
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
309 new = p.loads(s)
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
310 for checkArgName in expected:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
311 got = repr(getattr(new, checkArgName))
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
312 want = repr(expected[checkArgName])
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
313 self.assertEqual(got, want,
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
314 'pickled "%r", attribute "%s' %
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
315 (e, checkArgName))
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
316
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
317 def testWithTraceback(self):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
318 try:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
319 raise IndexError(4)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
320 except:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
321 tb = sys.exc_info()[2]
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
322
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
323 e = BaseException().with_traceback(tb)
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
324 self.assertIsInstance(e, BaseException)
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
325 self.assertEqual(e.__traceback__, tb)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
326
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
327 e = IndexError(5).with_traceback(tb)
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
328 self.assertIsInstance(e, IndexError)
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
329 self.assertEqual(e.__traceback__, tb)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
330
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
331 class MyException(Exception):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
332 pass
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
333
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
334 e = MyException().with_traceback(tb)
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
335 self.assertIsInstance(e, MyException)
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
336 self.assertEqual(e.__traceback__, tb)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
337
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
338 def testInvalidTraceback(self):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
339 try:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
340 Exception().__traceback__ = 5
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
341 except TypeError as e:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57962
diff changeset
342 self.assertIn("__traceback__ must be a traceback", str(e))
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
343 else:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
344 self.fail("No exception raised")
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
345
52509
d12b02abe6dc Fix segfaults when running test_exceptions with coverage tracing, caused by wrongly defining Exception.__context__ as a T_OBJECT structmember which does not set the member to NULL on None assignment, and generally does not do type checks. This could be used to crash the interpreter by setting any object to __context__. The same applies to __cause__. Also document the PyException_* functions.
Georg Brandl <georg@python.org>
parents: 48875
diff changeset
346 def testInvalidAttrs(self):
d12b02abe6dc Fix segfaults when running test_exceptions with coverage tracing, caused by wrongly defining Exception.__context__ as a T_OBJECT structmember which does not set the member to NULL on None assignment, and generally does not do type checks. This could be used to crash the interpreter by setting any object to __context__. The same applies to __cause__. Also document the PyException_* functions.
Georg Brandl <georg@python.org>
parents: 48875
diff changeset
347 self.assertRaises(TypeError, setattr, Exception(), '__cause__', 1)
d12b02abe6dc Fix segfaults when running test_exceptions with coverage tracing, caused by wrongly defining Exception.__context__ as a T_OBJECT structmember which does not set the member to NULL on None assignment, and generally does not do type checks. This could be used to crash the interpreter by setting any object to __context__. The same applies to __cause__. Also document the PyException_* functions.
Georg Brandl <georg@python.org>
parents: 48875
diff changeset
348 self.assertRaises(TypeError, delattr, Exception(), '__cause__')
d12b02abe6dc Fix segfaults when running test_exceptions with coverage tracing, caused by wrongly defining Exception.__context__ as a T_OBJECT structmember which does not set the member to NULL on None assignment, and generally does not do type checks. This could be used to crash the interpreter by setting any object to __context__. The same applies to __cause__. Also document the PyException_* functions.
Georg Brandl <georg@python.org>
parents: 48875
diff changeset
349 self.assertRaises(TypeError, setattr, Exception(), '__context__', 1)
d12b02abe6dc Fix segfaults when running test_exceptions with coverage tracing, caused by wrongly defining Exception.__context__ as a T_OBJECT structmember which does not set the member to NULL on None assignment, and generally does not do type checks. This could be used to crash the interpreter by setting any object to __context__. The same applies to __cause__. Also document the PyException_* functions.
Georg Brandl <georg@python.org>
parents: 48875
diff changeset
350 self.assertRaises(TypeError, delattr, Exception(), '__context__')
d12b02abe6dc Fix segfaults when running test_exceptions with coverage tracing, caused by wrongly defining Exception.__context__ as a T_OBJECT structmember which does not set the member to NULL on None assignment, and generally does not do type checks. This could be used to crash the interpreter by setting any object to __context__. The same applies to __cause__. Also document the PyException_* functions.
Georg Brandl <georg@python.org>
parents: 48875
diff changeset
351
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
352 def testNoneClearsTracebackAttr(self):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
353 try:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
354 raise IndexError(4)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
355 except:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
356 tb = sys.exc_info()[2]
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
357
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
358 e = Exception()
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
359 e.__traceback__ = tb
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
360 e.__traceback__ = None
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
361 self.assertEqual(e.__traceback__, None)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
362
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
363 def testChainingAttrs(self):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
364 e = Exception()
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
365 self.assertEqual(e.__context__, None)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
366 self.assertEqual(e.__cause__, None)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
367
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
368 e = TypeError()
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
369 self.assertEqual(e.__context__, None)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
370 self.assertEqual(e.__cause__, None)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
371
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
372 class MyException(EnvironmentError):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
373 pass
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
374
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
375 e = MyException()
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
376 self.assertEqual(e.__context__, None)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
377 self.assertEqual(e.__cause__, None)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
378
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
379 def testKeywordArgs(self):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
380 # test that builtin exception don't take keyword args,
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
381 # but user-defined subclasses can if they want
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
382 self.assertRaises(TypeError, BaseException, a=1)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
383
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
384 class DerivedException(BaseException):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
385 def __init__(self, fancy_arg):
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
386 BaseException.__init__(self)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
387 self.fancy_arg = fancy_arg
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
388
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
389 x = DerivedException(fancy_arg=42)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
390 self.assertEqual(x.fancy_arg, 42)
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
391
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
392 def testInfiniteRecursion(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
393 def f():
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
394 return f()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
395 self.assertRaises(RuntimeError, f)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
396
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
397 def g():
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
398 try:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
399 return g()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
400 except ValueError:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
401 return -1
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
402 self.assertRaises(RuntimeError, g)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
403
57930
9628eb7e98df Merged revisions 77047 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
404 def test_str(self):
9628eb7e98df Merged revisions 77047 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
405 # Make sure both instances and classes have a str representation.
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
406 self.assertTrue(str(Exception))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
407 self.assertTrue(str(Exception('a')))
57930
9628eb7e98df Merged revisions 77047 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
408 self.assertTrue(str(Exception('a', 'b')))
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
409
46725
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
410 def testExceptionCleanupNames(self):
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
411 # Make sure the local variable bound to the exception instance by
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
412 # an "except" statement is only visible inside the except block.
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
413 try:
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
414 raise Exception()
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
415 except Exception as e:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
416 self.assertTrue(e)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
417 del e
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
418 self.assertNotIn('e', locals())
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
419
46725
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
420 def testExceptionCleanupState(self):
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
421 # Make sure exception state is cleaned up as soon as the except
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
422 # block is left. See #2507
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
423
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
424 class MyException(Exception):
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
425 def __init__(self, obj):
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
426 self.obj = obj
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
427 class MyObj:
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
428 pass
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
429
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
430 def inner_raising_func():
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
431 # Create some references in exception value and traceback
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
432 local_ref = obj
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
433 raise MyException(obj)
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
434
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
435 # Qualified "except" with "as"
46725
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
436 obj = MyObj()
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
437 wr = weakref.ref(obj)
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
438 try:
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
439 inner_raising_func()
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
440 except MyException as e:
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
441 pass
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
442 obj = None
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
443 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
444 self.assertTrue(obj is None, "%s" % obj)
46725
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
445
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
446 # Qualified "except" without "as"
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
447 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
448 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
449 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
450 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
451 except MyException:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
452 pass
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
453 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
454 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
455 self.assertTrue(obj is None, "%s" % obj)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
456
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
457 # Bare "except"
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
458 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
459 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
460 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
461 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
462 except:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
463 pass
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
464 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
465 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
466 self.assertTrue(obj is None, "%s" % obj)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
467
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
468 # "except" with premature block leave
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
469 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
470 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
471 for i in [0]:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
472 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
473 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
474 except:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
475 break
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
476 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
477 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
478 self.assertTrue(obj is None, "%s" % obj)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
479
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
480 # "except" block raising another exception
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
481 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
482 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
483 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
484 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
485 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
486 except:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
487 raise KeyError
47722
7ba7901dad7f Implicit exception chaining via __context__ (PEP 3134).
Guido van Rossum <guido@python.org>
parents: 47594
diff changeset
488 except KeyError as e:
7ba7901dad7f Implicit exception chaining via __context__ (PEP 3134).
Guido van Rossum <guido@python.org>
parents: 47594
diff changeset
489 # We want to test that the except block above got rid of
7ba7901dad7f Implicit exception chaining via __context__ (PEP 3134).
Guido van Rossum <guido@python.org>
parents: 47594
diff changeset
490 # the exception raised in inner_raising_func(), but it
7ba7901dad7f Implicit exception chaining via __context__ (PEP 3134).
Guido van Rossum <guido@python.org>
parents: 47594
diff changeset
491 # also ends up in the __context__ of the KeyError, so we
7ba7901dad7f Implicit exception chaining via __context__ (PEP 3134).
Guido van Rossum <guido@python.org>
parents: 47594
diff changeset
492 # must clear the latter manually for our test to succeed.
7ba7901dad7f Implicit exception chaining via __context__ (PEP 3134).
Guido van Rossum <guido@python.org>
parents: 47594
diff changeset
493 e.__context__ = None
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
494 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
495 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
496 self.assertTrue(obj is None, "%s" % obj)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
497
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
498 # Some complicated construct
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
499 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
500 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
501 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
502 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
503 except MyException:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
504 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
505 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
506 raise
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
507 finally:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
508 raise
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
509 except MyException:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
510 pass
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
511 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
512 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
513 self.assertTrue(obj is None, "%s" % obj)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
514
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
515 # Inside an exception-silencing "with" block
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
516 class Context:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
517 def __enter__(self):
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
518 return self
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
519 def __exit__ (self, exc_type, exc_value, exc_tb):
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
520 return True
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
521 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
522 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
523 with Context():
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
524 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
525 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
526 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
527 self.assertTrue(obj is None, "%s" % obj)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
528
64620
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
529 def test_exception_target_in_nested_scope(self):
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
530 # issue 4617: This used to raise a SyntaxError
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
531 # "can not delete variable 'e' referenced in nested scope"
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
532 def print_error():
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
533 e
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
534 try:
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
535 something
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
536 except Exception as e:
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
537 print_error()
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
538 # implicit "del e" here
25cc226dc9bd #4617: Previously it was illegal to delete a name from the local
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 62598
diff changeset
539
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
540 def test_generator_leaking(self):
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
541 # Test that generator exception state doesn't leak into the calling
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
542 # frame
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
543 def yield_raise():
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
544 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
545 raise KeyError("caught")
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
546 except KeyError:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
547 yield sys.exc_info()[0]
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
548 yield sys.exc_info()[0]
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
549 yield sys.exc_info()[0]
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
550 g = yield_raise()
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
551 self.assertEqual(next(g), KeyError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
552 self.assertEqual(sys.exc_info()[0], None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
553 self.assertEqual(next(g), KeyError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
554 self.assertEqual(sys.exc_info()[0], None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
555 self.assertEqual(next(g), None)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
556
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
557 # Same test, but inside an exception handler
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
558 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
559 raise TypeError("foo")
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
560 except TypeError:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
561 g = yield_raise()
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
562 self.assertEqual(next(g), KeyError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
563 self.assertEqual(sys.exc_info()[0], TypeError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
564 self.assertEqual(next(g), KeyError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
565 self.assertEqual(sys.exc_info()[0], TypeError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
566 self.assertEqual(next(g), TypeError)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
567 del g
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
568 self.assertEqual(sys.exc_info()[0], TypeError)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
569
71151
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
570 def test_generator_leaking2(self):
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
571 # See issue 12475.
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
572 def g():
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
573 yield
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
574 try:
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
575 raise RuntimeError
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
576 except RuntimeError:
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
577 it = g()
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
578 next(it)
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
579 try:
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
580 next(it)
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
581 except StopIteration:
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
582 pass
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
583 self.assertEqual(sys.exc_info(), (None, None, None))
cc7ae81cfe91 restore a generator's caller's exception state both on yield and (last) return
Benjamin Peterson <benjamin@python.org>
parents: 68257
diff changeset
584
71157
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
585 def test_generator_doesnt_retain_old_exc(self):
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
586 def g():
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
587 self.assertIsInstance(sys.exc_info()[1], RuntimeError)
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
588 yield
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
589 self.assertEqual(sys.exc_info(), (None, None, None))
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
590 it = g()
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
591 try:
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
592 raise RuntimeError
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
593 except RuntimeError:
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
594 next(it)
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
595 self.assertRaises(StopIteration, next, it)
419871c62bb3 never retain a generator's caller's exception state on the generator after a yield/return
Benjamin Peterson <benjamin@python.org>
parents: 71151
diff changeset
596
59442
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
597 def test_generator_finalizing_and_exc_info(self):
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
598 # See #7173
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
599 def simple_gen():
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
600 yield 1
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
601 def run_gen():
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
602 gen = simple_gen()
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
603 try:
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
604 raise RuntimeError
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
605 except RuntimeError:
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
606 return next(gen)
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
607 run_gen()
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
608 gc_collect()
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
609 self.assertEqual(sys.exc_info(), (None, None, None))
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
610
71980
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
611 def _check_generator_cleanup_exc_state(self, testfunc):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
612 # Issue #12791: exception state is cleaned up as soon as a generator
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
613 # is closed (reference cycles are broken).
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
614 class MyException(Exception):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
615 def __init__(self, obj):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
616 self.obj = obj
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
617 class MyObj:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
618 pass
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
619
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
620 def raising_gen():
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
621 try:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
622 raise MyException(obj)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
623 except MyException:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
624 yield
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
625
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
626 obj = MyObj()
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
627 wr = weakref.ref(obj)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
628 g = raising_gen()
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
629 next(g)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
630 testfunc(g)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
631 g = obj = None
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
632 obj = wr()
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
633 self.assertIs(obj, None)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
634
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
635 def test_generator_throw_cleanup_exc_state(self):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
636 def do_throw(g):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
637 try:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
638 g.throw(RuntimeError())
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
639 except RuntimeError:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
640 pass
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
641 self._check_generator_cleanup_exc_state(do_throw)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
642
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
643 def test_generator_close_cleanup_exc_state(self):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
644 def do_close(g):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
645 g.close()
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
646 self._check_generator_cleanup_exc_state(do_close)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
647
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
648 def test_generator_del_cleanup_exc_state(self):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
649 def do_del(g):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
650 g = None
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
651 self._check_generator_cleanup_exc_state(do_del)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
652
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
653 def test_generator_next_cleanup_exc_state(self):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
654 def do_next(g):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
655 try:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
656 next(g)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
657 except StopIteration:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
658 pass
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
659 else:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
660 self.fail("should have raised StopIteration")
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
661 self._check_generator_cleanup_exc_state(do_next)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
662
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
663 def test_generator_send_cleanup_exc_state(self):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
664 def do_send(g):
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
665 try:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
666 g.send(None)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
667 except StopIteration:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
668 pass
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
669 else:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
670 self.fail("should have raised StopIteration")
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
671 self._check_generator_cleanup_exc_state(do_send)
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
672
47729
ee2823e1f23b improvements to the fix for #3114
Benjamin Peterson <benjamin@python.org>
parents: 47724
diff changeset
673 def test_3114(self):
ee2823e1f23b improvements to the fix for #3114
Benjamin Peterson <benjamin@python.org>
parents: 47724
diff changeset
674 # Bug #3114: in its destructor, MyObject retrieves a pointer to
ee2823e1f23b improvements to the fix for #3114
Benjamin Peterson <benjamin@python.org>
parents: 47724
diff changeset
675 # obsolete and/or deallocated objects.
47724
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
676 class MyObject:
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
677 def __del__(self):
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
678 nonlocal e
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
679 e = sys.exc_info()
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
680 e = ()
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
681 try:
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
682 raise Exception(MyObject())
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
683 except:
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
684 pass
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
685 self.assertEqual(e, (None, None, None))
47724
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
686
59111
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
687 def testUnicodeChangeAttributes(self):
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
688 # See issue 7309. This was a crasher.
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
689
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
690 u = UnicodeEncodeError('baz', 'xxxxx', 1, 5, 'foo')
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
691 self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: foo")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
692 u.end = 2
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
693 self.assertEqual(str(u), "'baz' codec can't encode character '\\x78' in position 1: foo")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
694 u.end = 5
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
695 u.reason = 0x345345345345345345
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
696 self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: 965230951443685724997")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
697 u.encoding = 4000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
698 self.assertEqual(str(u), "'4000' codec can't encode characters in position 1-4: 965230951443685724997")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
699 u.start = 1000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
700 self.assertEqual(str(u), "'4000' codec can't encode characters in position 1000-4: 965230951443685724997")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
701
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
702 u = UnicodeDecodeError('baz', b'xxxxx', 1, 5, 'foo')
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
703 self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: foo")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
704 u.end = 2
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
705 self.assertEqual(str(u), "'baz' codec can't decode byte 0x78 in position 1: foo")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
706 u.end = 5
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
707 u.reason = 0x345345345345345345
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
708 self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: 965230951443685724997")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
709 u.encoding = 4000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
710 self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1-4: 965230951443685724997")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
711 u.start = 1000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
712 self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1000-4: 965230951443685724997")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
713
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
714 u = UnicodeTranslateError('xxxx', 1, 5, 'foo')
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
715 self.assertEqual(str(u), "can't translate characters in position 1-4: foo")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
716 u.end = 2
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
717 self.assertEqual(str(u), "can't translate character '\\x78' in position 1: foo")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
718 u.end = 5
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
719 u.reason = 0x345345345345345345
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
720 self.assertEqual(str(u), "can't translate characters in position 1-4: 965230951443685724997")
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
721 u.start = 1000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
722 self.assertEqual(str(u), "can't translate characters in position 1000-4: 965230951443685724997")
48807
013d4d8407cb apply a fix for #3611 where the current exception context was deleted with a generator causing a segfault
Benjamin Peterson <benjamin@python.org>
parents: 48704
diff changeset
723
48589
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
724 def test_badisinstance(self):
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
725 # Bug #2542: if issubclass(e, MyException) raises an exception,
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
726 # it should be ignored
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
727 class Meta(type):
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
728 def __subclasscheck__(cls, subclass):
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
729 raise ValueError()
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
730 class MyException(Exception, metaclass=Meta):
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
731 pass
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
732
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
733 with captured_output("stderr") as stderr:
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
734 try:
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
735 raise KeyError()
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
736 except MyException as e:
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
737 self.fail("exception should not be a MyException")
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
738 except KeyError:
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
739 pass
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
740 except:
48875
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
741 self.fail("Should have raised KeyError")
48589
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
742 else:
48875
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
743 self.fail("Should have raised KeyError")
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
744
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
745 def g():
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
746 try:
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
747 return g()
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
748 except RuntimeError:
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
749 return sys.exc_info()
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
750 e, v, tb = g()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
751 self.assertTrue(isinstance(v, RuntimeError), type(v))
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57962
diff changeset
752 self.assertIn("maximum recursion depth exceeded", str(v))
48589
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
753
48598
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
754
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
755 def test_MemoryError(self):
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
756 # PyErr_NoMemory always raises the same exception instance.
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
757 # Check that the traceback is not doubled.
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
758 import traceback
48704
1ed86ce0beb2 add _testcapi.raise_memoryerror to make test_exceptions.test_MemoryError simpler
Benjamin Peterson <benjamin@python.org>
parents: 48601
diff changeset
759 from _testcapi import raise_memoryerror
48598
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
760 def raiseMemError():
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
761 try:
48704
1ed86ce0beb2 add _testcapi.raise_memoryerror to make test_exceptions.test_MemoryError simpler
Benjamin Peterson <benjamin@python.org>
parents: 48601
diff changeset
762 raise_memoryerror()
48598
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
763 except MemoryError as e:
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
764 tb = e.__traceback__
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
765 else:
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
766 self.fail("Should have raises a MemoryError")
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
767 return traceback.format_tb(tb)
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
768
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
769 tb1 = raiseMemError()
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
770 tb2 = raiseMemError()
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
771 self.assertEqual(tb1, tb2)
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
772
62598
f816b991e5b3 Merged revisions 82330 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59440
diff changeset
773 @cpython_only
57962
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
774 def test_exception_with_doc(self):
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
775 import _testcapi
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
776 doc2 = "This is a test docstring."
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
777 doc4 = "This is another test docstring."
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
778
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
779 self.assertRaises(SystemError, _testcapi.make_exception_with_doc,
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
780 "error1")
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
781
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
782 # test basic usage of PyErr_NewException
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
783 error1 = _testcapi.make_exception_with_doc("_testcapi.error1")
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
784 self.assertIs(type(error1), type)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
785 self.assertTrue(issubclass(error1, Exception))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
786 self.assertIsNone(error1.__doc__)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
787
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
788 # test with given docstring
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
789 error2 = _testcapi.make_exception_with_doc("_testcapi.error2", doc2)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
790 self.assertEqual(error2.__doc__, doc2)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
791
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
792 # test with explicit base (without docstring)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
793 error3 = _testcapi.make_exception_with_doc("_testcapi.error3",
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
794 base=error2)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
795 self.assertTrue(issubclass(error3, error2))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
796
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
797 # test with explicit base tuple
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
798 class C(object):
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
799 pass
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
800 error4 = _testcapi.make_exception_with_doc("_testcapi.error4", doc4,
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
801 (error3, C))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
802 self.assertTrue(issubclass(error4, error3))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
803 self.assertTrue(issubclass(error4, C))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
804 self.assertEqual(error4.__doc__, doc4)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
805
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
806 # test with explicit dictionary
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
807 error5 = _testcapi.make_exception_with_doc("_testcapi.error5", "",
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
808 error4, {'a': 1})
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
809 self.assertTrue(issubclass(error5, error4))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
810 self.assertEqual(error5.a, 1)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
811 self.assertEqual(error5.__doc__, "")
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
812
65690
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
813 def test_memory_error_cleanup(self):
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
814 # Issue #5437: preallocated MemoryError instances should not keep
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
815 # traceback objects alive.
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
816 from _testcapi import raise_memoryerror
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
817 class C:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
818 pass
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
819 wr = None
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
820 def inner():
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
821 nonlocal wr
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
822 c = C()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
823 wr = weakref.ref(c)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
824 raise_memoryerror()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
825 # We cannot use assertRaises since it manually deletes the traceback
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
826 try:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
827 inner()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
828 except MemoryError as e:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
829 self.assertNotEqual(wr(), None)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
830 else:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
831 self.fail("MemoryError not raised")
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
832 self.assertEqual(wr(), None)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
833
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
834 def test_recursion_error_cleanup(self):
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
835 # Same test as above, but with "recursion exceeded" errors
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
836 class C:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
837 pass
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
838 wr = None
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
839 def inner():
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
840 nonlocal wr
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
841 c = C()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
842 wr = weakref.ref(c)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
843 inner()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
844 # We cannot use assertRaises since it manually deletes the traceback
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
845 try:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
846 inner()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
847 except RuntimeError as e:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
848 self.assertNotEqual(wr(), None)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
849 else:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
850 self.fail("RuntimeError not raised")
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
851 self.assertEqual(wr(), None)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
852
72163
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
853 def test_errno_ENOTDIR(self):
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
854 # Issue #12802: "not a directory" errors are ENOTDIR even on Windows
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
855 with self.assertRaises(OSError) as cm:
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
856 os.listdir(__file__)
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
857 self.assertEqual(cm.exception.errno, errno.ENOTDIR, cm.exception)
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
858
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
859
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
860 def test_main():
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
861 run_unittest(ExceptionTests)
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
862
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
863 if __name__ == '__main__':
41646
6a016f3e0051 Got test_exceptions.py working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
864 unittest.main()