How to use the hunter.CodePrinter function in hunter

To help you get started, we’ve selected a few hunter 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 ionelmc / python-hunter / tests / test_hunter.py View on Github external
def run():
        output = StringIO()
        with t.trace(Q(
            ~Q(module_in=['re', 'sre', 'sre_parse']) & ~Q(module_startswith='namedtuple') & Q(kind='call'),
            actions=[
                CodePrinter(
                    stream=output
                ),
                VarsPrinter(
                    'line',
                    stream=output
                )
            ]
        )):
            _bulky_func_that_use_stdlib()
github ionelmc / python-hunter / tests / test_hunter.py View on Github external
def test_predicate_q_deduplicate_codeprinter():
    out = repr(Q(CodePrinter(), action=CodePrinter()))
    assert out.startswith('CodePrinter(')
github ionelmc / python-hunter / tests / test_hunter.py View on Github external
def test_fullsource_decorator_issue(LineMatcher):
    out = StringIO()
    with trace(kind='call', action=CodePrinter(stream=out)):
        foo = bar = lambda x: x

        @foo
        @bar
        def foo():
            return 1

        foo()

    lm = LineMatcher(out.getvalue().splitlines())
    lm.fnmatch_lines([
        '* call              @foo',
        '*    |              @bar',
        '*    *              def foo():',
    ])
github ionelmc / python-hunter / tests / test_hunter.py View on Github external
def test_predicate_q_deduplicate_codeprinter_inverted():
    out = repr(Q(CodePrinter(), action=CallPrinter()))
    assert out.startswith('CodePrinter(')
github ionelmc / python-hunter / tests / test_hunter.py View on Github external
def test_predicate_q_deduplicate_callprinter_inverted():
    out = repr(Q(CallPrinter(), action=CodePrinter()))
    assert out.startswith('CallPrinter(')
github ionelmc / python-hunter / tests / test_cookbook.py View on Github external
except Exception as exc:
        logger.info(repr(exc))
        baz()


def notsilenced():
    try:
        error()
    except Exception as exc:
        raise ValueError(exc)


RETURN_VALUE = opcode.opmap['RETURN_VALUE']


class DumpExceptions(hunter.CodePrinter):
    events = ()
    depth = 0
    count = 0
    exc = None

    def __init__(self, max_count=10, **kwargs):
        self.max_count = max_count
        self.backlog = collections.deque(maxlen=5)
        super(DumpExceptions, self).__init__(**kwargs)

    def __call__(self, event):
        self.count += 1
        if event.kind == 'exception':  # something interesting happened ;)
            self.events = list(self.backlog)
            self.events.append(event.detach(self.try_repr))
            self.exc = self.try_repr(event.arg[1])
github ionelmc / python-hunter / tests / test_hunter.py View on Github external
def test_locals():
    out = StringIO()
    with hunter.trace(
        lambda event: event.locals.get('node') == 'Foobar',
        module='test_hunter',
        function='foo',
        action=CodePrinter(stream=out)
    ):
        def foo():
            a = 1
            node = 'Foobar'
            node += 'x'
            a += 2
            return a

        foo()
    assert out.getvalue().endswith("node += 'x'\n")