From 6b3bac29fe8341385ad70b8c6145e8f355369339 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Tue, 7 Aug 2012 20:50:05 +0000 Subject: 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 --- ext/psych/emitter.c | 29 +++++++++++++++++++++++++---- ext/psych/extconf.rb | 2 +- ext/psych/lib/psych.rb | 2 +- ext/psych/lib/psych/handler.rb | 15 +++++++++++++++ ext/psych/lib/psych/json/yaml_events.rb | 4 ++-- ext/psych/lib/psych/visitors/emitter.rb | 15 +++++++++++---- ext/psych/lib/psych/visitors/to_ruby.rb | 11 +++++++++-- ext/psych/lib/psych/visitors/yaml_tree.rb | 4 +++- ext/psych/parser.c | 4 ++-- 9 files changed, 69 insertions(+), 17 deletions(-) (limited to 'ext') diff --git a/ext/psych/emitter.c b/ext/psych/emitter.c index 15fdcfe75b..f0d032649c 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 11f44ac104..ccc8c9c304 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 1895be6193..19d8b2bc12 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 a2aa6bb178..d3b99636c4 100644 --- a/ext/psych/lib/psych/handler.rb +++ b/ext/psych/lib/psych/handler.rb @@ -10,6 +10,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 01d4660f85..d054d9b458 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 30db17612d..c886e5092e 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 2e082f90b4..088301ac14 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 80af0466eb..948a976dd1 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 6f4c4569e9..0908a1b49f 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) { -- cgit v1.2.3