Getting pdb to work with django on windows
I was setting up django on Scott’s machine for django development. In addition to the normal quirks
manage.py runserver
doesn’t work normally. You get no updates in the shell when you load a page, unlike on unix systems, where you see access logs. When you a save a file in your django project, it forces the webserver to reload, at which point you see all the previous logs.
This behaviour is annoying enough for access logs, but it makes pdb unusable, which in turn makes development much less fun. After digging through the django/manage.py/basehttp/wsgi/pdb/cmd.py source I stumbled onto the problem.
Django uses
sys.stdout.write("foo")
to write access logs and other information from the server to the shell. Calling sys.stdout.flush(), will cause your shell to update with the most recent server output. When you save a file, it causes django to terminate the current server and start a new one. killing the current server causes stdout to flush. Making the server call flush in all the right places would require editting django code, or monkey patching, thankfully getting pdb to work under windows is much easier. use this function
import pdb
def set_trace():
p = pdb.Pdb()
p.use_rawinput = False
p.set_trace(sys._getframe().f_back)
set_trace()
if you look at the code in cmd.py, when raw_input is True, sys.stdout.flush() isn’t called, otherwise it is.
Good luck, I hope this is helpful