]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Interpreter/embedded_interpreter.py
Merge lldb trunk r366426, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Interpreter / embedded_interpreter.py
1 import sys
2 if sys.version_info[0] < 3:
3     import __builtin__ as builtins
4 else:
5     import builtins
6 import code
7 import lldb
8 import traceback
9
10 try:
11     import readline
12     import rlcompleter
13 except ImportError:
14     have_readline = False
15 except AttributeError:
16     # This exception gets hit by the rlcompleter when Linux is using
17     # the readline suppression import.
18     have_readline = False
19 else:
20     have_readline = True
21     if 'libedit' in readline.__doc__:
22         readline.parse_and_bind('bind ^I rl_complete')
23     else:
24         readline.parse_and_bind('tab: complete')
25
26 g_builtin_override_called = False
27
28
29 class LLDBQuitter(object):
30
31     def __init__(self, name):
32         self.name = name
33
34     def __repr__(self):
35         self()
36
37     def __call__(self, code=None):
38         global g_builtin_override_called
39         g_builtin_override_called = True
40         raise SystemExit(-1)
41
42
43 def setquit():
44     '''Redefine builtin functions 'quit()' and 'exit()' to print a message and raise an EOFError exception.'''
45     # This function will be called prior to each interactive
46     # interpreter loop or each single line, so we set the global
47     # g_builtin_override_called to False so we know if a SystemExit
48     # is thrown, we can catch it and tell the difference between
49     # a call to "quit()" or "exit()" and something like
50     # "sys.exit(123)"
51     global g_builtin_override_called
52     g_builtin_override_called = False
53     builtins.quit = LLDBQuitter('quit')
54     builtins.exit = LLDBQuitter('exit')
55
56 # When running one line, we might place the string to run in this string
57 # in case it would be hard to correctly escape a string's contents
58
59 g_run_one_line_str = None
60
61
62 def get_terminal_size(fd):
63     try:
64         import fcntl
65         import termios
66         import struct
67         hw = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
68     except:
69         hw = (0, 0)
70     return hw
71
72
73 def readfunc_stdio(prompt):
74     sys.stdout.write(prompt)
75     sys.stdout.flush()
76     return sys.stdin.readline().rstrip()
77
78
79 def run_python_interpreter(local_dict):
80     # Pass in the dictionary, for continuity from one session to the next.
81     setquit()
82     try:
83         fd = sys.stdin.fileno()
84         interacted = False
85         if get_terminal_size(fd)[1] == 0:
86             try:
87                 import termios
88                 old = termios.tcgetattr(fd)
89                 if old[3] & termios.ECHO:
90                     # Need to turn off echoing and restore
91                     new = termios.tcgetattr(fd)
92                     new[3] = new[3] & ~termios.ECHO
93                     try:
94                         termios.tcsetattr(fd, termios.TCSADRAIN, new)
95                         interacted = True
96                         code.interact(
97                             banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()'.",
98                             readfunc=readfunc_stdio,
99                             local=local_dict)
100                     finally:
101                         termios.tcsetattr(fd, termios.TCSADRAIN, old)
102             except:
103                 pass
104             # Don't need to turn off echoing
105             if not interacted:
106                 code.interact(
107                     banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
108                     readfunc=readfunc_stdio,
109                     local=local_dict)
110         else:
111             # We have a real interactive terminal
112             code.interact(
113                 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
114                 local=local_dict)
115     except SystemExit as e:
116         global g_builtin_override_called
117         if not g_builtin_override_called:
118             print('Script exited with %s' % (e))
119
120
121 def run_one_line(local_dict, input_string):
122     global g_run_one_line_str
123     setquit()
124     try:
125         repl = code.InteractiveConsole(local_dict)
126         if input_string:
127             repl.runsource(input_string)
128         elif g_run_one_line_str:
129             repl.runsource(g_run_one_line_str)
130
131     except SystemExit as e:
132         global g_builtin_override_called
133         if not g_builtin_override_called:
134             print('Script exited with %s' % (e))