1# 2# Copyright (c) 2004-2018 JEP AUTHORS. 3# 4# This file is licensed under the the zlib/libpng License. 5# 6# This software is provided 'as-is', without any express or implied 7# warranty. In no event will the authors be held liable for any 8# damages arising from the use of this software. 9# 10# Permission is granted to anyone to use this software for any 11# purpose, including commercial applications, and to alter it and 12# redistribute it freely, subject to the following restrictions: 13# 14# 1. The origin of this software must not be misrepresented; you 15# must not claim that you wrote the original software. If you use 16# this software in a product, an acknowledgment in the product 17# documentation would be appreciated but is not required. 18# 19# 2. Altered source versions must be plainly marked as such, and 20# must not be misrepresented as being the original software. 21# 22# 3. This notice may not be removed or altered from any source 23# distribution. 24# 25 26from __future__ import print_function 27import traceback 28import os 29import sys 30 31history_file = None 32 33# python 3 renamed raw_input to input 34if sys.version_info[0] >= 3: 35 raw_input = input 36 37has_readline = False 38 39try: 40 import readline 41 has_readline = True 42except ImportError: 43 try: 44 import pyreadline as readline 45 has_readline = True 46 except ImportError: 47 msg = """ 48 No readline available. History will not be available. 49 """ 50 if os.name == "posix": 51 msg += """ 52 You may want to set the LD_PRELOAD environment variable, see the 53 README file for details. 54 55 i.e.: export LD_PRELOAD=/usr/lib/libpython2.7.so.1.0 56 """ 57 elif os.name == "nt": 58 msg += """ 59 For Windows use pyreadline and get it from the official git 60 repo on github: 61 https://github.com/pyreadline/pyreadline 62 63 Do NOT use the version on pypi.python.org, and therefore 64 Do NOT use the version installed by pip. It is out of date 65 and doesn't work with Jep! 66 """ 67 print(msg) 68except OSError as e: 69 if hasattr(e, 'winerror'): 70 print("Windows error importing readline: " + str(e)) 71 print("Please try using the latest pyreadline from https://github.com/pyreadline/pyreadline") 72 else: 73 print("Error importing readline: " + str(e)) 74 75if has_readline: 76 try: 77 import rlcompleter 78 readline.set_completer(rlcompleter.Completer(locals()).complete) 79 readline.parse_and_bind("tab: complete") 80 except: 81 pass 82 try: 83 history_file = os.path.join(os.path.expanduser('~'), '.jep') 84 if not os.path.exists(history_file): 85 readline.write_history_file(history_file) 86 else: 87 readline.read_history_file(history_file) 88 except IOError as err: 89 pass 90 91 92PS1 = ">>> " 93PS2 = "... " 94 95evalLines = [] 96 97def jepeval(line): 98 global evalLines 99 if not line: 100 if evalLines: 101 code = "\n".join(evalLines) 102 evalLines = None 103 exec(compile(code, '<stdin>', 'single'), globals(), globals()) 104 return True 105 elif not evalLines: 106 try: 107 exec(compile(line, '<stdin>', 'single'), globals(), globals()) 108 return True 109 except SyntaxError as err: 110 evalLines = [line] 111 return False 112 else: 113 evalLines.append(line) 114 return False 115 116def prompt(jep): 117 try: 118 line = None 119 while True: 120 ran = True 121 try: 122 ran = jepeval(line) 123 except Exception as err: 124 printedErr = False 125 try: 126 if len(err.args): 127 if 'printStackTrace' in dir(err.args[0]): 128 err.args[0].printStackTrace() 129 printedErr = True 130 except Exception as exc: 131 print("Error printing stacktrace:", str(exc)) 132 finally: 133 if not printedErr: 134 print(str(err)) 135 136 try: 137 if ran: 138 line = raw_input(PS1) 139 else: 140 line = raw_input(PS2) 141 except: 142 break 143 144 finally: 145 if has_readline: 146 try: 147 readline.write_history_file(history_file) 148 except IOError as err: 149 pass 150