llvm-project
136 строк · 4.0 Кб
1import sys2
3if sys.version_info[0] < 3:4import __builtin__ as builtins5else:6import builtins7import code8import lldb9import traceback10
11try:12import readline13import rlcompleter14except ImportError:15have_readline = False16except AttributeError:17# This exception gets hit by the rlcompleter when Linux is using18# the readline suppression import.19have_readline = False20else:21have_readline = True22if "libedit" in readline.__doc__:23readline.parse_and_bind("bind ^I rl_complete")24else:25readline.parse_and_bind("tab: complete")26
27# When running one line, we might place the string to run in this string
28# in case it would be hard to correctly escape a string's contents
29
30g_run_one_line_str = None31
32
33def get_terminal_size(fd):34try:35import fcntl36import termios37import struct38
39hw = struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))40except:41hw = (0, 0)42return hw43
44
45class LLDBExit(SystemExit):46pass47
48
49def strip_and_check_exit(line):50line = line.rstrip()51if line in ("exit", "quit"):52raise LLDBExit53return line54
55
56def readfunc(prompt):57line = input(prompt)58return strip_and_check_exit(line)59
60
61def readfunc_stdio(prompt):62sys.stdout.write(prompt)63sys.stdout.flush()64line = sys.stdin.readline()65# Readline always includes a trailing newline character unless the file66# ends with an incomplete line. An empty line indicates EOF.67if not line:68raise EOFError69return strip_and_check_exit(line)70
71
72def run_python_interpreter(local_dict):73# Pass in the dictionary, for continuity from one session to the next.74try:75fd = sys.stdin.fileno()76interacted = False77if get_terminal_size(fd)[1] == 0:78try:79import termios80
81old = termios.tcgetattr(fd)82if old[3] & termios.ECHO:83# Need to turn off echoing and restore84new = termios.tcgetattr(fd)85new[3] = new[3] & ~termios.ECHO86try:87termios.tcsetattr(fd, termios.TCSADRAIN, new)88interacted = True89code.interact(90banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()'.",91readfunc=readfunc_stdio,92local=local_dict,93)94finally:95termios.tcsetattr(fd, termios.TCSADRAIN, old)96except:97pass98# Don't need to turn off echoing99if not interacted:100code.interact(101banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",102readfunc=readfunc_stdio,103local=local_dict,104)105else:106# We have a real interactive terminal107code.interact(108banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",109readfunc=readfunc,110local=local_dict,111)112except LLDBExit:113pass114except SystemExit as e:115if e.code:116print("Script exited with code %s" % e.code)117
118
119def run_one_line(local_dict, input_string):120global g_run_one_line_str121try:122input_string = strip_and_check_exit(input_string)123repl = code.InteractiveConsole(local_dict)124if input_string:125# A newline is appended to support one-line statements containing126# control flow. For example "if True: print(1)" silently does127# nothing, but works with a newline: "if True: print(1)\n".128input_string += "\n"129repl.runsource(input_string)130elif g_run_one_line_str:131repl.runsource(g_run_one_line_str)132except LLDBExit:133pass134except SystemExit as e:135if e.code:136print("Script exited with code %s" % e.code)137