Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/embedded_interpreter.py
39587 views
import sys12if sys.version_info[0] < 3:3import __builtin__ as builtins4else:5import builtins6import code7import lldb8import traceback910try:11import readline12import rlcompleter13except ImportError:14have_readline = False15except AttributeError:16# This exception gets hit by the rlcompleter when Linux is using17# the readline suppression import.18have_readline = False19else:20have_readline = True21if "libedit" in readline.__doc__:22readline.parse_and_bind("bind ^I rl_complete")23else:24readline.parse_and_bind("tab: complete")2526# When running one line, we might place the string to run in this string27# in case it would be hard to correctly escape a string's contents2829g_run_one_line_str = None303132def get_terminal_size(fd):33try:34import fcntl35import termios36import struct3738hw = struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))39except:40hw = (0, 0)41return hw424344class LLDBExit(SystemExit):45pass464748def strip_and_check_exit(line):49line = line.rstrip()50if line in ("exit", "quit"):51raise LLDBExit52return line535455def readfunc(prompt):56line = input(prompt)57return strip_and_check_exit(line)585960def readfunc_stdio(prompt):61sys.stdout.write(prompt)62sys.stdout.flush()63line = sys.stdin.readline()64# Readline always includes a trailing newline character unless the file65# ends with an incomplete line. An empty line indicates EOF.66if not line:67raise EOFError68return strip_and_check_exit(line)697071def run_python_interpreter(local_dict):72# Pass in the dictionary, for continuity from one session to the next.73try:74fd = sys.stdin.fileno()75interacted = False76if get_terminal_size(fd)[1] == 0:77try:78import termios7980old = termios.tcgetattr(fd)81if old[3] & termios.ECHO:82# Need to turn off echoing and restore83new = termios.tcgetattr(fd)84new[3] = new[3] & ~termios.ECHO85try:86termios.tcsetattr(fd, termios.TCSADRAIN, new)87interacted = True88code.interact(89banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()'.",90readfunc=readfunc_stdio,91local=local_dict,92)93finally:94termios.tcsetattr(fd, termios.TCSADRAIN, old)95except:96pass97# Don't need to turn off echoing98if not interacted:99code.interact(100banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",101readfunc=readfunc_stdio,102local=local_dict,103)104else:105# We have a real interactive terminal106code.interact(107banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",108readfunc=readfunc,109local=local_dict,110)111except LLDBExit:112pass113except SystemExit as e:114if e.code:115print("Script exited with code %s" % e.code)116117118def run_one_line(local_dict, input_string):119global g_run_one_line_str120try:121input_string = strip_and_check_exit(input_string)122repl = code.InteractiveConsole(local_dict)123if input_string:124# A newline is appended to support one-line statements containing125# control flow. For example "if True: print(1)" silently does126# nothing, but works with a newline: "if True: print(1)\n".127input_string += "\n"128repl.runsource(input_string)129elif g_run_one_line_str:130repl.runsource(g_run_one_line_str)131except LLDBExit:132pass133except SystemExit as e:134if e.code:135print("Script exited with code %s" % e.code)136137138