Skip to content

Commit 3bdfaf6

Browse files
authored
Fix timeout type error (#18)
Fixed TypeError when IO#getch timed out `rb_io_wait` returns a bit-flags Integer representing available events, or Qfalse if timed out. Also the result of `NUM2INT` is not a `VALUE`. ``` $ ./bin/ruby -v -rio/console -e "p IO.console.getch(intr: true, time: 0.1)" ruby 3.0.0dev (2020-10-09T20:27:30Z master 5ea2ea74cc) [x64-mingw32] -e:1:in `getch': no implicit conversion of false into Integer (TypeError) from -e:1:in `<main>' ```
1 parent c0b3986 commit 3bdfaf6

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

ext/io/console/console.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ console_getch(int argc, VALUE *argv, VALUE io)
546546
if (w < 0) rb_eof_error();
547547
if (!(w & RB_WAITFD_IN)) return Qnil;
548548
# else
549-
VALUE result = RB_NUM2INT(rb_io_wait(io, RUBY_IO_READABLE, timeout));
549+
VALUE result = rb_io_wait(io, RUBY_IO_READABLE, timeout);
550550
if (result == Qfalse) return Qnil;
551551
# endif
552552
}

test/io/console/test_io_console.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ def test_sync
471471
ensure
472472
IO.console(:close)
473473
end
474+
475+
def test_getch_timeout
476+
assert_nil(IO.console.getch(intr: true, time: 0.1, min: 0))
477+
end
474478
end
475479
end
476480

0 commit comments

Comments
 (0)