[pypy-commit] pypy reverse-debugger: Add --color=dark or --color=light options to ask for colorized source code

arigo pypy.commits at gmail.com
Sun Jul 10 05:07:23 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: reverse-debugger
Changeset: r85645:6d9691593717
Date: 2016-07-10 11:08 +0200
http://bitbucket.org/pypy/pypy/changeset/6d9691593717/

Log:	Add --color=dark or --color=light options to ask for colorized
	source code

diff --git a/rpython/translator/revdb/interact.py b/rpython/translator/revdb/interact.py
--- a/rpython/translator/revdb/interact.py
+++ b/rpython/translator/revdb/interact.py
@@ -1,6 +1,6 @@
 import sys, os, re
 import subprocess, socket
-import traceback
+import traceback, linecache
 from contextlib import contextmanager
 try:
     import readline
@@ -16,7 +16,8 @@
 
 class RevDebugControl(object):
 
-    def __init__(self, revdb_log_filename, executable=None):
+    def __init__(self, revdb_log_filename, executable=None,
+                 pygments_background=None):
         with open(revdb_log_filename, 'rb') as f:
             header = f.readline()
         assert header.endswith('\n')
@@ -28,7 +29,9 @@
             executable = fields[1]
         if not os.path.isfile(executable):
             raise ValueError("executable %r not found" % (executable,))
-        self.pgroup = ReplayProcessGroup(executable, revdb_log_filename)
+        linecacheoutput = self.getlinecacheoutput(pygments_background)
+        self.pgroup = ReplayProcessGroup(executable, revdb_log_filename,
+                                         linecacheoutput)
         self.print_extra_pending_info = None
 
     def interact(self):
@@ -365,3 +368,21 @@
         #
         self._bp_new(argument, 'W', compiled_code, nids=nids)
         self.pgroup.update_watch_values()
+
+    def getlinecacheoutput(self, pygments_background):
+        if not pygments_background or pygments_background == 'off':
+            return
+        try:
+            from pygments import highlight
+            from pygments.lexers import PythonLexer
+            from pygments.formatters import TerminalFormatter
+        except ImportError:
+            return None
+        #
+        lexer = PythonLexer()
+        fmt = TerminalFormatter(bg=pygments_background)
+        #
+        def linecacheoutput(filename, lineno):
+            line = linecache.getline(filename, lineno)
+            return highlight(line, lexer, fmt)
+        return linecacheoutput
diff --git a/rpython/translator/revdb/process.py b/rpython/translator/revdb/process.py
--- a/rpython/translator/revdb/process.py
+++ b/rpython/translator/revdb/process.py
@@ -65,7 +65,8 @@
 
     def __init__(self, pid, control_socket,
                  breakpoints_cache=AllBreakpoints(),
-                 printed_objects=frozenset()):
+                 printed_objects=frozenset(),
+                 linecacheoutput=None):
         self.pid = pid
         self.control_socket = control_socket
         self.tainted = False
@@ -75,6 +76,7 @@
         #     either already discovered in this child
         #     (if uid < currently_created_objects), or that will
         #     automatically be discovered when we move forward
+        self.linecacheoutput = linecacheoutput or linecache.getline
 
     def _recv_all(self, size):
         pieces = []
@@ -134,7 +136,8 @@
         self.expect_ready()
         other = ReplayProcess(child_pid, s1,
                               breakpoints_cache=self.breakpoints_cache,
-                              printed_objects=self.printed_objects)
+                              printed_objects=self.printed_objects,
+                              linecacheoutput=self.linecacheoutput)
         other.expect_ready()
         #print >> sys.stderr, 'CLONED', self.current_time
         return other
@@ -184,7 +187,7 @@
                 self.update_times(msg)
                 break
             elif msg.cmd == ANSWER_LINECACHE:
-                line = linecache.getline(msg.extra, msg.arg1)
+                line = self.linecacheoutput(msg.extra, msg.arg1)
                 if line == '':
                     line = '?'
                 if msg.arg2:    # strip?
@@ -215,13 +218,14 @@
     STEP_RATIO = 0.25           # subprocess n is between subprocess n-1
                                 #   and the end, at this fraction of interval
 
-    def __init__(self, executable, revdb_log_filename):
+    def __init__(self, executable, revdb_log_filename, linecacheoutput=None):
         s1, s2 = socket.socketpair()
         initial_subproc = subprocess.Popen(
             [executable, '--revdb-replay', revdb_log_filename,
              str(s2.fileno())])
         s2.close()
-        child = ReplayProcess(initial_subproc.pid, s1)
+        child = ReplayProcess(initial_subproc.pid, s1,
+                              linecacheoutput=linecacheoutput)
         msg = child.expect(ANSWER_INIT, INIT_VERSION_NUMBER, Ellipsis)
         self.total_stop_points = msg.arg2
         if self.total_stop_points == 0:
diff --git a/rpython/translator/revdb/revdb.py b/rpython/translator/revdb/revdb.py
--- a/rpython/translator/revdb/revdb.py
+++ b/rpython/translator/revdb/revdb.py
@@ -10,11 +10,14 @@
     parser.add_argument('-x', '--executable', dest='executable',
                         help='name of the executable file '
                              'that recorded the log')
+    parser.add_argument('-c', '--color', dest='color',
+                        help='colorize source code (dark,light,off)')
     options = parser.parse_args()
 
     sys.path.insert(0, os.path.abspath(
         os.path.join(__file__, '..', '..', '..', '..')))
 
     from rpython.translator.revdb.interact import RevDebugControl
-    ctrl = RevDebugControl(options.log, executable=options.executable)
+    ctrl = RevDebugControl(options.log, executable=options.executable,
+                           pygments_background=options.color)
     ctrl.interact()


More information about the pypy-commit mailing list