ruby-changes:31462
From: tenderlove <ko1@a...>
Date: Wed, 6 Nov 2013 04:15:48 +0900 (JST)
Subject: [ruby-changes:31462] tenderlove:r43541 (trunk): * ext/psych/lib/psych/visitors/to_ruby.rb: process merge keys before
tenderlove 2013-11-06 04:15:40 +0900 (Wed, 06 Nov 2013) New Revision: 43541 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43541 Log: * ext/psych/lib/psych/visitors/to_ruby.rb: process merge keys before reviving objects. Fixes GH psych #168 * test/psych/test_merge_keys.rb: test for change https://github.com/tenderlove/psych/issues/168 Modified files: trunk/ChangeLog trunk/ext/psych/lib/psych/visitors/to_ruby.rb trunk/test/psych/test_merge_keys.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 43540) +++ ChangeLog (revision 43541) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Nov 6 04:14:25 2013 Aaron Patterson <aaron@t...> + + * ext/psych/lib/psych/visitors/to_ruby.rb: process merge keys before + reviving objects. Fixes GH psych #168 + * test/psych/test_merge_keys.rb: test for change + https://github.com/tenderlove/psych/issues/168 + Tue Nov 5 21:21:47 2013 Tanaka Akira <akr@f...> * test/ruby/test_thread.rb (test_thread_join_in_trap): Index: ext/psych/lib/psych/visitors/to_ruby.rb =================================================================== --- ext/psych/lib/psych/visitors/to_ruby.rb (revision 43540) +++ ext/psych/lib/psych/visitors/to_ruby.rb (revision 43541) @@ -156,7 +156,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L156 if Psych.load_tags[o.tag] return revive(resolve_class(Psych.load_tags[o.tag]), o) end - return revive_hash({}, o) unless o.tag + return revive_hash(register(o, {}), o) unless o.tag case o.tag when /^!ruby\/struct:?(.*)?$/ @@ -256,7 +256,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L256 set when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/ - revive_hash resolve_class($1).new, o + revive_hash register(o, resolve_class($1).new), o when '!omap', 'tag:yaml.org,2002:omap' map = register(o, class_loader.psych_omap.new) @@ -266,7 +266,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L266 map else - revive_hash({}, o) + revive_hash(register(o, {}), o) end end @@ -295,8 +295,6 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L295 end def revive_hash hash, o - @st[o.anchor] = hash if o.anchor - o.children.each_slice(2) { |k,v| key = accept(k) val = accept(v) @@ -334,10 +332,8 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L332 end def revive klass, node - s = klass.allocate - @st[node.anchor] = s if node.anchor - h = Hash[*node.children.map { |c| accept c }] - init_with(s, h, node) + s = register(node, klass.allocate) + init_with(s, revive_hash({}, node), node) end def init_with o, h, node Index: test/psych/test_merge_keys.rb =================================================================== --- test/psych/test_merge_keys.rb (revision 43540) +++ test/psych/test_merge_keys.rb (revision 43541) @@ -2,6 +2,24 @@ require_relative 'helper' https://github.com/ruby/ruby/blob/trunk/test/psych/test_merge_keys.rb#L2 module Psych class TestMergeKeys < TestCase + class Product + attr_reader :bar + end + + def test_mergekey_with_object + s = <<-eoyml +foo: &foo + bar: 10 +product: + !ruby/object:#{Product.name} + <<: *foo + eoyml + hash = Psych.load s + assert_equal({"bar" => 10}, hash["foo"]) + product = hash["product"] + assert_equal 10, product.bar + end + def test_merge_nil yaml = <<-eoyml defaults: &defaults -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/