How to use the executing.Source.for_frame function in executing

To help you get started, we’ve selected a few executing examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github alexmojaki / executing / tests / test_main.py View on Github external
if time.time() - self.start_time > 45 * 60:
                # Avoid travis time limit of 50 minutes
                raise TimeOut

            lineno = linestarts.get(inst.offset, lineno)
            if not inst.opname.startswith((
                    'BINARY_', 'UNARY_', 'LOAD_ATTR', 'LOAD_METHOD', 'LOOKUP_METHOD',
                    'SLICE+', 'COMPARE_OP', 'CALL_', 'IS_OP', 'CONTAINS_OP',
            )):
                continue
            frame = C()
            frame.f_lasti = inst.offset
            frame.f_code = code
            frame.f_globals = globals()
            frame.f_lineno = lineno
            source = Source.for_frame(frame)
            node = None

            try:
                try:
                    node = Source.executing(frame).node
                except Exception:
                    if inst.opname.startswith(('COMPARE_OP', 'CALL_')):
                        continue
                    if isinstance(only(source.statements_at_line(lineno)), (ast.AugAssign, ast.Import)):
                        continue
                    raise
            except Exception:
                print(source.text, lineno, inst, node and ast.dump(node), code, file=sys.stderr, sep='\n')
                raise

            nodes[node].append((inst, frame.__dict__))
github alexmojaki / executing / tests / samples / tests.py View on Github external
def test_file(self):
        source = Source.for_frame(inspect.currentframe())
        code = compile(source.text, source.filename, 'exec')
        instructions = get_instructions(code)
        lineno = None
        for inst in instructions:
            if inst.starts_line is not None:
                lineno = inst.starts_line
            if not inst.opname.startswith(
                    ('BINARY_', 'UNARY_', 'LOAD_ATTR', 'LOAD_METHOD', 'LOOKUP_METHOD', 'COMPARE_OP')):
                continue
            frame = C()
            frame.f_lasti = inst.offset
            frame.f_code = code
            frame.f_globals = globals()
            frame.f_lineno = lineno
            print(inst.opname)
            assert Source.executing(frame).node is not None
github alexmojaki / heartrate / heartrate / core.py View on Github external
def gen():
            frame = current_frame()
            while frame:
                code = frame.f_code
                filename = code.co_filename
                name = Source.for_frame(frame).code_qualname(code)
                yield (
                    filename,
                    frame.f_lineno,
                    name,
                    highlight_stack_frame(frame),
                    include_file(filename)
                )
                frame = frame.f_back