@@ -150,7 +150,7 @@ def testFindAvailable(self):
150150 else :
151151 self .fail ("got a connection to an available port: " + str (portnum ))
152152
153- class contextlib (unittest .TestCase ):
153+ class test_contextlib (unittest .TestCase ):
154154 def testNoCM (self ):
155155 with NoCM as foo :
156156 pass
@@ -161,139 +161,6 @@ def testNoCM(self):
161161 pass
162162 self .failUnlessEqual (foo , None )
163163
164- def testNested (self ):
165- class SomeExc (Exception ):
166- pass
167- @contextmanager
168- def just_one ():
169- yield 1
170- @contextmanager
171- def just_two ():
172- try :
173- yield 2
174- except :
175- print ("wtf" )
176- @contextmanager
177- def raise_exc ():
178- raise SomeExc ("foo" )
179- yield 1
180- @contextmanager
181- def finally_exc ():
182- try :
183- yield 1
184- finally :
185- raise SomeExc ("foo" )
186- @contextmanager
187- def suppress ():
188- try :
189- yield None
190- except SomeExc :
191- pass
192- @contextmanager
193- def expect_and_raise (exc , rexc ):
194- try :
195- yield None
196- except exc :
197- raise rexc ("bleh" )
198-
199- with Nested () as foo :
200- pass
201- self .failUnlessEqual (foo , ())
202- try :
203- with Nested () as foo :
204- self .failUnlessEqual (foo , ())
205- raise SomeExc ("bar" )
206- except SomeExc :
207- pass
208- else :
209- self .fail ("empty Nested did not raise exception" )
210-
211- with Nested (just_one ()) as foo :
212- pass
213- self .failUnlessEqual (foo , (1 ,))
214-
215- with Nested (just_one (), just_one ()) as foo :
216- pass
217- self .failUnlessEqual (foo , (1 ,1 ))
218-
219- # NoCM won't raise a RuntimeError on re-use.
220- N = Nested (NoCM )
221- with N :
222- pass
223- try :
224- with N :
225- pass
226- except RuntimeError :
227- pass
228- else :
229- self .fail ("exhausted Nested() failed to raise RuntimeError" )
230-
231- # unsuppressed exceptions on entry
232- try :
233- with Nested (raise_exc ()):
234- pass
235- except SomeExc :
236- pass
237- else :
238- self .fail ("nested didn't raise SomeExc" )
239- try :
240- with Nested (just_one (), raise_exc ()):
241- pass
242- except SomeExc :
243- pass
244- else :
245- self .fail ("nested didn't raise SomeExc" )
246-
247- # suppressed SomeExc during entry--disallowed.
248- try :
249- with Nested (suppress (), raise_exc ()):
250- pass
251- except RuntimeError :
252- pass
253- else :
254- self .fail ("nested didn't raise RuntimeError on suppressed partial entry" )
255-
256- # suppress should stop it, and that's okay because we're already
257- # inside the block.
258- with Nested (suppress (), finally_exc ()):
259- raise SomeExc ("foo" )
260-
261- # This test case validates that the context is being
262- # properly set.
263- class ThisExc (Exception ):
264- pass
265- try :
266- with Nested (expect_and_raise (ThisExc , SomeExc )):
267- raise ThisExc ("FOO" )
268- except SomeExc as e :
269- self .failUnlessEqual (type (e .__context__ ), ThisExc )
270- else :
271- self .fail ("failed to raise exception" )
272-
273- class ThatExc (Exception ):
274- pass
275- try :
276- with Nested (expect_and_raise (ThisExc , SomeExc ), expect_and_raise (ThatExc , ThisExc )):
277- raise ThatExc ("BAFOON" )
278- except SomeExc as e :
279- # ThatExc -> ThisExc -> SomeExc
280- self .failUnlessEqual (type (e .__cause__ ), ThisExc )
281- self .failUnlessEqual (type (e .__cause__ .__cause__ ), ThatExc )
282-
283- try :
284- with Nested (just_one ()) as foo :
285- self .failUnlessEqual (foo , (1 ,))
286- raise SomeExc ("inside the block" )
287- except SomeExc as e :
288- pass
289- else :
290- self .fail ("Nested didn't pass up exception raised in block" )
291-
292- # Slightly more complex suppression.
293- with Nested (just_two (), suppress (), expect_and_raise (ThisExc , SomeExc ), expect_and_raise (ThatExc , ThisExc )) as foo :
294- raise ThatExc ("MOOF" )
295- self .failUnlessEqual (foo [0 ], 2 )
296-
297164if __name__ == '__main__' :
298165 from types import ModuleType
299166 this = ModuleType ("this" )
0 commit comments