Skip to content

Commit 51a3c37

Browse files
committed
Added a debug utility module on the same level as south,html5lib etc.
- Legacy-Id: 4446
1 parent a422032 commit 51a3c37

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

debug.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
import sys
3+
import inspect
4+
import syslog
5+
from pprint import pformat
6+
7+
# A debug decorator, written by Paul Butler, taken from
8+
# http://paulbutler.org/archives/python-debugging-with-decorators/
9+
# Additional functions and decorator functionality added by
10+
# Henrik Levkowetz
11+
12+
# Number of times to indent output
13+
# A list is used to force access by reference
14+
__report_indent = [4]
15+
increment = 2
16+
debug = True
17+
syslog.openlog("debug", syslog.LOG_PID, syslog.LOG_USER)
18+
19+
def set_indent(i):
20+
__report_indent[0] = i
21+
22+
def trace(fn): # renamed from 'report' by henrik 16 Jun 2011
23+
"""Decorator to print information about a function
24+
call for use while debugging.
25+
Prints function name, arguments, and call number
26+
when the function is called. Prints this information
27+
again along with the return value when the function
28+
returns.
29+
"""
30+
def fix(s,n=32):
31+
if len(s) > n+3:
32+
s = s[:n]+"..."
33+
s = s.replace('\n',' ')
34+
return s
35+
def wrap(*params,**kwargs):
36+
call = wrap.callcount = wrap.callcount + 1
37+
38+
indent = ' ' * __report_indent[0]
39+
fc = "%s(%s)" % (fn.__name__, ', '.join(
40+
[fix(repr(a)) for a in params] +
41+
["%s = %s" % (a, fix(repr(b))) for a,b in kwargs.items()]
42+
))
43+
44+
print "%s* %s [#%s]" % (indent, fc, call)
45+
__report_indent[0] += increment
46+
ret = fn(*params,**kwargs)
47+
__report_indent[0] -= increment
48+
sys.stderr.write( "%s %s [#%s] ==> %s\n" % (indent, fc, call, repr(ret)))
49+
50+
return ret
51+
wrap.callcount = 0
52+
if debug:
53+
return wrap
54+
else:
55+
return fn
56+
57+
def show(name):
58+
if debug:
59+
frame = inspect.stack()[1][0]
60+
value = eval(name, frame.f_globals, frame.f_locals)
61+
indent = ' ' * (__report_indent[0])
62+
sys.stderr.write("%s%s: %s\n" % (indent, name, value))
63+
64+
def log(name):
65+
if debug:
66+
frame = inspect.stack()[1][0]
67+
value = eval(name, frame.f_globals, frame.f_locals)
68+
indent = ' ' * (__report_indent[0])
69+
syslog.syslog("%s%s: %s" % (indent, name, value))
70+
71+
def pprint(name):
72+
if debug:
73+
frame = inspect.stack()[1][0]
74+
value = eval(name, frame.f_globals, frame.f_locals)
75+
indent = ' ' * (__report_indent[0])
76+
sys.stdout.write("%s%s:\n" % (indent, name))
77+
lines = pformat(value).split('\n')
78+
for line in lines:
79+
sys.stdout.write("%s %s\n"%(indent, line))
80+
81+
def say(s):
82+
if debug:
83+
indent = ' ' * (__report_indent[0])
84+
sys.stderr.write("%s%s\n" % (indent, s))
85+

ietf/person/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def short(self):
2929
prefix, first, middle, last, suffix = self.ascii_parts()
3030
return (first and first[0]+"." or "")+(middle or "")+" "+last+(suffix and " "+suffix or "")
3131
def plain_name(self):
32+
if self.ascii_short:
33+
return self.ascii_short
3234
prefix, first, middle, last, suffix = name_parts(self.name)
3335
return u" ".join([first, last])
3436
def last_name(self):

0 commit comments

Comments
 (0)