Skip to content

Commit

Permalink
* ext/io/console/console.c (io_getch): default delegating method
Browse files Browse the repository at this point in the history
  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
  • Loading branch information
nobu committed Jan 25, 2012
1 parent 97f0b0f commit 8335ce7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Thu Jan 26 03:24:02 2012 Nobuyoshi Nakada <[email protected]>

* 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 <[email protected]>

* file.c (rb_file_s_basename): ignore non-ascii extension in
Expand Down
10 changes: 10 additions & 0 deletions ext/io/console/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
}
}
43 changes: 26 additions & 17 deletions ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
12 changes: 10 additions & 2 deletions test/io/console/test_io_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'pty'
rescue LoadError
end
require_relative '../../ruby/envutil'

class TestIO_Console < Test::Unit::TestCase
def test_raw
Expand Down Expand Up @@ -193,8 +194,6 @@ def helper
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)"]
Expand All @@ -212,6 +211,7 @@ def test_noctty
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])',
Expand All @@ -226,3 +226,11 @@ def test_noctty
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

0 comments on commit 8335ce7

Please sign in to comment.