32

我正在定义一个上下文管理器类,如果在实例化过程中满足某些条件,我希望能够跳过代码块而不引发异常。例如,

class My_Context(object):
    def __init__(self,mode=0):
        """
        if mode = 0, proceed as normal
        if mode = 1, do not execute block
        """
        self.mode=mode
    def __enter__(self):
        if self.mode==1:
            print 'Exiting...'
            CODE TO EXIT PREMATURELY
    def __exit__(self, type, value, traceback):
        print 'Exiting...'

with My_Context(mode=1):
    print 'Executing block of codes...'
4

7 回答 7

23

根据PEP-343with声明翻译自:

with EXPR as VAR:
    BLOCK

到:

mgr = (EXPR)
exit = type(mgr).__exit__  # Not calling it yet
value = type(mgr).__enter__(mgr)
exc = True
try:
    try:
        VAR = value  # Only if "as VAR" is present
        BLOCK
    except:
        # The exceptional case is handled here
        exc = False
        if not exit(mgr, *sys.exc_info()):
            raise
        # The exception is swallowed if exit() returns true
finally:
    # The normal and non-local-goto cases are handled here
    if exc:
        exit(mgr, None, None, None)

如您所见,从调用__enter__()上下文管理器的方法可以跳过BLOCKwith 语句的主体(“”),没有什么明显可以做的。

人们已经在withhacks__enter__()等项目中完成了特定于 Python 实现的事情,例如在. 我记得 Alex Martelli 在一两年前在 stackoverflow 上发布了一个非常有趣的 with-hack(不记得足够多的帖子来搜索和找到它)。

但是对您的问题/问题的简单回答是,您不能按照您的要求做,跳过 with 语句的主体,而不诉诸所谓的“深层魔法”(这在 python 实现之间不一定可移植)。使用深奥的魔法,你也许可以做到,但我建议只做一些练习,看看它是如何完成的,而不是在“生产代码”中。

于 2012-09-26T03:54:27.110 回答
23

如果您想要一个使用withhacks(特别是来自AnonymousBlocksInPython )的想法的临时解决方案,这将起作用:

import sys
import inspect

class My_Context(object):
    def __init__(self,mode=0):
        """
        if mode = 0, proceed as normal
        if mode = 1, do not execute block
        """
        self.mode=mode
    def __enter__(self):
        if self.mode==1:
            print 'Met block-skipping criterion ...'
            # Do some magic
            sys.settrace(lambda *args, **keys: None)
            frame = inspect.currentframe(1)
            frame.f_trace = self.trace
    def trace(self, frame, event, arg):
        raise
    def __exit__(self, type, value, traceback):
        print 'Exiting context ...'
        return True

比较以下内容:

with My_Context(mode=1):
    print 'Executing block of code ...'

with My_Context(mode=0):
    print 'Executing block of code ... '
于 2012-09-26T04:56:36.833 回答
11

来自withhacks的其他答案(特别是来自AnonymousBlocksInPython )提到的 hack 的 python 3 更新 :

class SkipWithBlock(Exception):
    pass


class SkipContextManager:
    def __init__(self, skip):
        self.skip = skip

    def __enter__(self):
        if self.skip:
            sys.settrace(lambda *args, **keys: None)
            frame = sys._getframe(1)
            frame.f_trace = self.trace

    def trace(self, frame, event, arg):
        raise SkipWithBlock()

    def __exit__(self, type, value, traceback):
        if type is None:
            return  # No exception
        if issubclass(type, SkipWithBlock):
            return True  # Suppress special SkipWithBlock exception


with SkipContextManager(skip=True):    
    print('In the with block')  # Won't be called
print('Out of the with block')

正如 joe 之前提到的,这是一个应该避免的 hack:

当进入一个新的本地范围时调用方法 trace() ,即当您的 with 块中的代码开始时。当此处引发异常时,它会被exit () 捕获。这就是这个黑客的工作原理。我应该补充一点,这在很大程度上是一种 hack,不应该依赖它。神奇的 sys.settrace() 实际上并不是语言定义的一部分,它恰好在 CPython 中。此外,调试器依赖 sys.settrace() 来完成他们的工作,所以自己使用它会干扰它。您不应该使用此代码的原因有很多。仅供参考。

于 2019-02-19T11:40:33.287 回答
6

根据@Peter 的回答,这是一个不使用字符串操作但应该以相同方式工作的版本:

from contextlib import contextmanager

@contextmanager
def skippable_context(skip):
    skip_error = ValueError("Skipping Context Exception")
    prev_entered = getattr(skippable_context, "entered", False)
    skippable_context.entered = False

    def command():
        skippable_context.entered = True
        if skip:
            raise skip_error

    try:
        yield command
    except ValueError as err:
        if err != skip_error:
            raise
    finally:
        assert skippable_context.entered, "Need to call returned command at least once."
        skippable_context.entered = prev_entered


print("=== Running with skip disabled ===")
with skippable_context(skip=False) as command:
    command()
    print("Entering this block")
print("... Done")

print("=== Running with skip enabled ===")
with skippable_context(skip=True) as command:
    command()
    raise NotImplementedError("... But this will never be printed")
print("... Done")

于 2020-01-14T15:19:12.510 回答
3

不幸的是,您尝试做的事情是不可能的。如果__enter__引发异常,则在with语句(__exit__未调用)处引发该异常。如果它没有引发异常,则将返回值提供给块并执行该块。

我能想到的最接近的事情是块明确检查的标志:

class Break(Exception):
    pass

class MyContext(object):
    def __init__(self,mode=0):
        """
        if mode = 0, proceed as normal
        if mode = 1, do not execute block
        """
        self.mode=mode
    def __enter__(self):
        if self.mode==1:
            print 'Exiting...'
        return self.mode
    def __exit__(self, type, value, traceback):
        if type is None:
            print 'Normal exit...'
            return # no exception
        if issubclass(type, Break):
            return True # suppress exception
        print 'Exception exit...'

with MyContext(mode=1) as skip:
    if skip: raise Break()
    print 'Executing block of codes...'

Break()这也使您可以在块的中间加注with以模拟正常的break语句。

于 2012-09-26T03:50:40.827 回答
2

上下文管理器不是正确的构造。您要求将主体执行n次,在本例中为零或一。如果您查看一般情况,n其中n >= 0,您最终会得到一个 for 循环:

def do_squares(n):
  for i in range(n):
    yield i ** 2

for x in do_squares(3):
  print('square: ', x)

for x in do_squares(0):
  print('this does not print')

在您的情况下,这是更特殊的目的,并且不需要绑定到循环变量:

def should_execute(mode=0):
  if mode == 0:
    yield

for _ in should_execute(0):
  print('this prints')

for _ in should_execute(1):
  print('this does not')
于 2020-04-27T17:52:44.510 回答
1

另一个略显老套的选项使用exec. 这很方便,因为可以对其进行修改以执行任意操作(例如,上下文块的记忆):

from contextlib import contextmanager


@contextmanager
def skippable_context_exec(skip):
    SKIP_STRING = 'Skipping Context Exception'
    old_value = skippable_context_exec.is_execed if hasattr(skippable_context_exec, 'is_execed') else False
    skippable_context_exec.is_execed=False
    command = "skippable_context_exec.is_execed=True; "+("raise ValueError('{}')".format(SKIP_STRING) if skip else '')
    try:
        yield command
    except ValueError as err:
        if SKIP_STRING not in str(err):
            raise
    finally:
        assert skippable_context_exec.is_execed, "You never called exec in your context block."
        skippable_context_exec.is_execed = old_value


print('=== Running with skip disabled ===')
with skippable_context_exec(skip=False) as command:
    exec(command)
    print('Entering this block')
print('... Done')

print('=== Running with skip enabled ===')
with skippable_context_exec(skip=True) as command:
    exec(command)
    print('... But this will never be printed')
print('... Done')

如果有一些东西可以摆脱 exec 而没有奇怪的副作用,那就太好了,所以如果你能想到一种方法,我会全力以赴。该问题的当前主要答案似乎可以做到这一点,但存在一些问题

于 2019-10-31T16:07:54.510 回答