diff --git a/ChangeLog b/ChangeLog index f7ba56a..65a20f6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,43 @@ +Tue Oct 4 06:43:47 2011 Aaron Patterson + + * ext/psych/lib/psych.rb: update psych version. + * ext/psych/psych.gemspec: generate new gemspec for new version. + +Tue Oct 4 06:29:55 2011 Aaron Patterson + + * ext/psych/lib/psych.rb: calling `yaml` rather than `to_yaml`. + * ext/psych/lib/psych/nodes/node.rb: Rename `to_yaml` to just `yaml` + in order to avoid YAML::ENGINE switching from replacing this method. + * test/psych/helper.rb: fix tests for method name change. + * test/psych/test_document.rb: ditto + * test/psych/visitors/test_emitter.rb: ditto + +Tue Oct 4 06:20:19 2011 Aaron Patterson + + * ext/psych/lib/psych/scalar_scanner.rb: Match values against the + floating point spec defined in YAML to avoid erronious parses. + * test/psych/test_numeric.rb: corresponding test. + +Tue Oct 4 05:59:24 2011 Aaron Patterson + + * ext/psych/lib/psych/visitors/to_ruby.rb: ToRuby visitor can be + constructed with a ScalarScanner. + * ext/psych/lib/psych/visitors/yaml_tree.rb: ScalarScanner can be + passed to the YAMLTree visitor. + +Tue Oct 4 05:47:23 2011 Aaron Patterson + + * ext/psych/lib/psych/visitors/to_ruby.rb: Define Regexp::NOENCODING + for 1.9.2 backwards compatibility. + * ext/psych/lib/psych/visitors/yaml_tree.rb: Fix Date string + generation for 1.9.2 backwards compatibility. + +Fri Sep 2 04:05:25 2011 Aaron Patterson + + * ext/psych/lib/psych/visitors/yaml_tree.rb: emit strings tagged as + ascii-8bit as binary in YAML. + * test/psych/test_string.rb: corresponding test. + Wed Nov 30 18:22:10 2011 Nobuyoshi Nakada * complex.c (nucomp_rationalize): fix function. [ruby-core:40667] diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb index 9507bbb..f9052f9 100644 --- a/ext/psych/lib/psych.rb +++ b/ext/psych/lib/psych.rb @@ -90,7 +90,7 @@ require 'psych/json' module Psych # The version is Psych you're using - VERSION = '1.2.1' + VERSION = '1.2.2' # The version of libyaml Psych is using LIBYAML_VERSION = Psych.libyaml_version.join '.' @@ -187,7 +187,7 @@ module Psych visitor = Psych::Visitors::YAMLTree.new options visitor << o - visitor.tree.to_yaml io, options + visitor.tree.yaml io, options end ### @@ -201,7 +201,7 @@ module Psych objects.each do |o| visitor << o end - visitor.tree.to_yaml + visitor.tree.yaml end ### @@ -209,7 +209,7 @@ module Psych def self.to_json o visitor = Psych::Visitors::JSONTree.new visitor << o - visitor.tree.to_yaml + visitor.tree.yaml end ### diff --git a/ext/psych/lib/psych/nodes/node.rb b/ext/psych/lib/psych/nodes/node.rb index 2a5573a..0cefe44 100644 --- a/ext/psych/lib/psych/nodes/node.rb +++ b/ext/psych/lib/psych/nodes/node.rb @@ -40,13 +40,14 @@ module Psych # Convert this node to YAML. # # See also Psych::Visitors::Emitter - def to_yaml io = nil, options = {} + def yaml io = nil, options = {} real_io = io || StringIO.new(''.encode('utf-8')) Visitors::Emitter.new(real_io, options).accept self return real_io.string unless io io end + alias :to_yaml :yaml end end end diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb index 29d66ee..3e8acbb 100644 --- a/ext/psych/lib/psych/scalar_scanner.rb +++ b/ext/psych/lib/psych/scalar_scanner.rb @@ -7,6 +7,12 @@ module Psych # Taken from http://yaml.org/type/timestamp.html TIME = /^\d{4}-\d{1,2}-\d{1,2}([Tt]|\s+)\d{1,2}:\d\d:\d\d(\.\d*)?(\s*Z|[-+]\d{1,2}(:\d\d)?)?/ + # Taken from http://yaml.org/type/float.html + FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9.]*([eE][-+][0-9]+)?(?# base 10) + |[-+]?[0-9][0-9_,]*(:[0-5]?[0-9])+\.[0-9_]*(?# base 60) + |[-+]?\.(inf|Inf|INF)(?# infinity) + |\.(nan|NaN|NAN)(?# not a number))$/x + # Create a new scanner def initialize @string_cache = {} @@ -67,9 +73,16 @@ module Psych i += (n.to_f * 60 ** (e - 2).abs) end i - else - return Integer(string.gsub(/[,_]/, '')) rescue ArgumentError + when FLOAT return Float(string.gsub(/[,_]/, '')) rescue ArgumentError + + @string_cache[string] = true + string + else + if string.count('.') < 2 + return Integer(string.gsub(/[,_]/, '')) rescue ArgumentError + end + @string_cache[string] = true string end diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb index b8eb698..ca046c5 100644 --- a/ext/psych/lib/psych/visitors/to_ruby.rb +++ b/ext/psych/lib/psych/visitors/to_ruby.rb @@ -1,14 +1,18 @@ require 'psych/scalar_scanner' +unless defined?(Regexp::NOENCODING) + Regexp::NOENCODING = 32 +end + module Psych module Visitors ### # This class walks a YAML AST, converting each node to ruby class ToRuby < Psych::Visitors::Visitor - def initialize - super + def initialize ss = ScalarScanner.new + super() @st = {} - @ss = ScalarScanner.new + @ss = ss @domain_types = Psych.domain_types end diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index eef6125..5a09285 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -12,13 +12,13 @@ module Psych alias :finished? :finished alias :started? :started - def initialize options = {}, emitter = Psych::TreeBuilder.new + def initialize options = {}, emitter = TreeBuilder.new, ss = ScalarScanner.new super() @started = false @finished = false @emitter = emitter @st = {} - @ss = ScalarScanner.new + @ss = ss @options = options @dispatch_cache = Hash.new do |h,klass| @@ -214,12 +214,19 @@ module Psych end end + def binary? string + string.encoding == Encoding::ASCII_8BIT || + string.index("\x00") || + string.count("\x00-\x7F", "^ -~\t\r\n").fdiv(string.length) > 0.3 + end + private :binary? + def visit_String o plain = false quote = false style = Nodes::Scalar::ANY - if o.index("\x00") || o.count("\x00-\x7F", "^ -~\t\r\n").fdiv(o.length) > 0.3 + if binary?(o) str = [o].pack('m').chomp tag = '!binary' # FIXME: change to below when syck is removed #tag = 'tag:yaml.org,2002:binary' @@ -304,11 +311,27 @@ module Psych end private - def format_time time - if time.utc? - time.strftime("%Y-%m-%d %H:%M:%S.%9N Z") - else - time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z") + # '%:z' was no defined until 1.9.3 + if RUBY_VERSION < '1.9.3' + def format_time time + formatted = time.strftime("%Y-%m-%d %H:%M:%S.%9N") + + if time.utc? + formatted += " Z" + else + zone = time.strftime('%z') + formatted += " #{zone[0,3]}:#{zone[3,5]}" + end + + formatted + end + else + def format_time time + if time.utc? + time.strftime("%Y-%m-%d %H:%M:%S.%9N Z") + else + time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z") + end end end diff --git a/ext/psych/psych.gemspec b/ext/psych/psych.gemspec new file mode 100644 index 0000000..35e122a --- /dev/null +++ b/ext/psych/psych.gemspec @@ -0,0 +1,22 @@ +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = "psych" + s.version = "1.2.2" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Aaron Patterson"] + s.date = "2011-10-03" + s.description = "Psych is a YAML parser and emitter. Psych leverages libyaml[http://libyaml.org]\nfor its YAML parsing and emitting capabilities. In addition to wrapping\nlibyaml, Psych also knows how to serialize and de-serialize most Ruby objects\nto and from the YAML format." + s.email = ["aaron@tenderlovemaking.com"] + s.extensions = ["ext/psych/extconf.rb"] + s.files = [".autotest", ".travis.yml", "CHANGELOG.rdoc", "Gemfile", "Manifest.txt", "README.rdoc", "Rakefile", "ext/psych/emitter.c", "ext/psych/emitter.h", "ext/psych/extconf.rb", "ext/psych/parser.c", "ext/psych/parser.h", "ext/psych/psych.c", "ext/psych/psych.h", "ext/psych/to_ruby.c", "ext/psych/to_ruby.h", "ext/psych/yaml_tree.c", "ext/psych/yaml_tree.h", "lib/psych.rb", "lib/psych/coder.rb", "lib/psych/core_ext.rb", "lib/psych/deprecated.rb", "lib/psych/handler.rb", "lib/psych/json.rb", "lib/psych/json/ruby_events.rb", "lib/psych/json/stream.rb", "lib/psych/json/tree_builder.rb", "lib/psych/json/yaml_events.rb", "lib/psych/nodes.rb", "lib/psych/nodes/alias.rb", "lib/psych/nodes/document.rb", "lib/psych/nodes/mapping.rb", "lib/psych/nodes/node.rb", "lib/psych/nodes/scalar.rb", "lib/psych/nodes/sequence.rb", "lib/psych/nodes/stream.rb", "lib/psych/omap.rb", "lib/psych/parser.rb", "lib/psych/scalar_scanner.rb", "lib/psych/set.rb", "lib/psych/stream.rb", "lib/psych/streaming.rb", "lib/psych/tree_builder.rb", "lib/psych/visitors.rb", "lib/psych/visitors/depth_first.rb", "lib/psych/visitors/emitter.rb", "lib/psych/visitors/json_tree.rb", "lib/psych/visitors/to_ruby.rb", "lib/psych/visitors/visitor.rb", "lib/psych/visitors/yaml_tree.rb", "test/psych/helper.rb", "test/psych/json/test_stream.rb", "test/psych/nodes/test_enumerable.rb", "test/psych/test_alias_and_anchor.rb", "test/psych/test_array.rb", "test/psych/test_boolean.rb", "test/psych/test_class.rb", "test/psych/test_coder.rb", "test/psych/test_date_time.rb", "test/psych/test_deprecated.rb", "test/psych/test_document.rb", "test/psych/test_emitter.rb", "test/psych/test_encoding.rb", "test/psych/test_engine_manager.rb", "test/psych/test_exception.rb", "test/psych/test_hash.rb", "test/psych/test_json_tree.rb", "test/psych/test_merge_keys.rb", "test/psych/test_nil.rb", "test/psych/test_null.rb", "test/psych/test_numeric.rb", "test/psych/test_object.rb", "test/psych/test_omap.rb", "test/psych/test_parser.rb", "test/psych/test_psych.rb", "test/psych/test_scalar.rb", "test/psych/test_scalar_scanner.rb", "test/psych/test_serialize_subclasses.rb", "test/psych/test_set.rb", "test/psych/test_stream.rb", "test/psych/test_string.rb", "test/psych/test_struct.rb", "test/psych/test_symbol.rb", "test/psych/test_tainted.rb", "test/psych/test_to_yaml_properties.rb", "test/psych/test_tree_builder.rb", "test/psych/test_yaml.rb", "test/psych/test_yamldbm.rb", "test/psych/test_yamlstore.rb", "test/psych/visitors/test_depth_first.rb", "test/psych/visitors/test_emitter.rb", "test/psych/visitors/test_to_ruby.rb", "test/psych/visitors/test_yaml_tree.rb", ".gemtest"] + s.homepage = "http://github.com/tenderlove/psych" + s.rdoc_options = ["--main", "README.rdoc"] + s.require_paths = ["lib"] + s.required_ruby_version = Gem::Requirement.new(">= 1.9.2") + s.rubyforge_project = "psych" + s.rubygems_version = "1.8.10" + s.summary = "Psych is a YAML parser and emitter" + s.test_files = ["test/psych/json/test_stream.rb", "test/psych/nodes/test_enumerable.rb", "test/psych/test_alias_and_anchor.rb", "test/psych/test_array.rb", "test/psych/test_boolean.rb", "test/psych/test_class.rb", "test/psych/test_coder.rb", "test/psych/test_date_time.rb", "test/psych/test_deprecated.rb", "test/psych/test_document.rb", "test/psych/test_emitter.rb", "test/psych/test_encoding.rb", "test/psych/test_engine_manager.rb", "test/psych/test_exception.rb", "test/psych/test_hash.rb", "test/psych/test_json_tree.rb", "test/psych/test_merge_keys.rb", "test/psych/test_nil.rb", "test/psych/test_null.rb", "test/psych/test_numeric.rb", "test/psych/test_object.rb", "test/psych/test_omap.rb", "test/psych/test_parser.rb", "test/psych/test_psych.rb", "test/psych/test_scalar.rb", "test/psych/test_scalar_scanner.rb", "test/psych/test_serialize_subclasses.rb", "test/psych/test_set.rb", "test/psych/test_stream.rb", "test/psych/test_string.rb", "test/psych/test_struct.rb", "test/psych/test_symbol.rb", "test/psych/test_tainted.rb", "test/psych/test_to_yaml_properties.rb", "test/psych/test_tree_builder.rb", "test/psych/test_yaml.rb", "test/psych/test_yamldbm.rb", "test/psych/test_yamlstore.rb", "test/psych/visitors/test_depth_first.rb", "test/psych/visitors/test_emitter.rb", "test/psych/visitors/test_to_ruby.rb", "test/psych/visitors/test_yaml_tree.rb"] +end diff --git a/test/psych/helper.rb b/test/psych/helper.rb index 61049d6..8108e99 100644 --- a/test/psych/helper.rb +++ b/test/psych/helper.rb @@ -32,7 +32,7 @@ module Psych def assert_cycle( obj ) v = Visitors::YAMLTree.new v << obj - assert_equal(obj, Psych.load(v.tree.to_yaml)) + assert_equal(obj, Psych.load(v.tree.yaml)) assert_equal( obj, Psych::load(Psych.dump(obj))) assert_equal( obj, Psych::load( obj.psych_to_yaml ) ) end diff --git a/test/psych/test_document.rb b/test/psych/test_document.rb index 55add5f..05d9bbf 100644 --- a/test/psych/test_document.rb +++ b/test/psych/test_document.rb @@ -18,12 +18,12 @@ module Psych end def test_emit_tag - assert_match('%TAG ! tag:tenderlovemaking.com,2009:', @stream.to_yaml) + assert_match('%TAG ! tag:tenderlovemaking.com,2009:', @stream.yaml) end def test_emit_multitag @doc.tag_directives << ['!!', 'foo.com,2009:'] - yaml = @stream.to_yaml + yaml = @stream.yaml assert_match('%TAG ! tag:tenderlovemaking.com,2009:', yaml) assert_match('%TAG !! foo.com,2009:', yaml) end @@ -31,7 +31,7 @@ module Psych def test_emit_bad_tag assert_raises(RuntimeError) do @doc.tag_directives = [['!']] - @stream.to_yaml + @stream.yaml end end @@ -40,7 +40,7 @@ module Psych end def test_emit_version - assert_match('%YAML 1.1', @stream.to_yaml) + assert_match('%YAML 1.1', @stream.yaml) end end end diff --git a/test/psych/test_numeric.rb b/test/psych/test_numeric.rb new file mode 100644 index 0000000..9adb058 --- /dev/null +++ b/test/psych/test_numeric.rb @@ -0,0 +1,14 @@ +require 'psych/helper' + +module Psych + ### + # Test numerics from YAML spec: + # http://yaml.org/type/float.html + # http://yaml.org/type/int.html + class TestNumeric < TestCase + def test_non_float_with_0 + str = Psych.load('--- 090') + assert_equal '090', str + end + end +end diff --git a/test/psych/test_string.rb b/test/psych/test_string.rb index 96d77e0..51f1280 100644 --- a/test/psych/test_string.rb +++ b/test/psych/test_string.rb @@ -2,6 +2,14 @@ require 'psych/helper' module Psych class TestString < TestCase + def test_tagged_binary_should_be_dumped_as_binary + string = "hello world!" + string.force_encoding 'ascii-8bit' + yml = Psych.dump string + assert_match(/binary/, yml) + assert_equal string, Psych.load(yml) + end + def test_binary_string_null string = "\x00" yml = Psych.dump string diff --git a/test/psych/visitors/test_emitter.rb b/test/psych/visitors/test_emitter.rb index de27b45..780c953 100644 --- a/test/psych/visitors/test_emitter.rb +++ b/test/psych/visitors/test_emitter.rb @@ -46,7 +46,7 @@ module Psych @visitor.accept s assert_match(/1.1/, @io.string) - assert_equal @io.string, s.to_yaml + assert_equal @io.string, s.yaml end def test_document_implicit_end @@ -61,8 +61,8 @@ module Psych @visitor.accept s assert_match(/key: value/, @io.string) - assert_equal @io.string, s.to_yaml - assert(/\.\.\./ !~ s.to_yaml) + assert_equal @io.string, s.yaml + assert(/\.\.\./ !~ s.yaml) end def test_scalar @@ -76,7 +76,7 @@ module Psych @visitor.accept s assert_match(/hello/, @io.string) - assert_equal @io.string, s.to_yaml + assert_equal @io.string, s.yaml end def test_scalar_with_tag @@ -91,7 +91,7 @@ module Psych assert_match(/str/, @io.string) assert_match(/hello/, @io.string) - assert_equal @io.string, s.to_yaml + assert_equal @io.string, s.yaml end def test_sequence @@ -107,7 +107,7 @@ module Psych @visitor.accept s assert_match(/- hello/, @io.string) - assert_equal @io.string, s.to_yaml + assert_equal @io.string, s.yaml end def test_mapping @@ -122,7 +122,7 @@ module Psych @visitor.accept s assert_match(/key: value/, @io.string) - assert_equal @io.string, s.to_yaml + assert_equal @io.string, s.yaml end def test_alias @@ -137,7 +137,7 @@ module Psych @visitor.accept s assert_match(/&A key: \*A/, @io.string) - assert_equal @io.string, s.to_yaml + assert_equal @io.string, s.yaml end end end