Skip to content

Commit a758639

Browse files
committed
Tweaked the debug module, added traceback capability and a combined name/value/type info funciton.
- Legacy-Id: 6306
1 parent 92b9da1 commit a758639

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

debug.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import time as timeutils
33
import inspect
4+
45
try:
56
import syslog
67
logger = syslog.syslog
@@ -15,11 +16,14 @@
1516
pformat = lambda x: x
1617

1718
import cProfile
19+
import traceback as tb
20+
1821
try:
1922
from django.conf import settings
2023
debug = settings.DEBUG
2124
except ImportError:
2225
debug = True
26+
2327
from decorator import decorator
2428

2529
# A debug decorator, written by Paul Butler, taken from
@@ -34,7 +38,7 @@
3438
# Number of times to indent output
3539
# A list is used to force access by reference
3640
_report_indent = [4]
37-
_mark = timeutils.clock()
41+
_mark = [ timeutils.clock() ]
3842

3943
def set_indent(i):
4044
_report_indent[0] = i
@@ -79,16 +83,15 @@ def wrap(fn, *params,**kwargs):
7983
return fn
8084

8185
def mark():
82-
say("! mark")
83-
_mark = timeutils.clock()
86+
_mark[0] = timeutils.clock()
8487

8588
def lap(s):
86-
tau = timeutils.clock() - _mark
89+
tau = timeutils.clock() - _mark[0]
8790
say("> %s: %.3fs since mark" % (s, tau))
8891

8992
def clock(s):
9093
lap(s)
91-
_mark = timeutils.clock()
94+
_mark[0] = timeutils.clock()
9295

9396
def time(fn):
9497
"""Decorator to print timing information about a function call.
@@ -170,3 +173,16 @@ def wrapper(*args, **kwargs):
170173
else:
171174
return fn
172175

176+
def traceback():
177+
if debug:
178+
indent = ' ' * (_report_indent[0])
179+
for s in tb.format_stack()[:-1]:
180+
sys.stderr.write("%s%s" % (indent, s))
181+
182+
def info(name):
183+
if debug:
184+
frame = inspect.stack()[1][0]
185+
value = eval(name, frame.f_globals, frame.f_locals)
186+
indent = ' ' * (_report_indent[0])
187+
sys.stderr.write("%s%s: %s %s\n" % (indent, name, value, type(value)))
188+

0 commit comments

Comments
 (0)