diff --git a/ChangeLog b/ChangeLog index 6c9da63..16c0a0f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,68 @@ +Tue Jul 31 10:36:12 2012 Aaron Patterson + + * ext/psych/lib/psych.rb: updated to released version. + + * ext/psych/psych.gemspec: ditto + +Thu Jul 19 09:33:46 2012 Aaron Patterson + + * 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. + +Tue Jul 17 03:56:34 2012 Aaron Patterson + + * 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 + +Sat Jun 16 01:27:14 2012 Aaron Patterson + + * ext/psych/lib/psych.rb: bumping psych to 1.3.3 + * ext/psych/psych.gemspec: ditto + +Fri May 18 15:53:05 2012 KOSAKI Motohiro + + * ext/psych/extconf.rb: Use an exception instaed of bare abort. + +Fri May 18 01:28:21 2012 Aaron Patterson + + * ext/psych/parser.c (transcode_string): fix encoding index names. + Thanks markizko for reporting. + +Wed May 16 05:11:29 2012 Aaron Patterson + + * 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 + +Wed May 16 01:31:21 2012 Aaron Patterson + + * 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. + +Wed May 16 01:15:45 2012 Aaron Patterson + + * ext/psych/lib/psych/visitors/yaml_tree.rb: keep a reference to + custom coders so that GC does not impact dumped yaml reference ids. + +Mon Apr 30 04:43:53 2012 Aaron Patterson + + * ext/psych/lib/psych/json/yaml_events.rb: implicit styles should not + be changeable for JSON events. + Sun Jul 29 04:32:31 2012 Nobuyoshi Nakada * configure.in (ruby_pc): make configurable. [Bug #6051] diff --git a/ext/psych/emitter.c b/ext/psych/emitter.c index 15fdcfe..f0d0326 100644 --- a/ext/psych/emitter.c +++ b/ext/psych/emitter.c @@ -2,6 +2,9 @@ VALUE cPsychEmitter; static ID id_write; +static ID id_line_width; +static ID id_indentation; +static ID id_canonical; static void emit(yaml_emitter_t * emitter, yaml_event_t * event) { @@ -39,15 +42,30 @@ static VALUE allocate(VALUE klass) return Data_Wrap_Struct(klass, 0, dealloc, emitter); } -/* call-seq: Psych::Emitter.new(io) +/* call-seq: Psych::Emitter.new(io, options = Psych::Emitter::OPTIONS) * * Create a new Psych::Emitter that writes to +io+. */ -static VALUE initialize(VALUE self, VALUE io) +static VALUE initialize(int argc, VALUE *argv, VALUE self) { yaml_emitter_t * emitter; + VALUE io, options; + VALUE line_width; + VALUE indent; + VALUE canonical; + Data_Get_Struct(self, yaml_emitter_t, emitter); + if (rb_scan_args(argc, argv, "11", &io, &options) == 2) { + line_width = rb_funcall(options, id_line_width, 0); + indent = rb_funcall(options, id_indentation, 0); + canonical = rb_funcall(options, id_canonical, 0); + + yaml_emitter_set_width(emitter, NUM2INT(line_width)); + yaml_emitter_set_indent(emitter, NUM2INT(indent)); + yaml_emitter_set_canonical(emitter, Qtrue == canonical ? 1 : 0); + } + yaml_emitter_set_output(emitter, writer, (void *)io); return self; @@ -494,7 +512,7 @@ void Init_psych_emitter() rb_define_alloc_func(cPsychEmitter, allocate); - rb_define_method(cPsychEmitter, "initialize", initialize, 1); + rb_define_method(cPsychEmitter, "initialize", initialize, -1); rb_define_method(cPsychEmitter, "start_stream", start_stream, 1); rb_define_method(cPsychEmitter, "end_stream", end_stream, 0); rb_define_method(cPsychEmitter, "start_document", start_document, 3); @@ -512,6 +530,9 @@ void Init_psych_emitter() rb_define_method(cPsychEmitter, "line_width", line_width, 0); rb_define_method(cPsychEmitter, "line_width=", set_line_width, 1); - id_write = rb_intern("write"); + id_write = rb_intern("write"); + id_line_width = rb_intern("line_width"); + id_indentation = rb_intern("indentation"); + id_canonical = rb_intern("canonical"); } /* vim: set noet sws=4 sw=4: */ diff --git a/ext/psych/extconf.rb b/ext/psych/extconf.rb index 11f44ac..ccc8c9c 100644 --- a/ext/psych/extconf.rb +++ b/ext/psych/extconf.rb @@ -5,7 +5,7 @@ require 'mkmf' dir_config 'libyaml' def asplode missing - abort "#{missing} is missing. Please install libyaml." + raise "#{missing} is missing. Please install libyaml." end asplode('yaml.h') unless find_header 'yaml.h' diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb index 1895be6..19d8b2b 100644 --- a/ext/psych/lib/psych.rb +++ b/ext/psych/lib/psych.rb @@ -93,7 +93,7 @@ require 'psych/handlers/document_stream' module Psych # The version is Psych you're using - VERSION = '1.3.2' + VERSION = '1.3.4' # The version of libyaml Psych is using LIBYAML_VERSION = Psych.libyaml_version.join '.' diff --git a/ext/psych/lib/psych/handler.rb b/ext/psych/lib/psych/handler.rb index a2aa6bb..d3b9963 100644 --- a/ext/psych/lib/psych/handler.rb +++ b/ext/psych/lib/psych/handler.rb @@ -11,6 +11,21 @@ module Psych # See Psych::Parser for more details class Handler ### + # Configuration options for dumping YAML. + class DumperOptions + attr_accessor :line_width, :indentation, :canonical + + def initialize + @line_width = 0 + @indentation = 2 + @canonical = false + end + end + + # Default dumping options + OPTIONS = DumperOptions.new + + ### # Called with +encoding+ when the YAML stream starts. This method is # called once per stream. A stream may contain multiple documents. # diff --git a/ext/psych/lib/psych/json/yaml_events.rb b/ext/psych/lib/psych/json/yaml_events.rb index 01d4660..d054d9b 100644 --- a/ext/psych/lib/psych/json/yaml_events.rb +++ b/ext/psych/lib/psych/json/yaml_events.rb @@ -10,11 +10,11 @@ module Psych end def start_mapping anchor, tag, implicit, style - super(anchor, nil, implicit, Nodes::Mapping::FLOW) + super(anchor, nil, true, Nodes::Mapping::FLOW) end def start_sequence anchor, tag, implicit, style - super(anchor, nil, implicit, Nodes::Sequence::FLOW) + super(anchor, nil, true, Nodes::Sequence::FLOW) end def scalar value, anchor, tag, plain, quoted, style diff --git a/ext/psych/lib/psych/visitors/emitter.rb b/ext/psych/lib/psych/visitors/emitter.rb index 30db176..c886e50 100644 --- a/ext/psych/lib/psych/visitors/emitter.rb +++ b/ext/psych/lib/psych/visitors/emitter.rb @@ -2,10 +2,17 @@ module Psych module Visitors class Emitter < Psych::Visitors::Visitor def initialize io, options = {} - @handler = Psych::Emitter.new io - @handler.indentation = options[:indentation] if options[:indentation] - @handler.canonical = options[:canonical] if options[:canonical] - @handler.line_width = options[:line_width] if options[:line_width] + opts = [:indentation, :canonical, :line_width].find_all { |opt| + options.key?(opt) + } + + if opts.empty? + @handler = Psych::Emitter.new io + else + du = Handler::DumperOptions.new + opts.each { |option| du.send :"#{option}=", options[option] } + @handler = Psych::Emitter.new io, du + end end def visit_Psych_Nodes_Stream o diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb index 2e082f9..088301a 100644 --- a/ext/psych/lib/psych/visitors/to_ruby.rb +++ b/ext/psych/lib/psych/visitors/to_ruby.rb @@ -147,8 +147,8 @@ module Psych string = members.delete 'str' if klass - string = klass.allocate - string.replace string + string = klass.allocate.replace string + register(o, string) end init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o) @@ -222,6 +222,13 @@ module Psych when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/ revive_hash resolve_class($1).new, o + when '!omap', 'tag:yaml.org,2002:omap' + map = register(o, Psych::Omap.new) + o.children.each_slice(2) do |l,r| + map[accept(l)] = accept r + end + map + else revive_hash({}, o) end diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index 80af046..948a976 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -20,6 +20,7 @@ module Psych @st = {} @ss = ss @options = options + @coders = [] @dispatch_cache = Hash.new do |h,klass| method = "visit_#{(klass.name || '').split('::').join('_')}" @@ -253,7 +254,7 @@ module Psych maptag = '!ruby/string' maptag << ":#{o.class}" unless o.class == ::String - @emitter.start_mapping nil, maptag, false, Nodes::Mapping::BLOCK + register o, @emitter.start_mapping(nil, maptag, false, Nodes::Mapping::BLOCK) @emitter.scalar 'str', nil, nil, true, false, Nodes::Scalar::ANY @emitter.scalar str, nil, tag, plain, quote, style @@ -406,6 +407,7 @@ module Psych end def dump_coder o + @coders << o tag = Psych.dump_tags[o.class] unless tag klass = o.class == Object ? nil : o.class.name diff --git a/ext/psych/parser.c b/ext/psych/parser.c index 6f4c456..0908a1b 100644 --- a/ext/psych/parser.c +++ b/ext/psych/parser.c @@ -79,8 +79,8 @@ static VALUE make_exception(yaml_parser_t * parser, VALUE path) static VALUE transcode_string(VALUE src, int * parser_encoding) { int utf8 = rb_utf8_encindex(); - int utf16le = rb_enc_find_index("UTF16_LE"); - int utf16be = rb_enc_find_index("UTF16_BE"); + int utf16le = rb_enc_find_index("UTF-16LE"); + int utf16be = rb_enc_find_index("UTF-16BE"); int source_encoding = rb_enc_get_index(src); if (source_encoding == utf8) { diff --git a/test/psych/test_alias_and_anchor.rb b/test/psych/test_alias_and_anchor.rb index 48771d6..aa4773b 100644 --- a/test/psych/test_alias_and_anchor.rb +++ b/test/psych/test_alias_and_anchor.rb @@ -1,5 +1,13 @@ require 'psych/helper' +class ObjectWithInstanceVariables + attr_accessor :var1, :var2 +end + +class SubStringWithInstanceVariables < String + attr_accessor :var1 +end + module Psych class TestAliasAndAnchor < TestCase def test_mri_compatibility @@ -14,6 +22,40 @@ EOYAML result.each {|el| assert_same(result[0], el) } end + def test_mri_compatibility_object_with_ivars + yaml = < 'b' }, 'foo'] end + def test_another_subclass_with_attributes + y = Y.new.tap {|y| y.val = 1} + y << "foo" << "bar" + y = Psych.load Psych.dump y + + assert_equal %w{foo bar}, y + assert_equal Y, y.class + assert_equal 1, y.val + end + def test_subclass yaml = Psych.dump X.new assert_match X.name, yaml diff --git a/test/psych/test_omap.rb b/test/psych/test_omap.rb index 53f55f5..34df724 100644 --- a/test/psych/test_omap.rb +++ b/test/psych/test_omap.rb @@ -2,6 +2,13 @@ require 'psych/helper' module Psych class TestOmap < TestCase + def test_parse_as_map + o = Psych.load "--- !!omap\na: 1\nb: 2" + assert_kind_of Psych::Omap, o + assert_equal 1, o['a'] + assert_equal 2, o['b'] + end + def test_self_referential map = Psych::Omap.new map['foo'] = 'bar' diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index c7d5c60..77aefc6 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -9,6 +9,13 @@ module Psych attr_accessor :val end + def test_another_subclass_with_attributes + y = Psych.load Psych.dump Y.new("foo").tap {|y| y.val = 1} + assert_equal "foo", y + assert_equal Y, y.class + assert_equal 1, y.val + end + def test_backwards_with_syck x = Psych.load "--- !str:#{X.name} foo\n\n" assert_equal X, x.class