3 if sys
.version_info
[0] < 3:
4 import __builtin__
as builtins
16 except AttributeError:
17 # This exception gets hit by the rlcompleter when Linux is using
18 # the readline suppression import.
22 if "libedit" in readline
.__doc
__:
23 readline
.parse_and_bind("bind ^I rl_complete")
25 readline
.parse_and_bind("tab: complete")
27 # When running one line, we might place the string to run in this string
28 # in case it would be hard to correctly escape a string's contents
30 g_run_one_line_str
= None
33 def get_terminal_size(fd
):
39 hw
= struct
.unpack("hh", fcntl
.ioctl(fd
, termios
.TIOCGWINSZ
, "1234"))
45 class LLDBExit(SystemExit):
49 def strip_and_check_exit(line
):
51 if line
in ("exit", "quit"):
58 return strip_and_check_exit(line
)
61 def readfunc_stdio(prompt
):
62 sys
.stdout
.write(prompt
)
64 line
= sys
.stdin
.readline()
65 # Readline always includes a trailing newline character unless the file
66 # ends with an incomplete line. An empty line indicates EOF.
69 return strip_and_check_exit(line
)
72 def run_python_interpreter(local_dict
):
73 # Pass in the dictionary, for continuity from one session to the next.
75 fd
= sys
.stdin
.fileno()
77 if get_terminal_size(fd
)[1] == 0:
81 old
= termios
.tcgetattr(fd
)
82 if old
[3] & termios
.ECHO
:
83 # Need to turn off echoing and restore
84 new
= termios
.tcgetattr(fd
)
85 new
[3] = new
[3] & ~termios
.ECHO
87 termios
.tcsetattr(fd
, termios
.TCSADRAIN
, new
)
90 banner
="Python Interactive Interpreter. To exit, type 'quit()', 'exit()'.",
91 readfunc
=readfunc_stdio
,
95 termios
.tcsetattr(fd
, termios
.TCSADRAIN
, old
)
98 # Don't need to turn off echoing
101 banner
="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
102 readfunc
=readfunc_stdio
,
106 # We have a real interactive terminal
108 banner
="Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.",
114 except SystemExit as e
:
116 print("Script exited with code %s" % e
.code
)
119 def run_one_line(local_dict
, input_string
):
120 global g_run_one_line_str
122 input_string
= strip_and_check_exit(input_string
)
123 repl
= code
.InteractiveConsole(local_dict
)
125 # A newline is appended to support one-line statements containing
126 # control flow. For example "if True: print(1)" silently does
127 # nothing, but works with a newline: "if True: print(1)\n".
129 repl
.runsource(input_string
)
130 elif g_run_one_line_str
:
131 repl
.runsource(g_run_one_line_str
)
134 except SystemExit as e
:
136 print("Script exited with code %s" % e
.code
)