Project

General

Profile

Feature #11358 ยป 0001-io.c-argf_read_nonblock-support-exception-false.patch

normalperson (Eric Wong), 07/16/2015 10:02 AM

View differences:

io.c
2619 2619
    return ret;
2620 2620
}
2621 2621

  
2622
static VALUE
2623
io_nonblock_eof(VALUE opts)
2624
{
2625
    if (!no_exception_p(opts)) {
2626
        rb_eof_error();
2627
    }
2628
    return Qnil;
2629
}
2630

  
2622 2631
/*
2623 2632
 *  call-seq:
2624 2633
 *     ios.read_nonblock(maxlen)              -> string
......
2680 2689
    ret = io_getpartial(argc, argv, io, opts, 1);
2681 2690

  
2682 2691
    if (NIL_P(ret)) {
2683
	if (no_exception_p(opts))
2684
	    return Qnil;
2685
	else
2686
	    rb_eof_error();
2692
	return io_nonblock_eof(opts);
2687 2693
    }
2688 2694
    return ret;
2689 2695
}
......
11139 11145
    return Qnil;
11140 11146
}
11141 11147

  
11142
static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock);
11148
static VALUE argf_getpartial(int argc, VALUE *argv, VALUE argf, VALUE opts,
11149
                             int nonblock);
11143 11150

  
11144 11151
/*
11145 11152
 *  call-seq:
......
11164 11171
static VALUE
11165 11172
argf_readpartial(int argc, VALUE *argv, VALUE argf)
11166 11173
{
11167
    return argf_getpartial(argc, argv, argf, 0);
11174
    return argf_getpartial(argc, argv, argf, Qnil, 0);
11168 11175
}
11169 11176

  
11170 11177
/*
......
11178 11185
static VALUE
11179 11186
argf_read_nonblock(int argc, VALUE *argv, VALUE argf)
11180 11187
{
11181
    return argf_getpartial(argc, argv, argf, 1);
11188
    VALUE opts;
11189

  
11190
    rb_scan_args(argc, argv, "11:", NULL, NULL, &opts);
11191

  
11192
    if (!NIL_P(opts))
11193
        argc--;
11194

  
11195
    return argf_getpartial(argc, argv, argf, opts, 1);
11182 11196
}
11183 11197

  
11184 11198
static VALUE
11185
argf_getpartial(int argc, VALUE *argv, VALUE argf, int nonblock)
11199
argf_getpartial(int argc, VALUE *argv, VALUE argf, VALUE opts, int nonblock)
11186 11200
{
11187 11201
    VALUE tmp, str, length;
11188 11202

  
......
11205 11219
			 RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
11206 11220
    }
11207 11221
    else {
11208
        tmp = io_getpartial(argc, argv, ARGF.current_file, Qnil, nonblock);
11222
        tmp = io_getpartial(argc, argv, ARGF.current_file, opts, nonblock);
11209 11223
    }
11210 11224
    if (NIL_P(tmp)) {
11211 11225
        if (ARGF.next_p == -1) {
11212
            rb_eof_error();
11226
	    return io_nonblock_eof(opts);
11213 11227
        }
11214 11228
        argf_close(argf);
11215 11229
        ARGF.next_p = 1;
11216
        if (RARRAY_LEN(ARGF.argv) == 0)
11217
            rb_eof_error();
11230
        if (RARRAY_LEN(ARGF.argv) == 0) {
11231
	    return io_nonblock_eof(opts);
11232
	}
11218 11233
        if (NIL_P(str))
11219 11234
            str = rb_str_new(NULL, 0);
11220 11235
        return str;
test/ruby/test_argf.rb
856 856
      assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read))
857 857
    end
858 858
  end
859

  
860
  def test_read_nonblock
861
    ruby('-e', <<-SRC) do |f|
862
      $stdout.sync = true
863
      :wait_readable == ARGF.read_nonblock(1, "", exception: false) or
864
        abort "did not return :wait_readable"
865

  
866
      begin
867
        ARGF.read_nonblock(1)
868
        abort 'fail to raise IO::WaitReadable'
869
      rescue IO::WaitReadable
870
      end
871
      puts 'starting select'
872

  
873
      IO.select([ARGF]) == [[ARGF], [], []] or
874
        abort 'did not awaken for readability (before byte)'
875

  
876
      buf = ''
877
      buf.object_id == ARGF.read_nonblock(1, buf).object_id or
878
        abort "read destination buffer failed"
879
      print buf
880

  
881
      IO.select([ARGF]) == [[ARGF], [], []] or
882
        abort 'did not awaken for readability (before EOF)'
883

  
884
      ARGF.read_nonblock(1, buf, exception: false) == nil or
885
        abort "EOF should return nil if exception: false"
886

  
887
      begin
888
        ARGF.read_nonblock(1, buf)
889
        abort 'fail to raise IO::WaitReadable'
890
      rescue EOFError
891
        puts 'done with eof'
892
      end
893
    SRC
894
      f.sync = true
895
      assert_equal "starting select\n", f.gets
896
      f.write('.') # wake up from IO.select
897
      assert_equal '.', f.read(1)
898
      f.close_write
899
      assert_equal "done with eof\n", f.gets
900
    end
901
  end
859 902
end
860
-