ionelmc / python-hunter

Hunter is a flexible code tracing toolkit.
https://python-hunter.readthedocs.io/
BSD 2-Clause "Simplified" License
796 stars 46 forks source link

Crash on passing function to hunter.trace() #32

Closed Wilfred closed 6 years ago

Wilfred commented 7 years ago
def fibs(x):
    if x == 0:
        return 1
    if x == 1:
        return 1

    temp1 = fibs(x-1)
    temp2 = fibs(x-2)

    return temp1 + temp2

import hunter
hunter.trace(fibs)

print fibs(4)

This produces the exception:

$ python foo.py 
Traceback (most recent call last):
  File "src/hunter/_tracer.pyx", line 24, in hunter._tracer.trace_func (src/hunter/_tracer.c:1153)
  File "src/hunter/_predicates.pyx", line 288, in hunter._predicates.fast_When_call (src/hunter/_predicates.c:7391)
  File "foo.py", line 8, in fibs
    temp1 = fibs(x-1)
TypeError: unsupported operand type(s) for -: 'hunter._event.Event' and 'int'
Exception TypeError: "unsupported operand type(s) for -: 'hunter._event.Event' and 'int'" in <module 'threading' from '/opt/ahl/releases/python/2.7.3/lib64/python2.7/threading.pyc'> ignored
Error in sys.exitfunc:
Traceback (most recent call last):
  File "src/hunter/_tracer.pyx", line 24, in hunter._tracer.trace_func (src/hunter/_tracer.c:1153)
  File "src/hunter/_predicates.pyx", line 288, in hunter._predicates.fast_When_call (src/hunter/_predicates.c:7391)
  File "foo.py", line 8, in fibs
    temp1 = fibs(x-1)
TypeError: unsupported operand type(s) for -: 'hunter._event.Event' and 'int'
Exception TypeError: "unsupported operand type(s) for -: 'hunter._event.Event' and 'int'" in <function _remove at 0x2abaa1416938> ignored
Exception TypeError: "unsupported operand type(s) for -: 'hunter._event.Event' and 'int'" in <function _remove at 0x2abaa1418848> ignored

The correct usage is probably hunter.trace(function=fibs) (which executes fibs but does not print any tracing information or hunter.trace(function='fibs') (which traces as I'd expect).

I thought I could pass a function because these docs: https://python-hunter.readthedocs.io/en/latest/introduction.html#the-trace-function suggest that I can pass any function. I am probably misunderstanding that paragraph.

ionelmc commented 7 years ago

It say "Callbacks that take an event argument:" - I could repeat that I guess ... propose better wording?

Hunter is not designed to trace individual functions, rather it's an advanced toolkit for users to write their own tracers.

ionelmc commented 6 years ago

See https://python-hunter.readthedocs.io/en/latest/reference.html#hunter.wrap