LLDB mainline
embedded_interpreter.py
Go to the documentation of this file.
1import sys
2import builtins
3import code
4import lldb
5import traceback
6
7try:
8 import readline
9 import rlcompleter
10except ImportError:
11 have_readline = False
12except AttributeError:
13 # This exception gets hit by the rlcompleter when Linux is using
14 # the readline suppression import.
15 have_readline = False
16else:
17 have_readline = True
18
20 if hasattr(readline, "backend"):
21 return readline.backend == "editline"
22 return "libedit" in getattr(readline, "__doc__", "")
23
24 if is_libedit():
25 readline.parse_and_bind("bind ^I rl_complete")
26 else:
27 readline.parse_and_bind("tab: complete")
28
29# When running one line, we might place the string to run in this string
30# in case it would be hard to correctly escape a string's contents
31
32g_run_one_line_str = None
33
34
36 pass
37
38
40 line = line.rstrip()
41 if line in ("exit", "quit"):
42 raise LLDBExit
43 return line
44
45
46def readfunc(prompt):
47 line = input(prompt)
48 return strip_and_check_exit(line)
49
50
51def readfunc_stdio(prompt):
52 sys.stdout.write(prompt)
53 sys.stdout.flush()
54 line = sys.stdin.readline()
55 # Readline always includes a trailing newline character unless the file
56 # ends with an incomplete line. An empty line indicates EOF.
57 if not line:
58 raise EOFError
59 return strip_and_check_exit(line)
60
61
62def run_python_interpreter(local_dict):
63 # Pass in the dictionary, for continuity from one session to the next.
64 try:
65 banner = "Python Interactive Interpreter. To exit, type 'quit()', 'exit()'."
66 input_func = readfunc_stdio
67
68 is_atty = sys.stdin.isatty()
69 if is_atty:
70 banner = "Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D."
71 input_func = readfunc
72
73 code.interact(banner=banner, readfunc=input_func, local=local_dict)
74 except LLDBExit:
75 pass
76 except SystemExit as e:
77 if e.code:
78 print("Script exited with code %s" % e.code)
79
80def run_one_line(local_dict, input_string):
81 global g_run_one_line_str
82 try:
83 input_string = strip_and_check_exit(input_string)
84 repl = code.InteractiveConsole(local_dict)
85 if input_string:
86 # A newline is appended to support one-line statements containing
87 # control flow. For example "if True: print(1)" silently does
88 # nothing, but works with a newline: "if True: print(1)\n".
89 input_string += "\n"
90 repl.runsource(input_string)
91 elif g_run_one_line_str:
92 repl.runsource(g_run_one_line_str)
93 except LLDBExit:
94 pass
95 except SystemExit as e:
96 if e.code:
97 print("Script exited with code %s" % e.code)
run_one_line(local_dict, input_string)