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


Python GeneratorContextManager.__enter__方法代码示例

本文整理汇总了Python中contextlib.GeneratorContextManager.__enter__方法的典型用法代码示例。如果您正苦于以下问题:Python GeneratorContextManager.__enter__方法的具体用法?Python GeneratorContextManager.__enter__怎么用?Python GeneratorContextManager.__enter__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在contextlib.GeneratorContextManager的用法示例。


在下文中一共展示了GeneratorContextManager.__enter__方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: testMultipleComplexTargets

# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __enter__ [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:IronLanguages,项目名称:ironpython2,代码行数:19,代码来源:test_with.py

示例2: __enter__

# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __enter__ [as 别名]
def __enter__(self):
        self.enter_called = True
        return GeneratorContextManager.__enter__(self) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:5,代码来源:test_with.py

示例3: testExitAttributeError

# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __enter__ [as 别名]
def testExitAttributeError(self):
        class LacksExit(object):
            def __enter__(self):
                pass

        def fooLacksExit():
            foo = LacksExit()
            with foo: pass
        self.assertRaises(AttributeError, fooLacksExit) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:11,代码来源:test_with.py

示例4: testEnterThrows

# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __enter__ [as 别名]
def testEnterThrows(self):
        class EnterThrows(object):
            def __enter__(self):
                raise RuntimeError("Enter threw")
            def __exit__(self, *args):
                pass

        def shouldThrow():
            ct = EnterThrows()
            self.foo = None
            with ct as self.foo:
                pass
        self.assertRaises(RuntimeError, shouldThrow)
        self.assertEqual(self.foo, None) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:16,代码来源:test_with.py

示例5: testRaisedStopIteration2

# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __enter__ [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

示例6: testRaisedGeneratorExit2

# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __enter__ [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

示例7: testErrorsInBool

# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __enter__ [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

示例8: testExitTrueSwallowsException

# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __enter__ [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


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