Context Manager Hackery

浪子不回头ぞ 提交于 2019-12-04 16:36:45

The awful hack you're using does a lot of things with nasty, subtle consequences. I doubt the author fully understood it (if they did, they wouldn't have used a bare raise, and they wouldn't have tried to pass inspect.currentframe an argument it doesn't take). Incidentally, the incorrect usage of inspect.currentframe causes the code to fail with a TypeError instead of doing what you describe, so for the rest of this answer, I'll assume that call is replaced with sys._getframe(1), which produces the described behavior.


One of the things the hack relies on is setting a local trace function with frame.f_trace = self.trace. This local trace function will raise an exception on the first line inside the with block... or at least, that's what it normally does.

Python calls trace functions when certain trace events happen. One of those trace events is the start of a new source line. Python determines that a new source line has started by checking whether the current bytecode instruction index corresponds to either the first instruction of a line, or an instruction at an index prior to the last instruction executed. You can see that in maybe_call_line_trace in Python/ceval.c.

Python only updates instr_prev, the variable used to determine the last instruction executed, when tracing is active. However, once the local trace function raises an exception, it is automatically deactivated, and instr_prev stops receiving updates.

When the local trace function is set, the next two instructions it could activate on are the STORE_NAME to set c (or STORE_FAST if you put the code in a function), and the LOAD_NAME to load the print function for the next line (or LOAD_GLOBAL if you put the code in a function).

The first time through the loop, it activates on LOAD_NAME, and instr_prev is set to that instruction's index. The local trace function is then disabled, because it raised an exception.

The second time through the loop, instr_prev is still set to the index of the LOAD_NAME, so Python thinks the STORE_NAME marks the beginning of a new line. The local trace function activates on STORE_NAME, and the exception prevents the assignment to c.

You can see the instructions where the local trace function activates by inspecting frame.f_lasti in trace, and comparing the results to the instruction indices in the output of dis.dis. For example, the following variant of your code:

import sys
import inspect
import dis

class SkippableContext(object):
    def __enter__(self):
        print('   ... Skipping Context')
        sys.settrace(lambda *args, **keys: None)
        frame = sys._getframe(1)
        frame.f_trace = self.trace
        return 'SET BY TRICKY CONTEXT MANAGER!!'

    def trace(self, frame, event, arg):
        print(frame.f_lasti)
        raise Exception

    def __exit__(self, type, value, traceback):
        return True

def f():
    for i in range(2):
        c='not set'
        with SkippableContext() as c:
            print('This code is not printed')
            c = 'set in context'
        print('c: {}'.format(c))

f()
dis.dis(f)

produces the following output:

   ... Skipping Context
26
c: SET BY TRICKY CONTEXT MANAGER!!
   ... Skipping Context
24
c: not set
 21           0 SETUP_LOOP              64 (to 66)
              2 LOAD_GLOBAL              0 (range)
              4 LOAD_CONST               1 (2)
              6 CALL_FUNCTION            1
              8 GET_ITER
        >>   10 FOR_ITER                52 (to 64)
             12 STORE_FAST               0 (i)

 22          14 LOAD_CONST               2 ('not set')
             16 STORE_FAST               1 (c)

 23          18 LOAD_GLOBAL              1 (SkippableContext)
             20 CALL_FUNCTION            0
             22 SETUP_WITH              18 (to 42)
             24 STORE_FAST               1 (c)

 24          26 LOAD_GLOBAL              2 (print)
             28 LOAD_CONST               3 ('This code is not printed')
             30 CALL_FUNCTION            1
             32 POP_TOP

 25          34 LOAD_CONST               4 ('set in context')
             36 STORE_FAST               1 (c)
             38 POP_BLOCK
             40 LOAD_CONST               0 (None)
        >>   42 WITH_CLEANUP_START
             44 WITH_CLEANUP_FINISH
             46 END_FINALLY

 26          48 LOAD_GLOBAL              2 (print)
             50 LOAD_CONST               5 ('c: {}')
             52 LOAD_METHOD              3 (format)
             54 LOAD_FAST                1 (c)
             56 CALL_METHOD              1
             58 CALL_FUNCTION            1
             60 POP_TOP
             62 JUMP_ABSOLUTE           10
        >>   64 POP_BLOCK
        >>   66 LOAD_CONST               0 (None)
             68 RETURN_VALUE

The 26 printed the first time corresponds to the index of the LOAD_GLOBAL, and the 24 printed the second time corresponds to the index of the STORE_FAST.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!