Project

General

Profile

Feature #7103 ยป patch.diff

Glass_saga (Masaki Matsushita), 10/02/2012 11:51 PM

View differences:

io.c
4792 4792
    rb_io_ext_int_to_encs(ext_enc, int_enc, enc_p, enc2_p);
4793 4793
}
4794 4794

  
4795
static void
4796
mode_enc(rb_io_t *fptr, const char *estr)
4797
{
4798
    clear_codeconv(fptr);
4799

  
4800
    parse_mode_enc(estr, &fptr->encs.enc, &fptr->encs.enc2, NULL);
4801
}
4802

  
4803
static void
4804
rb_io_mode_enc(rb_io_t *fptr, const char *modestr)
4805
{
4806
    const char *p = strchr(modestr, ':');
4807
    if (p) {
4808
	mode_enc(fptr, p+1);
4809
    }
4810
}
4811

  
4812 4795
int
4813 4796
rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
4814 4797
{
......
6344 6327
static VALUE
6345 6328
rb_io_reopen(int argc, VALUE *argv, VALUE file)
6346 6329
{
6347
    VALUE fname, nmode;
6330
    VALUE fname, nmode, opt;
6348 6331
    int oflags;
6349 6332
    rb_io_t *fptr;
6350 6333

  
6351 6334
    rb_secure(4);
6352
    if (rb_scan_args(argc, argv, "11", &fname, &nmode) == 1) {
6335
    if (rb_scan_args(argc, argv, "11:", &fname, &nmode, &opt) == 1) {
6353 6336
	VALUE tmp = rb_io_check_io(fname);
6354 6337
	if (!NIL_P(tmp)) {
6355 6338
	    return io_reopen(file, tmp);
......
6364 6347
	MEMZERO(fptr, rb_io_t, 1);
6365 6348
    }
6366 6349

  
6367
    if (!NIL_P(nmode)) {
6368
	VALUE intmode = rb_check_to_int(nmode);
6350
    if (!NIL_P(nmode) || !NIL_P(opt)) {
6369 6351
	int fmode;
6352
	convconfig_t convconfig;
6370 6353

  
6371
	if (!NIL_P(intmode)) {
6372
	    oflags = NUM2INT(intmode);
6373
	    fmode = rb_io_oflags_fmode(oflags);
6374
	}
6375
	else {
6376
	    fmode = rb_io_modestr_fmode(StringValueCStr(nmode));
6377
	}
6378

  
6354
	rb_io_extract_modeenc(&nmode, 0, opt, &oflags, &fmode, &convconfig);
6379 6355
	if (IS_PREP_STDIO(fptr) &&
6380 6356
            ((fptr->mode & FMODE_READWRITE) & (fmode & FMODE_READWRITE)) !=
6381 6357
            (fptr->mode & FMODE_READWRITE)) {
......
6385 6361
		     rb_io_fmode_modestr(fmode));
6386 6362
	}
6387 6363
	fptr->mode = fmode;
6388
	if (NIL_P(intmode)) {
6389
	    rb_io_mode_enc(fptr, StringValueCStr(nmode));
6390
	}
6391
        fptr->encs.ecflags = 0;
6392
        fptr->encs.ecopts = Qnil;
6364
        fptr->encs = convconfig;
6365
    }
6366
    else {
6367
	oflags = rb_io_fmode_oflags(fptr->mode);
6393 6368
    }
6394 6369

  
6395 6370
    fptr->pathv = rb_str_new_frozen(fname);
6396
    oflags = rb_io_fmode_oflags(fptr->mode);
6397 6371
    if (fptr->fd < 0) {
6398 6372
        fptr->fd = rb_sysopen(fptr->pathv, oflags, 0666);
6399 6373
	fptr->stdio_file = 0;
test/ruby/test_io.rb
1732 1732
    }
1733 1733
  end
1734 1734

  
1735
  def test_reopen_opt
1736
    make_tempfile {|t|
1737
      open(__FILE__) do |f|
1738
        assert_nothing_raised {
1739
          f.reopen(t.path, "r", binmode: true)
1740
          assert_equal("foo\n", f.gets)
1741
        }
1742
      end
1743

  
1744
      open(__FILE__) do |f|
1745
        assert_nothing_raised {
1746
          f.reopen(t.path, nil, autoclose: false)
1747
          assert_equal("foo\n", f.gets)
1748
        }
1749
      end
1750
    }
1751
  end
1752

  
1735 1753
  def test_foreach
1736 1754
    a = []
1737 1755
    IO.foreach("|" + EnvUtil.rubybin + " -e 'puts :foo; puts :bar; puts :baz'") {|x| a << x }