Skip to content

Commit

Permalink
Reorganize code, display fewer variables
Browse files Browse the repository at this point in the history
  • Loading branch information
patrys committed Jun 18, 2012
1 parent a158393 commit 88b86bd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 50 deletions.
35 changes: 3 additions & 32 deletions great_justice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'''

import contextlib
import pprint
import inspect
import sys
import traceback

Expand All @@ -23,42 +23,13 @@ def what_happen(logger=None):
while trace:
frame = trace.tb_frame
trace = trace.tb_next
filename = frame.f_code.co_filename
filename = inspect.getsourcefile(frame)
# skip self
filename_base = filename.rsplit('.', 1)[0]
local_base = __file__.rsplit('.', 1)[0]
if filename_base == local_base:
continue
utils.log(logger,
structure.CodeLine(filename, frame.f_lineno, frame.f_code.co_name))
line, tokens = utils.get_code(filename, frame.f_lineno, frame.f_globals)
if line:
utils.log(logger, structure.Code(line), indent=1)
for key in tokens:
value = (frame.f_locals.get(key) or
frame.f_globals.get(key) or
frame.f_builtins.get(key))
if value:
try:
value = pprint.pformat(value, width=60)
except Exception: # pylint: disable=W0703
utils.log(
logger,
structure.ShortVariable(
key,
'<EXCEPTION RAISED WHILE TRYING TO PRINT>'),
indent=2)
else:
if value.count('\n'):
utils.log(logger, structure.LongVariable(key),
indent=2)
utils.log(logger, structure.Value(value), indent=3)
else:
utils.log(logger,
structure.ShortVariable(key, value), indent=2)
else:
utils.log(logger, structure.UndefinedVariable(key),
indent=2)
utils.log_frame(logger, frame)
utils.log(
logger,
structure.ExceptionValue(
Expand Down
78 changes: 61 additions & 17 deletions great_justice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,45 @@
'''

import ast
import inspect
import linecache
import pprint
import textwrap

def get_code(filename, lineno, module_globals):
from . import structure

def get_module_context(obj):
'''
Instead of providing the last line of a multi-line expression,
try to provide a valid context by looking for a smallest chunk
of code that can be compiled
Instead of providing the whole module source, try to provide a valid
context by looking for a smallest chunk of code that can be compiled
'''
filename = inspect.getsourcefile(obj)
linecache.checkcache(filename)
code = linecache.getline(filename, lineno, module_globals)
if not code:
return None, []
tree = None
tokens = []
lineno = inspect.getlineno(obj)
code = linecache.getline(filename, lineno, obj.f_globals)
lines = 0
while True:
try:
tree = ast.parse(textwrap.dedent(code) + '\n')
ast.parse(textwrap.dedent(code) + '\n')
except SyntaxError:
if not lineno or lines > 10:
break
return code
lines += 1
prev_line = linecache.getline(filename, lineno - lines,
module_globals)
obj.f_globals)
code = prev_line + code
else:
for node in ast.walk(tree):
if isinstance(node, ast.Name):
tokens.append(node.id)
break
return textwrap.dedent(code.strip()), tokens
return textwrap.dedent(code.strip('\n\r'))

def get_source(obj):
'''
Get the source code for the frame object
'''
lines, lineno = inspect.findsource(obj)
if inspect.isframe(obj) and obj.f_globals is obj.f_locals:
return get_module_context(obj)
return textwrap.dedent(
''.join(inspect.getblock(lines[lineno:])).strip('\n\r'))

def log(logger, info, indent=0):
'''
Expand All @@ -51,3 +58,40 @@ def log(logger, info, indent=0):
lines = info.prettyformat().splitlines()
for line in lines:
print ' ' * indent + line

def log_frame(logger, frame):
'''
Parse and log a single frame of a traceback
'''
filename = inspect.getsourcefile(frame)
log(logger,
structure.CodeLine(filename, frame.f_lineno, frame.f_code.co_name))
code = get_source(frame)
missing = object()
if code:
log(logger, structure.Code(code), indent=1)
for key in sorted(frame.f_code.co_varnames):
value = frame.f_locals.get(
key,
frame.f_globals.get(
key,
frame.f_builtins.get(key, missing)))
if value is not missing:
try:
value = pprint.pformat(value, width=60)
except Exception: # pylint: disable=W0703
log(logger,
structure.ShortVariable(
key,
'<EXCEPTION RAISED WHILE TRYING TO PRINT>'),
indent=2)
else:
if value.count('\n'):
log(logger, structure.LongVariable(key), indent=2)
log(logger, structure.Value(value), indent=3)
else:
log(logger, structure.ShortVariable(key, value),
indent=2)
else:
log(logger, structure.UndefinedVariable(key), indent=2)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
author='Patryk Zawadzki',
author_email='patrys@gmail.com',
description='Debug every ZIG',
version = '2012.6.4',
version = '2012.6.5',
packages = find_packages(),
classifiers=CLASSIFIERS,
install_requires=REQUIREMENTS,
Expand Down

0 comments on commit 88b86bd

Please sign in to comment.