How to use birdseye - 10 common examples

To help you get started, we’ve selected a few birdseye 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 / samples / bird.py View on Github external
filename = None
        while context >= 0:
            frame = frame.f_back
            filename = inspect.getsourcefile(frame)
            if filename is not None:
                context -= 1
        filename = os.path.abspath(filename)

        if frame.f_globals.get('__name__') != '__main__':
            if PY3 and self._treetrace_hidden_with_stmt.__name__ not in frame.f_globals:
                raise RuntimeError(
                    'To trace an imported module, you must import birdseye before '
                    'importing that module.')
            return

        lines = read_source_file(filename).splitlines()
        lines[:frame.f_lineno] = [''] * frame.f_lineno
        source = '\n'.join(lines)
        self.exec_string(source, filename, frame.f_globals, frame.f_locals, deep)
        sys.exit(0)
github alexmojaki / executing / tests / samples / tracer.py View on Github external
def visit_stmt(self, node):
        # type: (ast.stmt) -> ast.With
        """
        Every statement in the original code becomes:

        with _treetrace_hidden_with_stmt(_tree_index):
            

        where the _treetrace_hidden_with_stmt function is the the corresponding method with the
        TreeTracerBase and traced_file arguments already filled in (see _trace_methods_dict)
        """
        context_expr = self._create_simple_marker_call(
            super(_NodeVisitor, self).generic_visit(node),
            TreeTracerBase._treetrace_hidden_with_stmt)

        if PY3:
            wrapped = ast.With(
                items=[ast.withitem(context_expr=context_expr)],
                body=[node],
            )
        else:
            wrapped = ast.With(
                context_expr=context_expr,
                body=[node],
            )
        ast.copy_location(wrapped, node)
        ast.fix_missing_locations(wrapped)
        return wrapped
github alexmojaki / birdseye / tests / test_birdseye.py View on Github external
            @eye
            def __repr__(self):
                return '%s(label=%r)' % (self.__class__.__name__, self.label)
github alexmojaki / birdseye / tests / test_birdseye.py View on Github external
def test_future_imports(self):
        from tests.future_tests import with_future, without_future
        self.assertEqual(with_future.foo(), eye(with_future.foo)())
        self.assertEqual(without_future.foo(), eye(without_future.foo)())
github alexmojaki / birdseye / tests / test_birdseye.py View on Github external
def test_future_imports(self):
        from tests.future_tests import with_future, without_future
        self.assertEqual(with_future.foo(), eye(with_future.foo)())
        self.assertEqual(without_future.foo(), eye(without_future.foo)())
github alexmojaki / snoop / tests / samples / enabled.py View on Github external
def main():
    call_id = eye._last_call_id
    foo()
    assert call_id is eye._last_call_id
github alexmojaki / birdseye / tests / test_birdseye.py View on Github external
        @eye
        def test_A():
            self.assertEqual(a.label, 'hello')
            self.assertEqual(a.length, 3)
            self.assertEqual(a.thing, 'thing')
            self.assertEqual(repr(a), "A(label='hello')")
github alexmojaki / executing / tests / samples / tracer2.py View on Github external
def _trace(self, func, *args, **kwargs):
        # noinspection PyUnresolvedReferences
        from birdseye import eye

        traced = eye(func)
        traced = self.config.snoop(*args, **kwargs)(traced)

        @functools.wraps(func)
        def wrapper(*func_args, **func_kwargs):
            if self.config.enabled:
                final_func = traced
            else:
                final_func = func

            return final_func(*func_args, **func_kwargs)

        return wrapper
github alexmojaki / executing / tests / samples / tracer.py View on Github external
def find_code(root_code):
            # type: (CodeType) -> None
            for const in root_code.co_consts:  # type: CodeType
                if not inspect.iscode(const):
                    continue
                matches = (const.co_firstlineno == func.__code__.co_firstlineno and
                           const.co_name == func.__code__.co_name)
                if matches:
                    code_options.append(const)
                find_code(const)

        find_code(traced_file.code)

        if len(code_options) > 1:
            # Currently lambdas aren't allowed anyway, but should be in the future
            assert is_lambda(func)
            raise ValueError("Failed to trace lambda. Convert the function to a def.")
        new_func_code = code_options[0]  # type: CodeType

        # Give the new function access to the hooks
        # We have to use the original __globals__ and not a copy
        # because it's the actual module namespace that may get updated by other code
        func.__globals__.update(self._trace_methods_dict(traced_file))

        # http://stackoverflow.com/a/13503277/2482744
        # noinspection PyArgumentList
        new_func = FunctionType(new_func_code, func.__globals__, func.__name__, func.__defaults__, func.__closure__)
        update_wrapper(new_func, func)  # type: FunctionType
        if PY3:
            new_func.__kwdefaults__ = getattr(func, '__kwdefaults__', None)
        new_func.traced_file = traced_file
        return new_func