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 if "libedit" in readline.__doc__:
19 readline.parse_and_bind("bind ^I rl_complete")
20 else:
21 readline.parse_and_bind("tab: complete")
22
23# When running one line, we might place the string to run in this string
24# in case it would be hard to correctly escape a string's contents
25
26g_run_one_line_str = None
27
28
30 try:
31 import fcntl
32 import termios
33 import struct
34
35 hw = struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))
36 except:
37 hw = (0, 0)
38 return hw
39
40
42 pass
43
44
46 line = line.rstrip()
47 if line in ("exit", "quit"):
48 raise LLDBExit
49 return line
50
51
52def readfunc(prompt):
53 line = input(prompt)
54 return strip_and_check_exit(line)
55
56
57def readfunc_stdio(prompt):
58 sys.stdout.write(prompt)
59 sys.stdout.flush()
60 line = sys.stdin.readline()
61 # Readline always includes a trailing newline character unless the file
62 # ends with an incomplete line. An empty line indicates EOF.
63 if not line:
64 raise EOFError
65 return strip_and_check_exit(line)
66
67
68def run_python_interpreter(local_dict):
69 # Pass in the dictionary, for continuity from one session to the next.
70 try:
71 fd = sys.stdin.fileno()
72 interacted = False
73 if get_terminal_size(fd)[1] == 0:
74 try:
75 import termios
76
77 old = termios.tcgetattr(fd)
78 if old[3] & termios.ECHO:
79 # Need to turn off echoing and restore
80 new = termios.tcgetattr(fd)
81 new[3] = new[3] & ~termios.ECHO
82 try:
83 termios.tcsetattr(fd, termios.TCSADRAIN, new)
84 interacted = True
85 code.interact(
86 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()'.",
87 readfunc=readfunc_stdio,
88 local=local_dict,
89 )
90 finally:
91 termios.tcsetattr(fd, termios.TCSADRAIN, old)
92 except:
93 pass
94 # Don't need to turn off echoing
95 if not interacted:
96 code.interact(
97 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
98 readfunc=readfunc_stdio,
99 local=local_dict,
100 )
101 else:
102 # We have a real interactive terminal
103 code.interact(
104 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
105 readfunc=readfunc,
106 local=local_dict,
107 )
108 except LLDBExit:
109 pass
110 except SystemExit as e:
111 if e.code:
112 print("Script exited with code %s" % e.code)
113
114
115def run_one_line(local_dict, input_string):
116 global g_run_one_line_str
117 try:
118 input_string = strip_and_check_exit(input_string)
119 repl = code.InteractiveConsole(local_dict)
120 if input_string:
121 # A newline is appended to support one-line statements containing
122 # control flow. For example "if True: print(1)" silently does
123 # nothing, but works with a newline: "if True: print(1)\n".
124 input_string += "\n"
125 repl.runsource(input_string)
126 elif g_run_one_line_str:
127 repl.runsource(g_run_one_line_str)
128 except LLDBExit:
129 pass
130 except SystemExit as e:
131 if e.code:
132 print("Script exited with code %s" % e.code)
run_one_line(local_dict, input_string)