Skip to content

Commit 7a7bb64

Browse files
committed
* ext/psych/lib/psych/visitors/to_ruby.rb: correctly register
self-referential strings. Fixes tenderlove/psych #135 * test/psych/test_string.rb: appropriate test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40137 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent fbb29bc commit 7a7bb64

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Sat Apr 6 02:06:04 2013 Aaron Patterson <[email protected]>
2+
3+
* ext/psych/lib/psych/visitors/to_ruby.rb: correctly register
4+
self-referential strings. Fixes tenderlove/psych #135
5+
6+
* test/psych/test_string.rb: appropriate test.
7+
18
Sat Apr 6 01:21:56 2013 Tanaka Akira <[email protected]>
29

310
* ext/socket/init.c (cloexec_accept): Fix a compile error on

ext/psych/lib/psych/visitors/to_ruby.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,25 @@ def visit_Psych_Nodes_Mapping o
180180
end
181181

182182
when /^!(?:str|ruby\/string)(?::(.*))?/, 'tag:yaml.org,2002:str'
183-
klass = resolve_class($1)
184-
members = Hash[*o.children.map { |c| accept c }]
185-
string = members.delete 'str'
183+
klass = resolve_class($1)
184+
members = {}
185+
string = nil
186186

187-
if klass
188-
string = klass.allocate.replace string
189-
register(o, string)
190-
end
187+
o.children.each_slice(2) do |k,v|
188+
key = accept k
189+
value = accept v
191190

191+
if key == 'str'
192+
if klass
193+
string = klass.allocate.replace value
194+
else
195+
string = value
196+
end
197+
register(o, string)
198+
else
199+
members[key] = value
200+
end
201+
end
192202
init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
193203
when /^!ruby\/array:(.*)$/
194204
klass = resolve_class($1)

test/psych/test_string.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ def initialize
1515
end
1616
end
1717

18+
def test_string_subclass_with_anchor
19+
y = Psych.load <<-eoyml
20+
---
21+
body:
22+
string: &70121654388580 !ruby/string
23+
str: ! 'foo'
24+
x:
25+
body: *70121654388580
26+
eoyml
27+
assert_equal({"body"=>{"string"=>"foo", "x"=>{"body"=>"foo"}}}, y)
28+
end
29+
30+
def test_self_referential_string
31+
y = Psych.load <<-eoyml
32+
---
33+
string: &70121654388580 !ruby/string
34+
str: ! 'foo'
35+
body: *70121654388580
36+
eoyml
37+
38+
assert_equal({"string"=>"foo"}, y)
39+
value = y['string']
40+
assert_equal value, value.instance_variable_get(:@body)
41+
end
42+
1843
def test_another_subclass_with_attributes
1944
y = Psych.load Psych.dump Y.new("foo").tap {|y| y.val = 1}
2045
assert_equal "foo", y

0 commit comments

Comments
 (0)