ruby-changes:22493
From: naruse <ko1@a...>
Date: Sat, 11 Feb 2012 03:21:19 +0900 (JST)
Subject: [ruby-changes:22493] naruse:r34542 (ruby_1_9_3): merge revision(s) 33662,33666,33770: [Backport #5793]
naruse 2012-02-11 03:21:01 +0900 (Sat, 11 Feb 2012) New Revision: 34542 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=34542 Log: merge revision(s) 33662,33666,33770: [Backport #5793] ------------------------------------------------------------------------ r33662 | usa | 2011-11-08 02:48:11 +0900 (Tue, 08 Nov 2011) | 18 lines * include/ruby/encoding.h (ECONV_NEWLINE_DECORATOR_READ_MASK, ECONV_NEWLINE_DECORATOR_WRITE_MASK): new macro. * io.c (rb_io_extract_modeenc, pipe_open, prep_stdio, argf_next_argv): set TEXTMODE_NEWLINE_DECORATOR_ON_WRITE for textmode on creating IO if the flag is available. * io.c (make_writeconv): drop decorators for reading. * io.c (make_readconv): drop decorators for writing. * io.c (do_writeconv): existing writeconv is not the condition to raise ArgumentError. should check textmode or not. * test/ruby/test_io_m17n.rb (TestIO_M17N#test_{cr,lf,crlf}_decorator_on_stdout): test above changes. ------------------------------------------------------------------------ * include/ruby/encoding.h (ECONV_NEWLINE_DECORATOR_READ_MASK, ECONV_NEWLINE_DECORATOR_WRITE_MASK): new macro. * io.c (rb_io_extract_modeenc, pipe_open, prep_stdio, argf_next_argv): set TEXTMODE_NEWLINE_DECORATOR_ON_WRITE for textmode on creating IO if the flag is available. * io.c (make_writeconv): drop decorators for reading. * io.c (make_readconv): drop decorators for writing. * io.c (do_writeconv): existing writeconv is not the condition to raise ArgumentError. should check textmode or not. * test/ruby/test_io_m17n.rb (TestIO_M17N#test_{cr,lf,crlf}_decorator_on_stdout): test above changes. * io.c (argf_next_argv): wrong timing of setting ecflags. fixed the failure of TestArgf#test_textmode introduced at r33662. Modified files: branches/ruby_1_9_3/ChangeLog branches/ruby_1_9_3/include/ruby/encoding.h branches/ruby_1_9_3/io.c branches/ruby_1_9_3/test/ruby/test_io_m17n.rb branches/ruby_1_9_3/version.h Index: ruby_1_9_3/include/ruby/encoding.h =================================================================== --- ruby_1_9_3/include/ruby/encoding.h (revision 34541) +++ ruby_1_9_3/include/ruby/encoding.h (revision 34542) @@ -305,6 +305,8 @@ #define ECONV_DECORATOR_MASK 0x0000ff00 #define ECONV_NEWLINE_DECORATOR_MASK 0x00003f00 +#define ECONV_NEWLINE_DECORATOR_READ_MASK 0x00000f00 +#define ECONV_NEWLINE_DECORATOR_WRITE_MASK 0x00003000 #define ECONV_UNIVERSAL_NEWLINE_DECORATOR 0x00000100 #define ECONV_CRLF_NEWLINE_DECORATOR 0x00001000 Index: ruby_1_9_3/ChangeLog =================================================================== --- ruby_1_9_3/ChangeLog (revision 34541) +++ ruby_1_9_3/ChangeLog (revision 34542) @@ -1,3 +1,8 @@ +Sat Feb 11 03:20:22 2012 NAKAMURA Usaku <usa@r...> + + * io.c (argf_next_argv): wrong timing of setting ecflags. + fixed the failure of TestArgf#test_textmode introduced at r33662. + Sat Feb 11 03:19:45 2012 NAKAMURA Usaku <usa@r...> * test/ruby/test_io_m17n.rb @@ -3,5 +8,25 @@ (TestIO_M17N#test_default_stdout_stderr_mode): new test for r33627-33629. see [backport #5565] +Sat Feb 11 03:20:22 2012 NAKAMURA Usaku <usa@r...> + * include/ruby/encoding.h (ECONV_NEWLINE_DECORATOR_READ_MASK, + ECONV_NEWLINE_DECORATOR_WRITE_MASK): new macro. + + * io.c (rb_io_extract_modeenc, pipe_open, prep_stdio, argf_next_argv): + set TEXTMODE_NEWLINE_DECORATOR_ON_WRITE for textmode on creating IO + if the flag is available. + + * io.c (make_writeconv): drop decorators for reading. + + * io.c (make_readconv): drop decorators for writing. + + * io.c (do_writeconv): existing writeconv is not the condition to raise + ArgumentError. should check textmode or not. + + * test/ruby/test_io_m17n.rb + (TestIO_M17N#test_{cr,lf,crlf}_decorator_on_stdout): test above + changes. + + Sat Feb 11 03:19:45 2012 NAKAMURA Usaku <usa@r...> Index: ruby_1_9_3/io.c =================================================================== --- ruby_1_9_3/io.c (revision 34541) +++ ruby_1_9_3/io.c (revision 34542) @@ -750,16 +750,8 @@ fptr->writeconv_initialized = 1; - ecflags = fptr->encs.ecflags; + ecflags = fptr->encs.ecflags & ~ECONV_NEWLINE_DECORATOR_READ_MASK; ecopts = fptr->encs.ecopts; -#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE - if (NEED_NEWLINE_DECORATOR_ON_WRITE(fptr) && - (!(ecflags & ECONV_NEWLINE_DECORATOR_MASK) || - (ecflags & ECONV_UNIVERSAL_NEWLINE_DECORATOR))) { - ecflags &= ~ECONV_UNIVERSAL_NEWLINE_DECORATOR; - ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE; - } -#endif if (!fptr->encs.enc || (fptr->encs.enc == rb_ascii8bit_encoding() && !fptr->encs.enc2)) { /* no encoding conversion */ @@ -897,6 +889,8 @@ return len; } +# define MODE_BTMODE(a,b,c) ((fmode & FMODE_BINMODE) ? (b) : \ + (fmode & FMODE_TEXTMODE) ? (c) : (a)) static VALUE do_writeconv(VALUE str, rb_io_t *fptr) { @@ -906,12 +900,14 @@ make_writeconv(fptr); if (fptr->writeconv) { +#define fmode (fptr->mode) if (!NIL_P(fptr->writeconv_asciicompat)) common_encoding = fptr->writeconv_asciicompat; - else if (!rb_enc_asciicompat(rb_enc_get(str))) { + else if (MODE_BTMODE(DEFAULT_TEXTMODE,0,1) && !rb_enc_asciicompat(rb_enc_get(str))) { rb_raise(rb_eArgError, "ASCII incompatible string written for text mode IO without encoding conversion: %s", rb_enc_name(rb_enc_get(str))); } +#undef fmode } else { if (fptr->encs.enc2) @@ -1669,7 +1665,7 @@ int ecflags; VALUE ecopts; const char *sname, *dname; - ecflags = fptr->encs.ecflags; + ecflags = fptr->encs.ecflags & ~ECONV_NEWLINE_DECORATOR_WRITE_MASK; ecopts = fptr->encs.ecopts; if (fptr->encs.enc2) { sname = rb_enc_name(fptr->encs.enc2); @@ -4078,8 +4074,6 @@ static const char* rb_io_fmode_modestr(int fmode) { -# define MODE_BTMODE(a,b,c) ((fmode & FMODE_BINMODE) ? (b) : \ - (fmode & FMODE_TEXTMODE) ? (c) : (a)) if (fmode & FMODE_APPEND) { if ((fmode & FMODE_READWRITE) == FMODE_READWRITE) { return MODE_BTMODE("a+", "ab+", "at+"); @@ -4544,6 +4538,11 @@ ecflags = (fmode & FMODE_READABLE) ? MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR, 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0; +#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE + ecflags |= (fmode & FMODE_WRITABLE) ? + MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE, + 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0; +#endif ecopts = Qnil; } else { @@ -4579,6 +4578,11 @@ ecflags = (fmode & FMODE_READABLE) ? MODE_BTMODE(ECONV_DEFAULT_NEWLINE_DECORATOR, 0, ECONV_UNIVERSAL_NEWLINE_DECORATOR) : 0; +#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE + ecflags |= (fmode & FMODE_WRITABLE) ? + MODE_BTMODE(TEXTMODE_NEWLINE_DECORATOR_ON_WRITE, + 0, TEXTMODE_NEWLINE_DECORATOR_ON_WRITE) : 0; +#endif ecflags = rb_econv_prepare_options(opthash, &ecopts, ecflags); if (rb_io_extract_encoding_option(opthash, &enc, &enc2, &fmode)) { @@ -5268,8 +5272,15 @@ if (convconfig) { fptr->encs = *convconfig; } - else if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) { - fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR; + else { + if (NEED_NEWLINE_DECORATOR_ON_READ(fptr)) { + fptr->encs.ecflags |= ECONV_UNIVERSAL_NEWLINE_DECORATOR; + } +#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE + if (NEED_NEWLINE_DECORATOR_ON_WRITE(fptr)) { + fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE; + } +#endif } fptr->pid = pid; @@ -6437,6 +6448,9 @@ GetOpenFile(io, fptr); fptr->encs.ecflags |= ECONV_DEFAULT_NEWLINE_DECORATOR; +#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE + fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE; +#endif fptr->stdio_file = f; return io; @@ -6983,7 +6997,9 @@ if (stdout_binmode) rb_io_binmode(rb_stdout); } fmode = FMODE_READABLE; - if (!ARGF.binmode) fmode |= DEFAULT_TEXTMODE; + if (!ARGF.binmode) { + fmode |= DEFAULT_TEXTMODE; + } ARGF.current_file = prep_io(fr, fmode, rb_cFile, fn); if (!NIL_P(write_io)) { rb_io_set_write_io(ARGF.current_file, write_io); @@ -6999,6 +7015,9 @@ fptr->encs.ecflags &= ~ECONV_NEWLINE_DECORATOR_MASK; if (!ARGF.binmode) { fptr->encs.ecflags |= ECONV_DEFAULT_NEWLINE_DECORATOR; +#ifdef TEXTMODE_NEWLINE_DECORATOR_ON_WRITE + fptr->encs.ecflags |= TEXTMODE_NEWLINE_DECORATOR_ON_WRITE; +#endif } } ARGF.next_p = 0; Index: ruby_1_9_3/version.h =================================================================== --- ruby_1_9_3/version.h (revision 34541) +++ ruby_1_9_3/version.h (revision 34542) @@ -1,5 +1,5 @@ #define RUBY_VERSION "1.9.3" -#define RUBY_PATCHLEVEL 87 +#define RUBY_PATCHLEVEL 88 #define RUBY_RELEASE_DATE "2012-02-11" #define RUBY_RELEASE_YEAR 2012 Index: ruby_1_9_3/test/ruby/test_io_m17n.rb =================================================================== --- ruby_1_9_3/test/ruby/test_io_m17n.rb (revision 34541) +++ ruby_1_9_3/test/ruby/test_io_m17n.rb (revision 34542) @@ -2117,4 +2117,61 @@ end end end if /mswin|mingw/ =~ RUBY_PLATFORM + + def test_cr_decorator_on_stdout + with_pipe do |in_r, in_w| + with_pipe do |out_r, out_w| + pid = Process.spawn({}, EnvUtil.rubybin, in: in_r, out: out_w) + in_r.close + out_w.close + in_w.write <<-EOS + STDOUT.set_encoding('locale', nil, newline: :cr) + STDOUT.puts "abc" + STDOUT.flush + EOS + in_w.close + Process.wait pid + assert_equal "abc\r", out_r.binmode.read + out_r.close + end + end + end + + def test_lf_decorator_on_stdout + with_pipe do |in_r, in_w| + with_pipe do |out_r, out_w| + pid = Process.spawn({}, EnvUtil.rubybin, in: in_r, out: out_w) + in_r.close + out_w.close + in_w.write <<-EOS + STDOUT.set_encoding('locale', nil, newline: :lf) + STDOUT.puts "abc" + STDOUT.flush + EOS + in_w.close + Process.wait pid + assert_equal "abc\n", out_r.binmode.read + out_r.close + end + end + end + + def test_crlf_decorator_on_stdout + with_pipe do |in_r, in_w| + with_pipe do |out_r, out_w| + pid = Process.spawn({}, EnvUtil.rubybin, in: in_r, out: out_w) + in_r.close + out_w.close + in_w.write <<-EOS + STDOUT.set_encoding('locale', nil, newline: :crlf) + STDOUT.puts "abc" + STDOUT.flush + EOS + in_w.close + Process.wait pid + assert_equal "abc\r\n", out_r.binmode.read + out_r.close + end + end + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/