better filename handling in tracer

This commit is contained in:
Anthony Scopatz 2016-02-03 01:29:06 -05:00
parent 83c337178e
commit 9c3506cb14
4 changed files with 23 additions and 21 deletions

View file

@ -14,14 +14,14 @@ from xonsh.ast import pdump
from xonsh.parser import Parser
from tools import (mock_xonsh_env, skip_if, VER_3_4, VER_3_5, VER_MAJOR_MINOR,
VER_MAJOR_MINOR_MICRO)
VER_FULL)
PARSER = None
DEBUG_LEVEL = 0
#DEBUG_LEVEL = 100
# a lot of col_offset data changed from Py v3.5.0 -> v3.5.1
INC_ATTRS = (3, 5, 1) <= VER_MAJOR_MINOR_MICRO
INC_ATTRS = (3, 5, 1) <= VER_FULL
def setup():
# only setup one parser

View file

@ -16,7 +16,7 @@ from xonsh.built_ins import ensure_list_of_strs
VER_3_4 = (3, 4)
VER_3_5 = (3, 5)
VER_MAJOR_MINOR = sys.version_info[:2]
VER_MAJOR_MINOR_MICRO = sys.version_info[:3]
VER_FULL = sys.version_info[:3]
ON_MAC = (platform.system() == 'Darwin')
def sp(cmd):

View file

@ -1082,3 +1082,8 @@ def backup_file(fname):
base, ext = os.path.splitext(fname)
newfname = base + '.' + datetime.now().isoformat() + ext
shutil.move(fname, newfname)
def normabspath(p):
"""Retuns as normalized absolute path, namely, normcase(abspath(p))"""
return os.path.normcase(os.path.abspath(p))

View file

@ -1,15 +1,18 @@
"""Implements a xonsh tracer."""
import os
import sys
import inspect
import linecache
from xonsh.tools import DefaultNotGiven, print_color, pygments_version
from xonsh.tools import DefaultNotGiven, print_color, pygments_version, normabspath
from xonsh import inspectors
if pygments_version():
from xonsh import pyghooks
import pygments
import pygments.formatters.terminal
else:
pyghooks = None
from xonsh.environ import _replace_home as replace_home
class TracerType(object):
"""Represents a xonsh tracer object, which keeps track of all tracing
@ -39,37 +42,31 @@ class TracerType(object):
"""Starts tracing a file."""
if len(self.files) == 0:
self.prev_tracer = sys.gettrace()
self.files.add(filename)
self.files.add(normabspath(filename))
sys.settrace(self.trace)
print(self.files)
def stop(self, filename):
"""Stops tracing a file."""
self.files.discard(filename)
self.files.discard(normabspath(filename))
if len(self.files) == 0:
sys.settrace(self.prev_tracer)
self.prev_tracer = DefaultNotGiven
def trace(self, frame, event, arg):
"""Implements a line tracing function."""
fname = frame.f_code.co_filename
#print(f)
#print(fname, frame.f_lineno)
#if event != 'line' and event != 'call':
#if event != 'line':
# return self.trace
#fname = frame.f_code.co_filename
fname = inspectors.find_file(frame)
if fname in self.files:
print('-'*10)
#lineno = frame.f_back.f_lineno
lineno = frame.f_lineno
print(frame.f_code.co_filename, lineno)
#line = linecache.getline(fname, lineno)
line = inspect.getsource(frame)
print(line)
#s = format_line(fname, lineno, line, color=self.usecolor,
# lexer=self.lexer, formatter=self.formatter)
#print_color(s)
line = linecache.getline(fname, lineno).rstrip()
s = format_line(fname, lineno, line, color=self.usecolor,
lexer=self.lexer, formatter=self.formatter).rstrip()
print_color(s)
return self.trace
tracer = TracerType()
COLORLESS_LINE = '{fname}:{lineno}:{line}'
@ -79,11 +76,11 @@ COLOR_LINE = ('{{PURPLE}}{fname}{{BLUE}}:'
def format_line(fname, lineno, line, color=True, lexer=None, formatter=None):
"""Formats a trace line suitable for printing."""
fname = min(fname, replace_home(fname), os.path.relpath(fname), key=len)
if not color:
return COLORLESS_LINE.format(fname=fname, lineno=lineno, line=line)
if pyghooks is not None:
lexer = lexer or pyghooks.XonshLexer()
formatter = formatter or pygments.formatters.terminal.TerminalFormatter()
line = pygments.highlight(line, lexer, formatter)
print(repr(line))
return COLOR_LINE.format(fname=fname, lineno=lineno, line=line)