Project

General

Profile

Backport #5537 ยป psych122.patch

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

View differences:

ChangeLog
1
Tue Oct  4 06:43:47 2011  Aaron Patterson <aaron@tenderlovemaking.com>
2

  
3
	* ext/psych/lib/psych.rb: update psych version.
4
	* ext/psych/psych.gemspec: generate new gemspec for new version.
5

  
6
Tue Oct  4 06:29:55 2011  Aaron Patterson <aaron@tenderlovemaking.com>
7

  
8
	* ext/psych/lib/psych.rb: calling `yaml` rather than `to_yaml`.
9
	* ext/psych/lib/psych/nodes/node.rb: Rename `to_yaml` to just `yaml`
10
	  in order to avoid YAML::ENGINE switching from replacing this method.
11
	* test/psych/helper.rb: fix tests for method name change.
12
	* test/psych/test_document.rb: ditto
13
	* test/psych/visitors/test_emitter.rb: ditto
14

  
15
Tue Oct  4 06:20:19 2011  Aaron Patterson <aaron@tenderlovemaking.com>
16

  
17
	* ext/psych/lib/psych/scalar_scanner.rb: Match values against the
18
	  floating point spec defined in YAML to avoid erronious parses.
19
	* test/psych/test_numeric.rb: corresponding test.
20

  
21
Tue Oct  4 05:59:24 2011  Aaron Patterson <aaron@tenderlovemaking.com>
22

  
23
	* ext/psych/lib/psych/visitors/to_ruby.rb: ToRuby visitor can be
24
	  constructed with a ScalarScanner.
25
	* ext/psych/lib/psych/visitors/yaml_tree.rb: ScalarScanner can be
26
	  passed to the YAMLTree visitor.
27

  
28
Tue Oct  4 05:47:23 2011  Aaron Patterson <aaron@tenderlovemaking.com>
29

  
30
	* ext/psych/lib/psych/visitors/to_ruby.rb: Define Regexp::NOENCODING
31
	  for 1.9.2 backwards compatibility.
32
	* ext/psych/lib/psych/visitors/yaml_tree.rb: Fix Date string
33
	  generation for 1.9.2 backwards compatibility.
34

  
35
Fri Sep  2 04:05:25 2011  Aaron Patterson <aaron@tenderlovemaking.com>
36

  
37
	* ext/psych/lib/psych/visitors/yaml_tree.rb: emit strings tagged as
38
	  ascii-8bit as binary in YAML.
39
	* test/psych/test_string.rb: corresponding test.
40

  
1 41
Wed Nov 30 18:22:10 2011  Nobuyoshi Nakada  <nobu@ruby-lang.org>
2 42

  
3 43
	* complex.c (nucomp_rationalize): fix function.  [ruby-core:40667]
ext/psych/lib/psych.rb
90 90

  
91 91
module Psych
92 92
  # The version is Psych you're using
93
  VERSION         = '1.2.1'
93
  VERSION         = '1.2.2'
94 94

  
95 95
  # The version of libyaml Psych is using
96 96
  LIBYAML_VERSION = Psych.libyaml_version.join '.'
......
187 187

  
188 188
    visitor = Psych::Visitors::YAMLTree.new options
189 189
    visitor << o
190
    visitor.tree.to_yaml io, options
190
    visitor.tree.yaml io, options
191 191
  end
192 192

  
193 193
  ###
......
201 201
    objects.each do |o|
202 202
      visitor << o
203 203
    end
204
    visitor.tree.to_yaml
204
    visitor.tree.yaml
205 205
  end
206 206

  
207 207
  ###
......
209 209
  def self.to_json o
210 210
    visitor = Psych::Visitors::JSONTree.new
211 211
    visitor << o
212
    visitor.tree.to_yaml
212
    visitor.tree.yaml
213 213
  end
214 214

  
215 215
  ###
ext/psych/lib/psych/nodes/node.rb
40 40
      # Convert this node to YAML.
41 41
      #
42 42
      # See also Psych::Visitors::Emitter
43
      def to_yaml io = nil, options = {}
43
      def yaml io = nil, options = {}
44 44
        real_io = io || StringIO.new(''.encode('utf-8'))
45 45

  
46 46
        Visitors::Emitter.new(real_io, options).accept self
47 47
        return real_io.string unless io
48 48
        io
49 49
      end
50
      alias :to_yaml :yaml
50 51
    end
51 52
  end
52 53
end
ext/psych/lib/psych/scalar_scanner.rb
7 7
    # Taken from http://yaml.org/type/timestamp.html
8 8
    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)?)?/
9 9

  
10
    # Taken from http://yaml.org/type/float.html
11
    FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9.]*([eE][-+][0-9]+)?(?# base 10)
12
              |[-+]?[0-9][0-9_,]*(:[0-5]?[0-9])+\.[0-9_]*(?# base 60)
13
              |[-+]?\.(inf|Inf|INF)(?# infinity)
14
              |\.(nan|NaN|NAN)(?# not a number))$/x
15

  
10 16
    # Create a new scanner
11 17
    def initialize
12 18
      @string_cache = {}
......
67 73
          i += (n.to_f * 60 ** (e - 2).abs)
68 74
        end
69 75
        i
70
      else
71
        return Integer(string.gsub(/[,_]/, '')) rescue ArgumentError
76
      when FLOAT
72 77
        return Float(string.gsub(/[,_]/, '')) rescue ArgumentError
78

  
79
        @string_cache[string] = true
80
        string
81
      else
82
        if string.count('.') < 2
83
          return Integer(string.gsub(/[,_]/, '')) rescue ArgumentError
84
        end
85

  
73 86
        @string_cache[string] = true
74 87
        string
75 88
      end
ext/psych/lib/psych/visitors/to_ruby.rb
1 1
require 'psych/scalar_scanner'
2 2

  
3
unless defined?(Regexp::NOENCODING)
4
  Regexp::NOENCODING = 32
5
end
6

  
3 7
module Psych
4 8
  module Visitors
5 9
    ###
6 10
    # This class walks a YAML AST, converting each node to ruby
7 11
    class ToRuby < Psych::Visitors::Visitor
8
      def initialize
9
        super
12
      def initialize ss = ScalarScanner.new
13
        super()
10 14
        @st = {}
11
        @ss = ScalarScanner.new
15
        @ss = ss
12 16
        @domain_types = Psych.domain_types
13 17
      end
14 18

  
ext/psych/lib/psych/visitors/yaml_tree.rb
12 12
      alias :finished? :finished
13 13
      alias :started? :started
14 14

  
15
      def initialize options = {}, emitter = Psych::TreeBuilder.new
15
      def initialize options = {}, emitter = TreeBuilder.new, ss = ScalarScanner.new
16 16
        super()
17 17
        @started  = false
18 18
        @finished = false
19 19
        @emitter  = emitter
20 20
        @st       = {}
21
        @ss       = ScalarScanner.new
21
        @ss       = ss
22 22
        @options  = options
23 23

  
24 24
        @dispatch_cache = Hash.new do |h,klass|
......
214 214
        end
215 215
      end
216 216

  
217
      def binary? string
218
        string.encoding == Encoding::ASCII_8BIT ||
219
          string.index("\x00") ||
220
          string.count("\x00-\x7F", "^ -~\t\r\n").fdiv(string.length) > 0.3
221
      end
222
      private :binary?
223

  
217 224
      def visit_String o
218 225
        plain = false
219 226
        quote = false
220 227
        style = Nodes::Scalar::ANY
221 228

  
222
        if o.index("\x00") || o.count("\x00-\x7F", "^ -~\t\r\n").fdiv(o.length) > 0.3
229
        if binary?(o)
223 230
          str   = [o].pack('m').chomp
224 231
          tag   = '!binary' # FIXME: change to below when syck is removed
225 232
          #tag   = 'tag:yaml.org,2002:binary'
......
304 311
      end
305 312

  
306 313
      private
307
      def format_time time
308
        if time.utc?
309
          time.strftime("%Y-%m-%d %H:%M:%S.%9N Z")
310
        else
311
          time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")
314
      # '%:z' was no defined until 1.9.3
315
      if RUBY_VERSION < '1.9.3'
316
        def format_time time
317
          formatted = time.strftime("%Y-%m-%d %H:%M:%S.%9N")
318

  
319
          if time.utc?
320
            formatted += " Z"
321
          else
322
            zone = time.strftime('%z')
323
            formatted += " #{zone[0,3]}:#{zone[3,5]}"
324
          end
325

  
326
          formatted
327
        end
328
      else
329
        def format_time time
330
          if time.utc?
331
            time.strftime("%Y-%m-%d %H:%M:%S.%9N Z")
332
          else
333
            time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")
334
          end
312 335
        end
313 336
      end
314 337

  
ext/psych/psych.gemspec
1
# -*- encoding: utf-8 -*-
2

  
3
Gem::Specification.new do |s|
4
  s.name = "psych"
5
  s.version = "1.2.2"
6

  
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
  s.authors = ["Aaron Patterson"]
9
  s.date = "2011-10-03"
10
  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."
11
  s.email = ["aaron@tenderlovemaking.com"]
12
  s.extensions = ["ext/psych/extconf.rb"]
13
  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"]
14
  s.homepage = "http://github.com/tenderlove/psych"
15
  s.rdoc_options = ["--main", "README.rdoc"]
16
  s.require_paths = ["lib"]
17
  s.required_ruby_version = Gem::Requirement.new(">= 1.9.2")
18
  s.rubyforge_project = "psych"
19
  s.rubygems_version = "1.8.10"
20
  s.summary = "Psych is a YAML parser and emitter"
21
  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"]
22
end
test/psych/helper.rb
32 32
    def assert_cycle( obj )
33 33
      v = Visitors::YAMLTree.new
34 34
      v << obj
35
      assert_equal(obj, Psych.load(v.tree.to_yaml))
35
      assert_equal(obj, Psych.load(v.tree.yaml))
36 36
      assert_equal( obj, Psych::load(Psych.dump(obj)))
37 37
      assert_equal( obj, Psych::load( obj.psych_to_yaml ) )
38 38
    end
test/psych/test_document.rb
18 18
    end
19 19

  
20 20
    def test_emit_tag
21
      assert_match('%TAG ! tag:tenderlovemaking.com,2009:', @stream.to_yaml)
21
      assert_match('%TAG ! tag:tenderlovemaking.com,2009:', @stream.yaml)
22 22
    end
23 23

  
24 24
    def test_emit_multitag
25 25
      @doc.tag_directives << ['!!', 'foo.com,2009:']
26
      yaml = @stream.to_yaml
26
      yaml = @stream.yaml
27 27
      assert_match('%TAG ! tag:tenderlovemaking.com,2009:', yaml)
28 28
      assert_match('%TAG !! foo.com,2009:', yaml)
29 29
    end
......
31 31
    def test_emit_bad_tag
32 32
      assert_raises(RuntimeError) do
33 33
        @doc.tag_directives = [['!']]
34
        @stream.to_yaml
34
        @stream.yaml
35 35
      end
36 36
    end
37 37

  
......
40 40
    end
41 41

  
42 42
    def test_emit_version
43
      assert_match('%YAML 1.1', @stream.to_yaml)
43
      assert_match('%YAML 1.1', @stream.yaml)
44 44
    end
45 45
  end
46 46
end
test/psych/test_numeric.rb
1
require 'psych/helper'
2

  
3
module Psych
4
  ###
5
  # Test numerics from YAML spec:
6
  # http://yaml.org/type/float.html
7
  # http://yaml.org/type/int.html
8
  class TestNumeric < TestCase
9
    def test_non_float_with_0
10
      str = Psych.load('--- 090')
11
      assert_equal '090', str
12
    end
13
  end
14
end
test/psych/test_string.rb
2 2

  
3 3
module Psych
4 4
  class TestString < TestCase
5
    def test_tagged_binary_should_be_dumped_as_binary
6
      string = "hello world!"
7
      string.force_encoding 'ascii-8bit'
8
      yml = Psych.dump string
9
      assert_match(/binary/, yml)
10
      assert_equal string, Psych.load(yml)
11
    end
12

  
5 13
    def test_binary_string_null
6 14
      string = "\x00"
7 15
      yml = Psych.dump string
test/psych/visitors/test_emitter.rb
46 46
        @visitor.accept s
47 47

  
48 48
        assert_match(/1.1/, @io.string)
49
        assert_equal @io.string, s.to_yaml
49
        assert_equal @io.string, s.yaml
50 50
      end
51 51

  
52 52
      def test_document_implicit_end
......
61 61
        @visitor.accept s
62 62

  
63 63
        assert_match(/key: value/, @io.string)
64
        assert_equal @io.string, s.to_yaml
65
        assert(/\.\.\./ !~ s.to_yaml)
64
        assert_equal @io.string, s.yaml
65
        assert(/\.\.\./ !~ s.yaml)
66 66
      end
67 67

  
68 68
      def test_scalar
......
76 76
        @visitor.accept s
77 77

  
78 78
        assert_match(/hello/, @io.string)
79
        assert_equal @io.string, s.to_yaml
79
        assert_equal @io.string, s.yaml
80 80
      end
81 81

  
82 82
      def test_scalar_with_tag
......
91 91

  
92 92
        assert_match(/str/, @io.string)
93 93
        assert_match(/hello/, @io.string)
94
        assert_equal @io.string, s.to_yaml
94
        assert_equal @io.string, s.yaml
95 95
      end
96 96

  
97 97
      def test_sequence
......
107 107
        @visitor.accept s
108 108

  
109 109
        assert_match(/- hello/, @io.string)
110
        assert_equal @io.string, s.to_yaml
110
        assert_equal @io.string, s.yaml
111 111
      end
112 112

  
113 113
      def test_mapping
......
122 122
        @visitor.accept s
123 123

  
124 124
        assert_match(/key: value/, @io.string)
125
        assert_equal @io.string, s.to_yaml
125
        assert_equal @io.string, s.yaml
126 126
      end
127 127

  
128 128
      def test_alias
......
137 137
        @visitor.accept s
138 138

  
139 139
        assert_match(/&A key: \*A/, @io.string)
140
        assert_equal @io.string, s.to_yaml
140
        assert_equal @io.string, s.yaml
141 141
      end
142 142
    end
143 143
  end