Permalink
Browse files

Get rid of imperfect ast, support Python 3

  • Loading branch information...
1 parent 5b57b6d commit 7b66396bb71bb02747a33d591676f9e5d667e5bc @patrys committed Jun 19, 2012
Showing with 60 additions and 42 deletions.
  1. +4 −4 great_justice/__init__.py
  2. +41 −4 great_justice/structure.py
  3. +12 −33 great_justice/utils.py
  4. +3 −1 setup.py
View
8 great_justice/__init__.py
@@ -45,8 +45,8 @@ def what_happen(logger=None):
utils.log(
logger,
structure.ExceptionValue(
- ''.join(traceback.format_exception(exc_type, exc_value,
- trace)).strip()))
+ ''.join(traceback.format_exception_only(exc_type,
+ exc_value)).strip()))
@contextlib.contextmanager
def take_your_time(logger=None):
@@ -98,8 +98,8 @@ def log_call(self, frame, event, arg):
utils.log(
self.logger, structure.ExceptionValue(
''.join(
- traceback.format_exception(
- arg[0], arg[1], None)).strip()),
+ traceback.format_exception_only(arg[0],
+ arg[1])).strip()),
indent=self._indent)
return self.log_call
View
45 great_justice/structure.py
@@ -102,12 +102,39 @@ def __init__(self, variable_name):
UndefinedValue(u'<undefined>')]
+class CurrentLine(Structure):
+ '''
+ The highlighted line of code
+ '''
+ # pylint: disable=R0903
+ attrs = {'color': 'white'}
+
+
+class CodeLine(Structure):
+ '''
+ The regular lines
+ '''
+ # pylint: disable=R0903
+ attrs = {'color': 'white', 'attrs': ['dark']}
+
+
class Code(Structure):
'''
A piece of code
'''
# pylint: disable=R0903
- attrs = {'attrs': ['bold']}
+ def __init__(self, prefix, line, suffix):
+ self.args = []
+ for pl in prefix:
+ self.args.append(u' ')
+ self.args.append(CodeLine(pl))
+ self.args.append(u'\n')
+ self.args.append(u'')
+ self.args.append(CurrentLine(line))
+ for sl in suffix:
+ self.args.append(u'\n')
+ self.args.append(u' ')
+ self.args.append(CodeLine(sl))
class CodeFileName(Structure):
@@ -118,17 +145,27 @@ class CodeFileName(Structure):
attrs = {'attrs': ['bold']}
-class CodeLine(Structure):
+class CodeScope(Structure):
+ '''
+ A scope name (function, <module> etc.)
+ '''
+ # pylint: disable=R0903
+ attrs = {'attrs': ['bold']}
+
+
+class FileReference(Structure):
'''
A code reference
'''
# pylint: disable=R0903
- def __init__(self, filename, line_no):
+ def __init__(self, filename, line_no, scope):
# pylint: disable=W0231
self.args = [u'File "',
CodeFileName(filename),
u'", line ',
- CodeLineNo(line_no)]
+ CodeLineNo(line_no),
+ u', in ',
+ CodeScope(scope)]
class CodeLineNo(Structure):
View
45 great_justice/utils.py
@@ -4,46 +4,25 @@
Helper utils
'''
-import ast
import inspect
import linecache
import pprint
-import textwrap
from . import structure
-def get_module_context(obj):
+def get_source(obj):
'''
- 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
+ Get the source code for the frame object
'''
filename = inspect.getsourcefile(obj)
linecache.checkcache(filename)
lineno = inspect.getlineno(obj)
- code = linecache.getline(filename, lineno, obj.f_globals)
- lines = 0
- while True:
- try:
- ast.parse(textwrap.dedent(code) + '\n')
- except SyntaxError:
- if not lineno or lines > 10:
- return code
- lines += 1
- prev_line = linecache.getline(filename, lineno - lines,
- obj.f_globals)
- code = prev_line + code
- else:
- 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'))
+ prefix = [linecache.getline(filename, ln).strip('\r\n') or u'~'
+ for ln in range(lineno-2, lineno)]
+ current = linecache.getline(filename, lineno).strip('\r\n')
+ suffix = [linecache.getline(filename, ln).strip('\r\n') or u'~'
+ for ln in range(lineno+1, lineno+3)]
+ return prefix, current, suffix
def log(logger, info, indent=0):
'''
@@ -65,7 +44,7 @@ def log_call(logger, frame, indent=0):
'''
filename = inspect.getsourcefile(frame)
log(logger,
- structure.CodeLine(filename, frame.f_lineno),
+ structure.FileReference(filename, frame.f_lineno, frame.f_code.co_name),
indent=indent)
def log_invocation(logger, frame, indent=0):
@@ -87,10 +66,10 @@ def log_frame(logger, frame, indent=0):
Parse and log a single frame of a traceback
'''
log_call(logger, frame, indent=indent)
- code = get_source(frame)
+ prefix, line, suffix = get_source(frame)
missing = object()
- if code:
- log(logger, structure.Code(code), indent=indent+1)
+ if line:
+ log(logger, structure.Code(prefix, line, suffix), indent=indent+1)
for key in sorted(frame.f_code.co_varnames):
value = frame.f_locals.get(
key,
View
4 setup.py
@@ -10,6 +10,7 @@
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
+ 'Programming Language :: Python :: 3.2',
'Topic :: Software Development :: Debuggers',
'Topic :: Software Development :: Libraries :: Python Modules']
@@ -19,9 +20,10 @@
author='Patryk Zawadzki',
author_email='patrys@gmail.com',
description='Debug every ZIG',
- version = '2012.6.7',
+ version = '2012.6.8',
packages = find_packages(),
classifiers=CLASSIFIERS,
install_requires=REQUIREMENTS,
platforms=['any'],
+ use_2to3 = True,
zip_safe=True)

0 comments on commit 7b66396

Please sign in to comment.