From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.2 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00 shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: spew@80x24.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id D0DB220392 for ; Thu, 16 Jul 2015 09:57:18 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] io.c (argf_read_nonblock): support `exception: false' Date: Thu, 16 Jul 2015 09:57:17 +0000 Message-Id: <1437040637-25485-1-git-send-email-e@80x24.org> X-Mailer: git-send-email 2.5.0.rc1.33.g9b79a96 List-Id: This is a preparation for [ruby-core:69892] ("io.c: avoid kwarg parsing in C API") since I noticed ARGF.read_nonblock did not properly catch up to the `exception: false' change. * io.c (argf_read_nonblock): support `exception: false' (io_nonblock_eof): new function (io_read_nonblock): use io_nonblock_eof (argf_getpartial): accept kwargs hash for `exception: false' * test/ruby/test_argf.rb (test_read_nonblock): new test --- io.c | 39 +++++++++++++++++++++++++++------------ test/ruby/test_argf.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/io.c b/io.c index ca62500..3616d0a 100644 --- a/io.c +++ b/io.c @@ -2619,6 +2619,15 @@ io_readpartial(int argc, VALUE *argv, VALUE io) return ret; } +static VALUE +io_nonblock_eof(VALUE opts) +{ + if (!no_exception_p(opts)) { + rb_eof_error(); + } + return Qnil; +} + /* * call-seq: * ios.read_nonblock(maxlen) -> string @@ -2680,10 +2689,7 @@ io_read_nonblock(int argc, VALUE *argv, VALUE io) ret = io_getpartial(argc, argv, io, opts, 1); if (NIL_P(ret)) { - if (no_exception_p(opts)) - return Qnil; - else - rb_eof_error(); + return io_nonblock_eof(opts); } return ret; } @@ -11139,7 +11145,8 @@ argf_forward_call(VALUE arg) return Qnil; } -static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock); +static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, VALUE opts, + int nonblock); /* * call-seq: @@ -11164,7 +11171,7 @@ static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock); static VALUE argf_readpartial(int argc, VALUE *argv, VALUE argf) { - return argf_getpartial(argc, argv, argf, 0); + return argf_getpartial(argc, argv, argf, Qnil, 0); } /* @@ -11178,11 +11185,18 @@ argf_readpartial(int argc, VALUE *argv, VALUE argf) static VALUE argf_read_nonblock(int argc, VALUE *argv, VALUE argf) { - return argf_getpartial(argc, argv, argf, 1); + VALUE opts; + + rb_scan_args(argc, argv, "11:", NULL, NULL, &opts); + + if (!NIL_P(opts)) + argc--; + + return argf_getpartial(argc, argv, argf, opts, 1); } static VALUE -argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock) +argf_getpartial(int argc, VALUE *argv, VALUE argf, VALUE opts, int nonblock) { VALUE tmp, str, length; @@ -11205,16 +11219,17 @@ argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock) RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0); } else { - tmp = io_getpartial(argc, argv, ARGF.current_file, Qnil, nonblock); + tmp = io_getpartial(argc, argv, ARGF.current_file, opts, nonblock); } if (NIL_P(tmp)) { if (ARGF.next_p == -1) { - rb_eof_error(); + return io_nonblock_eof(opts); } argf_close(argf); ARGF.next_p = 1; - if (RARRAY_LEN(ARGF.argv) == 0) - rb_eof_error(); + if (RARRAY_LEN(ARGF.argv) == 0) { + return io_nonblock_eof(opts); + } if (NIL_P(str)) str = rb_str_new(NULL, 0); return str; diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index 6975b83..a9ba9f6 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -856,4 +856,47 @@ class TestArgf < Test::Unit::TestCase assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) end end + + def test_read_nonblock + ruby('-e', <<-SRC) do |f| + $stdout.sync = true + :wait_readable == ARGF.read_nonblock(1, "", exception: false) or + abort "did not return :wait_readable" + + begin + ARGF.read_nonblock(1) + abort 'fail to raise IO::WaitReadable' + rescue IO::WaitReadable + end + puts 'starting select' + + IO.select([ARGF]) == [[ARGF], [], []] or + abort 'did not awaken for readability (before byte)' + + buf = '' + buf.object_id == ARGF.read_nonblock(1, buf).object_id or + abort "read destination buffer failed" + print buf + + IO.select([ARGF]) == [[ARGF], [], []] or + abort 'did not awaken for readability (before EOF)' + + ARGF.read_nonblock(1, buf, exception: false) == nil or + abort "EOF should return nil if exception: false" + + begin + ARGF.read_nonblock(1, buf) + abort 'fail to raise IO::WaitReadable' + rescue EOFError + puts 'done with eof' + end + SRC + f.sync = true + assert_equal "starting select\n", f.gets + f.write('.') # wake up from IO.select + assert_equal '.', f.read(1) + f.close_write + assert_equal "done with eof\n", f.gets + end + end end -- 2.5.0.rc1.33.g9b79a96