[前][次][番号順一覧][スレッド一覧]

ruby-changes:41006

From: nobu <ko1@a...>
Date: Sun, 13 Dec 2015 18:49:40 +0900 (JST)
Subject: [ruby-changes:41006] nobu:r53085 (trunk): io.c: encoding in warnings

nobu	2015-12-13 18:49:25 +0900 (Sun, 13 Dec 2015)

  New Revision: 53085

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53085

  Log:
    io.c: encoding in warnings
    
    * io.c (parse_mode_enc): preserve encoding of mode string in
      warnings.

  Modified files:
    trunk/ChangeLog
    trunk/error.c
    trunk/io.c
    trunk/test/ruby/test_io_m17n.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53084)
+++ ChangeLog	(revision 53085)
@@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Sun Dec 13 18:46:31 2015  Nobuyoshi Nakada  <nobu@r...>
+Sun Dec 13 18:49:25 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* io.c (parse_mode_enc): preserve encoding of mode string in
+	  warnings.
 
 	* io.c (io_encname_bom_p): check BOM prefix only, not including
 	  UTF prefix.
Index: io.c
===================================================================
--- io.c	(revision 53084)
+++ io.c	(revision 53085)
@@ -5054,13 +5054,14 @@ rb_io_ext_int_to_encs(rb_encoding *ext, https://github.com/ruby/ruby/blob/trunk/io.c#L5054
 }
 
 static void
-unsupported_encoding(const char *name)
+unsupported_encoding(const char *name, rb_encoding *enc)
 {
-    rb_warn("Unsupported encoding %s ignored", name);
+    rb_enc_warn(enc, "Unsupported encoding %s ignored", name);
 }
 
 static void
-parse_mode_enc(const char *estr, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
+parse_mode_enc(const char *estr, rb_encoding *estr_enc,
+	       rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
 {
     const char *p;
     char encname[ENCODING_MAXNAMELEN+1];
@@ -5080,7 +5081,7 @@ parse_mode_enc(const char *estr, rb_enco https://github.com/ruby/ruby/blob/trunk/io.c#L5081
 	    fmode |= FMODE_SETENC_BY_BOM;
 	}
 	else {
-	    rb_warn("BOM with non-UTF encoding %s is nonsense", estr);
+	    rb_enc_warn(estr_enc, "BOM with non-UTF encoding %s is nonsense", estr);
 	    fmode &= ~FMODE_SETENC_BY_BOM;
 	}
     }
@@ -5101,7 +5102,7 @@ parse_mode_enc(const char *estr, rb_enco https://github.com/ruby/ruby/blob/trunk/io.c#L5102
 	ext_enc = rb_enc_from_index(idx);
     else {
 	if (idx != -2)
-	    unsupported_encoding(estr);
+	    unsupported_encoding(estr, estr_enc);
 	ext_enc = NULL;
     }
 
@@ -5114,7 +5115,7 @@ parse_mode_enc(const char *estr, rb_enco https://github.com/ruby/ruby/blob/trunk/io.c#L5115
 	else {
 	    idx2 = rb_enc_find_index(p);
 	    if (idx2 < 0)
-		unsupported_encoding(p);
+		unsupported_encoding(p, estr_enc);
 	    else if (!(fmode & FMODE_SETENC_BY_BOM) && (idx2 == idx)) {
 		int_enc = (rb_encoding *)Qnil;
 	    }
@@ -5181,7 +5182,8 @@ rb_io_extract_encoding_option(VALUE opt, https://github.com/ruby/ruby/blob/trunk/io.c#L5182
     if (!NIL_P(encoding)) {
 	extracted = 1;
 	if (!NIL_P(tmp = rb_check_string_type(encoding))) {
-	    parse_mode_enc(StringValueCStr(tmp), enc_p, enc2_p, fmode_p);
+	    parse_mode_enc(StringValueCStr(tmp), rb_enc_get(tmp),
+			   enc_p, enc2_p, fmode_p);
 	}
 	else {
 	    rb_io_ext_int_to_encs(rb_to_encoding(encoding), NULL, enc_p, enc2_p, 0);
@@ -5286,7 +5288,7 @@ rb_io_extract_modeenc(VALUE *vmode_p, VA https://github.com/ruby/ruby/blob/trunk/io.c#L5288
         p = strchr(p, ':');
         if (p) {
             has_enc = 1;
-            parse_mode_enc(p+1, &enc, &enc2, &fmode);
+            parse_mode_enc(p+1, rb_enc_get(vmode), &enc, &enc2, &fmode);
         }
 	else {
 	    rb_encoding *e;
@@ -5592,7 +5594,8 @@ rb_file_open_internal(VALUE io, VALUE fi https://github.com/ruby/ruby/blob/trunk/io.c#L5594
     convconfig_t convconfig;
 
     if (p) {
-        parse_mode_enc(p+1, &convconfig.enc, &convconfig.enc2, &fmode);
+        parse_mode_enc(p+1, rb_usascii_encoding(),
+		       &convconfig.enc, &convconfig.enc2, &fmode);
     }
     else {
 	rb_encoding *e;
@@ -9429,7 +9432,7 @@ static rb_encoding * https://github.com/ruby/ruby/blob/trunk/io.c#L9432
 find_encoding(VALUE v)
 {
     rb_encoding *enc = rb_find_encoding(v);
-    if (!enc) unsupported_encoding(StringValueCStr(v));
+    if (!enc) rb_warn("Unsupported encoding %"PRIsVALUE" ignored", v);
     return enc;
 }
 
@@ -9475,8 +9478,8 @@ io_encoding_set(rb_io_t *fptr, VALUE v1, https://github.com/ruby/ruby/blob/trunk/io.c#L9478
 	}
 	else {
 	    tmp = rb_check_string_type(v1);
-	    if (!NIL_P(tmp) && rb_enc_asciicompat(rb_enc_get(tmp))) {
-                parse_mode_enc(RSTRING_PTR(tmp), &enc, &enc2, NULL);
+	    if (!NIL_P(tmp) && rb_enc_asciicompat(enc = rb_enc_get(tmp))) {
+                parse_mode_enc(RSTRING_PTR(tmp), enc, &enc, &enc2, NULL);
 		SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags);
                 ecflags = rb_econv_prepare_options(opt, &ecopts, ecflags);
 	    }
Index: error.c
===================================================================
--- error.c	(revision 53084)
+++ error.c	(revision 53085)
@@ -231,7 +231,6 @@ rb_warn(const char *fmt, ...) https://github.com/ruby/ruby/blob/trunk/error.c#L231
     rb_write_error_str(mesg);
 }
 
-#if 0
 void
 rb_enc_warn(rb_encoding *enc, const char *fmt, ...)
 {
@@ -245,7 +244,6 @@ rb_enc_warn(rb_encoding *enc, const char https://github.com/ruby/ruby/blob/trunk/error.c#L244
     va_end(args);
     rb_write_error_str(mesg);
 }
-#endif
 
 /* rb_warning() reports only in verbose mode */
 void
Index: test/ruby/test_io_m17n.rb
===================================================================
--- test/ruby/test_io_m17n.rb	(revision 53084)
+++ test/ruby/test_io_m17n.rb	(revision 53085)
@@ -2107,6 +2107,14 @@ EOT https://github.com/ruby/ruby/blob/trunk/test/ruby/test_io_m17n.rb#L2107
       open(IO::NULL, "w:bom|us-ascii") {|f| enc = f.external_encoding}
     }
     assert_equal(Encoding::US_ASCII, enc)
+
+    tlhInganHol = "\u{f8e4 f8d9 f8d7 f8dc f8d0 f8db} \u{f8d6 f8dd f8d9}"
+    EnvUtil.with_default_external(Encoding::UTF_8) {
+      assert_warn(/#{tlhInganHol}/) {
+        open(IO::NULL, "w:bom|#{tlhInganHol}") {|f| enc = f.external_encoding}
+      }
+    }
+    assert_nil(enc)
   end
 
   def test_cbuf

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]