LLDB
mainline
llvm-project
lldb
source
Interpreter
embedded_interpreter.py
Go to the documentation of this file.
1
import
sys
2
import
builtins
3
import
code
4
import
lldb
5
import
traceback
6
7
try
:
8
import
readline
9
import
rlcompleter
10
except
ImportError:
11
have_readline =
False
12
except
AttributeError:
13
# This exception gets hit by the rlcompleter when Linux is using
14
# the readline suppression import.
15
have_readline =
False
16
else
:
17
have_readline =
True
18
19
def
is_libedit
():
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
32
g_run_one_line_str =
None
33
34
35
class
LLDBExit
(
SystemExit
):
36
pass
37
38
39
def
strip_and_check_exit
(line):
40
line = line.rstrip()
41
if
line
in
(
"exit"
,
"quit"
):
42
raise
LLDBExit
43
return
line
44
45
46
def
readfunc
(prompt):
47
line = input(prompt)
48
return
strip_and_check_exit
(line)
49
50
51
def
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
62
def
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
80
def
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)
SystemExit
embedded_interpreter.LLDBExit
Definition
embedded_interpreter.py:35
embedded_interpreter.readfunc
readfunc(prompt)
Definition
embedded_interpreter.py:46
embedded_interpreter.readfunc_stdio
readfunc_stdio(prompt)
Definition
embedded_interpreter.py:51
embedded_interpreter.run_one_line
run_one_line(local_dict, input_string)
Definition
embedded_interpreter.py:80
embedded_interpreter.is_libedit
is_libedit()
Definition
embedded_interpreter.py:19
embedded_interpreter.strip_and_check_exit
strip_and_check_exit(line)
Definition
embedded_interpreter.py:39
embedded_interpreter.run_python_interpreter
run_python_interpreter(local_dict)
Definition
embedded_interpreter.py:62
Generated on
for LLDB by
1.14.0