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

ruby-changes:37195

From: tenderlove <ko1@a...>
Date: Fri, 16 Jan 2015 15:39:24 +0900 (JST)
Subject: [ruby-changes:37195] tenderlove:r49276 (trunk): * ext/psych/lib/psych/visitors/yaml_tree.rb: only dump hash

tenderlove	2015-01-16 15:39:13 +0900 (Fri, 16 Jan 2015)

  New Revision: 49276

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

  Log:
    * ext/psych/lib/psych/visitors/yaml_tree.rb: only dump hash
      subclasses. Thanks Joe Eli McIlvain <joe.eli.mac@g...>
    
    * test/psych/test_hash.rb: test for change

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/lib/psych/visitors/yaml_tree.rb
    trunk/test/psych/test_hash.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49275)
+++ ChangeLog	(revision 49276)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Jan 16 15:37:49 2015  Aaron Patterson <aaron@t...>
+
+	* ext/psych/lib/psych/visitors/yaml_tree.rb: only dump hash
+	  subclasses. Thanks Joe Eli McIlvain <joe.eli.mac@g...>
+
+	* test/psych/test_hash.rb: test for change
+
 Fri Jan 16 15:35:21 2015  Aaron Patterson <aaron@t...>
 
 	* ext/psych/lib/psych.rb: bump version
Index: ext/psych/lib/psych/visitors/yaml_tree.rb
===================================================================
--- ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 49275)
+++ ext/psych/lib/psych/visitors/yaml_tree.rb	(revision 49276)
@@ -325,10 +325,11 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L325
           style = Nodes::Scalar::SINGLE_QUOTED
         end
 
-        ivars = find_ivars o
+        is_primitive = o.class == ::String
+        ivars = find_ivars o, is_primitive
 
         if ivars.empty?
-          unless o.class == ::String
+          unless is_primitive
             tag = "!ruby/string:#{o.class}"
             plain = false
             quote = false
@@ -367,45 +368,15 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L368
       end
 
       def visit_Hash o
-        ivars    = o.instance_variables
-
-        if ivars.any?
-          tag = "!ruby/hash-with-ivars"
-          tag << ":#{o.class}" unless o.class == ::Hash
-
-          register(o, @emitter.start_mapping(nil, tag, false, Psych::Nodes::Mapping::BLOCK))
-
-          @emitter.scalar 'elements', nil, nil, true, false, Nodes::Scalar::ANY
-
-          @emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
+        if o.class == ::Hash
+          register(o, @emitter.start_mapping(nil, nil, true, Psych::Nodes::Mapping::BLOCK))
           o.each do |k,v|
             accept k
             accept v
           end
           @emitter.end_mapping
-
-          @emitter.scalar 'ivars', nil, nil, true, false, Nodes::Scalar::ANY
-
-          @emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
-          o.instance_variables.each do |ivar|
-            accept ivar
-            accept o.instance_variable_get ivar
-          end
-          @emitter.end_mapping
-
-          @emitter.end_mapping
         else
-          tag      = o.class == ::Hash ? nil : "!ruby/hash:#{o.class}"
-          implicit = !tag
-
-          register(o, @emitter.start_mapping(nil, tag, implicit, Psych::Nodes::Mapping::BLOCK))
-
-          o.each do |k,v|
-            accept k
-            accept v
-          end
-
-          @emitter.end_mapping
+          visit_hash_subclass o
         end
       end
 
@@ -468,7 +439,8 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L439
 
       def visit_array_subclass o
         tag = "!ruby/array:#{o.class}"
-        if o.instance_variables.empty?
+        ivars = o.instance_variables
+        if ivars.empty?
           node = @emitter.start_sequence(nil, tag, false, Nodes::Sequence::BLOCK)
           register o, node
           o.each { |c| accept c }
@@ -486,6 +458,35 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L458
           # Dump the ivars
           accept 'ivars'
           @emitter.start_mapping(nil, nil, true, Nodes::Sequence::BLOCK)
+          ivars.each do |ivar|
+            accept ivar
+            accept o.instance_variable_get ivar
+          end
+          @emitter.end_mapping
+
+          @emitter.end_mapping
+        end
+      end
+
+      def visit_hash_subclass o
+        ivars = o.instance_variables
+        if ivars.any?
+          tag = "!ruby/hash-with-ivars:#{o.class}"
+          node = @emitter.start_mapping(nil, tag, false, Psych::Nodes::Mapping::BLOCK)
+          register(o, node)
+
+          # Dump the elements
+          accept 'elements'
+          @emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
+          o.each do |k,v|
+            accept k
+            accept v
+          end
+          @emitter.end_mapping
+
+          # Dump the ivars
+          accept 'ivars'
+          @emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
           o.instance_variables.each do |ivar|
             accept ivar
             accept o.instance_variable_get ivar
@@ -493,6 +494,15 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L494
           @emitter.end_mapping
 
           @emitter.end_mapping
+        else
+          tag = "!ruby/hash:#{o.class}"
+          node = @emitter.start_mapping(nil, tag, false, Psych::Nodes::Mapping::BLOCK)
+          register(o, node)
+          o.each do |k,v|
+            accept k
+            accept v
+          end
+          @emitter.end_mapping
         end
       end
 
@@ -524,7 +534,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L534
       end
 
       # FIXME: remove this method once "to_yaml_properties" is removed
-      def find_ivars target
+      def find_ivars target, is_primitive=false
         begin
           loc = target.method(:to_yaml_properties).source_location.first
           unless loc.start_with?(Psych::DEPRECATED) || loc.end_with?('rubytypes.rb')
@@ -538,7 +548,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L548
           # and it's OK to skip it since it's only to emit a warning.
         end
 
-        target.instance_variables
+        is_primitive ? [] : target.instance_variables
       end
 
       def register target, yaml_obj
Index: test/psych/test_hash.rb
===================================================================
--- test/psych/test_hash.rb	(revision 49275)
+++ test/psych/test_hash.rb	(revision 49276)
@@ -38,12 +38,6 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_hash.rb#L38
       assert_cycle t1
     end
 
-    def test_hash_with_ivars
-      @hash.instance_variable_set :@foo, 'bar'
-      dup = Psych.load Psych.dump @hash
-      assert_equal 'bar', dup.instance_variable_get(:@foo)
-    end
-
     def test_hash_subclass_with_ivars
       x = X.new
       x[:a] = 'b'

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

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