本文整理汇总了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)
示例2: __enter__
# 需要导入模块: from contextlib import GeneratorContextManager [as 别名]
# 或者: from contextlib.GeneratorContextManager import __enter__ [as 别名]
def __enter__(self):
self.enter_called = True
return GeneratorContextManager.__enter__(self)
示例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)
示例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)
示例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)
示例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)
示例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)
示例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")