14 if 'libedit' in readline.__doc__:
15 readline.parse_and_bind('bind ^I rl_complete')
17 readline.parse_and_bind('tab: complete')
19 g_builtin_override_called = False
21 class LLDBQuitter(object):
22 def __init__(self, name):
26 def __call__(self, code=None):
27 global g_builtin_override_called
28 g_builtin_override_called = True
32 '''Redefine builtin functions 'quit()' and 'exit()' to print a message and raise an EOFError exception.'''
33 # This function will be called prior to each interactive
34 # interpreter loop or each single line, so we set the global
35 # g_builtin_override_called to False so we know if a SystemExit
36 # is thrown, we can catch it and tell the difference between
37 # a call to "quit()" or "exit()" and something like
39 global g_builtin_override_called
40 g_builtin_override_called = False
41 __builtin__.quit = LLDBQuitter('quit')
42 __builtin__.exit = LLDBQuitter('exit')
44 # When running one line, we might place the string to run in this string
45 # in case it would be hard to correctly escape a string's contents
47 g_run_one_line_str = None
50 def get_terminal_size(fd):
52 import fcntl, termios, struct
53 hw = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
58 def readfunc_stdio(prompt):
59 sys.stdout.write(prompt)
60 return sys.stdin.readline()
62 def run_python_interpreter (local_dict):
63 # Pass in the dictionary, for continuity from one session to the next.
66 fd = sys.stdin.fileno();
68 if get_terminal_size(fd)[1] == 0:
71 old = termios.tcgetattr(fd)
72 if old[3] & termios.ECHO:
73 # Need to turn off echoing and restore
74 new = termios.tcgetattr(fd)
75 new[3] = new[3] & ~termios.ECHO
77 termios.tcsetattr(fd, termios.TCSADRAIN, new)
79 code.interact(banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()'.", readfunc=readfunc_stdio, local=local_dict)
81 termios.tcsetattr(fd, termios.TCSADRAIN, old)
84 # Don't need to turn off echoing
86 code.interact(banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.", readfunc=readfunc_stdio, local=local_dict)
88 # We have a real interactive terminal
89 code.interact(banner="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.", local=local_dict)
90 except SystemExit as e:
91 global g_builtin_override_called
92 if not g_builtin_override_called:
93 print 'Script exited with %s' %(e)
95 def run_one_line (local_dict, input_string):
96 global g_run_one_line_str
99 repl = code.InteractiveConsole(local_dict);
101 repl.runsource (input_string)
102 elif g_run_one_line_str:
103 repl.runsource (g_run_one_line_str)
105 except SystemExit as e:
106 global g_builtin_override_called
107 if not g_builtin_override_called:
108 print 'Script exited with %s' %(e)