annotate Lib/test/test_with.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 59670b7b77a6
children dba99828d91c 16759f62eb22
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
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
216 def assertAfterWithManagerInvariantsWithError(self, mock_manager):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
217 self.assertTrue(mock_manager.enter_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
218 self.assertTrue(mock_manager.exit_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
219 self.assertEqual(mock_manager.exit_args[0], RuntimeError)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
220 self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
221
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
222 def assertAfterWithGeneratorInvariantsWithError(self, mock_generator):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
223 self.assertTrue(mock_generator.yielded)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
224 self.assertTrue(mock_generator.stopped)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
225
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
226
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
227 class NonexceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
228 def testInlineGeneratorSyntax(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
229 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
230 pass
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 def testUnboundGenerator(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
233 mock = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
234 with mock:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
235 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
236 self.assertAfterWithManagerInvariantsNoError(mock)
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 testInlineGeneratorBoundSyntax(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
239 with mock_contextmanager_generator() as foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
240 self.assertInWithGeneratorInvariants(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
241 # 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
242 self.assertAfterWithGeneratorInvariantsNoError(foo)
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 testInlineGeneratorBoundToExistingVariable(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
245 foo = None
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
246 with mock_contextmanager_generator() as foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
247 self.assertInWithGeneratorInvariants(foo)
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 testInlineGeneratorBoundToDottedVariable(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
251 with mock_contextmanager_generator() as self.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
252 self.assertInWithGeneratorInvariants(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
253 self.assertAfterWithGeneratorInvariantsNoError(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
254
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
255 def testBoundGenerator(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
256 mock = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
257 with mock as foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
258 self.assertInWithGeneratorInvariants(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
259 self.assertInWithManagerInvariants(mock)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
260 self.assertAfterWithGeneratorInvariantsNoError(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
261 self.assertAfterWithManagerInvariantsNoError(mock)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
262
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
263 def testNestedSingleStatements(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
264 mock_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
265 with mock_a as foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
266 mock_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
267 with mock_b as bar:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
268 self.assertInWithManagerInvariants(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
269 self.assertInWithManagerInvariants(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
270 self.assertInWithGeneratorInvariants(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
271 self.assertInWithGeneratorInvariants(bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
272 self.assertAfterWithManagerInvariantsNoError(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
273 self.assertAfterWithGeneratorInvariantsNoError(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.assertInWithGeneratorInvariants(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
276 self.assertAfterWithManagerInvariantsNoError(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
277 self.assertAfterWithGeneratorInvariantsNoError(foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
278
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
279
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
280 class NestedNonexceptionalTestCase(unittest.TestCase,
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
281 ContextmanagerAssertionMixin):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
282 def testSingleArgInlineGeneratorSyntax(self):
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
283 with Nested(mock_contextmanager_generator()):
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
284 pass
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 def testSingleArgUnbound(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
287 mock_contextmanager = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
288 mock_nested = MockNested(mock_contextmanager)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
289 with mock_nested:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
290 self.assertInWithManagerInvariants(mock_contextmanager)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
291 self.assertInWithManagerInvariants(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
292 self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
293 self.assertAfterWithManagerInvariantsNoError(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
294
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
295 def testSingleArgBoundToNonTuple(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
296 m = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
297 # 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
298 # assigned to foo.
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
299 with Nested(m) as foo:
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
300 self.assertInWithManagerInvariants(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
301 self.assertAfterWithManagerInvariantsNoError(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
302
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
303 def testSingleArgBoundToSingleElementParenthesizedList(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
304 m = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
305 # 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
306 # assigned to foo.
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
307 with Nested(m) as (foo):
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
308 self.assertInWithManagerInvariants(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
309 self.assertAfterWithManagerInvariantsNoError(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
310
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
311 def testSingleArgBoundToMultipleElementTupleError(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
312 def shouldThrowValueError():
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
313 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
314 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
315 self.assertRaises(ValueError, shouldThrowValueError)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
316
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
317 def testSingleArgUnbound(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
318 mock_contextmanager = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
319 mock_nested = MockNested(mock_contextmanager)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
320 with mock_nested:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
321 self.assertInWithManagerInvariants(mock_contextmanager)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
322 self.assertInWithManagerInvariants(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
323 self.assertAfterWithManagerInvariantsNoError(mock_contextmanager)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
324 self.assertAfterWithManagerInvariantsNoError(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
325
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
326 def testMultipleArgUnbound(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
327 m = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
328 n = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
329 o = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
330 mock_nested = MockNested(m, n, o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
331 with mock_nested:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
332 self.assertInWithManagerInvariants(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
333 self.assertInWithManagerInvariants(n)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
334 self.assertInWithManagerInvariants(o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
335 self.assertInWithManagerInvariants(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
336 self.assertAfterWithManagerInvariantsNoError(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
337 self.assertAfterWithManagerInvariantsNoError(n)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
338 self.assertAfterWithManagerInvariantsNoError(o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
339 self.assertAfterWithManagerInvariantsNoError(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
340
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
341 def testMultipleArgBound(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
342 mock_nested = MockNested(mock_contextmanager_generator(),
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
343 mock_contextmanager_generator(), mock_contextmanager_generator())
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
344 with mock_nested as (m, n, o):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
345 self.assertInWithGeneratorInvariants(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
346 self.assertInWithGeneratorInvariants(n)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
347 self.assertInWithGeneratorInvariants(o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
348 self.assertInWithManagerInvariants(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
349 self.assertAfterWithGeneratorInvariantsNoError(m)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
350 self.assertAfterWithGeneratorInvariantsNoError(n)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
351 self.assertAfterWithGeneratorInvariantsNoError(o)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
352 self.assertAfterWithManagerInvariantsNoError(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
353
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
354
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
355 class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
356 def testSingleResource(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
357 cm = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
358 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
359 with cm as self.resource:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
360 self.assertInWithManagerInvariants(cm)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
361 self.assertInWithGeneratorInvariants(self.resource)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
362 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
363 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
364 self.assertAfterWithManagerInvariantsWithError(cm)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
365 self.assertAfterWithGeneratorInvariantsWithError(self.resource)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
366
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
367 def testNestedSingleStatements(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
368 mock_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
369 mock_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
370 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
371 with mock_a as self.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
372 with mock_b as self.bar:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
373 self.assertInWithManagerInvariants(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
374 self.assertInWithManagerInvariants(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
375 self.assertInWithGeneratorInvariants(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
376 self.assertInWithGeneratorInvariants(self.bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
377 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
378 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
379 self.assertAfterWithManagerInvariantsWithError(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
380 self.assertAfterWithManagerInvariantsWithError(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
381 self.assertAfterWithGeneratorInvariantsWithError(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
382 self.assertAfterWithGeneratorInvariantsWithError(self.bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
383
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
384 def testMultipleResourcesInSingleStatement(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
385 cm_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
386 cm_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
387 mock_nested = MockNested(cm_a, cm_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
388 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
389 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
390 self.assertInWithManagerInvariants(cm_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
391 self.assertInWithManagerInvariants(cm_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
392 self.assertInWithManagerInvariants(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
393 self.assertInWithGeneratorInvariants(self.resource_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
394 self.assertInWithGeneratorInvariants(self.resource_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
395 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
396 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
397 self.assertAfterWithManagerInvariantsWithError(cm_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
398 self.assertAfterWithManagerInvariantsWithError(cm_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
399 self.assertAfterWithManagerInvariantsWithError(mock_nested)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
400 self.assertAfterWithGeneratorInvariantsWithError(self.resource_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
401 self.assertAfterWithGeneratorInvariantsWithError(self.resource_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
402
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
403 def testNestedExceptionBeforeInnerStatement(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
404 mock_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
405 mock_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
406 self.bar = None
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
407 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
408 with mock_a as self.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
409 self.assertInWithManagerInvariants(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
410 self.assertInWithGeneratorInvariants(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
411 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
412 with mock_b as self.bar:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
413 pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
414 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
415 self.assertAfterWithManagerInvariantsWithError(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
416 self.assertAfterWithGeneratorInvariantsWithError(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
417
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
418 # 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
419 self.assertEqual(self.bar, None)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
420 self.assertFalse(mock_b.enter_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
421 self.assertFalse(mock_b.exit_called)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
422 self.assertEqual(mock_b.exit_args, None)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
423
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
424 def testNestedExceptionAfterInnerStatement(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
425 mock_a = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
426 mock_b = mock_contextmanager_generator()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
427 def shouldThrow():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
428 with mock_a as self.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
429 with mock_b as self.bar:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
430 self.assertInWithManagerInvariants(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
431 self.assertInWithManagerInvariants(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
432 self.assertInWithGeneratorInvariants(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
433 self.assertInWithGeneratorInvariants(self.bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
434 self.raiseTestException()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
435 self.assertRaises(RuntimeError, shouldThrow)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
436 self.assertAfterWithManagerInvariantsWithError(mock_a)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
437 self.assertAfterWithManagerInvariantsNoError(mock_b)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
438 self.assertAfterWithGeneratorInvariantsWithError(self.foo)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
439 self.assertAfterWithGeneratorInvariantsNoError(self.bar)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
440
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
441 def testRaisedStopIteration1(self):
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
442 # 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
443 @contextmanager
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
444 def cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
445 yield
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
447 def shouldThrow():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
448 with cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
449 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
450
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
451 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
452
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
453 def testRaisedStopIteration2(self):
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
454 # 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
455 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
456 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
457 pass
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
458 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
459 pass
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
460
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
461 def shouldThrow():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
462 with cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
463 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
464
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
465 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
466
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
467 def testRaisedStopIteration3(self):
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
468 # 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
469 # From bug 1705170
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
470 @contextmanager
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
471 def cm():
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
472 yield
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
473
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
474 def shouldThrow():
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
475 with cm():
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
476 raise iter([]).next()
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
477
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
478 self.assertRaises(StopIteration, shouldThrow)
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
479
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
480 def testRaisedGeneratorExit1(self):
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
481 # 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
482 @contextmanager
37446
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
483 def cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
484 yield
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
485
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
486 def shouldThrow():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
487 with cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
488 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
489
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
490 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
491
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
492 def testRaisedGeneratorExit2(self):
43787
f474de973404 Fix issue #1705170 (backport from trunk)
Nick Coghlan <ncoghlan@gmail.com>
parents: 38101
diff changeset
493 # 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
494 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
495 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
496 pass
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
497 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
498 pass
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
499
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
500 def shouldThrow():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
501 with cm():
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
502 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
503
2cd1e5b15e05 Fix SF#1462485: StopIteration raised in body of 'with' statement suppressed
Phillip J. Eby <pje@telecommunity.com>
parents: 36926
diff changeset
504 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
505
50106
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
506 def testErrorsInBool(self):
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
507 # issue4589: __exit__ return code may raise an exception
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
508 # when looking at its truth value.
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
509
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
510 class cm(object):
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
511 def __init__(self, bool_conversion):
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
512 class Bool:
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
513 def __nonzero__(self):
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
514 return bool_conversion()
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
515 self.exit_result = Bool()
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
516 def __enter__(self):
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
517 return 3
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
518 def __exit__(self, a, b, c):
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
519 return self.exit_result
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
520
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
521 def trueAsBool():
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
522 with cm(lambda: True):
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
523 self.fail("Should NOT see this")
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
524 trueAsBool()
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
525
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
526 def falseAsBool():
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
527 with cm(lambda: False):
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
528 self.fail("Should raise")
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
529 self.assertRaises(AssertionError, falseAsBool)
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
530
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
531 def failAsBool():
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
532 with cm(lambda: 1//0):
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
533 self.fail("Should NOT see this")
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
534 self.assertRaises(ZeroDivisionError, failAsBool)
59670b7b77a6 Merged revisions 67688 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46613
diff changeset
535
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
536
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
537 class NonLocalFlowControlTestCase(unittest.TestCase):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
538
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
539 def testWithBreak(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
540 counter = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
541 while True:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
542 counter += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
543 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
544 counter += 10
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
545 break
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
546 counter += 100 # Not reached
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
547 self.assertEqual(counter, 11)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
548
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
549 def testWithContinue(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
550 counter = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
551 while True:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
552 counter += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
553 if counter > 2:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
554 break
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
555 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
556 counter += 10
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
557 continue
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
558 counter += 100 # Not reached
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
559 self.assertEqual(counter, 12)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
560
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
561 def testWithReturn(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
562 def foo():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
563 counter = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
564 while True:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
565 counter += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
566 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
567 counter += 10
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
568 return counter
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
569 counter += 100 # Not reached
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
570 self.assertEqual(foo(), 11)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
571
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
572 def testWithYield(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
573 def gen():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
574 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
575 yield 12
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
576 yield 13
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
577 x = list(gen())
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
578 self.assertEqual(x, [12, 13])
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
579
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
580 def testWithRaise(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
581 counter = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
582 try:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
583 counter += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
584 with mock_contextmanager_generator():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
585 counter += 10
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
586 raise RuntimeError
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
587 counter += 100 # Not reached
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
588 except RuntimeError:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
589 self.assertEqual(counter, 11)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
590 else:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
591 self.fail("Didn't raise RuntimeError")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
592
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
593
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
594 class AssignmentTargetTestCase(unittest.TestCase):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
595
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
596 def testSingleComplexTarget(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
597 targets = {1: [0, 1, 2]}
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
598 with mock_contextmanager_generator() as targets[1][0]:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
599 self.assertEqual(targets.keys(), [1])
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
600 self.assertEqual(targets[1][0].__class__, MockResource)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
601 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
602 self.assertEqual(targets.keys(), [1])
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
603 self.assertEqual(targets[1][1].__class__, MockResource)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
604 with mock_contextmanager_generator() as targets[2]:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
605 keys = targets.keys()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
606 keys.sort()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
607 self.assertEqual(keys, [1, 2])
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
608 class C: pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
609 blah = C()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
610 with mock_contextmanager_generator() as blah.foo:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
611 self.assertEqual(hasattr(blah, "foo"), True)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
612
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
613 def testMultipleComplexTargets(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
614 class C:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
615 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
616 def __exit__(self, t, v, tb): pass
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
617 targets = {1: [0, 1, 2]}
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
618 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
619 self.assertEqual(targets, {1: [1, 2, 3]})
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
620 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
621 self.assertEqual(targets, {1: [3, 2, 1]})
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
622 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
623 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
624 class B: pass
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
625 blah = B()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
626 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
627 self.assertEqual(blah.one, 1)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
628 self.assertEqual(blah.two, 2)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
629 self.assertEqual(blah.three, 3)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
630
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
631
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
632 class ExitSwallowsExceptionTestCase(unittest.TestCase):
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
633
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
634 def testExitTrueSwallowsException(self):
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
635 class AfricanSwallow:
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
636 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
637 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
638 try:
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
639 with AfricanSwallow():
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
640 1/0
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
641 except ZeroDivisionError:
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
642 self.fail("ZeroDivisionError should have been swallowed")
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
643
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
644 def testExitFalseDoesntSwallowException(self):
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
645 class EuropeanSwallow:
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
646 def __enter__(self): pass
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
647 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
648 try:
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
649 with EuropeanSwallow():
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
650 1/0
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
651 except ZeroDivisionError:
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
652 pass
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
653 else:
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36737
diff changeset
654 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
655
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
656
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
657 def test_main():
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
658 run_unittest(FailureTestCase, NonexceptionalTestCase,
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
659 NestedNonexceptionalTestCase, ExceptionalTestCase,
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
660 NonLocalFlowControlTestCase,
36737
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
661 AssignmentTargetTestCase,
fa92fe15251a Updates to the with-statement:
Guido van Rossum <guido@python.org>
parents: 36726
diff changeset
662 ExitSwallowsExceptionTestCase)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
663
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
664
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
665 if __name__ == '__main__':
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
666 test_main()