diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-01-25 18:24:09 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-01-25 18:24:09 +0000 |
commit | 8335ce7065be10565e0c1fb8971df2b43d9d4d95 (patch) | |
tree | a0a9a8ab282bf416e8a4e8cff9bb1c125407f159 | |
parent | 97f0b0f55818420b4c7a7002c92c08ad78a7e579 (diff) | |
download | ruby-8335ce7065be10565e0c1fb8971df2b43d9d4d95.tar.gz |
* ext/io/console/console.c (io_getch): default delegating method
for StringIO. https://github.com/nobu/io-console/issues/4
* ext/stringio/stringio.c: moved some methods to hidden modules.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34376 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | ext/io/console/console.c | 10 | ||||
-rw-r--r-- | ext/stringio/stringio.c | 43 | ||||
-rw-r--r-- | test/io/console/test_io_console.rb | 12 |
4 files changed, 53 insertions, 19 deletions
@@ -1,3 +1,10 @@ +Thu Jan 26 03:24:02 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * ext/io/console/console.c (io_getch): default delegating method + for StringIO. https://github.com/nobu/io-console/issues/4 + + * ext/stringio/stringio.c: moved some methods to hidden modules. + Wed Jan 25 13:27:42 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> * file.c (rb_file_s_basename): ignore non-ascii extension in diff --git a/ext/io/console/console.c b/ext/io/console/console.c index 5b6705d45b..228ac80fef 100644 --- a/ext/io/console/console.c +++ b/ext/io/console/console.c @@ -718,6 +718,12 @@ console_dev(VALUE klass) return con; } +static VALUE +io_getch(int argc, VALUE *argv, VALUE io) +{ + return rb_funcall2(io, rb_intern("getc"), argc, argv); +} + /* * IO console methods */ @@ -746,4 +752,8 @@ InitVM_console(void) rb_define_method(rb_cIO, "oflush", console_oflush, 0); rb_define_method(rb_cIO, "ioflush", console_ioflush, 0); rb_define_singleton_method(rb_cIO, "console", console_dev, 0); + { + VALUE mReadable = rb_define_module_under(rb_cIO, "readable"); + rb_define_method(mReadable, "getch", io_getch, -1); + } } diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index ad18644a43..54d36c96a8 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -806,7 +806,7 @@ strio_ungetbyte(VALUE self, VALUE c) static VALUE strio_readchar(VALUE self) { - VALUE c = strio_getc(self); + VALUE c = rb_funcall2(self, rb_intern("getc"), 0, 0); if (NIL_P(c)) rb_eof_error(); return c; } @@ -820,7 +820,7 @@ strio_readchar(VALUE self) static VALUE strio_readbyte(VALUE self) { - VALUE c = strio_getbyte(self); + VALUE c = rb_funcall2(self, rb_intern("getbyte"), 0, 0); if (NIL_P(c)) rb_eof_error(); return c; } @@ -1037,7 +1037,7 @@ strio_gets(int argc, VALUE *argv, VALUE self) static VALUE strio_readline(int argc, VALUE *argv, VALUE self) { - VALUE line = strio_gets(argc, argv, self); + VALUE line = rb_funcall2(self, rb_intern("gets"), argc, argv); if (NIL_P(line)) rb_eof_error(); return line; } @@ -1288,14 +1288,14 @@ strio_read(int argc, VALUE *argv, VALUE self) static VALUE strio_sysread(int argc, VALUE *argv, VALUE self) { - VALUE val = strio_read(argc, argv, self); + VALUE val = rb_funcall2(self, rb_intern("read"), argc, argv); if (NIL_P(val)) { rb_eof_error(); } return val; } -#define strio_syswrite strio_write +#define strio_syswrite rb_io_write /* * call-seq: @@ -1459,25 +1459,13 @@ Init_stringio() rb_define_method(StringIO, "getc", strio_getc, 0); rb_define_method(StringIO, "ungetc", strio_ungetc, 1); rb_define_method(StringIO, "ungetbyte", strio_ungetbyte, 1); - rb_define_method(StringIO, "readchar", strio_readchar, 0); rb_define_method(StringIO, "getbyte", strio_getbyte, 0); - rb_define_method(StringIO, "readbyte", strio_readbyte, 0); rb_define_method(StringIO, "gets", strio_gets, -1); - rb_define_method(StringIO, "readline", strio_readline, -1); rb_define_method(StringIO, "readlines", strio_readlines, -1); rb_define_method(StringIO, "read", strio_read, -1); - rb_define_method(StringIO, "sysread", strio_sysread, -1); - rb_define_method(StringIO, "readpartial", strio_sysread, -1); - rb_define_method(StringIO, "read_nonblock", strio_sysread, -1); rb_define_method(StringIO, "write", strio_write, 1); - rb_define_method(StringIO, "<<", strio_addstr, 1); - rb_define_method(StringIO, "print", strio_print, -1); - rb_define_method(StringIO, "printf", strio_printf, -1); rb_define_method(StringIO, "putc", strio_putc, 1); - rb_define_method(StringIO, "puts", strio_puts, -1); - rb_define_method(StringIO, "syswrite", strio_syswrite, 1); - rb_define_method(StringIO, "write_nonblock", strio_syswrite, 1); rb_define_method(StringIO, "isatty", strio_isatty, 0); rb_define_method(StringIO, "tty?", strio_isatty, 0); @@ -1490,4 +1478,25 @@ Init_stringio() rb_define_method(StringIO, "external_encoding", strio_external_encoding, 0); rb_define_method(StringIO, "internal_encoding", strio_internal_encoding, 0); rb_define_method(StringIO, "set_encoding", strio_set_encoding, -1); + + { + VALUE mReadable = rb_define_module_under(rb_cIO, "readable"); + rb_define_method(mReadable, "readchar", strio_readchar, 0); + rb_define_method(mReadable, "readbyte", strio_readbyte, 0); + rb_define_method(mReadable, "readline", strio_readline, -1); + rb_define_method(mReadable, "sysread", strio_sysread, -1); + rb_define_method(mReadable, "readpartial", strio_sysread, -1); + rb_define_method(mReadable, "read_nonblock", strio_sysread, -1); + rb_include_module(StringIO, mReadable); + } + { + VALUE mWritable = rb_define_module_under(rb_cIO, "writable"); + rb_define_method(mWritable, "<<", strio_addstr, 1); + rb_define_method(mWritable, "print", strio_print, -1); + rb_define_method(mWritable, "printf", strio_printf, -1); + rb_define_method(mWritable, "puts", strio_puts, -1); + rb_define_method(mWritable, "syswrite", strio_syswrite, 1); + rb_define_method(mWritable, "write_nonblock", strio_syswrite, 1); + rb_include_module(StringIO, mWritable); + } } diff --git a/test/io/console/test_io_console.rb b/test/io/console/test_io_console.rb index 05ab1a353e..1616342b86 100644 --- a/test/io/console/test_io_console.rb +++ b/test/io/console/test_io_console.rb @@ -4,6 +4,7 @@ begin require 'pty' rescue LoadError end +require_relative '../../ruby/envutil' class TestIO_Console < Test::Unit::TestCase def test_raw @@ -193,8 +194,6 @@ class TestIO_Console < Test::Unit::TestCase end if defined?(PTY) and defined?(IO::console) class TestIO_Console < Test::Unit::TestCase - require_relative '../../ruby/envutil' - case when Process.respond_to?(:daemon) noctty = [EnvUtil.rubybin, "-e", "Process.daemon(true)"] @@ -212,6 +211,7 @@ class TestIO_Console < Test::Unit::TestCase t2 = Tempfile.new("console") t2.close cmd = NOCTTY + [ + '--disable=gems', '-rio/console', '-e', 'open(ARGV[0], "w") {|f| f.puts IO.console.inspect}', '-e', 'File.unlink(ARGV[1])', @@ -226,3 +226,11 @@ class TestIO_Console < Test::Unit::TestCase end end end if defined?(IO.console) + +class TestIO_Console < Test::Unit::TestCase + def test_stringio_getch + assert_ruby_status(%w"--disable=gems -rstringio -rio/console", "exit(StringIO.method_defined?(:getch))") + assert_ruby_status(%w"--disable=gems -rio/console -rstringio", "exit(StringIO.method_defined?(:getch))") + assert_ruby_status(%w"--disable=gems -rstringio", "exit(!StringIO.method_defined?(:getch))") + end +end |