mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/yaml/types.rb: Likewise, pass self to YAML::quick_emit;
merged from 1.9. * lib/yaml.rb (quick_emit): use combination of object_id and hash to identify repeated object references, since GC will reuse memory of objects during output of YAML. [ruby-Bugs-8548] [ruby-Bugs-3698]; merged from 1.9. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16082 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
80627a281e
commit
bf11d6ee76
5 changed files with 34 additions and 12 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,3 +1,13 @@
|
||||||
|
Sat Apr 19 20:16:52 2008 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* lib/yaml/types.rb: Likewise, pass self to YAML::quick_emit;
|
||||||
|
merged from 1.9.
|
||||||
|
|
||||||
|
* lib/yaml.rb (quick_emit): use combination of object_id and hash to
|
||||||
|
identify repeated object references, since GC will reuse memory of
|
||||||
|
objects during output of YAML. [ruby-Bugs-8548] [ruby-Bugs-3698];
|
||||||
|
merged from 1.9.
|
||||||
|
|
||||||
Sat Apr 19 20:05:39 2008 Akinori MUSHA <knu@iDaemons.org>
|
Sat Apr 19 20:05:39 2008 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
* array.c (rb_ary_equal, rb_ary_eql, rb_ary_hash, rb_ary_cmp):
|
* array.c (rb_ary_equal, rb_ary_eql, rb_ary_hash, rb_ary_cmp):
|
||||||
|
|
|
@ -384,6 +384,10 @@ module YAML
|
||||||
else
|
else
|
||||||
emitter.reset( opts )
|
emitter.reset( opts )
|
||||||
end
|
end
|
||||||
|
oid =
|
||||||
|
case oid when Fixnum, NilClass; oid
|
||||||
|
else oid = "#{oid.object_id}-#{oid.hash}"
|
||||||
|
end
|
||||||
out.emit( oid, &e )
|
out.emit( oid, &e )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ class Object
|
||||||
def to_yaml_style; end
|
def to_yaml_style; end
|
||||||
def to_yaml_properties; instance_variables.sort; end
|
def to_yaml_properties; instance_variables.sort; end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
out.map( taguri, to_yaml_style ) do |map|
|
out.map( taguri, to_yaml_style ) do |map|
|
||||||
to_yaml_properties.each do |m|
|
to_yaml_properties.each do |m|
|
||||||
map.add( m[1..-1], instance_variable_get( m ) )
|
map.add( m[1..-1], instance_variable_get( m ) )
|
||||||
|
@ -35,7 +35,7 @@ class Hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
out.map( taguri, to_yaml_style ) do |map|
|
out.map( taguri, to_yaml_style ) do |map|
|
||||||
each do |k, v|
|
each do |k, v|
|
||||||
map.add( k, v )
|
map.add( k, v )
|
||||||
|
@ -83,7 +83,7 @@ class Struct
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
#
|
#
|
||||||
# Basic struct is passed as a YAML map
|
# Basic struct is passed as a YAML map
|
||||||
#
|
#
|
||||||
|
@ -104,7 +104,7 @@ class Array
|
||||||
yaml_as "tag:yaml.org,2002:seq"
|
yaml_as "tag:yaml.org,2002:seq"
|
||||||
def yaml_initialize( tag, val ); concat( val.to_a ); end
|
def yaml_initialize( tag, val ); concat( val.to_a ); end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
out.seq( taguri, to_yaml_style ) do |seq|
|
out.seq( taguri, to_yaml_style ) do |seq|
|
||||||
each do |x|
|
each do |x|
|
||||||
seq.add( x )
|
seq.add( x )
|
||||||
|
@ -124,7 +124,7 @@ class Exception
|
||||||
o
|
o
|
||||||
end
|
end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
out.map( taguri, to_yaml_style ) do |map|
|
out.map( taguri, to_yaml_style ) do |map|
|
||||||
map.add( 'message', message )
|
map.add( 'message', message )
|
||||||
to_yaml_properties.each do |m|
|
to_yaml_properties.each do |m|
|
||||||
|
@ -161,7 +161,7 @@ class String
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( is_complex_yaml? ? object_id : nil, opts ) do |out|
|
YAML::quick_emit( is_complex_yaml? ? self : nil, opts ) do |out|
|
||||||
if is_binary_data?
|
if is_binary_data?
|
||||||
out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal )
|
out.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), :literal )
|
||||||
elsif to_yaml_properties.empty?
|
elsif to_yaml_properties.empty?
|
||||||
|
@ -227,7 +227,7 @@ class Range
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
# if self.begin.is_complex_yaml? or self.begin.respond_to? :to_str or
|
# if self.begin.is_complex_yaml? or self.begin.respond_to? :to_str or
|
||||||
# self.end.is_complex_yaml? or self.end.respond_to? :to_str or
|
# self.end.is_complex_yaml? or self.end.respond_to? :to_str or
|
||||||
# not to_yaml_properties.empty?
|
# not to_yaml_properties.empty?
|
||||||
|
@ -310,7 +310,7 @@ class Time
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
tz = "Z"
|
tz = "Z"
|
||||||
# from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
|
# from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
|
||||||
unless self.utc?
|
unless self.utc?
|
||||||
|
@ -347,7 +347,7 @@ end
|
||||||
class Date
|
class Date
|
||||||
yaml_as "tag:yaml.org,2002:timestamp#ymd"
|
yaml_as "tag:yaml.org,2002:timestamp#ymd"
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, :plain )
|
out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, :plain )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,7 +45,7 @@ module YAML
|
||||||
class Object
|
class Object
|
||||||
def self.tag_subclasses?; false; end
|
def self.tag_subclasses?; false; end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
out.map( "tag:ruby.yaml.org,2002:object:#{ @class }", to_yaml_style ) do |map|
|
out.map( "tag:ruby.yaml.org,2002:object:#{ @class }", to_yaml_style ) do |map|
|
||||||
@ivars.each do |k,v|
|
@ivars.each do |k,v|
|
||||||
map.add( k, v )
|
map.add( k, v )
|
||||||
|
@ -123,7 +123,7 @@ module YAML
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( self.object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
out.seq( taguri, to_yaml_style ) do |seq|
|
out.seq( taguri, to_yaml_style ) do |seq|
|
||||||
self.each do |v|
|
self.each do |v|
|
||||||
seq.add( Hash[ *v ] )
|
seq.add( Hash[ *v ] )
|
||||||
|
@ -173,7 +173,7 @@ module YAML
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
def to_yaml( opts = {} )
|
def to_yaml( opts = {} )
|
||||||
YAML::quick_emit( self.object_id, opts ) do |out|
|
YAML::quick_emit( self, opts ) do |out|
|
||||||
out.seq( taguri, to_yaml_style ) do |seq|
|
out.seq( taguri, to_yaml_style ) do |seq|
|
||||||
self.each do |v|
|
self.each do |v|
|
||||||
seq.add( Hash[ *v ] )
|
seq.add( Hash[ *v ] )
|
||||||
|
|
|
@ -1272,6 +1272,14 @@ EOY
|
||||||
assert_equal([{}], o.keys)
|
assert_equal([{}], o.keys)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#
|
||||||
|
# contributed by riley lynch [ruby-Bugs-8548]
|
||||||
|
#
|
||||||
|
def test_object_id_collision
|
||||||
|
omap = YAML::Omap.new
|
||||||
|
1000.times { |i| omap["key_#{i}"] = { "value" => i } }
|
||||||
|
raise "id collision in ordered map" if omap.to_yaml =~ /id\d+/
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if $0 == __FILE__
|
if $0 == __FILE__
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue