| 13 | __all__ = ["ExitConsole", "run_shell"] |
| 14 | |
| 15 | class HistoryConsole(code.InteractiveConsole): |
| 16 | def __init__(self, locals=None, filename="<console>", |
| 17 | histfile=os.path.expanduser("~/.m1n1-history")): |
| 18 | code.InteractiveConsole.__init__(self, locals, filename) |
| 19 | self.histfile = histfile |
| 20 | self.init_history(histfile) |
| 21 | self.poll_func = None |
| 22 | |
| 23 | def init_history(self, histfile): |
| 24 | readline.parse_and_bind("tab: complete") |
| 25 | if hasattr(readline, "read_history_file"): |
| 26 | try: |
| 27 | readline.read_history_file(histfile) |
| 28 | except FileNotFoundError: |
| 29 | pass |
| 30 | except PermissionError as e: |
| 31 | print(f"Failed reading history from {histfile}: {e}", file=sys.stderr) |
| 32 | if sys.platform == "darwin": |
| 33 | print(f" On macOS this may be caused by extended attributes. " |
| 34 | f"Try: xattr -c {histfile}", file=sys.stderr) |
| 35 | |
| 36 | def save_history(self): |
| 37 | readline.set_history_length(10000) |
| 38 | try: |
| 39 | readline.write_history_file(self.histfile) |
| 40 | except OSError as e: |
| 41 | print(f"Failed writing history to {self.histfile}: {e}", file=sys.stderr) |
| 42 | |
| 43 | def showtraceback(self): |
| 44 | type, value, tb = sys.exc_info() |
| 45 | traceback.print_exception(type, value, tb) |
| 46 | |
| 47 | def runcode(self, code): |
| 48 | super().runcode(code) |
| 49 | if self.poll_func: |
| 50 | self.poll_func() |
| 51 | if "mon" in self.locals: |
| 52 | try: |
| 53 | self.locals["mon"].poll() |
| 54 | except Exception as e: |
| 55 | print(f"mon.poll() failed: {e!r}") |
| 56 | if "u" in self.locals: |
| 57 | self.locals["u"].push_simd() |
| 58 | |
| 59 | |
| 60 | class ExitConsole(SystemExit): |