blob: c103b7e2e1d32d359bfd52048e989b766ed2b540 [file] [log] [blame]
Zachary Turner5f3fd802015-10-16 17:52:32 +00001import sys
2if sys.version_info[0] < 3:
3 import __builtin__ as builtins
4else:
5 import builtins
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006import code
Greg Clayton44d93782014-01-27 23:43:24 +00007import lldb
Chris Lattner30fdc8d2010-06-08 16:52:24 +00008import traceback
9
Greg Clayton44d93782014-01-27 23:43:24 +000010try:
11 import readline
12 import rlcompleter
13except ImportError:
14 have_readline = False
Todd Fiala9bb71b72014-02-26 07:39:20 +000015except AttributeError:
16 # This exception gets hit by the rlcompleter when Linux is using
17 # the readline suppression import.
18 have_readline = False
Greg Clayton44d93782014-01-27 23:43:24 +000019else:
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')
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Greg Clayton44d93782014-01-27 23:43:24 +000026g_builtin_override_called = False
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028
Greg Clayton44d93782014-01-27 23:43:24 +000029class LLDBQuitter(object):
Kate Stoneb9c1b512016-09-06 20:57:50 +000030
Greg Clayton44d93782014-01-27 23:43:24 +000031 def __init__(self, name):
32 self.name = name
Kate Stoneb9c1b512016-09-06 20:57:50 +000033
Greg Clayton44d93782014-01-27 23:43:24 +000034 def __repr__(self):
35 self()
Kate Stoneb9c1b512016-09-06 20:57:50 +000036
Greg Clayton44d93782014-01-27 23:43:24 +000037 def __call__(self, code=None):
38 global g_builtin_override_called
39 g_builtin_override_called = True
40 raise SystemExit(-1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042
Greg Clayton44d93782014-01-27 23:43:24 +000043def 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
Zachary Turner5f3fd802015-10-16 17:52:32 +000053 builtins.quit = LLDBQuitter('quit')
54 builtins.exit = LLDBQuitter('exit')
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
Greg Clayton44d93782014-01-27 23:43:24 +000056# 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
Greg Clayton44d93782014-01-27 23:43:24 +000059g_run_one_line_str = None
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
Greg Clayton44d93782014-01-27 23:43:24 +000062def get_terminal_size(fd):
63 try:
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 import fcntl
65 import termios
66 import struct
Greg Clayton44d93782014-01-27 23:43:24 +000067 hw = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
68 except:
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 hw = (0, 0)
Greg Clayton44d93782014-01-27 23:43:24 +000070 return hw
Caroline Tice2f88aad2011-01-14 00:29:16 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072
Greg Clayton44d93782014-01-27 23:43:24 +000073def readfunc_stdio(prompt):
74 sys.stdout.write(prompt)
Greg Clayton48e17572014-09-15 22:46:25 +000075 return sys.stdin.readline().rstrip()
Caroline Tice2f88aad2011-01-14 00:29:16 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077
78def run_python_interpreter(local_dict):
Greg Clayton44d93782014-01-27 23:43:24 +000079 # Pass in the dictionary, for continuity from one session to the next.
80 setquit()
81 try:
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 fd = sys.stdin.fileno()
Greg Clayton44d93782014-01-27 23:43:24 +000083 interacted = False
84 if get_terminal_size(fd)[1] == 0:
85 try:
86 import termios
87 old = termios.tcgetattr(fd)
88 if old[3] & termios.ECHO:
89 # Need to turn off echoing and restore
90 new = termios.tcgetattr(fd)
91 new[3] = new[3] & ~termios.ECHO
92 try:
93 termios.tcsetattr(fd, termios.TCSADRAIN, new)
94 interacted = True
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 code.interact(
96 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()'.",
97 readfunc=readfunc_stdio,
98 local=local_dict)
Greg Clayton44d93782014-01-27 23:43:24 +000099 finally:
100 termios.tcsetattr(fd, termios.TCSADRAIN, old)
101 except:
102 pass
103 # Don't need to turn off echoing
104 if not interacted:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 code.interact(
106 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
107 readfunc=readfunc_stdio,
108 local=local_dict)
Greg Clayton44d93782014-01-27 23:43:24 +0000109 else:
110 # We have a real interactive terminal
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 code.interact(
112 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
113 local=local_dict)
Greg Clayton44d93782014-01-27 23:43:24 +0000114 except SystemExit as e:
115 global g_builtin_override_called
116 if not g_builtin_override_called:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 print('Script exited with %s' % (e))
Caroline Tice2f88aad2011-01-14 00:29:16 +0000118
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119
120def run_one_line(local_dict, input_string):
Greg Clayton44d93782014-01-27 23:43:24 +0000121 global g_run_one_line_str
122 setquit()
123 try:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 repl = code.InteractiveConsole(local_dict)
Greg Clayton44d93782014-01-27 23:43:24 +0000125 if input_string:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 repl.runsource(input_string)
Greg Clayton44d93782014-01-27 23:43:24 +0000127 elif g_run_one_line_str:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 repl.runsource(g_run_one_line_str)
Greg Clayton44d93782014-01-27 23:43:24 +0000129
130 except SystemExit as e:
131 global g_builtin_override_called
132 if not g_builtin_override_called:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 print('Script exited with %s' % (e))