annotate Lib/test/test_with.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
1 #!/usr/bin/env python
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
2
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
3 """Unit tests for the with statement specified in PEP 343."""
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
4
36726
bbf16fd024df from __future__ import with_statement addon for 'with', mostly written by
Thomas Wouters <thomas@python.org>
parents: 36723
diff changeset
5
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
6 __author__ = "Mike Bland"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
7 __email__ = "mbland at acm dot org"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
8
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
9 import sys
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
10 import unittest
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
11 from collections import deque
38101
6bc1e83a97a6 Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents: 38084
diff changeset
12 from contextlib import GeneratorContextManager, contextmanager
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
13 from test.test_support import run_unittest
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
14
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
15
38101
6bc1e83a97a6 Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents: 38084
diff changeset
16 class MockContextManager(GeneratorContextManager):
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
17 def __init__(self, gen):
38101
6bc1e83a97a6 Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents: 38084
diff changeset
18 GeneratorContextManager.__init__(self, gen)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
19 self.enter_called = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
20 self.exit_called = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
21 self.exit_args = None
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
22
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
23 def __enter__(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
24 self.enter_called = True
38101
6bc1e83a97a6 Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents: 38084
diff changeset
25 return GeneratorContextManager.__enter__(self)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
26
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
27 def __exit__(self, type, value, traceback):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
28 self.exit_called = True
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
29 self.exit_args = (type, value, traceback)
38101
6bc1e83a97a6 Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents: 38084
diff changeset
30 return GeneratorContextManager.__exit__(self, type,
6bc1e83a97a6 Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents: 38084
diff changeset
31 value, traceback)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
32
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
33
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
34 def mock_contextmanager(func):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
35 def helper(*args, **kwds):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
36 return MockContextManager(func(*args, **kwds))
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
37 return helper
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
38
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
39
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
40 class MockResource(object):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
41 def __init__(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
42 self.yielded = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
43 self.stopped = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
44
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
45
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
46 @mock_contextmanager
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
47 def mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
48 mock = MockResource()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
49 try:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
50 mock.yielded = True
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
51 yield mock
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
52 finally:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
53 mock.stopped = True
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
54
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
55
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
56 class Nested(object):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
57
38084
a812d0fae74a Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents: 37984
diff changeset
58 def __init__(self, *managers):
a812d0fae74a Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents: 37984
diff changeset
59 self.managers = managers
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
60 self.entered = None
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
61
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
62 def __enter__(self):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
63 if self.entered is not None:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
64 raise RuntimeError("Context is not reentrant")
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
65 self.entered = deque()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
66 vars = []
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
67 try:
38084
a812d0fae74a Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents: 37984
diff changeset
68 for mgr in self.managers:
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
69 vars.append(mgr.__enter__())
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
70 self.entered.appendleft(mgr)
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
71 except:
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
72 if not self.__exit__(*sys.exc_info()):
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
73 raise
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
74 return vars
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
75
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
76 def __exit__(self, *exc_info):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
77 # Behave like nested with statements
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
78 # first in, last out
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
79 # New exceptions override old ones
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
80 ex = exc_info
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
81 for mgr in self.entered:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
82 try:
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
83 if mgr.__exit__(*ex):
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
84 ex = (None, None, None)
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
85 except:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
86 ex = sys.exc_info()
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
87 self.entered = None
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
88 if ex is not exc_info:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
89 raise ex[0], ex[1], ex[2]
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
90
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
91
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
92 class MockNested(Nested):
38084
a812d0fae74a Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents: 37984
diff changeset
93 def __init__(self, *managers):
a812d0fae74a Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents: 37984
diff changeset
94 Nested.__init__(self, *managers)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
95 self.enter_called = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
96 self.exit_called = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
97 self.exit_args = None
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
98
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
99 def __enter__(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
100 self.enter_called = True
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
101 return Nested.__enter__(self)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
102
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
103 def __exit__(self, *exc_info):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
104 self.exit_called = True
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
105 self.exit_args = exc_info
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
106 return Nested.__exit__(self, *exc_info)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
107
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
108
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
109 class FailureTestCase(unittest.TestCase):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
110 def testNameError(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
111 def fooNotDeclared():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
112 with foo: pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
113 self.assertRaises(NameError, fooNotDeclared)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
114
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
115 def testEnterAttributeError(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
116 class LacksEnter(object):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
117 def __exit__(self, type, value, traceback):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
118 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
119
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
120 def fooLacksEnter():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
121 foo = LacksEnter()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
122 with foo: pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
123 self.assertRaises(AttributeError, fooLacksEnter)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
124
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
125 def testExitAttributeError(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
126 class LacksExit(object):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
127 def __enter__(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
128 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
129
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
130 def fooLacksExit():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
131 foo = LacksExit()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
132 with foo: pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
133 self.assertRaises(AttributeError, fooLacksExit)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
134
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
135 def assertRaisesSyntaxError(self, codestr):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
136 def shouldRaiseSyntaxError(s):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
137 compile(s, '', 'single')
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
138 self.assertRaises(SyntaxError, shouldRaiseSyntaxError, codestr)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
139
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
140 def testAssignmentToNoneError(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
141 self.assertRaisesSyntaxError('with mock as None:\n pass')
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
142 self.assertRaisesSyntaxError(
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
143 'with mock as (None):\n'
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
144 ' pass')
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
145
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
146 def testAssignmentToEmptyTupleError(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
147 self.assertRaisesSyntaxError(
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
148 'with mock as ():\n'
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
149 ' pass')
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
150
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
151 def testAssignmentToTupleOnlyContainingNoneError(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
152 self.assertRaisesSyntaxError('with mock as None,:\n pass')
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
153 self.assertRaisesSyntaxError(
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
154 'with mock as (None,):\n'
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
155 ' pass')
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
156
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
157 def testAssignmentToTupleContainingNoneError(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
158 self.assertRaisesSyntaxError(
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
159 'with mock as (foo, None, bar):\n'
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
160 ' pass')
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
161
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
162 def testEnterThrows(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
163 class EnterThrows(object):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
164 def __enter__(self):
38084
a812d0fae74a Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents: 37984
diff changeset
165 raise RuntimeError("Enter threw")
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
166 def __exit__(self, *args):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
167 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
168
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
169 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
170 ct = EnterThrows()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
171 self.foo = None
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
172 with ct as self.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
173 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
174 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
175 self.assertEqual(self.foo, None)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
176
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
177 def testExitThrows(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
178 class ExitThrows(object):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
179 def __enter__(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
180 return
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
181 def __exit__(self, *args):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
182 raise RuntimeError(42)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
183 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
184 with ExitThrows():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
185 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
186 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
187
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
188 class ContextmanagerAssertionMixin(object):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
189 TEST_EXCEPTION = RuntimeError("test exception")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
190
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
191 def assertInWithManagerInvariants(self, mock_manager):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
192 self.assertTrue(mock_manager.enter_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
193 self.assertFalse(mock_manager.exit_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
194 self.assertEqual(mock_manager.exit_args, None)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
195
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
196 def assertAfterWithManagerInvariants(self, mock_manager, exit_args):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
197 self.assertTrue(mock_manager.enter_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
198 self.assertTrue(mock_manager.exit_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
199 self.assertEqual(mock_manager.exit_args, exit_args)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
200
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
201 def assertAfterWithManagerInvariantsNoError(self, mock_manager):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
202 self.assertAfterWithManagerInvariants(mock_manager,
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
203 (None, None, None))
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
204
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
205 def assertInWithGeneratorInvariants(self, mock_generator):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
206 self.assertTrue(mock_generator.yielded)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
207 self.assertFalse(mock_generator.stopped)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
208
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
209 def assertAfterWithGeneratorInvariantsNoError(self, mock_generator):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
210 self.assertTrue(mock_generator.yielded)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
211 self.assertTrue(mock_generator.stopped)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
212
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
213 def raiseTestException(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
214 raise self.TEST_EXCEPTION
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
215
58704
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
216 def assertAfterWithManagerInvariantsWithError(self, mock_manager,
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
217 exc_type=None):
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
218 self.assertTrue(mock_manager.enter_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
219 self.assertTrue(mock_manager.exit_called)
58704
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
220 if exc_type is None:
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
221 self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
222 exc_type = type(self.TEST_EXCEPTION)
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
223 self.assertEqual(mock_manager.exit_args[0], exc_type)
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
224 # Test the __exit__ arguments. Issue #7853
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
225 self.assertIsInstance(mock_manager.exit_args[1], exc_type)
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
226 self.assertIsNot(mock_manager.exit_args[2], None)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
227
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
228 def assertAfterWithGeneratorInvariantsWithError(self, mock_generator):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
229 self.assertTrue(mock_generator.yielded)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
230 self.assertTrue(mock_generator.stopped)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
231
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
232
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
233 class NonexceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
234 def testInlineGeneratorSyntax(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
235 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
236 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
237
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
238 def testUnboundGenerator(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
239 mock = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
240 with mock:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
241 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
242 self.assertAfterWithManagerInvariantsNoError(mock)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
243
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
244 def testInlineGeneratorBoundSyntax(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
245 with mock_contextmanager_generator() as foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
246 self.assertInWithGeneratorInvariants(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
247 # FIXME: In the future, we'll try to keep the bound names from leaking
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
248 self.assertAfterWithGeneratorInvariantsNoError(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
249
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
250 def testInlineGeneratorBoundToExistingVariable(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
251 foo = None
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
252 with mock_contextmanager_generator() as foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
253 self.assertInWithGeneratorInvariants(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
254 self.assertAfterWithGeneratorInvariantsNoError(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
255
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
256 def testInlineGeneratorBoundToDottedVariable(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
257 with mock_contextmanager_generator() as self.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
258 self.assertInWithGeneratorInvariants(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
259 self.assertAfterWithGeneratorInvariantsNoError(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
260
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
261 def testBoundGenerator(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
262 mock = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
263 with mock as foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
264 self.assertInWithGeneratorInvariants(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
265 self.assertInWithManagerInvariants(mock)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
266 self.assertAfterWithGeneratorInvariantsNoError(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
267 self.assertAfterWithManagerInvariantsNoError(mock)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
268
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
269 def testNestedSingleStatements(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
270 mock_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
271 with mock_a as foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
272 mock_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
273 with mock_b as bar:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
274 self.assertInWithManagerInvariants(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
275 self.assertInWithManagerInvariants(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
276 self.assertInWithGeneratorInvariants(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
277 self.assertInWithGeneratorInvariants(bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
278 self.assertAfterWithManagerInvariantsNoError(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
279 self.assertAfterWithGeneratorInvariantsNoError(bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
280 self.assertInWithManagerInvariants(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
281 self.assertInWithGeneratorInvariants(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
282 self.assertAfterWithManagerInvariantsNoError(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
283 self.assertAfterWithGeneratorInvariantsNoError(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
284
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
285
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
286 class NestedNonexceptionalTestCase(unittest.TestCase,
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
287 ContextmanagerAssertionMixin):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
288 def testSingleArgInlineGeneratorSyntax(self):
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
289 with Nested(mock_contextmanager_generator()):
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
290 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
291
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
292 def testSingleArgBoundToNonTuple(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
293 m = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
294 # This will bind all the arguments to nested() into a single list
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
295 # assigned to foo.
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
296 with Nested(m) as foo:
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
297 self.assertInWithManagerInvariants(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
298 self.assertAfterWithManagerInvariantsNoError(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
299
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
300 def testSingleArgBoundToSingleElementParenthesizedList(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
301 m = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
302 # This will bind all the arguments to nested() into a single list
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
303 # assigned to foo.
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
304 with Nested(m) as (foo):
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
305 self.assertInWithManagerInvariants(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
306 self.assertAfterWithManagerInvariantsNoError(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
307
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
308 def testSingleArgBoundToMultipleElementTupleError(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
309 def shouldThrowValueError():
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
310 with Nested(mock_contextmanager_generator()) as (foo, bar):
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
311 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
312 self.assertRaises(ValueError, shouldThrowValueError)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
313
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
314 def testSingleArgUnbound(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
315 mock_contextmanager = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
316 mock_nested = MockNested(mock_contextmanager)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
317 with mock_nested:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
318 self.assertInWithManagerInvariants(mock_contextmanager)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
319 self.assertInWithManagerInvariants(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
320 self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
321 self.assertAfterWithManagerInvariantsNoError(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
322
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
323 def testMultipleArgUnbound(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
324 m = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
325 n = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
326 o = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
327 mock_nested = MockNested(m, n, o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
328 with mock_nested:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
329 self.assertInWithManagerInvariants(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
330 self.assertInWithManagerInvariants(n)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
331 self.assertInWithManagerInvariants(o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
332 self.assertInWithManagerInvariants(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
333 self.assertAfterWithManagerInvariantsNoError(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
334 self.assertAfterWithManagerInvariantsNoError(n)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
335 self.assertAfterWithManagerInvariantsNoError(o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
336 self.assertAfterWithManagerInvariantsNoError(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
337
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
338 def testMultipleArgBound(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
339 mock_nested = MockNested(mock_contextmanager_generator(),
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
340 mock_contextmanager_generator(), mock_contextmanager_generator())
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
341 with mock_nested as (m, n, o):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
342 self.assertInWithGeneratorInvariants(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
343 self.assertInWithGeneratorInvariants(n)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
344 self.assertInWithGeneratorInvariants(o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
345 self.assertInWithManagerInvariants(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
346 self.assertAfterWithGeneratorInvariantsNoError(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
347 self.assertAfterWithGeneratorInvariantsNoError(n)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
348 self.assertAfterWithGeneratorInvariantsNoError(o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
349 self.assertAfterWithManagerInvariantsNoError(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
350
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
351
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
352 class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
353 def testSingleResource(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
354 cm = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
355 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
356 with cm as self.resource:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
357 self.assertInWithManagerInvariants(cm)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
358 self.assertInWithGeneratorInvariants(self.resource)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
359 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
360 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
361 self.assertAfterWithManagerInvariantsWithError(cm)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
362 self.assertAfterWithGeneratorInvariantsWithError(self.resource)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
363
58704
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
364 def testExceptionNormalized(self):
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
365 cm = mock_contextmanager_generator()
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
366 def shouldThrow():
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
367 with cm as self.resource:
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
368 # Note this relies on the fact that 1 // 0 produces an exception
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
369 # that is not normalized immediately.
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
370 1 // 0
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
371 self.assertRaises(ZeroDivisionError, shouldThrow)
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
372 self.assertAfterWithManagerInvariantsWithError(cm, ZeroDivisionError)
de5fa925dfea add a test for #7853; the exception must be normalized for with
Benjamin Peterson <benjamin@python.org>
parents: 58669
diff changeset
373
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
374 def testNestedSingleStatements(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
375 mock_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
376 mock_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
377 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
378 with mock_a as self.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
379 with mock_b as self.bar:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
380 self.assertInWithManagerInvariants(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
381 self.assertInWithManagerInvariants(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
382 self.assertInWithGeneratorInvariants(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
383 self.assertInWithGeneratorInvariants(self.bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
384 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
385 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
386 self.assertAfterWithManagerInvariantsWithError(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
387 self.assertAfterWithManagerInvariantsWithError(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
388 self.assertAfterWithGeneratorInvariantsWithError(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
389 self.assertAfterWithGeneratorInvariantsWithError(self.bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
390
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
391 def testMultipleResourcesInSingleStatement(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
392 cm_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
393 cm_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
394 mock_nested = MockNested(cm_a, cm_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
395 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
396 with mock_nested as (self.resource_a, self.resource_b):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
397 self.assertInWithManagerInvariants(cm_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
398 self.assertInWithManagerInvariants(cm_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
399 self.assertInWithManagerInvariants(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
400 self.assertInWithGeneratorInvariants(self.resource_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
401 self.assertInWithGeneratorInvariants(self.resource_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
402 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
403 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
404 self.assertAfterWithManagerInvariantsWithError(cm_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
405 self.assertAfterWithManagerInvariantsWithError(cm_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
406 self.assertAfterWithManagerInvariantsWithError(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
407 self.assertAfterWithGeneratorInvariantsWithError(self.resource_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
408 self.assertAfterWithGeneratorInvariantsWithError(self.resource_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
409
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
410 def testNestedExceptionBeforeInnerStatement(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
411 mock_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
412 mock_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
413 self.bar = None
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
414 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
415 with mock_a as self.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
416 self.assertInWithManagerInvariants(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
417 self.assertInWithGeneratorInvariants(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
418 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
419 with mock_b as self.bar:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
420 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
421 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
422 self.assertAfterWithManagerInvariantsWithError(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
423 self.assertAfterWithGeneratorInvariantsWithError(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
424
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
425 # The inner statement stuff should never have been touched
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
426 self.assertEqual(self.bar, None)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
427 self.assertFalse(mock_b.enter_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
428 self.assertFalse(mock_b.exit_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
429 self.assertEqual(mock_b.exit_args, None)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
430
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
431 def testNestedExceptionAfterInnerStatement(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
432 mock_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
433 mock_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
434 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
435 with mock_a as self.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
436 with mock_b as self.bar:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
437 self.assertInWithManagerInvariants(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
438 self.assertInWithManagerInvariants(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
439 self.assertInWithGeneratorInvariants(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
440 self.assertInWithGeneratorInvariants(self.bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
441 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
442 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
443 self.assertAfterWithManagerInvariantsWithError(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
444 self.assertAfterWithManagerInvariantsNoError(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
445 self.assertAfterWithGeneratorInvariantsWithError(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
446 self.assertAfterWithGeneratorInvariantsNoError(self.bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
447
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
448 def testRaisedStopIteration1(self):
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
449 # From bug 1462485
38101
6bc1e83a97a6 Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents: 38084
diff changeset
450 @contextmanager
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
451 def cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
452 yield
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
453
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
454 def shouldThrow():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
455 with cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
456 raise StopIteration("from with")
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
457
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
458 self.assertRaises(StopIteration, shouldThrow)
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
459
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
460 def testRaisedStopIteration2(self):
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
461 # From bug 1462485
38084
a812d0fae74a Get rid of __context__, per the latest changes to PEP 343 and python-dev
Guido van Rossum <guido@python.org>
parents: 37984
diff changeset
462 class cm(object):
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
463 def __enter__(self):
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
464 pass
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
465 def __exit__(self, type, value, traceback):
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
466 pass
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
467
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
468 def shouldThrow():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
469 with cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
470 raise StopIteration("from with")
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
471
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
472 self.assertRaises(StopIteration, shouldThrow)
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
473
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
474 def testRaisedStopIteration3(self):
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
475 # Another variant where the exception hasn't been instantiated
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
476 # From bug 1705170
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
477 @contextmanager
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
478 def cm():
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
479 yield
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
480
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
481 def shouldThrow():
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
482 with cm():
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
483 raise iter([]).next()
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
484
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
485 self.assertRaises(StopIteration, shouldThrow)
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
486
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
487 def testRaisedGeneratorExit1(self):
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
488 # From bug 1462485
38101
6bc1e83a97a6 Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Nick Coghlan <ncoghlan@gmail.com>
parents: 38084
diff changeset
489 @contextmanager
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
490 def cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
491 yield
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
492
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
493 def shouldThrow():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
494 with cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
495 raise GeneratorExit("from with")
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
496
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
497 self.assertRaises(GeneratorExit, shouldThrow)
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
498
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
499 def testRaisedGeneratorExit2(self):
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
500 # From bug 1462485
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
501 class cm (object):
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
502 def __enter__(self):
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
503 pass
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
504 def __exit__(self, type, value, traceback):
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
505 pass
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
506
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
507 def shouldThrow():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
508 with cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
509 raise GeneratorExit("from with")
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
510
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
511 self.assertRaises(GeneratorExit, shouldThrow)
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
512
50099
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
513 def testErrorsInBool(self):
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
514 # issue4589: __exit__ return code may raise an exception
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
515 # when looking at its truth value.
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
516
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
517 class cm(object):
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
518 def __init__(self, bool_conversion):
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
519 class Bool:
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
520 def __nonzero__(self):
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
521 return bool_conversion()
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
522 self.exit_result = Bool()
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
523 def __enter__(self):
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
524 return 3
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
525 def __exit__(self, a, b, c):
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
526 return self.exit_result
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
527
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
528 def trueAsBool():
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
529 with cm(lambda: True):
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
530 self.fail("Should NOT see this")
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
531 trueAsBool()
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
532
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
533 def falseAsBool():
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
534 with cm(lambda: False):
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
535 self.fail("Should raise")
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
536 self.assertRaises(AssertionError, falseAsBool)
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
537
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
538 def failAsBool():
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50106
diff changeset
539 with cm(lambda: 1 // 0):
50099
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
540 self.fail("Should NOT see this")
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
541 self.assertRaises(ZeroDivisionError, failAsBool)
694d04ed2b43 Backported issue #4589 to Python 2.5.3: Propagated an exception thrown by a
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 49296
diff changeset
542
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
543
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
544 class NonLocalFlowControlTestCase(unittest.TestCase):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
545
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
546 def testWithBreak(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
547 counter = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
548 while True:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
549 counter += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
550 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
551 counter += 10
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
552 break
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
553 counter += 100 # Not reached
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
554 self.assertEqual(counter, 11)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
555
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
556 def testWithContinue(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
557 counter = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
558 while True:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
559 counter += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
560 if counter > 2:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
561 break
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
562 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
563 counter += 10
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
564 continue
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
565 counter += 100 # Not reached
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
566 self.assertEqual(counter, 12)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
567
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
568 def testWithReturn(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
569 def foo():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
570 counter = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
571 while True:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
572 counter += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
573 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
574 counter += 10
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
575 return counter
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
576 counter += 100 # Not reached
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
577 self.assertEqual(foo(), 11)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
578
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
579 def testWithYield(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
580 def gen():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
581 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
582 yield 12
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
583 yield 13
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
584 x = list(gen())
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
585 self.assertEqual(x, [12, 13])
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
586
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
587 def testWithRaise(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
588 counter = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
589 try:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
590 counter += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
591 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
592 counter += 10
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
593 raise RuntimeError
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
594 counter += 100 # Not reached
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
595 except RuntimeError:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
596 self.assertEqual(counter, 11)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
597 else:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
598 self.fail("Didn't raise RuntimeError")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
599
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
600
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
601 class AssignmentTargetTestCase(unittest.TestCase):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
602
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
603 def testSingleComplexTarget(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
604 targets = {1: [0, 1, 2]}
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
605 with mock_contextmanager_generator() as targets[1][0]:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
606 self.assertEqual(targets.keys(), [1])
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
607 self.assertEqual(targets[1][0].__class__, MockResource)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
608 with mock_contextmanager_generator() as targets.values()[0][1]:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
609 self.assertEqual(targets.keys(), [1])
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
610 self.assertEqual(targets[1][1].__class__, MockResource)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
611 with mock_contextmanager_generator() as targets[2]:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
612 keys = targets.keys()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
613 keys.sort()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
614 self.assertEqual(keys, [1, 2])
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
615 class C: pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
616 blah = C()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
617 with mock_contextmanager_generator() as blah.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
618 self.assertEqual(hasattr(blah, "foo"), True)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
619
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
620 def testMultipleComplexTargets(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
621 class C:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
622 def __enter__(self): return 1, 2, 3
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
623 def __exit__(self, t, v, tb): pass
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
624 targets = {1: [0, 1, 2]}
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
625 with C() as (targets[1][0], targets[1][1], targets[1][2]):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
626 self.assertEqual(targets, {1: [1, 2, 3]})
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
627 with C() as (targets.values()[0][2], targets.values()[0][1], targets.values()[0][0]):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
628 self.assertEqual(targets, {1: [3, 2, 1]})
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
629 with C() as (targets[1], targets[2], targets[3]):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
630 self.assertEqual(targets, {1: 1, 2: 2, 3: 3})
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
631 class B: pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
632 blah = B()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
633 with C() as (blah.one, blah.two, blah.three):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
634 self.assertEqual(blah.one, 1)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
635 self.assertEqual(blah.two, 2)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
636 self.assertEqual(blah.three, 3)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
637
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
638
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
639 class ExitSwallowsExceptionTestCase(unittest.TestCase):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
640
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
641 def testExitTrueSwallowsException(self):
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
642 class AfricanSwallow:
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
643 def __enter__(self): pass
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
644 def __exit__(self, t, v, tb): return True
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
645 try:
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
646 with AfricanSwallow():
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50106
diff changeset
647 1 // 0
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
648 except ZeroDivisionError:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
649 self.fail("ZeroDivisionError should have been swallowed")
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
650
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
651 def testExitFalseDoesntSwallowException(self):
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
652 class EuropeanSwallow:
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
653 def __enter__(self): pass
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
654 def __exit__(self, t, v, tb): return False
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
655 try:
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
656 with EuropeanSwallow():
63556
dba99828d91c Merged revisions 77942,79023 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 50106
diff changeset
657 1 // 0
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
658 except ZeroDivisionError:
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
659 pass
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
660 else:
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
661 self.fail("ZeroDivisionError should have been raised")
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
662
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
663
54337
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
664 class NestedWith(unittest.TestCase):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
665
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
666 class Dummy(object):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
667 def __init__(self, value=None, gobble=False):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
668 if value is None:
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
669 value = self
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
670 self.value = value
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
671 self.gobble = gobble
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
672 self.enter_called = False
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
673 self.exit_called = False
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
674
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
675 def __enter__(self):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
676 self.enter_called = True
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
677 return self.value
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
678
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
679 def __exit__(self, *exc_info):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
680 self.exit_called = True
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
681 self.exc_info = exc_info
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
682 if self.gobble:
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
683 return True
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
684
54569
843178cf0899 Better name for "Ctor".
Georg Brandl <georg@python.org>
parents: 54337
diff changeset
685 class InitRaises(object):
54337
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
686 def __init__(self): raise RuntimeError()
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
687
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
688 class EnterRaises(object):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
689 def __enter__(self): raise RuntimeError()
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
690 def __exit__(self, *exc_info): pass
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
691
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
692 class ExitRaises(object):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
693 def __enter__(self): pass
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
694 def __exit__(self, *exc_info): raise RuntimeError()
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
695
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
696 def testNoExceptions(self):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
697 with self.Dummy() as a, self.Dummy() as b:
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
698 self.assertTrue(a.enter_called)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
699 self.assertTrue(b.enter_called)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
700 self.assertTrue(a.exit_called)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
701 self.assertTrue(b.exit_called)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
702
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
703 def testExceptionInExprList(self):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
704 try:
54569
843178cf0899 Better name for "Ctor".
Georg Brandl <georg@python.org>
parents: 54337
diff changeset
705 with self.Dummy() as a, self.InitRaises():
54337
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
706 pass
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
707 except:
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
708 pass
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
709 self.assertTrue(a.enter_called)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
710 self.assertTrue(a.exit_called)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
711
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
712 def testExceptionInEnter(self):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
713 try:
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
714 with self.Dummy() as a, self.EnterRaises():
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
715 self.fail('body of bad with executed')
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
716 except RuntimeError:
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
717 pass
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
718 else:
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
719 self.fail('RuntimeError not reraised')
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
720 self.assertTrue(a.enter_called)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
721 self.assertTrue(a.exit_called)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
722
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
723 def testExceptionInExit(self):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
724 body_executed = False
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
725 with self.Dummy(gobble=True) as a, self.ExitRaises():
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
726 body_executed = True
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
727 self.assertTrue(a.enter_called)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
728 self.assertTrue(a.exit_called)
54799
6dd507b952cf add missing assertion #6313
Benjamin Peterson <benjamin@python.org>
parents: 54798
diff changeset
729 self.assertTrue(body_executed)
54337
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
730 self.assertNotEqual(a.exc_info[0], None)
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
731
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
732 def testEnterReturnsTuple(self):
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
733 with self.Dummy(value=(1,2)) as (a1, a2), \
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
734 self.Dummy(value=(10, 20)) as (b1, b2):
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58707
diff changeset
735 self.assertEqual(1, a1)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58707
diff changeset
736 self.assertEqual(2, a2)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58707
diff changeset
737 self.assertEqual(10, b1)
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58707
diff changeset
738 self.assertEqual(20, b2)
54337
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
739
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
740 def test_main():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
741 run_unittest(FailureTestCase, NonexceptionalTestCase,
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
742 NestedNonexceptionalTestCase, ExceptionalTestCase,
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
743 NonLocalFlowControlTestCase,
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
744 AssignmentTargetTestCase,
54337
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
745 ExitSwallowsExceptionTestCase,
16759f62eb22 Allow multiple context managers in one with statement, as proposed
Georg Brandl <georg@python.org>
parents: 50106
diff changeset
746 NestedWith)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
747
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
748
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
749 if __name__ == '__main__':
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
750 test_main()