本文整理汇总了Python中contextlib.GeneratorContextManager.__exit__方法的典型用法代码示例。如果您正苦于以下问题:Python GeneratorContextManager.__exit__方法的具体用法?Python GeneratorContextManager.__exit__怎么用?Python GeneratorContextManager.__exit__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类contextlib.GeneratorContextManager
的用法示例。
在下文中一共展示了GeneratorContextManager.__exit__方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testMultipleComplexTargets
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def testMultipleComplexTargets(self):
class C:
def __enter__(self): return 1, 2, 3
def __exit__(self, t, v, tb): pass
targets = {1: [0, 1, 2]}
with C() as (targets[1][0], targets[1][1], targets[1][2]):
self.assertEqual(targets, {1: [1, 2, 3]})
with C() as (targets.values()[0][2], targets.values()[0][1], targets.values()[0][0]):
self.assertEqual(targets, {1: [3, 2, 1]})
with C() as (targets[1], targets[2], targets[3]):
self.assertEqual(targets, {1: 1, 2: 2, 3: 3})
class B: pass
blah = B()
with C() as (blah.one, blah.two, blah.three):
self.assertEqual(blah.one, 1)
self.assertEqual(blah.two, 2)
self.assertEqual(blah.three, 3)
示例2: __exit__
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def __exit__(self, type, value, traceback):
self.exit_called = True
self.exit_args = (type, value, traceback)
return GeneratorContextManager.__exit__(self, type,
value, traceback)
示例3: __enter__
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def __enter__(self):
if self.entered is not None:
raise RuntimeError("Context is not reentrant")
self.entered = deque()
vars = []
try:
for mgr in self.managers:
vars.append(mgr.__enter__())
self.entered.appendleft(mgr)
except:
if not self.__exit__(*sys.exc_info()):
raise
return vars
示例4: testEnterAttributeError
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def testEnterAttributeError(self):
class LacksEnter(object):
def __exit__(self, type, value, traceback):
pass
def fooLacksEnter():
foo = LacksEnter()
with foo: pass
self.assertRaises(AttributeError, fooLacksEnter)
示例5: testExitThrows
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def testExitThrows(self):
class ExitThrows(object):
def __enter__(self):
return
def __exit__(self, *args):
raise RuntimeError(42)
def shouldThrow():
with ExitThrows():
pass
self.assertRaises(RuntimeError, shouldThrow)
示例6: assertAfterWithManagerInvariantsWithError
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def assertAfterWithManagerInvariantsWithError(self, mock_manager,
exc_type=None):
self.assertTrue(mock_manager.enter_called)
self.assertTrue(mock_manager.exit_called)
if exc_type is None:
self.assertEqual(mock_manager.exit_args[1], self.TEST_EXCEPTION)
exc_type = type(self.TEST_EXCEPTION)
self.assertEqual(mock_manager.exit_args[0], exc_type)
# Test the __exit__ arguments. Issue #7853
self.assertIsInstance(mock_manager.exit_args[1], exc_type)
self.assertIsNot(mock_manager.exit_args[2], None)
示例7: testRaisedStopIteration2
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def testRaisedStopIteration2(self):
# From bug 1462485
class cm(object):
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
pass
def shouldThrow():
with cm():
raise StopIteration("from with")
self.assertRaises(StopIteration, shouldThrow)
示例8: testRaisedGeneratorExit2
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def testRaisedGeneratorExit2(self):
# From bug 1462485
class cm (object):
def __enter__(self):
pass
def __exit__(self, type, value, traceback):
pass
def shouldThrow():
with cm():
raise GeneratorExit("from with")
self.assertRaises(GeneratorExit, shouldThrow)
示例9: testErrorsInBool
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def testErrorsInBool(self):
# issue4589: __exit__ return code may raise an exception
# when looking at its truth value.
class cm(object):
def __init__(self, bool_conversion):
class Bool:
def __nonzero__(self):
return bool_conversion()
self.exit_result = Bool()
def __enter__(self):
return 3
def __exit__(self, a, b, c):
return self.exit_result
def trueAsBool():
with cm(lambda: True):
self.fail("Should NOT see this")
trueAsBool()
def falseAsBool():
with cm(lambda: False):
self.fail("Should raise")
self.assertRaises(AssertionError, falseAsBool)
def failAsBool():
with cm(lambda: 1 // 0):
self.fail("Should NOT see this")
self.assertRaises(ZeroDivisionError, failAsBool)
示例10: testExitTrueSwallowsException
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def testExitTrueSwallowsException(self):
class AfricanSwallow:
def __enter__(self): pass
def __exit__(self, t, v, tb): return True
try:
with AfricanSwallow():
1 // 0
except ZeroDivisionError:
self.fail("ZeroDivisionError should have been swallowed")
示例11: testExitFalseDoesntSwallowException
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __exit__ [as 别名]
def testExitFalseDoesntSwallowException(self):
class EuropeanSwallow:
def __enter__(self): pass
def __exit__(self, t, v, tb): return False
try:
with EuropeanSwallow():
1 // 0
except ZeroDivisionError:
pass
else:
self.fail("ZeroDivisionError should have been raised")