CARVIEW |
Select Language
HTTP/2 200
server: nginx
content-type: text/html;charset=utf-8
cache-control: must-revalidate
expires: Fri, 01 Jan 1999 00:00:00 GMT
set-cookie: trac_form_token=463c95a9bd0281c3656fc984; HttpOnly; Path=/; Secure
set-cookie: trac_session=d5041ba7864eb1e8f92c40c1; expires=Tue, 21 Oct 2025 12:32:24 GMT; HttpOnly; Path=/; Secure
strict-transport-security: max-age=31536000; includeSubDomains; preload
permissions-policy: interest-cohort=()
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
accept-ranges: bytes
via: 1.1 varnish, 1.1 varnish
date: Wed, 23 Jul 2025 12:32:25 GMT
x-served-by: cache-fra-eddf8230166-FRA, cache-bom-vanm7210067-BOM
x-cache: MISS, MISS
x-cache-hits: 0, 0
x-timer: S1753273945.844711,VS0,VE273
vary: Accept-Encoding
CookBookDeveloperTools – Django
Back to Top
Django
The web framework for perfectionists with deadlines.
Issues
Inspect call stack
While playing arround with djangos internals I have found the following decorator usefull. It shows the call stack up to the decorated function:
def who_called_me(show_filename=False, out=None, indent=' '): def _wrapper(fn): def _inner_wrapper(*args, **kwargs): import sys import inspect output = out or sys.stdout assert hasattr(output, 'write'), \ 'argument \'out\' of function \'who_called_me\' must have a write method' index = 0 stack = inspect.stack() stack.reverse() # remove ourself from the stack list stack.pop() for record in stack: frame = record[0] line = frame.f_lineno func_name = frame.f_code.co_name if show_filename: descr = frame.f_code.co_filename else: descr = frame.f_globals["__name__"] print >>output, '%s%s@%s:%d' % (indent*index, descr, func_name, line) # to be safe explicitly delete the stack frame reference # @see https://docs.python.org/lib/inspect-stack.html del frame index += 1 del stack if hasattr(output, 'flush'): output.flush() return fn(*args, **kwargs) return _inner_wrapper return _wrapper
Usage Examples:
@who_called_me() def index(request): return render_to_response('phc/index.html', None, context_instance=RequestContext(request))
Prints this to stdout:
django.core.management@inner_run:995 django.core.servers.basehttp@run:643 SocketServer@serve_forever:201 SocketServer@handle_request:222 SocketServer@process_request:241 SocketServer@finish_request:254 django.core.servers.basehttp@__init__:537 SocketServer@__init__:521 django.core.servers.basehttp@handle:586 django.core.servers.basehttp@run:272 django.core.servers.basehttp@__call__:615 django.core.handlers.wsgi@__call__:145 django.core.handlers.base@get_response:74
Show filenames instead of module:
@who_called_me(show_filename=True) def index(request): return render_to_response('phc/index.html', None, context_instance=RequestContext(request))
Prints this to stdout:
/home/me/projects/django_ma/django/core/management.py@inner_run:995 /home/me/projects/django_ma/django/core/servers/basehttp.py@run:643 /usr/lib/python2.4/SocketServer.py@serve_forever:201 /usr/lib/python2.4/SocketServer.py@handle_request:222 /usr/lib/python2.4/SocketServer.py@process_request:241 /usr/lib/python2.4/SocketServer.py@finish_request:254 /home/me/projects/django_ma/django/core/servers/basehttp.py@__init__:537 /usr/lib/python2.4/SocketServer.py@__init__:521 /home/me/projects/django_ma/django/core/servers/basehttp.py@handle:586 /home/me/projects/django_ma/django/core/servers/basehttp.py@run:272 /home/me/projects/django_ma/django/core/servers/basehttp.py@__call__:615 /home/me/projects/django_ma/django/core/handlers/wsgi.py@__call__:145 /home/me/projects/django_ma/django/core/handlers/base.py@get_response:74
Or write output to a file:
@who_called_me(out=open('/tmp/who_called_me', 'a')) def index(request): return render_to_response('phc/index.html', None, context_instance=RequestContext(request))
Last modified
19 years ago
Last modified on Jun 1, 2006, 12:42:07 PM
Note:
See TracWiki
for help on using the wiki.
Download in other formats:
Django Links
Learn More
Get Involved
Follow Us
- Hosting by In-kind donors
- Design by Threespot &
© 2005-2025 Django SoftwareFoundation unless otherwise noted. Django is a registered trademark of the Django Software Foundation.