A smart trace() command

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
In settings.py:

`# Try to determine if the django server is running
try:
	from commands import getoutput
	DJANGO_SERVER = getoutput('ps ax').find('manage.py runserver ') > 0
except ImportError:
	DJANGO_SERVER = False
`

In a file which you import:

`from django.conf import settings

if settings.DJANGO_SERVER:
	try:
		import ipdb as pdb
	except ImportError:
		import pdb
		
	def trace():
		pdb.set_trace()
else:
	def trace():
		pass
`

In the file in which you want to insert a trace:

from vs.utils import trace  # vs.utils is where you defined the method above

# some code
trace()

More like this

  1. Load response.content in browser (for debugging) by tin_nqn 1 year ago
  2. Template context debugger with (I)Pdb by denis 3 years, 11 months ago
  3. Log Django exceptions to Apache error log in mod_wsgi by simon 3 years, 8 months ago
  4. Updated - Template context debugger with (I)Pdb by dnordberg 3 years, 9 months ago
  5. localsettings by elpaso66 3 years, 2 months ago

Comments

simon (on June 19, 2009):

This approach looks very risky to me - doesn't this mean that if you are using a separate runserver instance on your live server (which I sometimes do to fix bugs) your deployed instance will start firing up the debugger?

#

aparajita (on June 21, 2009):

Yes, you are correct. This was a quick and dirty hack to get the technique to work. I'm sure there are ways to make the test more robust so kind of thing you are talking about will be handled correctly.

#

(Forgotten your password?)