Merge pull request #1560 from waigx/feat/results-shortcuts

Add recent REPL results globals *i and most recent error *e
This commit is contained in:
Simon Gomizelj 2018-04-04 19:27:35 -04:00 committed by GitHub
commit 4d98cde663
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 14 deletions

View File

@ -20,6 +20,8 @@ New Features
------------------------------ ------------------------------
* Python 3.7 is now supported * Python 3.7 is now supported
* Added `mangle` and `unmangle` as core functions * Added `mangle` and `unmangle` as core functions
* More REPL history result variables: `*2`, `*3`. Added last REPL error
variable: `*e`
* `defclass` in Python 3 now supports specifying metaclasses and other * `defclass` in Python 3 now supports specifying metaclasses and other
keyword arguments keyword arguments
* Added a command-line option `-E` per CPython * Added a command-line option `-E` per CPython

View File

@ -73,8 +73,20 @@ class HyREPL(code.InteractiveConsole):
code.InteractiveConsole.__init__(self, locals=locals, code.InteractiveConsole.__init__(self, locals=locals,
filename=filename) filename=filename)
# Pre-mangle symbols for repl recent results: *1, *2, *3
self._repl_results_symbols = [mangle("*{}".format(i + 1)) for i in range(3)]
self.locals.update({sym: None for sym in self._repl_results_symbols})
def runsource(self, source, filename='<input>', symbol='single'): def runsource(self, source, filename='<input>', symbol='single'):
global SIMPLE_TRACEBACKS global SIMPLE_TRACEBACKS
def error_handler(e, use_simple_traceback=False):
self.locals[mangle("*e")] = e
if use_simple_traceback:
print(e, file=sys.stderr)
else:
self.showtraceback()
try: try:
try: try:
do = import_buffer_to_hst(source) do = import_buffer_to_hst(source)
@ -84,7 +96,7 @@ class HyREPL(code.InteractiveConsole):
if e.source is None: if e.source is None:
e.source = source e.source = source
e.filename = filename e.filename = filename
print(e, file=sys.stderr) error_handler(e, use_simple_traceback=True)
return False return False
try: try:
@ -101,24 +113,23 @@ class HyREPL(code.InteractiveConsole):
if e.source is None: if e.source is None:
e.source = source e.source = source
e.filename = filename e.filename = filename
if SIMPLE_TRACEBACKS: error_handler(e, use_simple_traceback=SIMPLE_TRACEBACKS)
print(e, file=sys.stderr)
else:
self.showtraceback()
return False return False
except Exception: except Exception as e:
self.showtraceback() error_handler(e)
return False return False
if value is not None: if value is not None:
# Make the last non-None value available to # Shift exisitng REPL results
# the user as `*1`. next_result = value
self.locals[mangle("*1")] = value for sym in self._repl_results_symbols:
self.locals[sym], next_result = next_result, self.locals[sym]
# Print the value. # Print the value.
try: try:
output = self.output_fn(value) output = self.output_fn(value)
except Exception: except Exception as e:
self.showtraceback() error_handler(e)
return False return False
print(output) print(output)
return False return False

View File

@ -75,8 +75,15 @@ def test_bin_hy_stdin_multiline():
def test_bin_hy_history(): def test_bin_hy_history():
output, _ = run_cmd("hy", '(+ "a" "b")\n(+ *1 "y" "z")') output, _ = run_cmd("hy", '''(+ "a" "b")
assert "'abyz'" in output (+ "c" "d")
(+ "e" "f")
(.format "*1: {}, *2: {}, *3: {}," *1 *2 *3)''')
assert "'*1: ef, *2: cd, *3: ab,'" in output
output, _ = run_cmd("hy", '''(raise (Exception "TEST ERROR"))
(+ "err: " (str *e))''')
assert "'err: TEST ERROR'" in output
def test_bin_hy_stdin_comments(): def test_bin_hy_stdin_comments():