why 'setprofile' print this
import sys
def a():
开发者_开发技巧 print 'aaa'
def profiler(frame, event, arg):
print event, frame.f_code.co_name, frame.f_lineno, "->", arg
# profiler is activated on the next call, return, or exception
sys.setprofile(profiler)
a()
call a 5 -> None#what is it
aaa
return a 6 -> None#what is it
return <module> 12 -> None#what is it
why print this.
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).
It seems like maybe you're wondering why arg is None. arg
has different meanings for each event. For "return", arg
is the value to be returned. For "exception", it's a triple of exception information. See http://docs.python.org/library/sys.html#sys.settrace for more information.
精彩评论