From 89b4f06a596e993a82630913f085a82265cb499c Mon Sep 17 00:00:00 2001 From: akr Date: Mon, 18 Aug 2008 08:24:49 +0000 Subject: * io.c (clear_readconv): extracted from rb_io_fptr_finalize. (mode_enc): call clear_readconv. (io_set_encoding): ditto. (argf_next_argv): ditto. (io_encoding_set): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18690 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++++++ io.c | 28 +++++++++++++++++++-------- test/ruby/test_io_m17n.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index be1fe85984..d2f166aa56 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Mon Aug 18 17:23:38 2008 Tanaka Akira + + * io.c (clear_readconv): extracted from rb_io_fptr_finalize. + (mode_enc): call clear_readconv. + (io_set_encoding): ditto. + (argf_next_argv): ditto. + (io_encoding_set): ditto. + Mon Aug 18 16:54:06 2008 Tanaka Akira * io.c (mode_enc): modify enc and enc2 consistently. diff --git a/io.c b/io.c index 18eefb2474..92db7be8e2 100644 --- a/io.c +++ b/io.c @@ -2894,6 +2894,19 @@ rb_io_fptr_cleanup(rb_io_t *fptr, int noraise) } } +static void +clear_readconv(rb_io_t *fptr) +{ + if (fptr->readconv) { + rb_econv_close(fptr->readconv); + fptr->readconv = NULL; + } + if (fptr->crbuf) { + free(fptr->crbuf); + fptr->crbuf = NULL; + } +} + int rb_io_fptr_finalize(rb_io_t *fptr) { @@ -2913,14 +2926,7 @@ rb_io_fptr_finalize(rb_io_t *fptr) free(fptr->wbuf); fptr->wbuf = 0; } - if (fptr->readconv) { - rb_econv_close(fptr->readconv); - fptr->readconv = NULL; - } - if (fptr->crbuf) { - free(fptr->crbuf); - fptr->crbuf = NULL; - } + clear_readconv(fptr); free(fptr); return 1; } @@ -3529,6 +3535,7 @@ mode_enc(rb_io_t *fptr, const char *estr) fptr->enc = 0; fptr->enc2 = 0; + clear_readconv(fptr); p0 = strrchr(estr, ':'); if (!p0) p1 = estr; @@ -4258,6 +4265,7 @@ io_set_encoding(VALUE io, VALUE opt) GetOpenFile(io, fptr); fptr->enc = 0; fptr->enc2 = 0; + clear_readconv(fptr); if (!NIL_P(encoding)) { rb_warn("Ignoring encoding parameter '%s': external_encoding is used", RSTRING_PTR(encoding)); @@ -5604,6 +5612,7 @@ argf_next_argv(VALUE argf) GetOpenFile(current_file, fptr); fptr->enc = argf_enc; fptr->enc2 = argf_enc2; + clear_readconv(fptr); } } else { @@ -6331,11 +6340,13 @@ io_encoding_set(rb_io_t *fptr, int argc, VALUE v1, VALUE v2) if (argc == 2) { fptr->enc2 = rb_to_encoding(v1); fptr->enc = rb_to_encoding(v2); + clear_readconv(fptr); } else if (argc == 1) { if (NIL_P(v1)) { fptr->enc = 0; fptr->enc2 = 0; + clear_readconv(fptr); } else { VALUE tmp = rb_check_string_type(v1); @@ -6345,6 +6356,7 @@ io_encoding_set(rb_io_t *fptr, int argc, VALUE v1, VALUE v2) else { fptr->enc = rb_to_encoding(v1); fptr->enc2 = 0; + clear_readconv(fptr); } } } diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index 7bcd051c36..e982722cfe 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -552,5 +552,54 @@ EOT } } end + + def test_set_encoding + with_pipe("utf-8:euc-jp") {|r, w| + s = "\u3042".force_encoding("ascii-8bit") + s << "\x82\xa0".force_encoding("ascii-8bit") + w << s + w.close + assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc) + r.set_encoding("shift_jis:euc-jp") + assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc) + } + end + + def test_set_encoding2 + with_pipe("utf-8:euc-jp") {|r, w| + s = "\u3042".force_encoding("ascii-8bit") + s << "\x82\xa0".force_encoding("ascii-8bit") + w << s + w.close + assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc) + r.set_encoding("shift_jis", "euc-jp") + assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc) + } + end + + def test_set_encoding_nil + with_pipe("utf-8:euc-jp") {|r, w| + s = "\u3042".force_encoding("ascii-8bit") + s << "\x82\xa0".force_encoding("ascii-8bit") + w << s + w.close + assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc) + r.set_encoding(nil) + assert_equal("\x82\xa0".force_encoding(Encoding.default_external), r.read) + } + end + + def test_set_encoding_enc + with_pipe("utf-8:euc-jp") {|r, w| + s = "\u3042".force_encoding("ascii-8bit") + s << "\x82\xa0".force_encoding("ascii-8bit") + w << s + w.close + assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc) + r.set_encoding(Encoding::Shift_JIS) + assert_equal("\x82\xa0".force_encoding(Encoding::Shift_JIS), r.getc) + } + end + end -- cgit v1.2.3