Project

General

Profile

Bug #6425

Psych issue with !!omap

Added by trans (Thomas Sawyer) over 7 years ago. Updated over 7 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]
Backport:
[ruby-core:45001]

Description

=begin

Psych doesn't seem to know ordered map. It parses fine, but loose the type when round-tripped.

a = YAML.load %{
--- !!omap
a: 1
b: 2
}
=> {"a"=>1, "b"=>2}
a.class
=> Hash
puts a.to_yaml


a: 1
b: 2

=end


Files

noname (500 Bytes) noname Anonymous, 05/14/2012 08:53 AM
noname (500 Bytes) noname Anonymous, 05/16/2012 01:23 AM

Associated revisions

Revision 5341e3cc
Added by tenderlove over 7 years ago

  • ext/psych/lib/psych/visitors/to_ruby.rb: convert omap tagged maps to
    Psych::Omap objects rather than hashes. [Bug #6425]

  • test/psych/test_omap.rb: pertinent test.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35657 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 35657
Added by tenderlove over 7 years ago

  • ext/psych/lib/psych/visitors/to_ruby.rb: convert omap tagged maps to
    Psych::Omap objects rather than hashes. [Bug #6425]

  • test/psych/test_omap.rb: pertinent test.

Revision 35657
Added by tenderlovemaking (Aaron Patterson) over 7 years ago

  • ext/psych/lib/psych/visitors/to_ruby.rb: convert omap tagged maps to
    Psych::Omap objects rather than hashes. [Bug #6425]

  • test/psych/test_omap.rb: pertinent test.

Revision 35657
Added by tenderlove over 7 years ago

  • ext/psych/lib/psych/visitors/to_ruby.rb: convert omap tagged maps to
    Psych::Omap objects rather than hashes. [Bug #6425]

  • test/psych/test_omap.rb: pertinent test.

Revision 35657
Added by tenderlove over 7 years ago

  • ext/psych/lib/psych/visitors/to_ruby.rb: convert omap tagged maps to
    Psych::Omap objects rather than hashes. [Bug #6425]

  • test/psych/test_omap.rb: pertinent test.

Revision 35657
Added by tenderlove over 7 years ago

  • ext/psych/lib/psych/visitors/to_ruby.rb: convert omap tagged maps to
    Psych::Omap objects rather than hashes. [Bug #6425]

  • test/psych/test_omap.rb: pertinent test.

Revision 35657
Added by tenderlove over 7 years ago

  • ext/psych/lib/psych/visitors/to_ruby.rb: convert omap tagged maps to
    Psych::Omap objects rather than hashes. [Bug #6425]

  • test/psych/test_omap.rb: pertinent test.

Revision 6b3bac29
Added by tenderlove over 7 years ago

merge revision(s) r36583, r36458, r36414, r36103, r35690, r35681, r35658, r35657, r35655, r35492: [Backport #6815]

* ext/psych/lib/psych.rb: updated to released version.

* ext/psych/psych.gemspec: ditto

* ext/psych/emitter.c (initialize): allow a configuration object to be
  passed to the constructor so that mutation isn't required after
  instantiation.

* ext/psych/lib/psych/handler.rb: add configuration object

* ext/psych/lib/psych/visitors/emitter.rb: use configuration object if
  extra configuration is present.

* ext/psych/lib/psych/visitors/to_ruby.rb: strings with YAML anchors
  are properly referenced. Patched by Joe Rafaniello via Github:
    https://github.com/tenderlove/psych/pull/69
* ext/psych/lib/psych/visitors/yaml_tree.rb: ditto
* test/psych/test_alias_and_anchor.rb: test for change

* ext/psych/lib/psych.rb: bumping psych to 1.3.3
* ext/psych/psych.gemspec: ditto

* ext/psych/extconf.rb: Use an exception instaed of bare abort.

* ext/psych/parser.c (transcode_string): fix encoding index names.
  Thanks markizko for reporting.

* ext/psych/lib/psych/visitors/to_ruby.rb: fix a bug with string
  subclass dumping and loading.

* test/psych/test_array.rb: pertinent tests

* test/psych/test_string.rb: ditto

* ext/psych/lib/psych/visitors/to_ruby.rb: convert omap tagged maps to
  Psych::Omap objects rather than hashes. [Bug #6425]

* test/psych/test_omap.rb: pertinent test.

* ext/psych/lib/psych/visitors/yaml_tree.rb: keep a reference to
  custom coders so that GC does not impact dumped yaml reference ids.

* ext/psych/lib/psych/json/yaml_events.rb: implicit styles should not
  be changeable for JSON events.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@36654 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

History

Updated by Anonymous over 7 years ago

On Sat, May 12, 2012 at 04:55:08AM +0900, trans (Thomas Sawyer) wrote:

Issue #6425 has been reported by trans (Thomas Sawyer).


Bug #6425: Psych issue with !!omap
https://bugs.ruby-lang.org/issues/6425

Author: trans (Thomas Sawyer)
Status: Open
Priority: Normal
Assignee:
Category: lib
Target version:
ruby -v: ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-linux]

=begin

Psych doesn't seem to know ordered map. It parses fine, but loose the type when round-tripped.

a = YAML.load %{
--- !!omap
a: 1
b: 2
}
=> {"a"=>1, "b"=>2}
a.class
=> Hash
puts a.to_yaml


a: 1
b: 2

=end

Are you sure this is Psych and not Syck? Psych raises a syntax error on
your example:

 irb(main):001:0> require 'psych'
 => true
 irb(main):002:0> require 'yaml'
 => true
 irb(main):003:0> a = YAML.load %{
 irb(main):004:0"   --- !!omap
 irb(main):005:0"   a: 1
 irb(main):006:0"   b: 2
 irb(main):007:0" }
 Psych::SyntaxError: (<unknown>): mapping values are not allowed in this context at line 3 column 4
         from /Users/aaron/.local/lib/ruby/2.0.0/psych.rb:203:in `parse'
         from /Users/aaron/.local/lib/ruby/2.0.0/psych.rb:203:in `parse_stream'
         from /Users/aaron/.local/lib/ruby/2.0.0/psych.rb:151:in `parse'
         from /Users/aaron/.local/lib/ruby/2.0.0/psych.rb:127:in `load'
         from (irb):3
         from /Users/aaron/.local/bin/irb:12:in `<main>'
 irb(main):008:0>

Can you provide a runnable test case to reproduce your error?

--
Aaron Patterson
http://tenderlovemaking.com/

Updated by trans (Thomas Sawyer) over 7 years ago

=begin

Ah, the indention I added was the problem. Try:

a = YAML.load %{
--- !!omap
a: 1
b: 2
}
a.class
a.to_yaml

As for a test case. Something like?

class TestYAMLOMap < Test::Unit::TestCase
def test_omap_round_trip
a = YAML.load "--- !!omap\n"a: 1\nb: 2\n"
s = a.to_yaml
assert s.index('!!omap')
end
end

Okay, it could be better, but it's a start.
=end

Updated by trans (Thomas Sawyer) over 7 years ago

Btw, just thought of something that might be important here. Technically a YAML OMap type is written:

--- !!omap

  • a: 1
  • b: 2

The additional array sequence ensures the order regardless of the parser. However, given Ruby's ordered hashes, it makes sense that it can handle both the array (sequence) and the hash (mapping) forms.

Updated by Anonymous over 7 years ago

On Tue, May 15, 2012 at 08:45:05AM +0900, trans (Thomas Sawyer) wrote:

Issue #6425 has been updated by trans (Thomas Sawyer).

=begin

Ah, the indention I added was the problem. Try:

a = YAML.load %{
--- !!omap
a: 1
b: 2
}
a.class
a.to_yaml

Perfect. I can verify this is a bug. Thanks for reporting it!

--
Aaron Patterson
http://tenderlovemaking.com/

#5

Updated by tenderlovemaking (Aaron Patterson) over 7 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r35657.
Thomas, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • ext/psych/lib/psych/visitors/to_ruby.rb: convert omap tagged maps to
    Psych::Omap objects rather than hashes. [Bug #6425]

  • test/psych/test_omap.rb: pertinent test.

Also available in: Atom PDF