From 57464618abee8c5d35d9a24767cdb62d1459b341 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 14 Jul 2017 10:53:35 +0000 Subject: [PATCH] io.c: textmode if newline decorator * io.c (validate_enc_binmode): newline decorator implies text mode now. [ruby-core:80270] [Bug #13350] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59336 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- NEWS | 4 ++++ io.c | 5 ++++- test/ruby/test_io_m17n.rb | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index af11da3550..db9e9432f4 100644 --- a/NEWS +++ b/NEWS @@ -32,6 +32,10 @@ with all sufficient information, see the ChangeLog file or Redmine * Dir.children [Feature #11302] * Dir.each_child [Feature #11302] +* File + + * :newline option to File.open implies text mode now. [Bug #13350] + * Integer * Integer.sqrt [Feature #13219] diff --git a/io.c b/io.c index 3482602326..60af120c18 100644 --- a/io.c +++ b/io.c @@ -5451,9 +5451,12 @@ validate_enc_binmode(int *fmode_p, int ecflags, rb_encoding *enc, rb_encoding *e !rb_enc_asciicompat(enc ? enc : rb_default_external_encoding())) rb_raise(rb_eArgError, "ASCII incompatible encoding needs binmode"); + if ((fmode & FMODE_BINMODE) && (ecflags & ECONV_NEWLINE_DECORATOR_MASK)) { + rb_raise(rb_eArgError, "newline decorator with binary mode"); + } if (!(fmode & FMODE_BINMODE) && (DEFAULT_TEXTMODE || (ecflags & ECONV_NEWLINE_DECORATOR_MASK))) { - fmode |= DEFAULT_TEXTMODE; + fmode |= FMODE_TEXTMODE; *fmode_p = fmode; } #if !DEFAULT_TEXTMODE diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index 4a7cf33883..ccb124a46b 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -1608,6 +1608,44 @@ EOT } end + + def test_binmode_decode_universal_newline + with_tmpdir { + generate_file("t.txt", "a\n") + assert_raise(ArgumentError) { + open("t.txt", "rb", newline: :universal) {} + } + } + end + + def test_default_mode_decode_universal_newline_gets + with_tmpdir { + generate_file("t.crlf", "a\r\nb\r\nc\r\n") + open("t.crlf", "r", newline: :universal) {|f| + assert_equal("a\n", f.gets) + assert_equal("b\n", f.gets) + assert_equal("c\n", f.gets) + assert_equal(nil, f.gets) + } + + generate_file("t.cr", "a\rb\rc\r") + open("t.cr", "r", newline: :universal) {|f| + assert_equal("a\n", f.gets) + assert_equal("b\n", f.gets) + assert_equal("c\n", f.gets) + assert_equal(nil, f.gets) + } + + generate_file("t.lf", "a\nb\nc\n") + open("t.lf", "r", newline: :universal) {|f| + assert_equal("a\n", f.gets) + assert_equal("b\n", f.gets) + assert_equal("c\n", f.gets) + assert_equal(nil, f.gets) + } + } + end + def test_read_newline_conversion_with_encoding_conversion with_tmpdir { generate_file("t.utf8.crlf", "a\r\nb\r\n")