当前位置: 首页>>代码示例>>Python>>正文


Python GeneratorContextManager.__exit__方法代码示例

本文整理汇总了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) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:19,代码来源:test_with.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:7,代码来源:test_with.py

示例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 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:15,代码来源:test_with.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:11,代码来源:test_with.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:12,代码来源:test_with.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:13,代码来源:test_with.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:15,代码来源:test_with.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:15,代码来源:test_with.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:31,代码来源:test_with.py

示例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") 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:11,代码来源:test_with.py

示例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") 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:13,代码来源:test_with.py


注:本文中的contextlib.GeneratorContextManager.__exit__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。