Project

General

Profile

Backport #5537 ยป psych122.patch

tenderlovemaking (Aaron Patterson), 12/07/2011 07:26 AM

View differences:

ChangeLog
Tue Oct 4 06:43:47 2011 Aaron Patterson <aaron@tenderlovemaking.com>
* 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 <aaron@tenderlovemaking.com>
* 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 <aaron@tenderlovemaking.com>
* 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 <aaron@tenderlovemaking.com>
* 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 <aaron@tenderlovemaking.com>
* 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 <aaron@tenderlovemaking.com>
* 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 <nobu@ruby-lang.org>
* complex.c (nucomp_rationalize): fix function. [ruby-core:40667]
ext/psych/lib/psych.rb
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 '.'
......
visitor = Psych::Visitors::YAMLTree.new options
visitor << o
visitor.tree.to_yaml io, options
visitor.tree.yaml io, options
end
###
......
objects.each do |o|
visitor << o
end
visitor.tree.to_yaml
visitor.tree.yaml
end
###
......
def self.to_json o
visitor = Psych::Visitors::JSONTree.new
visitor << o
visitor.tree.to_yaml
visitor.tree.yaml
end
###
ext/psych/lib/psych/nodes/node.rb
# 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
ext/psych/lib/psych/scalar_scanner.rb
# 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 = {}
......
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
ext/psych/lib/psych/visitors/to_ruby.rb
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
ext/psych/lib/psych/visitors/yaml_tree.rb
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|
......
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'
......
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
ext/psych/psych.gemspec
# -*- 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
test/psych/helper.rb
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
test/psych/test_document.rb
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
......
def test_emit_bad_tag
assert_raises(RuntimeError) do
@doc.tag_directives = [['!']]
@stream.to_yaml
@stream.yaml
end
end
......
end
def test_emit_version
assert_match('%YAML 1.1', @stream.to_yaml)
assert_match('%YAML 1.1', @stream.yaml)
end
end
end
test/psych/test_numeric.rb
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
test/psych/test_string.rb
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
test/psych/visitors/test_emitter.rb
@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
......
@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
......
@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
......
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
......
@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
......
@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
......
@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
    (1-1/1)