diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-01-08 14:16:13 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-01-08 14:16:13 -0800 |
commit | af308f8307899cb9e1c0fffea4bce3110a1c3926 (patch) | |
tree | f62717d4ec479d6b522b0d56ae038633b8c22555 | |
parent | 8f84ad0fc711a82a1040def861cb121e8985fd4c (diff) | |
download | psych-af308f8307899cb9e1c0fffea4bce3110a1c3926.tar.gz |
* 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
fixes #196
-rw-r--r-- | CHANGELOG.rdoc | 7 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 4 | ||||
-rw-r--r-- | test/psych/test_hash.rb | 28 |
3 files changed, 37 insertions, 2 deletions
diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 5cd3227..4c04f1a 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,10 @@ +Fri Jan 9 07:13:55 2015 Aaron Patterson <aaron@tenderlovemaking.com> + + * 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@tenderlovemaking.com> * ext/psych/lib/psych/visitors/to_ruby.rb: revive hashes with ivars diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index adf38a2..f353e9c 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -262,7 +262,7 @@ module Psych 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 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) diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb index 8d7ba1b..066df66 100644 --- a/test/psych/test_hash.rb +++ b/test/psych/test_hash.rb @@ -5,11 +5,39 @@ module Psych 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 |