本文整理汇总了Python中contextlib.ExitStack.push方法的典型用法代码示例。如果您正苦于以下问题:Python ExitStack.push方法的具体用法?Python ExitStack.push怎么用?Python ExitStack.push使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类contextlib.ExitStack
的用法示例。
在下文中一共展示了ExitStack.push方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_instance_bypass
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import push [as 别名]
def test_instance_bypass(self):
class Example(object):
pass
cm = Example()
cm.__exit__ = object()
stack = ExitStack()
self.assertRaises(AttributeError, stack.enter_context, cm)
stack.push(cm)
self.assertIs(tuple(stack._exit_callbacks)[-1], cm)
示例2: _exit
# 需要导入模块: from contextlib import ExitStack [as 别名]
# 或者: from contextlib.ExitStack import push [as 别名]
def _exit(self, exc_type, exc_val, exc_tb, daemon=False):
stack = ExitStack()
# called last
@stack.push
def exit_loop(exc_type, exc_val, exc_tb):
if self.client.is_closed:
return self._loop.__exit__(exc_type, exc_val, exc_tb)
if threading.current_thread() is threading.main_thread():
# TODO the main thread is not necessarily the last thread to finish.
# Should the signal handler be removed in case it isn't?
stack.push(self._signal_ctx)
# called first
# exit the client with the given daemon-ness, maybe leading the client to close
@stack.push
def exit_client(exc_type, exc_val, exc_tb):
return self._call(self.client._aexit(exc_type, exc_val, exc_tb, daemon=daemon))
return stack.__exit__(exc_type, exc_val, exc_tb)