The profiler
function gets called at each profiling event because you called sys.setprofile
on it.
Each time it's called, it prints a line, because you put an unconditional print
statement as its body. Why you did that, is hard for us to tell you, making your "why" questions really, truly peculiar.
Profiling events are just calls and returns, per the docs:
'call'
A function is called (or some other
code block entered).
'return'
A function (or other code block) is
about to return.
'c_call'
A C function is about to be called.
This may be an extension function or a
built-in.
'c_return'
A C function has returned.
Here's what I observe (Python 2.5 or 2.6, MacOSX) in a slightly simpler, sharper case:
>>> def a():
... print 'aaa'
...
>>> def profiler(frame, event, arg):
... print 'PROF %r %r' % (event, arg)
...
>>> sys.setprofile(profiler)
PROF 'return' None
>>> a()
PROF 'call' None
PROF 'c_call' <built-in function utf_8_decode>
PROF 'c_return' <built-in function utf_8_decode>
PROF 'return' (u'a()\n', 4)
PROF 'call' None
PROF 'call' None
aaa
PROF 'return' None
PROF 'return' None
Not sure why you don't see the c_call
and c_return
cases as you should -- maybe there is no implicit utf-8
conversion for printing in your specific platform (what OS? what level of Python? what IDE if any).