ruby-changes:42515
From: nobu <ko1@a...>
Date: Fri, 15 Apr 2016 09:10:36 +0900 (JST)
Subject: [ruby-changes:42515] nobu:r54589 (trunk): io/console: unknown key code for names with nul
nobu 2016-04-15 10:07:14 +0900 (Fri, 15 Apr 2016) New Revision: 54589 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54589 Log: io/console: unknown key code for names with nul * ext/io/console/console.c (console_key_pressed_p): raise the same exception, "unknown virtual key code", for names with nul chars. though console_win32_vk() considers the length and can deal with nul chars, rb_sprintf() raised at PRIsVALUE previously, so quote it if it is unprintable. Modified files: trunk/ChangeLog trunk/ext/io/console/console.c trunk/test/io/console/test_io_console.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 54588) +++ ChangeLog (revision 54589) @@ -1,3 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Apr 15 10:07:11 2016 Nobuyoshi Nakada <nobu@r...> + + * ext/io/console/console.c (console_key_pressed_p): raise the same + exception, "unknown virtual key code", for names with nul chars. + though console_win32_vk() considers the length and can deal with + nul chars, rb_sprintf() raised at PRIsVALUE previously, so quote + it if it is unprintable. + Fri Apr 15 09:02:58 2016 Nobuyoshi Nakada <nobu@r...> * ext/io/console/console.c (rb_sym2str): fallback definition for Index: ext/io/console/console.c =================================================================== --- ext/io/console/console.c (revision 54588) +++ ext/io/console/console.c (revision 54589) @@ -731,11 +731,14 @@ console_key_pressed_p(VALUE io, VALUE k) https://github.com/ruby/ruby/blob/trunk/ext/io/console/console.c#L731 const char *kn; if (SYMBOL_P(k)) { k = rb_sym2str(k); + kn = RSTRING_PTR(k); + } + else { + kn = StringValuePtr(k); } - kn = StringValueCStr(k); t = console_win32_vk(kn, RSTRING_LEN(k)); if (!t || (vk = (short)t->vk) == -1) { - rb_raise(rb_eArgError, "unknown virtual key code: %"PRIsVALUE, k); + rb_raise(rb_eArgError, "unknown virtual key code: % "PRIsVALUE, k); } } return GetKeyState(vk) & 0x80 ? Qtrue : Qfalse; Index: test/io/console/test_io_console.rb =================================================================== --- test/io/console/test_io_console.rb (revision 54588) +++ test/io/console/test_io_console.rb (revision 54589) @@ -340,6 +340,21 @@ defined?(IO.console) and TestIO_Console. https://github.com/ruby/ruby/blob/trunk/test/io/console/test_io_console.rb#L340 end end +defined?(IO.console) and IO.console and IO.console.respond_to?(:pressed?) and + TestIO_Console.class_eval do + def test_pressed_valid + assert_include([true, false], IO.console.pressed?("HOME")) + assert_include([true, false], IO.console.pressed?(:"HOME")) + end + + def test_pressed_invalid + e = assert_raise(ArgumentError) do + IO.console.pressed?("HOME\0") + end + assert_match(/unknown virtual key code/, e.message) + end +end + TestIO_Console.class_eval do def test_stringio_getch assert_separately %w"--disable=gems -rstringio -rio/console", %q{ -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/