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

ruby-changes:22259

From: naruse <ko1@a...>
Date: Sun, 15 Jan 2012 16:29:07 +0900 (JST)
Subject: [ruby-changes:22259] naruse:r34308 (trunk): * object.c (rb_inspect): raise the result is not compatible with

naruse	2012-01-15 16:28:50 +0900 (Sun, 15 Jan 2012)

  New Revision: 34308

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

  Log:
    * object.c (rb_inspect): raise the result is not compatible with
      the default external encoding. [ruby-core:42095] [Bug #5848]
      If the default external encoding is ASCII compatible, the encoding of
      inspected result must be compatible with it.
      If the default external encoding is ASCII incomapatible,
      the result must be ASCII only.

  Modified files:
    trunk/ChangeLog
    trunk/object.c
    trunk/test/ruby/test_m17n.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 34307)
+++ ChangeLog	(revision 34308)
@@ -1,3 +1,12 @@
+Sun Jan 15 14:09:48 2012  NARUSE, Yui  <naruse@r...>
+
+	* object.c (rb_inspect): raise the result is not compatible with
+	  the default external encoding. [ruby-core:42095] [Bug #5848]
+	  If the default external encoding is ASCII compatible, the encoding of
+	  inspected result must be compatible with it.
+	  If the default external encoding is ASCII incomapatible,
+	  the result must be ASCII only.
+
 Sun Jan 15 13:21:50 2012  NARUSE, Yui  <naruse@r...>
 
 	* ext/json/parser/parser.rl (json_string_unescape): workaround fix
Index: object.c
===================================================================
--- object.c	(revision 34307)
+++ object.c	(revision 34308)
@@ -368,12 +368,25 @@
     return str;
 }
 
+/*
+ * If the default external encoding is ASCII compatible, the encoding of
+ * inspected result must be compatible with it.
+ * If the default external encoding is ASCII incomapatible,
+ * the result must be ASCII only.
+ */
 VALUE
 rb_inspect(VALUE obj)
 {
-    VALUE s = rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
-    rb_enc_check(rb_enc_default_external(), s);
-    return s;
+    VALUE str = rb_obj_as_string(rb_funcall(obj, id_inspect, 0, 0));
+    rb_encoding *ext = rb_default_external_encoding();
+    if (!rb_enc_asciicompat(ext)) {
+	if (!rb_enc_str_asciionly_p(str))
+	    rb_raise(rb_eEncCompatError, "inspected result must be ASCII only if default external encoding is ASCII incompatible");
+	return str;
+    }
+    if (rb_enc_get(str) != ext && !rb_enc_str_asciionly_p(str))
+	rb_raise(rb_eEncCompatError, "inspected result must be ASCII only or use the same encoding with default external");
+    return str;
 }
 
 static int
Index: test/ruby/test_m17n.rb
===================================================================
--- test/ruby/test_m17n.rb	(revision 34307)
+++ test/ruby/test_m17n.rb	(revision 34308)
@@ -271,6 +271,34 @@
     Encoding.default_external = orig_ext
   end
 
+  def test_object_inspect_external
+    orig_v, $VERBOSE = $VERBOSE, false
+    orig_int, Encoding.default_internal = Encoding.default_internal, nil
+    orig_ext = Encoding.default_external
+    o = Object.new
+
+    Encoding.default_external = Encoding::UTF_16BE
+    def o.inspect
+      "abc"
+    end
+    assert_nothing_raised(Encoding::CompatibilityError) { [o].inspect }
+
+    def o.inspect
+      "abc".encode(Encoding.default_external)
+    end
+    assert_raise(Encoding::CompatibilityError) { [o].inspect }
+
+    Encoding.default_external = Encoding::US_ASCII
+    def o.inspect
+      "\u3042"
+    end
+    assert_raise(Encoding::CompatibilityError) { [o].inspect }
+  ensure
+    Encoding.default_internal = orig_int
+    Encoding.default_external = orig_ext
+    $VERBOSE = orig_v
+  end
+
   def test_str_dump
     [
       e("\xfe"),

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

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