How to ensure there are no pdb calls out of debugging configuration?
What do you suggest to get rid of pdb calls on production software? In my case, I'm developing a django website.
I don't know if I should:
- Monkey patch 开发者_运维问答pdb from settings.py (dependding on DEBUG boolean).
- Make a pdb wrapper for our project which expose set_trace or print basic log if DEBUG = True
- Dissalow comitting brakpoints on git hooks... (if you think it's the best idea how would you do that?).
The third one. You have to enforce some commit rules. For example, run a serie of tests before a commit, etc. This way, developpers have a simple way to check if a pdb break remain. If someone commit a set_trace, he has to bake a cake for the rest of the team.
This works fine in my company :-)
edit: you may present this method to your boss as CDD (Cake Driven Developpement)
The best option would be to have an extensive test suite, and to run the tests before pushing to production. Extraneous pdb
breakpoints will prevent the tests from passing.
If you can't do that, then option 2 is the best: write a utility to break into the debugger, and make it sensitive to the state of the settings. You still have to solve the problem of how to be sure people use the wrapper rather than a raw pdb
call though.
Ideally, You shouldn't be including debugging code in the first place. You can instead use a wrapper that sets breakpoints and invokes the main program for debugging, so that the main program contains no actual set_trace()
calls at all.
# foo.py
print "hello"
print "goodbye"
and
#debug_foo.py
import pdb
def run_foo():
execfile('foo.py')
db = pdb.Pdb()
db.set_break("foo.py", 2)
db.run("run_foo()")
Example:
[~]$ python foo.py hello goodbye [~]$ python foo.py > <string>(1)<module>() (Pdb) continue hello > /home/dbornside/foo.py(1)<module>() -> print "goodbye" (Pdb) continue goodbye [~]$
精彩评论