annotate Lib/test/test_exceptions.py @ 87690:66c1c9f32567 default tip

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