1# Copyright 2000-2010 Michael Hudson-Doyle <micahel@gmail.com> 2# Armin Rigo 3# 4# All Rights Reserved 5# 6# 7# Permission to use, copy, modify, and distribute this software and 8# its documentation for any purpose is hereby granted without fee, 9# provided that the above copyright notice appear in all copies and 10# that both that copyright notice and this permission notice appear in 11# supporting documentation. 12# 13# THE AUTHOR MICHAEL HUDSON DISCLAIMS ALL WARRANTIES WITH REGARD TO 14# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 15# AND FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 16# INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER 17# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF 18# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 19# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 21"""This is an alternative to python_reader which tries to emulate 22the CPython prompt as closely as possible, with the exception of 23allowing multiline input and multiline history entries. 24""" 25 26import sys 27from pyrepl.readline import multiline_input, _error, _get_reader 28 29 30def check(): # returns False if there is a problem initializing the state 31 try: 32 _get_reader() 33 except _error: 34 return False 35 return True 36 37 38def run_multiline_interactive_console(mainmodule=None, future_flags=0): 39 import code 40 import __main__ 41 mainmodule = mainmodule or __main__ 42 console = code.InteractiveConsole(mainmodule.__dict__, filename='<stdin>') 43 if future_flags: 44 console.compile.compiler.flags |= future_flags 45 46 def more_lines(unicodetext): 47 # ooh, look at the hack: 48 src = "#coding:utf-8\n"+unicodetext.encode('utf-8') 49 try: 50 code = console.compile(src, '<stdin>', 'single') 51 except (OverflowError, SyntaxError, ValueError): 52 return False 53 else: 54 return code is None 55 56 while 1: 57 try: 58 ps1 = getattr(sys, 'ps1', '>>> ') 59 ps2 = getattr(sys, 'ps2', '... ') 60 try: 61 statement = multiline_input(more_lines, ps1, ps2, 62 returns_unicode=True) 63 except EOFError: 64 break 65 more = console.push(statement) 66 assert not more 67 except KeyboardInterrupt: 68 console.write("\nKeyboardInterrupt\n") 69 console.resetbuffer() 70