Skip to content

Commit

Permalink
[frontend/pyreadline] Check errors and throw exceptions
Browse files Browse the repository at this point in the history
Fixes spec-cpp failure in spec/builtin-history
  • Loading branch information
Andy C committed Aug 12, 2023
1 parent 1be8eca commit c793774
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
15 changes: 12 additions & 3 deletions cpp/frontend_pyreadline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Readline::Readline()
latest_line_() {
#if HAVE_READLINE
using_history();
rl_readline_name = "oil";
rl_readline_name = "oils";
/* Force rebind of TAB to insert-tab */
rl_bind_key('\t', rl_insert);
/* Bind both ESC-TAB and ESC-ESC to the completion function */
Expand Down Expand Up @@ -115,7 +115,10 @@ void Readline::read_history_file(Str* path) {
if (path != nullptr) {
p = path->data();
}
read_history(p);
int err_num = read_history(p);
if (err_num) {
throw Alloc<IOError>(err_num);
}
#else
assert(0); // not implemented
#endif
Expand All @@ -127,7 +130,10 @@ void Readline::write_history_file(Str* path) {
if (path != nullptr) {
p = path->data();
}
write_history(p);
int err_num = write_history(p);
if (err_num) {
throw Alloc<IOError>(err_num);
}
#else
assert(0); // not implemented
#endif
Expand Down Expand Up @@ -194,6 +200,9 @@ void Readline::clear_history() {
void Readline::remove_history_item(int pos) {
#if HAVE_READLINE
HIST_ENTRY* entry = remove_history(pos);
if (!entry) {
throw Alloc<ValueError>(StrFormat("No history item at position %d", pos));
}
histdata_t data = free_history_entry(entry);
free(data);
#else
Expand Down
2 changes: 1 addition & 1 deletion pyext/line_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ setup_readline(void)

using_history();

rl_readline_name = "python";
rl_readline_name = "oils";
#if defined(PYOS_OS2) && defined(PYCC_GCC)
/* Allow $if term= in .inputrc to work */
rl_terminal_name = getenv("TERM");
Expand Down
19 changes: 17 additions & 2 deletions spec/builtin-history.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ status=1

#### history -d to delete history item

rm -f myhist
export HISTFILE=myhist

$SH --norc -i <<EOF
$SH --norc -i <<'EOF'
echo 42
echo 43
Expand All @@ -144,6 +145,8 @@ history -d -1
echo status=$?
history -d -2
echo status=$?
history -d 99
echo status=$?
case $SH in bash) echo '^D' ;; esac
Expand All @@ -154,8 +157,20 @@ EOF
43
44
status=0
status=2
status=2
status=2
^D
## END

## OK bash STDOUT:
42
43
44
status=0
status=0
status=1
status=1
status=1
^D
## END

Expand Down

0 comments on commit c793774

Please sign in to comment.