ruby-changes:65467
From: Jeremy <ko1@a...>
Date: Sat, 13 Mar 2021 17:56:01 +0900 (JST)
Subject: [ruby-changes:65467] b20e2c3f2a (ruby_3_0): Backport io-console 0.5.7 to Ruby 3.0 (#4252)
https://git.ruby-lang.org/ruby.git/commit/?id=b20e2c3f2a From b20e2c3f2a3ce227b9cf32d2bf02db0ac67eccb5 Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Sat, 13 Mar 2021 00:55:49 -0800 Subject: Backport io-console 0.5.7 to Ruby 3.0 (#4252) * [ruby/io-console] [DOC] Note that IO#getpass returns a chomped string IO#getpass uses String#chomp! on the read input line. https://github.com/ruby/io-console/commit/1e98c93bc8 * [ruby/io-console] Ignore chomp! result and return the modified string https://github.com/ruby/io-console/commit/09e5ccc729 * [ruby/io-console] Pre-define chomp! ID https://github.com/ruby/io-console/commit/028e1c9497 * [ruby/io-console] Shrink struct query_args https://github.com/ruby/io-console/commit/720be0a3e5 * [ruby/io-console] bump up to 0.5.7 https://github.com/ruby/io-console/commit/f55d7ebff6 Co-authored-by: Marcus Stollsteimer <sto.mar@w...> Co-authored-by: Nobuyoshi Nakada <nobu@r...> --- ext/io/console/console.c | 13 +++++++++---- ext/io/console/io-console.gemspec | 2 +- test/io/console/test_io_console.rb | 9 +++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/ext/io/console/console.c b/ext/io/console/console.c index ff4df73..2e24670 100644 --- a/ext/io/console/console.c +++ b/ext/io/console/console.c @@ -77,7 +77,7 @@ getattr(int fd, conmode *t) https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L77 static ID id_getc, id_console, id_close, id_min, id_time, id_intr; #if ENABLE_IO_GETPASS -static ID id_gets; +static ID id_gets, id_chomp_bang; #endif #ifdef HAVE_RB_SCHEDULER_TIMEOUT @@ -1223,8 +1223,8 @@ console_key_pressed_p(VALUE io, VALUE k) https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L1223 } #else struct query_args { - const char *qstr; - int opt; + char qstr[6]; + unsigned char opt; }; static int @@ -1562,7 +1562,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L1562 str_chomp(VALUE str) { if (!NIL_P(str)) { - str = rb_funcallv(str, rb_intern("chomp!"), 0, 0); + rb_funcallv(str, id_chomp_bang, 0, 0); } return str; } @@ -1574,6 +1574,10 @@ str_chomp(VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L1574 * Reads and returns a line without echo back. * Prints +prompt+ unless it is +nil+. * + * The newline character that terminates the + * read line is removed from the returned string, + * see String#chomp!. + * * You must require 'io/console' to use this method. */ static VALUE @@ -1618,6 +1622,7 @@ Init_console(void) https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L1622 id_getc = rb_intern("getc"); #if ENABLE_IO_GETPASS id_gets = rb_intern("gets"); + id_chomp_bang = rb_intern("chomp!"); #endif id_console = rb_intern("console"); id_close = rb_intern("close"); diff --git a/ext/io/console/io-console.gemspec b/ext/io/console/io-console.gemspec index 743e5e9..8a0df83 100644 --- a/ext/io/console/io-console.gemspec +++ b/ext/io/console/io-console.gemspec @@ -1,5 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/ext/io/console/io-console.gemspec#L1 # -*- ruby -*- -_VERSION = "0.5.6" +_VERSION = "0.5.7" Gem::Specification.new do |s| s.name = "io-console" diff --git a/test/io/console/test_io_console.rb b/test/io/console/test_io_console.rb index 3962de3..bec10c5 100644 --- a/test/io/console/test_io_console.rb +++ b/test/io/console/test_io_console.rb @@ -235,6 +235,15 @@ defined?(PTY) and defined?(IO.console) and TestIO_Console.class_eval do https://github.com/ruby/ruby/blob/trunk/test/io/console/test_io_console.rb#L235 assert_equal("\r\n", r.gets) assert_equal("\"asdf\"", r.gets.chomp) end + + run_pty("p IO.console.getpass('> ')") do |r, w| + assert_equal("> ", r.readpartial(10)) + sleep 0.1 + w.print "asdf\C-D\C-D" + sleep 0.1 + assert_equal("\r\n", r.gets) + assert_equal("\"asdf\"", r.gets.chomp) + end end def test_iflush -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/