diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-09-05 22:26:39 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-09-05 22:26:39 +0000 |
commit | d4384f1230af19b065f62a33b27ac21143235304 (patch) | |
tree | 78915355a05513722adcde9be7648a5c1e837046 | |
parent | a10a5ddaacb05368cee3beec82af47043aec6667 (diff) | |
download | ruby-d4384f1230af19b065f62a33b27ac21143235304.tar.gz |
* io.c (rb_io_extract_modeenc): raise an error for ASCII incompatible
encoding without binmode.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19166 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | io.c | 3 | ||||
-rw-r--r-- | test/ruby/test_io_m17n.rb | 58 |
3 files changed, 62 insertions, 4 deletions
@@ -1,3 +1,8 @@ +Sat Sep 6 07:24:49 2008 Tanaka Akira <akr@fsij.org> + + * io.c (rb_io_extract_modeenc): raise an error for ASCII incompatible + encoding without binmode. + Sat Sep 6 06:28:46 2008 Tanaka Akira <akr@fsij.org> * enc/trans/escape.trans: new file. @@ -3898,6 +3898,9 @@ rb_io_extract_modeenc(VALUE *vmode_p, VALUE opthash, if ((fmode & FMODE_BINMODE) && (fmode & FMODE_TEXTMODE)) rb_raise(rb_eArgError, "both textmode and binmode specified"); + if (enc && !rb_enc_asciicompat(enc) && !(fmode & FMODE_BINMODE)) + rb_raise(rb_eArgError, "ASCII incompatible encoding needs binmode"); + *vmode_p = vmode; *oflags_p = oflags; diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index 550d163bc2..39f3112d69 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -1227,10 +1227,34 @@ EOT def test_textmode_read_ascii_incompat_internal with_tmpdir { + # ascii incompatible internal encoding needs binmode. + assert_raise(ArgumentError) { + open("t.utf8.crlf", "rt:utf-8:utf-16be") {|f| } + } + assert_raise(ArgumentError) { + open("t.utf8.crlf", "r:utf-8:utf-16be") {|f| } + } + assert_raise(ArgumentError) { + open("t.utf16.crlf", "rt:utf-16be") {|f| } + } + assert_raise(ArgumentError) { + open("t.utf16.crlf", "r:utf-16be") {|f| } + } + } + end + + def test_binmode_read_ascii_incompat_internal + with_tmpdir { generate_file("t.utf8.crlf", "a\r\nb\r\n") - open("t.utf8.crlf", "rt:utf-8:utf-16be") {|f| + generate_file("t.utf16.crlf", "\0a\0\r\0\n\0b\0\r\0\n") + # ascii incompatible internal encoding needs binmode. + open("t.utf8.crlf", "rb:utf-8:utf-16be") {|f| + content = f.read + assert_equal("\0a\0\r\0\n\0b\0\r\0\n".force_encoding("UTF-16BE"), + content) + } + open("t.utf16.crlf", "rb:utf-16be") {|f| content = f.read - # textmode doesn't affect for ascii incompatible internal encoding. assert_equal("\0a\0\r\0\n\0b\0\r\0\n".force_encoding("UTF-16BE"), content) } @@ -1239,12 +1263,38 @@ EOT def test_textmode_write_ascii_incompat_internal with_tmpdir { - open("t.utf8.lf", "wt:utf-8:utf-16be") {|f| + # ascii incompatible internal encoding needs binmode. + assert_raise(ArgumentError) { + open("t.utf8", "wt:utf-8:utf-16be") {|f| } + } + assert_raise(ArgumentError) { + open("t.utf8", "w:utf-8:utf-16be") {|f| } + } + assert_raise(ArgumentError) { + open("t.utf8", "w:utf-8:utf-16be") {|f| } + } + assert_raise(ArgumentError) { + open("t.utf16", "wt:utf-16be") {|f| } + } + assert_raise(ArgumentError) { + open("t.utf16", "w:utf-16be") {|f| } + } + } + end + + def test_binmode_write_ascii_incompat_internal + with_tmpdir { + open("t.utf8.lf", "wb:utf-8:utf-16be") {|f| f.print "\0a\0\n\0b\0\n".force_encoding("UTF-16BE") } content = File.read("t.utf8.lf", :mode=>"rb:ascii-8bit") - # textmode doesn't affect for ascii incompatible internal encoding. assert_equal("a\nb\n", content) + + open("t.utf8.lf", "wb:utf-16be") {|f| + f.print "\0a\0\n\0b\0\n".force_encoding("UTF-16BE") + } + content = File.read("t.utf8.lf", :mode=>"rb:ascii-8bit") + assert_equal("\0a\0\n\0b\0\n", content) } end |