ruby-changes:37108
From: tenderlove <ko1@a...>
Date: Fri, 9 Jan 2015 07:15:31 +0900 (JST)
Subject: [ruby-changes:37108] tenderlove:r49189 (trunk): * ext/psych/lib/psych/visitors/to_ruby.rb: call `allocate` on hash
tenderlove 2015-01-09 07:15:20 +0900 (Fri, 09 Jan 2015) New Revision: 49189 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49189 Log: * ext/psych/lib/psych/visitors/to_ruby.rb: call `allocate` on hash subclasses. Fixes github.com/tenderlove/psych/issues/196 * test/psych/test_hash.rb: test for change Modified files: trunk/ChangeLog trunk/ext/psych/lib/psych/visitors/to_ruby.rb trunk/test/psych/test_hash.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 49188) +++ ChangeLog (revision 49189) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Jan 9 07:13:55 2015 Aaron Patterson <aaron@t...> + + * ext/psych/lib/psych/visitors/to_ruby.rb: call `allocate` on hash + subclasses. Fixes github.com/tenderlove/psych/issues/196 + + * test/psych/test_hash.rb: test for change + Fri Jan 9 06:58:43 2015 Aaron Patterson <aaron@t...> * ext/psych/lib/psych/visitors/to_ruby.rb: revive hashes with ivars Index: ext/psych/lib/psych/visitors/to_ruby.rb =================================================================== --- ext/psych/lib/psych/visitors/to_ruby.rb (revision 49188) +++ ext/psych/lib/psych/visitors/to_ruby.rb (revision 49189) @@ -262,7 +262,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L262 set when /^!ruby\/hash-with-ivars(?::(.*))?$/ - hash = $1 ? resolve_class($1).new : {} + hash = $1 ? resolve_class($1).allocate : {} o.children.each_slice(2) do |key, value| case key.value when 'elements' @@ -276,7 +276,7 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L276 hash when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/ - revive_hash register(o, resolve_class($1).new), o + revive_hash register(o, resolve_class($1).allocate), o when '!omap', 'tag:yaml.org,2002:omap' map = register(o, class_loader.psych_omap.new) Index: test/psych/test_hash.rb =================================================================== --- test/psych/test_hash.rb (revision 49188) +++ test/psych/test_hash.rb (revision 49189) @@ -5,11 +5,39 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_hash.rb#L5 class X < Hash end + class HashWithCustomInit < Hash + attr_reader :obj + def initialize(obj) + @obj = obj + end + end + + class HashWithCustomInitNoIvar < Hash + def initialize(obj) + # *shrug* + end + end + def setup super @hash = { :a => 'b' } end + def test_custom_initialized + a = [1,2,3,4,5] + t1 = HashWithCustomInit.new(a) + t2 = Psych.load(Psych.dump(t1)) + assert_equal t1, t2 + assert_cycle t1 + end + + def test_custom_initialize_no_ivar + t1 = HashWithCustomInitNoIvar.new(nil) + t2 = Psych.load(Psych.dump(t1)) + assert_equal t1, t2 + assert_cycle t1 + end + def test_hash_with_ivars @hash.instance_variable_set :@foo, 'bar' dup = Psych.load Psych.dump @hash -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/