[llvm-branch-commits] [lldb] r199524 - Fixed some issues with the embedded python interpreter:

Greg Clayton gclayton at apple.com
Fri Jan 17 15:11:38 PST 2014


Author: gclayton
Date: Fri Jan 17 17:11:38 2014
New Revision: 199524

URL: http://llvm.org/viewvc/llvm-project?rev=199524&view=rev
Log:
Fixed some issues with the embedded python interpreter:
1 - remove duplicate run_python_interpreter function
2 - if sys.stdin is a terminal with a valid width, then allow python readline to be used
3 - if sys.stdin in NOT a terminal with a valid width, then use a custom read function that doesn't use readline
4 - turn off echo on the terminal in step 3 if it is enabled and restore it afterward


Modified:
    lldb/branches/iohandler/source/Interpreter/embedded_interpreter.py

Modified: lldb/branches/iohandler/source/Interpreter/embedded_interpreter.py
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/iohandler/source/Interpreter/embedded_interpreter.py?rev=199524&r1=199523&r2=199524&view=diff
==============================================================================
--- lldb/branches/iohandler/source/Interpreter/embedded_interpreter.py (original)
+++ lldb/branches/iohandler/source/Interpreter/embedded_interpreter.py Fri Jan 17 17:11:38 2014
@@ -41,35 +41,62 @@ def setquit():
     __builtin__.quit = LLDBQuitter('quit')
     __builtin__.exit = LLDBQuitter('exit')
 
-def run_python_interpreter (dict):
-    global g_builtin_override_called
-    setquit()
-    try:
-        code.interact ("Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.", None, dict)
-    except SystemExit as e:
-        if not g_builtin_override_called:
-            print 'Script exited with %s' %(e)
-
 # When running one line, we might place the string to run in this string
 # in case it would be hard to correctly escape a string's contents
 
 g_run_one_line_str = None
 
-def run_python_interpreter (dict):
+
+def get_terminal_size(fd):
+    try:
+        import fcntl, termios, struct
+        hw = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
+    except:
+        hw = (0,0)
+    return hw
+
+def readfunc_stdio(prompt):
+    sys.stdout.write(prompt)
+    return sys.stdin.readline()
+
+def run_python_interpreter (local_dict):
     # Pass in the dictionary, for continuity from one session to the next.
     setquit()
     try:
-        code.interact(banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.", local=dict)
+        fd = sys.stdin.fileno();
+        interacted = False
+        if get_terminal_size(fd)[1] == 0:
+            try:
+                import termios
+                old = termios.tcgetattr(fd)
+                if old[3] & termios.ECHO:
+                    # Need to turn off echoing and restore
+                    new = termios.tcgetattr(fd)
+                    new[3] = new[3] & ~termios.ECHO
+                    try:
+                        termios.tcsetattr(fd, termios.TCSADRAIN, new)
+                        interacted = True
+                        code.interact(banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.", readfunc=readfunc_stdio, local=local_dict)
+                    finally:
+                        termios.tcsetattr(fd, termios.TCSADRAIN, old)
+            except:
+                pass
+            # Don't need to turn off echoing
+            if not interacted:
+                code.interact(banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.", readfunc=readfunc_stdio, local=local_dict)
+        else:
+            # We have a real interactive terminal
+            code.interact(banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.", local=local_dict)
     except SystemExit as e:
         global g_builtin_override_called
         if not g_builtin_override_called:
             print 'Script exited with %s' %(e)
 
-def run_one_line (dict, input_string):
+def run_one_line (local_dict, input_string):
     global g_run_one_line_str
     setquit()
     try:
-        repl = code.InteractiveConsole(dict);
+        repl = code.InteractiveConsole(local_dict);
         if input_string:
             repl.runsource (input_string)
         elif g_run_one_line_str:





More information about the llvm-branch-commits mailing list