ruby-changes:37448
From: tenderlove <ko1@a...>
Date: Fri, 6 Feb 2015 17:50:19 +0900 (JST)
Subject: [ruby-changes:37448] tenderlove:r49529 (trunk): * ext/psych/lib/psych/visitors/yaml_tree.rb: register nodes when
tenderlove 2015-02-06 17:50:00 +0900 (Fri, 06 Feb 2015) New Revision: 49529 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49529 Log: * ext/psych/lib/psych/visitors/yaml_tree.rb: register nodes when dumping objects with custom coders. [ruby-core:66215] [Bug #10496] * test/psych/test_coder.rb: test for fix Modified files: trunk/ChangeLog trunk/ext/psych/lib/psych/visitors/yaml_tree.rb trunk/test/psych/test_coder.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 49528) +++ ChangeLog (revision 49529) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Feb 6 17:47:05 2015 Aaron Patterson <aaron@t...> + + * ext/psych/lib/psych/visitors/yaml_tree.rb: register nodes when + dumping objects with custom coders. [ruby-core:66215] [Bug #10496] + + * test/psych/test_coder.rb: test for fix + Fri Feb 6 16:58:31 2015 Aaron Patterson <aaron@t...> * ext/psych/lib/psych/visitors/to_ruby.rb: fix support for regular Index: ext/psych/lib/psych/visitors/yaml_tree.rb =================================================================== --- ext/psych/lib/psych/visitors/yaml_tree.rb (revision 49528) +++ ext/psych/lib/psych/visitors/yaml_tree.rb (revision 49529) @@ -21,6 +21,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L21 end def register target, node + return unless target.respond_to? :object_id @targets << target @obj_to_node[target.object_id] = node end @@ -566,10 +567,10 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L567 c = Psych::Coder.new(tag) o.encode_with(c) - emit_coder c + emit_coder c, o end - def emit_coder c + def emit_coder c, o case c.type when :scalar @emitter.scalar c.scalar, nil, c.tag, c.tag.nil?, false, Nodes::Scalar::ANY @@ -580,7 +581,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/yaml_tree.rb#L581 end @emitter.end_sequence when :map - @emitter.start_mapping nil, c.tag, c.implicit, c.style + register o, @emitter.start_mapping(nil, c.tag, c.implicit, c.style) c.map.each do |k,v| accept k accept v Index: test/psych/test_coder.rb =================================================================== --- test/psych/test_coder.rb (revision 49528) +++ test/psych/test_coder.rb (revision 49529) @@ -95,6 +95,28 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_coder.rb#L95 end end + class Referential + attr_reader :a + + def initialize + @a = self + end + + def encode_with(c) + c['a'] = @a + end + + def init_with(c) + @a = c['a'] + end + end + + def test_self_referential + x = Referential.new + copy = Psych.load Psych.dump x + assert_equal copy, copy.a + end + def test_represent_with_object thing = Psych.load(Psych.dump(RepresentWithObject.new)) assert_equal 20, thing -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/