ruby-changes:28085
From: tenderlove <ko1@a...>
Date: Sat, 6 Apr 2013 02:11:35 +0900 (JST)
Subject: [ruby-changes:28085] tenderlove:r40137 (trunk): * ext/psych/lib/psych/visitors/to_ruby.rb: correctly register
tenderlove 2013-04-06 02:11:21 +0900 (Sat, 06 Apr 2013) New Revision: 40137 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40137 Log: * ext/psych/lib/psych/visitors/to_ruby.rb: correctly register self-referential strings. Fixes tenderlove/psych #135 * test/psych/test_string.rb: appropriate test. Modified files: trunk/ChangeLog trunk/ext/psych/lib/psych/visitors/to_ruby.rb trunk/test/psych/test_string.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 40136) +++ ChangeLog (revision 40137) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Apr 6 02:06:04 2013 Aaron Patterson <aaron@t...> + + * ext/psych/lib/psych/visitors/to_ruby.rb: correctly register + self-referential strings. Fixes tenderlove/psych #135 + + * test/psych/test_string.rb: appropriate test. + Sat Apr 6 01:21:56 2013 Tanaka Akira <akr@f...> * ext/socket/init.c (cloexec_accept): Fix a compile error on Index: ext/psych/lib/psych/visitors/to_ruby.rb =================================================================== --- ext/psych/lib/psych/visitors/to_ruby.rb (revision 40136) +++ ext/psych/lib/psych/visitors/to_ruby.rb (revision 40137) @@ -180,15 +180,25 @@ module Psych https://github.com/ruby/ruby/blob/trunk/ext/psych/lib/psych/visitors/to_ruby.rb#L180 end when /^!(?:str|ruby\/string)(?::(.*))?/, 'tag:yaml.org,2002:str' - klass = resolve_class($1) - members = Hash[*o.children.map { |c| accept c }] - string = members.delete 'str' + klass = resolve_class($1) + members = {} + string = nil - if klass - string = klass.allocate.replace string - register(o, string) - end + o.children.each_slice(2) do |k,v| + key = accept k + value = accept v + if key == 'str' + if klass + string = klass.allocate.replace value + else + string = value + end + register(o, string) + else + members[key] = value + end + end init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o) when /^!ruby\/array:(.*)$/ klass = resolve_class($1) Index: test/psych/test_string.rb =================================================================== --- test/psych/test_string.rb (revision 40136) +++ test/psych/test_string.rb (revision 40137) @@ -15,6 +15,31 @@ module Psych https://github.com/ruby/ruby/blob/trunk/test/psych/test_string.rb#L15 end end + def test_string_subclass_with_anchor + y = Psych.load <<-eoyml +--- +body: + string: &70121654388580 !ruby/string + str: ! 'foo' + x: + body: *70121654388580 + eoyml + assert_equal({"body"=>{"string"=>"foo", "x"=>{"body"=>"foo"}}}, y) + end + + def test_self_referential_string + y = Psych.load <<-eoyml +--- +string: &70121654388580 !ruby/string + str: ! 'foo' + body: *70121654388580 + eoyml + + assert_equal({"string"=>"foo"}, y) + value = y['string'] + assert_equal value, value.instance_variable_get(:@body) + end + def test_another_subclass_with_attributes y = Psych.load Psych.dump Y.new("foo").tap {|y| y.val = 1} assert_equal "foo", y -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/