annotate Lib/test/test_exceptions.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 2e587b9bae35
children 21cc413eb23c 267a4d4d9d65
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,
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
11 gc_collect, cpython_only, no_tracing)
19437
5fec1c9a21ea Turn OverflowWarning into an error locally, in order to make the
Guido van Rossum <guido@python.org>
parents: 19239
diff changeset
12
78304
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
13 class NaiveException(Exception):
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
14 def __init__(self, x):
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
15 self.x = x
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
16
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
17 class SlottedNaiveException(Exception):
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
18 __slots__ = ('x',)
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
19 def __init__(self, x):
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
20 self.x = x
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
21
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # XXX This is not really enough, each *operation* should be tested!
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
24 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
25
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
26 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
27 try:
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
28 raise exc("spam")
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
29 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
30 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
31 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
32 raise exc("spam")
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
33 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
34 buf2 = str(err)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
35 self.assertEqual(buf1, buf2)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
36 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
37
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
38 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
39 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
40 self.assertRaises(AttributeError, getattr, sys, "undefined_attribute")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
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 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
43 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
44 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
45 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
46 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
47 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
48 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
49 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
50 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
51 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
52 pass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
53 finally:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
54 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
55 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
56 unlink(TESTFN)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
72879
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
58 self.raise_catch(OSError, "OSError")
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
59 self.assertRaises(OSError, open, 'this file does not exist', 'r')
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
60
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.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
62 self.assertRaises(ImportError, __import__, "undefined_module")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
64 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
65 x = []
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
66 self.assertRaises(IndexError, x.__getitem__, 10)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
68 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
69 x = {}
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
70 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
71
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
72 self.raise_catch(KeyboardInterrupt, "KeyboardInterrupt")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
74 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
75
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(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
77 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
78 except NameError: pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
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 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
81 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
82 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
83 x += x # this simply shouldn't blow up
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
85 self.raise_catch(RuntimeError, "RuntimeError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
87 self.raise_catch(SyntaxError, "SyntaxError")
39782
c94f37054f19 Patch #1550800: make exec a function.
Georg Brandl <georg@python.org>
parents: 39543
diff changeset
88 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
89 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
90
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
91 self.raise_catch(IndentationError, "IndentationError")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
38669
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.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
94 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
95 '<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
96 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
97 else: self.fail("TabError not raised")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
38669
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(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
100
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
101 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
102 self.assertRaises(SystemExit, sys.exit, 0)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
104 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
105 try: [] + ()
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
106 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
107
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
108 self.raise_catch(ValueError, "ValueError")
42297
058abf878ad4 Make the test of chr() overflow pass.
Guido van Rossum <guido@python.org>
parents: 41992
diff changeset
109 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
110
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
111 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
112 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
113 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
114
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
115 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
116 try: x = 1/0
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
117 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
118
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
119 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
120 # 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
121 # 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
122
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
123 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
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 compile(src, '<fragment>', 'exec')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
126 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
127 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
128 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
129 else:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
130 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
131
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 = '''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
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 pass
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
135 finally:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
136 continue'''
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
137
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
138 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
139 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
140
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
141 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
142 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
143 continue
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
144 except:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
145 pass'''
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 12969
diff changeset
146
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
147 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
148 ckmsg("continue\n", "'continue' not properly in loop")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
62598
f816b991e5b3 Merged revisions 82330 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59440
diff changeset
150 @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
151 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
152 # 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
153 # exception object can't be constructed.
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
155 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
156 def __init__(self_):
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
157 raise RuntimeError("can't instantiate BadException")
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
159 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
160 pass
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
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 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
163 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
164 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
165 _testcapi.raise_exception(BadException, 1)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
166 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
167 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
168 co = tb.tb_frame.f_code
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
169 self.assertEqual(co.co_name, "test_capi1")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
170 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
171 else:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
172 self.fail("Expected exception")
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
173
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 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
175 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
176 try:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
177 _testcapi.raise_exception(BadException, 0)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
178 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
179 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
180 co = tb.tb_frame.f_code
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
181 self.assertEqual(co.co_name, "__init__")
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
182 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
183 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
184 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
185 else:
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
186 self.fail("Expected exception")
21309
2607433b13e9 Enable support for jython:
Finn Bock <bckfnn@worldonline.dk>
parents: 20117
diff changeset
187
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
188 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
189 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
190 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
191 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
192
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
193 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
194 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
195 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
196 test_capi3()
20117
e026f517f655 Add tests for new PyErr_NormalizeException() behavior
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19437
diff changeset
197
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
198 def test_WindowsError(self):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
199 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
200 WindowsError
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
201 except NameError:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
202 pass
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
203 else:
72879
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
204 self.assertIs(WindowsError, OSError)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
205 self.assertEqual(str(OSError(1001)), "1001")
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
206 self.assertEqual(str(OSError(1001, "message")),
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
207 "[Errno 1001] message")
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
208 # POSIX errno (9 aka EBADF) is untranslated
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
209 w = OSError(9, 'foo', 'bar')
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
210 self.assertEqual(w.errno, 9)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
211 self.assertEqual(w.winerror, None)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
212 self.assertEqual(str(w), "[Errno 9] foo: 'bar'")
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
213 # ERROR_PATH_NOT_FOUND (win error 3) becomes ENOENT (2)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
214 w = OSError(0, 'foo', 'bar', 3)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
215 self.assertEqual(w.errno, 2)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
216 self.assertEqual(w.winerror, 3)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
217 self.assertEqual(w.strerror, 'foo')
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
218 self.assertEqual(w.filename, 'bar')
78791
2e587b9bae35 Issue #15784: Modify OSError.__str__() to better distinguish between
Richard Oudkerk <shibturn@gmail.com>
parents: 78737
diff changeset
219 self.assertEqual(str(w), "[WinError 3] foo: 'bar'")
72879
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
220 # Unknown win error becomes EINVAL (22)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
221 w = OSError(0, 'foo', None, 1001)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
222 self.assertEqual(w.errno, 22)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
223 self.assertEqual(w.winerror, 1001)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
224 self.assertEqual(w.strerror, 'foo')
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
225 self.assertEqual(w.filename, None)
78791
2e587b9bae35 Issue #15784: Modify OSError.__str__() to better distinguish between
Richard Oudkerk <shibturn@gmail.com>
parents: 78737
diff changeset
226 self.assertEqual(str(w), "[WinError 1001] foo")
72879
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
227 # Non-numeric "errno"
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
228 w = OSError('bar', 'foo')
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
229 self.assertEqual(w.errno, 'bar')
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
230 self.assertEqual(w.winerror, None)
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
231 self.assertEqual(w.strerror, 'foo')
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
232 self.assertEqual(w.filename, None)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
233
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 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
235 # 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
236
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
237 exceptionList = [
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
238 (BaseException, (), {'args' : ()}),
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
239 (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
240 (BaseException, ('foo',),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
241 {'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
242 (BaseException, ('foo', 1),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
243 {'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
244 (SystemExit, ('foo',),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
245 {'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
246 (IOError, ('foo',),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
247 {'args' : ('foo',), 'filename' : None,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
248 '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
249 (IOError, ('foo', 'bar'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
250 {'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
251 '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
252 (IOError, ('foo', 'bar', 'baz'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
253 {'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
254 'errno' : 'foo', 'strerror' : 'bar'}),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
255 (IOError, ('foo', 'bar', 'baz', 'quux'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
256 {'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
257 (EnvironmentError, ('errnoStr', 'strErrorStr', 'filenameStr'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
258 {'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
259 '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
260 '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
261 (EnvironmentError, (1, 'strErrorStr', 'filenameStr'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
262 {'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
263 'strerror' : 'strErrorStr', 'filename' : 'filenameStr'}),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
264 (SyntaxError, (), {'msg' : None, 'text' : None,
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
265 'filename' : None, 'lineno' : None, 'offset' : None,
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40959
diff changeset
266 '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
267 (SyntaxError, ('msgStr',),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
268 {'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
269 '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
270 '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
271 (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
272 'textStr')),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
273 {'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
274 '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
275 '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
276 '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
277 '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
278 (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
279 'textStr', 'print_file_and_lineStr'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
280 {'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
281 '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
282 '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
283 '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
284 '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
285 (UnicodeError, (), {'args' : (),}),
41853
86a9d5f43307 Fix test_exceptions.py: There were still str8 objects
Walter Dörwald <walter@livinglogic.de>
parents: 41749
diff changeset
286 (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
287 'ordinal not in range'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
288 {'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
289 '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
290 'encoding' : 'ascii', 'object' : 'a',
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
291 'start' : 0, 'reason' : 'ordinal not in range'}),
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
292 (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
293 'ordinal not in range'),
43947
cbc8b3bd3157 Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents: 43777
diff changeset
294 {'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
295 '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
296 '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
297 '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
298 (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
299 'ordinal not in range'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
300 {'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
301 'ordinal not in range'),
41646
6a016f3e0051 Got test_exceptions.py working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
302 '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
303 '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
304 (UnicodeTranslateError, ("\u3042", 0, 1, "ouch"),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
305 {'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
306 '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
307 'start' : 0, 'end' : 1}),
78304
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
308 (NaiveException, ('foo',),
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
309 {'args': ('foo',), 'x': 'foo'}),
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
310 (SlottedNaiveException, ('foo',),
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
311 {'args': ('foo',), 'x': 'foo'}),
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37871
diff changeset
312 ]
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
313 try:
72879
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
314 # More tests are in test_WindowsError
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
315 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
316 (WindowsError, (1, 'strErrorStr', 'filenameStr'),
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
317 {'args' : (1, 'strErrorStr'),
72879
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
318 'strerror' : 'strErrorStr', 'winerror' : None,
41a1de81ef2b PEP 3151 / issue #12555: reworking the OS and IO exception hierarchy.
Antoine Pitrou <solipsis@pitrou.net>
parents: 72164
diff changeset
319 'errno' : 1, 'filename' : 'filenameStr'})
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
320 )
38673
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
321 except NameError:
a19c20c5e5c7 Merge the rest of the trunk.
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
322 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
323
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
324 for exc, args, expected in exceptionList:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
325 try:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
326 e = exc(*args)
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
327 except:
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43102
diff changeset
328 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
329 raise
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
330 else:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
331 # Verify module name
78304
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
332 if not type(e).__name__.endswith('NaiveException'):
68e2690a471d Issue #1692335: Move initial args assignment to BaseException.__new__
Richard Oudkerk <shibturn@gmail.com>
parents: 76942
diff changeset
333 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
334 # 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
335 s = str(e)
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
336 for checkArgName in expected:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
337 value = getattr(e, checkArgName)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
338 self.assertEqual(repr(value),
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
339 repr(expected[checkArgName]),
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
340 '%r.%s == %r, expected %r' % (
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
341 e, checkArgName,
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
342 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
343
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
344 # test for pickling support
42322
3e33215c66ed Getting rid of cPickle. Mmm, feels good!
Guido van Rossum <guido@python.org>
parents: 42297
diff changeset
345 for p in [pickle]:
41749
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
346 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
347 s = p.dumps(e, protocol)
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
348 new = p.loads(s)
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
349 for checkArgName in expected:
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
350 got = repr(getattr(new, checkArgName))
b085eb66b7f6 Merged revisions 55342-55406 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41716
diff changeset
351 want = repr(expected[checkArgName])
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
352 self.assertEqual(got, want,
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
353 'pickled "%r", attribute "%s' %
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
354 (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
355
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
356 def testWithTraceback(self):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
357 try:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
358 raise IndexError(4)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
359 except:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
360 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
361
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
362 e = BaseException().with_traceback(tb)
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
363 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
364 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
365
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
366 e = IndexError(5).with_traceback(tb)
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
367 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
368 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
369
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
370 class MyException(Exception):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
371 pass
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
372
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
373 e = MyException().with_traceback(tb)
58473
71acd4482e65 Merged revisions 77727 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58456
diff changeset
374 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
375 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
376
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
377 def testInvalidTraceback(self):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
378 try:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
379 Exception().__traceback__ = 5
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
380 except TypeError as e:
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57962
diff changeset
381 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
382 else:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
383 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
384
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
385 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
386 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
387 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
388 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
389 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
390
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
391 def testNoneClearsTracebackAttr(self):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
392 try:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
393 raise IndexError(4)
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
394 except:
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
395 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
396
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
397 e = Exception()
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
398 e.__traceback__ = tb
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
399 e.__traceback__ = None
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
400 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
401
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
402 def testChainingAttrs(self):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
403 e = Exception()
75292
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
404 self.assertIsNone(e.__context__)
76942
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 76272
diff changeset
405 self.assertIsNone(e.__cause__)
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
406
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
407 e = TypeError()
75292
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
408 self.assertIsNone(e.__context__)
76942
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 76272
diff changeset
409 self.assertIsNone(e.__cause__)
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
410
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
411 class MyException(EnvironmentError):
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
412 pass
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
413
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
414 e = MyException()
75292
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
415 self.assertIsNone(e.__context__)
76942
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 76272
diff changeset
416 self.assertIsNone(e.__cause__)
75292
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
417
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
418 def testChainingDescriptors(self):
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
419 try:
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
420 raise Exception()
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
421 except Exception as exc:
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
422 e = exc
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
423
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
424 self.assertIsNone(e.__context__)
76942
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 76272
diff changeset
425 self.assertIsNone(e.__cause__)
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 76272
diff changeset
426 self.assertFalse(e.__suppress_context__)
75292
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
427
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
428 e.__context__ = NameError()
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
429 e.__cause__ = None
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
430 self.assertIsInstance(e.__context__, NameError)
b299c4f31ff2 Close issue #6210: Implement PEP 409
Nick Coghlan <ncoghlan@gmail.com>
parents: 72879
diff changeset
431 self.assertIsNone(e.__cause__)
76942
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 76272
diff changeset
432 self.assertTrue(e.__suppress_context__)
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 76272
diff changeset
433 e.__suppress_context__ = False
b0eb7d2a9542 PEP 415: Implement suppression of __context__ display with an exception attribute
Benjamin Peterson <benjamin@python.org>
parents: 76272
diff changeset
434 self.assertFalse(e.__suppress_context__)
43102
981ecc526703 Issue #1066: implement PEP 3109, 2/3 of PEP 3134.
Collin Winter <collinw@gmail.com>
parents: 42629
diff changeset
435
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
436 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
437 # 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
438 # 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
439 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
440
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
441 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
442 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
443 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
444 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
445
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
446 x = DerivedException(fancy_arg=42)
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
447 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
448
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
449 @no_tracing
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
450 def testInfiniteRecursion(self):
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
451 def f():
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
452 return f()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
453 self.assertRaises(RuntimeError, f)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
454
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
455 def g():
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
456 try:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
457 return g()
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
458 except ValueError:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
459 return -1
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
460 self.assertRaises(RuntimeError, g)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38673
diff changeset
461
57930
9628eb7e98df Merged revisions 77047 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
462 def test_str(self):
9628eb7e98df Merged revisions 77047 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
463 # 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
464 self.assertTrue(str(Exception))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
465 self.assertTrue(str(Exception('a')))
57930
9628eb7e98df Merged revisions 77047 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 55593
diff changeset
466 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
467
46725
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
468 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
469 # 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
470 # 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
471 try:
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
472 raise Exception()
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
473 except Exception as e:
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
474 self.assertTrue(e)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
475 del e
58456
f0f221e1a7c0 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58415
diff changeset
476 self.assertNotIn('e', locals())
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
477
46725
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
478 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
479 # 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
480 # 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
481
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
482 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
483 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
484 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
485 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
486 pass
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
487
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
488 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
489 # 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
490 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
491 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
492
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
493 # 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
494 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
495 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
496 try:
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
497 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
498 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
499 pass
acd68277c4dd Antoine Pitrou's patch for bug 2507; exception state lives too long in
Barry Warsaw <barry@python.org>
parents: 44098
diff changeset
500 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
501 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
502 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
503
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
504 # Qualified "except" without "as"
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
505 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
506 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
507 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
508 inner_raising_func()
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 # Bare "except"
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
516 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
517 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
518 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
519 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
520 except:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
521 pass
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
522 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
523 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
524 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
525
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
526 # "except" with premature block leave
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
527 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
528 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
529 for i in [0]:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
530 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
531 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
532 except:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
533 break
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
534 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
535 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
536 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
537
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
538 # "except" block raising another exception
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
539 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
540 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
541 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
542 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
543 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
544 except:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
545 raise KeyError
47722
7ba7901dad7f Implicit exception chaining via __context__ (PEP 3134).
Guido van Rossum <guido@python.org>
parents: 47594
diff changeset
546 except KeyError as e:
7ba7901dad7f Implicit exception chaining via __context__ (PEP 3134).
Guido van Rossum <guido@python.org>
parents: 47594
diff changeset
547 # 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
548 # 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
549 # 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
550 # 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
551 e.__context__ = None
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
552 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
553 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
554 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
555
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
556 # Some complicated construct
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
557 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
558 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
559 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
560 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
561 except MyException:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
562 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
563 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
564 raise
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
565 finally:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
566 raise
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
567 except MyException:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
568 pass
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
569 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
570 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
571 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
572
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
573 # Inside an exception-silencing "with" block
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
574 class Context:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
575 def __enter__(self):
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
576 return self
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
577 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
578 return True
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
579 obj = MyObj()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
580 wr = weakref.ref(obj)
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
581 with Context():
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
582 inner_raising_func()
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
583 obj = None
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
584 obj = wr()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
585 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
586
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
587 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
588 # 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
589 # "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
590 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
591 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
592 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
593 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
594 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
595 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
596 # 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
597
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
598 def test_generator_leaking(self):
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
599 # 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
600 # frame
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
601 def yield_raise():
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
602 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
603 raise KeyError("caught")
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
604 except KeyError:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
605 yield sys.exc_info()[0]
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
606 yield sys.exc_info()[0]
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
607 yield sys.exc_info()[0]
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
608 g = yield_raise()
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
609 self.assertEqual(next(g), KeyError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
610 self.assertEqual(sys.exc_info()[0], None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
611 self.assertEqual(next(g), KeyError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
612 self.assertEqual(sys.exc_info()[0], None)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
613 self.assertEqual(next(g), None)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
614
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
615 # Same test, but inside an exception handler
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
616 try:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
617 raise TypeError("foo")
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
618 except TypeError:
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
619 g = yield_raise()
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
620 self.assertEqual(next(g), KeyError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
621 self.assertEqual(sys.exc_info()[0], TypeError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
622 self.assertEqual(next(g), KeyError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
623 self.assertEqual(sys.exc_info()[0], TypeError)
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
624 self.assertEqual(next(g), TypeError)
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47464
diff changeset
625 del g
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
626 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
627
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
628 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
629 # 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
630 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
631 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
632 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
633 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
634 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
635 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
636 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
637 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
638 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
639 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
640 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
641 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
642
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
643 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
644 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
645 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
646 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
647 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
648 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
649 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
650 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
651 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
652 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
653 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
654
59442
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
655 def test_generator_finalizing_and_exc_info(self):
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
656 # See #7173
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
657 def simple_gen():
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
658 yield 1
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
659 def run_gen():
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
660 gen = simple_gen()
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
661 try:
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
662 raise RuntimeError
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
663 except RuntimeError:
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
664 return next(gen)
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
665 run_gen()
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
666 gc_collect()
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
667 self.assertEqual(sys.exc_info(), (None, None, None))
da19894946e1 Merged revisions 78766 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59111
diff changeset
668
71980
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
669 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
670 # 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
671 # 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
672 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
673 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
674 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
675 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
676 pass
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
677
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
678 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
679 try:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
680 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
681 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
682 yield
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
683
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
684 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
685 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
686 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
687 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
688 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
689 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
690 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
691 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
692
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
693 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
694 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
695 try:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
696 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
697 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
698 pass
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
699 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
700
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
701 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
702 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
703 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
704 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
705
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
706 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
707 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
708 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
709 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
710
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
711 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
712 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
713 try:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
714 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
715 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
716 pass
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
717 else:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
718 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
719 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
720
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
721 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
722 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
723 try:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
724 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
725 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
726 pass
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
727 else:
867ce75b885c Issue #12791: Break reference cycles early when a generator exits with an exception.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71157
diff changeset
728 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
729 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
730
47729
ee2823e1f23b improvements to the fix for #3114
Benjamin Peterson <benjamin@python.org>
parents: 47724
diff changeset
731 def test_3114(self):
ee2823e1f23b improvements to the fix for #3114
Benjamin Peterson <benjamin@python.org>
parents: 47724
diff changeset
732 # 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
733 # 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
734 class MyObject:
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
735 def __del__(self):
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
736 nonlocal e
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
737 e = sys.exc_info()
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
738 e = ()
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
739 try:
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
740 raise Exception(MyObject())
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
741 except:
b33e8334f82f #3114 fix a bus error when deallocated exceptions were used
Benjamin Peterson <benjamin@python.org>
parents: 47722
diff changeset
742 pass
66340
2beb25369289 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65690
diff changeset
743 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
744
59111
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
745 def testUnicodeChangeAttributes(self):
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
746 # See issue 7309. This was a crasher.
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
747
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
748 u = UnicodeEncodeError('baz', 'xxxxx', 1, 5, 'foo')
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
749 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
750 u.end = 2
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
751 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
752 u.end = 5
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
753 u.reason = 0x345345345345345345
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
754 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
755 u.encoding = 4000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
756 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
757 u.start = 1000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
758 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
759
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
760 u = UnicodeDecodeError('baz', b'xxxxx', 1, 5, 'foo')
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
761 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
762 u.end = 2
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
763 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
764 u.end = 5
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
765 u.reason = 0x345345345345345345
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
766 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
767 u.encoding = 4000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
768 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
769 u.start = 1000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
770 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
771
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
772 u = UnicodeTranslateError('xxxx', 1, 5, 'foo')
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
773 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
774 u.end = 2
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
775 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
776 u.end = 5
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
777 u.reason = 0x345345345345345345
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
778 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
779 u.start = 1000
c8ef906b11b8 Merged revisions 78420 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 57930
diff changeset
780 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
781
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
782 @no_tracing
48589
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
783 def test_badisinstance(self):
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
784 # 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
785 # it should be ignored
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
786 class Meta(type):
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
787 def __subclasscheck__(cls, subclass):
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
788 raise ValueError()
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
789 class MyException(Exception, metaclass=Meta):
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
790 pass
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
791
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
792 with captured_output("stderr") as stderr:
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
793 try:
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
794 raise KeyError()
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
795 except MyException as e:
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
796 self.fail("exception should not be a MyException")
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
797 except KeyError:
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
798 pass
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
799 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
800 self.fail("Should have raised KeyError")
48589
1ec38b707cd6 Merged revisions 65320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 47729
diff changeset
801 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
802 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
803
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
804 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
805 try:
d6e86a96f9b3 Issue #2534: speed up isinstance() and issubclass() by 50-70%, so as to
Antoine Pitrou <solipsis@pitrou.net>
parents: 48815
diff changeset
806 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
807 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
808 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
809 e, v, tb = g()
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52509
diff changeset
810 self.assertTrue(isinstance(v, RuntimeError), type(v))
58415
f6a626f34840 use assert[Not]In where appropriate
Benjamin Peterson <benjamin@python.org>
parents: 57962
diff changeset
811 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
812
48598
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
813
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
814 def test_MemoryError(self):
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
815 # 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
816 # 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
817 import traceback
48704
1ed86ce0beb2 add _testcapi.raise_memoryerror to make test_exceptions.test_MemoryError simpler
Benjamin Peterson <benjamin@python.org>
parents: 48601
diff changeset
818 from _testcapi import raise_memoryerror
48598
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
819 def raiseMemError():
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
820 try:
48704
1ed86ce0beb2 add _testcapi.raise_memoryerror to make test_exceptions.test_MemoryError simpler
Benjamin Peterson <benjamin@python.org>
parents: 48601
diff changeset
821 raise_memoryerror()
48598
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
822 except MemoryError as e:
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
823 tb = e.__traceback__
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
824 else:
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
825 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
826 return traceback.format_tb(tb)
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
827
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
828 tb1 = raiseMemError()
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
829 tb2 = raiseMemError()
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
830 self.assertEqual(tb1, tb2)
de75e7280b7a Correct one of the "MemoryError oddities":
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48589
diff changeset
831
62598
f816b991e5b3 Merged revisions 82330 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59440
diff changeset
832 @cpython_only
57962
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
833 def test_exception_with_doc(self):
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
834 import _testcapi
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
835 doc2 = "This is a test docstring."
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
836 doc4 = "This is another test docstring."
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
837
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
838 self.assertRaises(SystemError, _testcapi.make_exception_with_doc,
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
839 "error1")
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
840
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
841 # test basic usage of PyErr_NewException
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
842 error1 = _testcapi.make_exception_with_doc("_testcapi.error1")
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
843 self.assertIs(type(error1), type)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
844 self.assertTrue(issubclass(error1, Exception))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
845 self.assertIsNone(error1.__doc__)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
846
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
847 # test with given docstring
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
848 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
849 self.assertEqual(error2.__doc__, doc2)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
850
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
851 # test with explicit base (without docstring)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
852 error3 = _testcapi.make_exception_with_doc("_testcapi.error3",
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
853 base=error2)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
854 self.assertTrue(issubclass(error3, error2))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
855
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
856 # test with explicit base tuple
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
857 class C(object):
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
858 pass
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
859 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
860 (error3, C))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
861 self.assertTrue(issubclass(error4, error3))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
862 self.assertTrue(issubclass(error4, C))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
863 self.assertEqual(error4.__doc__, doc4)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
864
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
865 # test with explicit dictionary
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
866 error5 = _testcapi.make_exception_with_doc("_testcapi.error5", "",
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
867 error4, {'a': 1})
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
868 self.assertTrue(issubclass(error5, error4))
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
869 self.assertEqual(error5.a, 1)
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
870 self.assertEqual(error5.__doc__, "")
c1972eef3579 Merged revisions 77088 via svnmerge from
Georg Brandl <georg@python.org>
parents: 57930
diff changeset
871
65690
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
872 def test_memory_error_cleanup(self):
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
873 # Issue #5437: preallocated MemoryError instances should not keep
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
874 # traceback objects alive.
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
875 from _testcapi import raise_memoryerror
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
876 class C:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
877 pass
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
878 wr = None
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
879 def inner():
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
880 nonlocal wr
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
881 c = C()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
882 wr = weakref.ref(c)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
883 raise_memoryerror()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
884 # 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
885 try:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
886 inner()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
887 except MemoryError as e:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
888 self.assertNotEqual(wr(), None)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
889 else:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
890 self.fail("MemoryError not raised")
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
891 self.assertEqual(wr(), None)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
892
68080
614e32900ef7 Issue #10990: Prevent tests from clobbering a set trace function.
Brett Cannon <bcannon@gmail.com>
parents: 66310
diff changeset
893 @no_tracing
65690
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
894 def test_recursion_error_cleanup(self):
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
895 # 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
896 class C:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
897 pass
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
898 wr = None
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
899 def inner():
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
900 nonlocal wr
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
901 c = C()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
902 wr = weakref.ref(c)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
903 inner()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
904 # 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
905 try:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
906 inner()
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
907 except RuntimeError as e:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
908 self.assertNotEqual(wr(), None)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
909 else:
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
910 self.fail("RuntimeError not raised")
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
911 self.assertEqual(wr(), None)
c6d86439aa91 Merged revisions 85896 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 59442
diff changeset
912
72163
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
913 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
914 # 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
915 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
916 os.listdir(__file__)
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
917 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
918
385c2ec78f16 Issue #12802: the Windows error ERROR_DIRECTORY (numbered 267) is now
Antoine Pitrou <solipsis@pitrou.net>
parents: 71980
diff changeset
919
76272
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
920 class ImportErrorTests(unittest.TestCase):
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
921
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
922 def test_attributes(self):
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
923 # Setting 'name' and 'path' should not be a problem.
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
924 exc = ImportError('test')
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
925 self.assertIsNone(exc.name)
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
926 self.assertIsNone(exc.path)
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
927
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
928 exc = ImportError('test', name='somemodule')
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
929 self.assertEqual(exc.name, 'somemodule')
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
930 self.assertIsNone(exc.path)
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
931
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
932 exc = ImportError('test', path='somepath')
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
933 self.assertEqual(exc.path, 'somepath')
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
934 self.assertIsNone(exc.name)
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
935
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
936 exc = ImportError('test', path='somepath', name='somename')
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
937 self.assertEqual(exc.name, 'somename')
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
938 self.assertEqual(exc.path, 'somepath')
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
939
78737
91909962d7f5 Issue #15778: Coerce ImportError.args to a string when it isn't
Brett Cannon <brett@python.org>
parents: 78304
diff changeset
940 def test_non_str_argument(self):
91909962d7f5 Issue #15778: Coerce ImportError.args to a string when it isn't
Brett Cannon <brett@python.org>
parents: 78304
diff changeset
941 # Issue #15778
91909962d7f5 Issue #15778: Coerce ImportError.args to a string when it isn't
Brett Cannon <brett@python.org>
parents: 78304
diff changeset
942 arg = b'abc'
91909962d7f5 Issue #15778: Coerce ImportError.args to a string when it isn't
Brett Cannon <brett@python.org>
parents: 78304
diff changeset
943 exc = ImportError(arg)
91909962d7f5 Issue #15778: Coerce ImportError.args to a string when it isn't
Brett Cannon <brett@python.org>
parents: 78304
diff changeset
944 self.assertEqual(str(arg), str(exc))
76272
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
945
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
946
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
947 def test_main():
76272
6825fd9b00ed Issue #1559549: Add 'name' and 'path' attributes to ImportError.
Brett Cannon <brett@python.org>
parents: 75375
diff changeset
948 run_unittest(ExceptionTests, ImportErrorTests)
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
949
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
950 if __name__ == '__main__':
41646
6a016f3e0051 Got test_exceptions.py working.
Guido van Rossum <guido@python.org>
parents: 41643
diff changeset
951 unittest.main()