How to print each line of a script as it is run only for the top-level script being run?

爱⌒轻易说出口 提交于 2019-12-03 15:41:43

问题


python's trace module will allow you to run a script printing each line of code as it is run in both the script and all imported modules like so:

 python -m trace -trace myscript.py

Is there a way to do the same thing, but only print the top-level calls, i.e. only print the lines in myscript.py as they are run?

I am trying to debug an abort trap failure and I can't figure out where it's dying. Unfortunately, using the full --trace takes forever - the script normally takes 2-3 minutes to run, and the full trace has been going for hours.


回答1:


I stumbled into this problem and found grep to be a quick and dirty solution:

python -m trace --trace my_script.py | grep my_script.py

My script runs in finite time though. This probably won't work well for more complex scripts.




回答2:


Maybe lptrace can be useful for you.




回答3:


It's not exactly what you want, but you may consider using py.test with "-s" which will prevent py.test from capturing the output of your print statment... So you can put some print statment here and there for each function you have in your script and create a dummy test which just execute your script as usual... You may then see where it failed.




回答4:


If you don't get a traceback, you can use the technique called bisection.

Edit your main function or script body and put an exit(1) call aproximately in the middle. This is the first bisection.

Execute your script. If it reaches your exit, you know the fault is in the second half. If not, it's in the first half.

Move the exit to half of the first half, or half of the second half and try again.

With each cycle, you can narrow the location of the fault down by half of the remaining code.

If you've narrowed it down to one of your own functions, bisect that.




回答5:


You may use decorator and decorate all the function of your module dynamically (no edit of each functions). You just need to paste these lines at the end of your script :

def dump_args(func):
    """This decorator dumps out the arguments passed to a function before calling it"""
    argnames = func.func_code.co_varnames[:func.func_code.co_argcount]
    fname = func.func_name
    def echo_func(*args,**kwargs):
        print fname, "(", ', '.join(
            '%s=%r' % entry
            for entry in zip(argnames,args[:len(argnames)])+[("args",list(args[len(argnames):]))]+[("kwargs",kwargs)]) +")"
    return echo_func

### Decorate all the above functions
import types
for k,v in globals().items():
    if isinstance(v, types.FunctionType):
        globals()[k] = dump_args(v)


Reference, code is coming from these two answers :
http://stackoverflow.com/questions/8951787/defining-python-decorators-for-a-complete-module
http://stackoverflow.com/questions/6200270/decorator-to-print-function-call-details-parameters-names-and-effective-values


来源:https://stackoverflow.com/questions/31964712/how-to-print-each-line-of-a-script-as-it-is-run-only-for-the-top-level-script-be

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!