2 if sys.version_info[0] < 3:
3 import __builtin__ as builtins
15 except AttributeError:
16 # This exception gets hit by the rlcompleter when Linux is using
17 # the readline suppression import.
21 if 'libedit' in readline.__doc__:
22 readline.parse_and_bind('bind ^I rl_complete')
24 readline.parse_and_bind('tab: complete')
26 g_builtin_override_called = False
29 class LLDBQuitter(object):
31 def __init__(self, name):
37 def __call__(self, code=None):
38 global g_builtin_override_called
39 g_builtin_override_called = True
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
51 global g_builtin_override_called
52 g_builtin_override_called = False
53 builtins.quit = LLDBQuitter('quit')
54 builtins.exit = LLDBQuitter('exit')
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
59 g_run_one_line_str = None
62 def get_terminal_size(fd):
67 hw = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
73 def readfunc_stdio(prompt):
74 sys.stdout.write(prompt)
76 line = sys.stdin.readline()
77 # Readline always includes a trailing newline character unless the file
78 # ends with an incomplete line. An empty line indicates EOF.
84 def run_python_interpreter(local_dict):
85 # Pass in the dictionary, for continuity from one session to the next.
88 fd = sys.stdin.fileno()
90 if get_terminal_size(fd)[1] == 0:
93 old = termios.tcgetattr(fd)
94 if old[3] & termios.ECHO:
95 # Need to turn off echoing and restore
96 new = termios.tcgetattr(fd)
97 new[3] = new[3] & ~termios.ECHO
99 termios.tcsetattr(fd, termios.TCSADRAIN, new)
102 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()'.",
103 readfunc=readfunc_stdio,
106 termios.tcsetattr(fd, termios.TCSADRAIN, old)
109 # Don't need to turn off echoing
112 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
113 readfunc=readfunc_stdio,
116 # We have a real interactive terminal
118 banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
120 except SystemExit as e:
121 global g_builtin_override_called
122 if not g_builtin_override_called:
123 print('Script exited with %s' % (e))
126 def run_one_line(local_dict, input_string):
127 global g_run_one_line_str
130 repl = code.InteractiveConsole(local_dict)
132 repl.runsource(input_string)
133 elif g_run_one_line_str:
134 repl.runsource(g_run_one_line_str)
136 except SystemExit as e:
137 global g_builtin_override_called
138 if not g_builtin_override_called:
139 print('Script exited with %s' % (e))