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

ruby-changes:56904

From: Nobuyoshi <ko1@a...>
Date: Sat, 10 Aug 2019 13:24:07 +0900 (JST)
Subject: [ruby-changes:56904] Nobuyoshi Nakada: ffdef3674a (master): Warn instance variable `E`

https://git.ruby-lang.org/ruby.git/commit/?id=ffdef3674a

From ffdef3674af921d7ddd550dc492dcdbed97f7ba5 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sat, 10 Aug 2019 13:18:41 +0900
Subject: Warn instance variable `E`

It is not dumped, as it is a short alias for `:encoding`.

diff --git a/ext/-test-/marshal/internal_ivar/internal_ivar.c b/ext/-test-/marshal/internal_ivar/internal_ivar.c
index 299da27..de0cf71 100644
--- a/ext/-test-/marshal/internal_ivar/internal_ivar.c
+++ b/ext/-test-/marshal/internal_ivar/internal_ivar.c
@@ -1,12 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/marshal/internal_ivar/internal_ivar.c#L1
 #include <ruby.h>
 
-static ID id_normal_ivar, id_internal_ivar;
+static ID id_normal_ivar, id_internal_ivar, id_encoding_short;
 
 static VALUE
-init(VALUE self, VALUE arg1, VALUE arg2)
+init(VALUE self, VALUE arg1, VALUE arg2, VALUE arg3)
 {
     rb_ivar_set(self, id_normal_ivar, arg1);
     rb_ivar_set(self, id_internal_ivar, arg2);
+    rb_ivar_set(self, id_encoding_short, arg3);
     return self;
 }
 
@@ -22,6 +23,12 @@ get_internal(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/-test-/marshal/internal_ivar/internal_ivar.c#L23
     return rb_attr_get(self, id_internal_ivar);
 }
 
+static VALUE
+get_encoding_short(VALUE self)
+{
+    return rb_attr_get(self, id_encoding_short);
+}
+
 void
 Init_internal_ivar(void)
 {
@@ -33,7 +40,9 @@ Init_internal_ivar(void) https://github.com/ruby/ruby/blob/trunk/ext/-test-/marshal/internal_ivar/internal_ivar.c#L40
     /* leave id_internal_ivar being 0 */
     id_internal_ivar = rb_make_internal_id();
 #endif
-    rb_define_method(newclass, "initialize", init, 2);
+    id_encoding_short = rb_intern_const("E");
+    rb_define_method(newclass, "initialize", init, 3);
     rb_define_method(newclass, "normal", get_normal, 0);
     rb_define_method(newclass, "internal", get_internal, 0);
+    rb_define_method(newclass, "encoding_short", get_encoding_short, 0);
 }
diff --git a/marshal.c b/marshal.c
index 5e0c30b..b6de650 100644
--- a/marshal.c
+++ b/marshal.c
@@ -576,7 +576,13 @@ w_obj_each(st_data_t key, st_data_t val, st_data_t a) https://github.com/ruby/ruby/blob/trunk/marshal.c#L576
     struct w_ivar_arg *ivarg = (struct w_ivar_arg *)a;
     struct dump_call_arg *arg = ivarg->dump;
 
-    if (to_be_skipped_id(id)) return ST_CONTINUE;
+    if (to_be_skipped_id(id)) {
+        if (id == s_encoding_short) {
+            rb_warn("instance variable `E' on class %"PRIsVALUE" is not dumped",
+                    CLASS_OF(arg->obj));
+        }
+        return ST_CONTINUE;
+    }
     if (!ivarg->num_ivar) {
         rb_raise(rb_eRuntimeError, "instance variable added to %"PRIsVALUE" instance",
                  CLASS_OF(arg->obj));
diff --git a/test/-ext-/marshal/test_internal_ivar.rb b/test/-ext-/marshal/test_internal_ivar.rb
index 5152966..a32138f 100644
--- a/test/-ext-/marshal/test_internal_ivar.rb
+++ b/test/-ext-/marshal/test_internal_ivar.rb
@@ -7,14 +7,18 @@ module Bug end https://github.com/ruby/ruby/blob/trunk/test/-ext-/marshal/test_internal_ivar.rb#L7
 module Bug::Marshal
   class TestInternalIVar < Test::Unit::TestCase
     def test_marshal
-      v = InternalIVar.new("hello", "world")
+      v = InternalIVar.new("hello", "world", "bye")
       assert_equal("hello", v.normal)
       assert_equal("world", v.internal)
-      dump = ::Marshal.dump(v)
+      assert_equal("bye", v.encoding_short)
+      dump = assert_warn(/instance variable `E' on class \S+ is not dumped/) {
+        ::Marshal.dump(v)
+      }
       v = assert_nothing_raised {break ::Marshal.load(dump)}
       assert_instance_of(InternalIVar, v)
       assert_equal("hello", v.normal)
       assert_nil(v.internal)
+      assert_nil(v.encoding_short)
     end
   end
 end
-- 
cgit v0.10.2


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

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