psych 3.0.3.pre4-java → 3.3.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +6 -0
  3. data/LICENSE +21 -0
  4. data/README.md +3 -6
  5. data/Rakefile +2 -16
  6. data/ext/java/{PsychEmitter.java → org/jruby/ext/psych/PsychEmitter.java} +9 -3
  7. data/ext/java/{PsychLibrary.java → org/jruby/ext/psych/PsychLibrary.java} +25 -1
  8. data/ext/java/{PsychParser.java → org/jruby/ext/psych/PsychParser.java} +0 -0
  9. data/ext/java/{PsychToRuby.java → org/jruby/ext/psych/PsychToRuby.java} +0 -0
  10. data/ext/java/{PsychYamlTree.java → org/jruby/ext/psych/PsychYamlTree.java} +0 -8
  11. data/ext/psych/depend +2 -0
  12. data/ext/psych/extconf.rb +6 -2
  13. data/ext/psych/psych.c +6 -3
  14. data/ext/psych/psych_parser.c +20 -33
  15. data/ext/psych/psych_yaml_tree.c +0 -12
  16. data/ext/psych/yaml/api.c +48 -47
  17. data/ext/psych/yaml/config.h +77 -7
  18. data/ext/psych/yaml/dumper.c +3 -3
  19. data/ext/psych/yaml/emitter.c +48 -19
  20. data/ext/psych/yaml/loader.c +210 -110
  21. data/ext/psych/yaml/parser.c +11 -6
  22. data/ext/psych/yaml/reader.c +3 -3
  23. data/ext/psych/yaml/scanner.c +52 -28
  24. data/ext/psych/yaml/yaml.h +41 -27
  25. data/ext/psych/yaml/yaml_private.h +46 -20
  26. data/lib/psych.rb +171 -77
  27. data/lib/psych/class_loader.rb +6 -4
  28. data/lib/psych/handler.rb +1 -1
  29. data/lib/psych/nodes/node.rb +2 -2
  30. data/lib/psych/scalar_scanner.rb +23 -36
  31. data/lib/psych/versions.rb +4 -3
  32. data/lib/psych/visitors/to_ruby.rb +42 -11
  33. data/lib/psych/visitors/visitor.rb +17 -3
  34. data/lib/psych/visitors/yaml_tree.rb +30 -42
  35. data/psych.gemspec +18 -14
  36. metadata +13 -58
  37. data/.travis.yml +0 -20
  38. data/CHANGELOG.rdoc +0 -576
@@ -35,9 +35,11 @@ module Psych
35
35
 
36
36
  constants.each do |const|
37
37
  konst = const_get const
38
- define_method(const.to_s.downcase) do
39
- load konst
40
- end
38
+ class_eval <<~RUBY
39
+ def #{const.to_s.downcase}
40
+ load #{konst.inspect}
41
+ end
42
+ RUBY
41
43
  end
42
44
 
43
45
  private
@@ -69,7 +71,7 @@ module Psych
69
71
  rescue
70
72
  nil
71
73
  end
72
- }.compact]
74
+ }.compact].freeze
73
75
 
74
76
  class Restricted < ClassLoader
75
77
  def initialize classes, symbols
@@ -105,7 +105,7 @@ module Psych
105
105
  # - first element
106
106
  # - *ponies
107
107
  #
108
- # &ponies is the achor, *ponies is the alias. In this case, alias is
108
+ # &ponies is the anchor, *ponies is the alias. In this case, alias is
109
109
  # called with "ponies".
110
110
  def alias anchor
111
111
  end
@@ -46,8 +46,8 @@ module Psych
46
46
  # Convert this node to Ruby.
47
47
  #
48
48
  # See also Psych::Visitors::ToRuby
49
- def to_ruby
50
- Visitors::ToRuby.create.accept(self)
49
+ def to_ruby(symbolize_names: false, freeze: false)
50
+ Visitors::ToRuby.create(symbolize_names: symbolize_names, freeze: freeze).accept(self)
51
51
  end
52
52
  alias :transform :to_ruby
53
53
 
@@ -14,16 +14,15 @@ module Psych
14
14
  |\.(nan|NaN|NAN)(?# not a number))$/x
15
15
 
16
16
  # Taken from http://yaml.org/type/int.html
17
- INTEGER = /^(?:[-+]?0b[0-1_]+ (?# base 2)
18
- |[-+]?0[0-7_]+ (?# base 8)
19
- |[-+]?(?:0|[1-9][0-9_]*) (?# base 10)
20
- |[-+]?0x[0-9a-fA-F_]+ (?# base 16))$/x
17
+ INTEGER = /^(?:[-+]?0b[0-1_,]+ (?# base 2)
18
+ |[-+]?0[0-7_,]+ (?# base 8)
19
+ |[-+]?(?:0|[1-9][0-9_,]*) (?# base 10)
20
+ |[-+]?0x[0-9a-fA-F_,]+ (?# base 16))$/x
21
21
 
22
22
  attr_reader :class_loader
23
23
 
24
24
  # Create a new scanner
25
25
  def initialize class_loader
26
- @string_cache = {}
27
26
  @symbol_cache = {}
28
27
  @class_loader = class_loader
29
28
  end
@@ -31,81 +30,70 @@ module Psych
31
30
  # Tokenize +string+ returning the Ruby object
32
31
  def tokenize string
33
32
  return nil if string.empty?
34
- return string if @string_cache.key?(string)
35
33
  return @symbol_cache[string] if @symbol_cache.key?(string)
36
34
 
37
- case string
38
35
  # Check for a String type, being careful not to get caught by hash keys, hex values, and
39
36
  # special floats (e.g., -.inf).
40
- when /^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/, /\n/
41
- if string.length > 5
42
- @string_cache[string] = true
43
- return string
44
- end
37
+ if string.match?(/^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/) || string.match?(/\n/)
38
+ return string if string.length > 5
45
39
 
46
- case string
47
- when /^[^ytonf~]/i
48
- @string_cache[string] = true
40
+ if string.match?(/^[^ytonf~]/i)
49
41
  string
50
- when '~', /^null$/i
42
+ elsif string == '~' || string.match?(/^null$/i)
51
43
  nil
52
- when /^(yes|true|on)$/i
44
+ elsif string.match?(/^(yes|true|on)$/i)
53
45
  true
54
- when /^(no|false|off)$/i
46
+ elsif string.match?(/^(no|false|off)$/i)
55
47
  false
56
48
  else
57
- @string_cache[string] = true
58
49
  string
59
50
  end
60
- when TIME
51
+ elsif string.match?(TIME)
61
52
  begin
62
53
  parse_time string
63
54
  rescue ArgumentError
64
55
  string
65
56
  end
66
- when /^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/
57
+ elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/)
67
58
  require 'date'
68
59
  begin
69
60
  class_loader.date.strptime(string, '%Y-%m-%d')
70
61
  rescue ArgumentError
71
62
  string
72
63
  end
73
- when /^\.inf$/i
64
+ elsif string.match?(/^\.inf$/i)
74
65
  Float::INFINITY
75
- when /^-\.inf$/i
66
+ elsif string.match?(/^-\.inf$/i)
76
67
  -Float::INFINITY
77
- when /^\.nan$/i
68
+ elsif string.match?(/^\.nan$/i)
78
69
  Float::NAN
79
- when /^:./
70
+ elsif string.match?(/^:./)
80
71
  if string =~ /^:(["'])(.*)\1/
81
72
  @symbol_cache[string] = class_loader.symbolize($2.sub(/^:/, ''))
82
73
  else
83
74
  @symbol_cache[string] = class_loader.symbolize(string.sub(/^:/, ''))
84
75
  end
85
- when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/
76
+ elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/)
86
77
  i = 0
87
78
  string.split(':').each_with_index do |n,e|
88
79
  i += (n.to_i * 60 ** (e - 2).abs)
89
80
  end
90
81
  i
91
- when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/
82
+ elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/)
92
83
  i = 0
93
84
  string.split(':').each_with_index do |n,e|
94
85
  i += (n.to_f * 60 ** (e - 2).abs)
95
86
  end
96
87
  i
97
- when FLOAT
98
- if string =~ /\A[-+]?\.\Z/
99
- @string_cache[string] = true
88
+ elsif string.match?(FLOAT)
89
+ if string.match?(/\A[-+]?\.\Z/)
100
90
  string
101
91
  else
102
92
  Float(string.gsub(/[,_]|\.([Ee]|$)/, '\1'))
103
93
  end
94
+ elsif string.match?(INTEGER)
95
+ parse_int string
104
96
  else
105
- int = parse_int string.gsub(/[,_]/, '')
106
- return int if int
107
-
108
- @string_cache[string] = true
109
97
  string
110
98
  end
111
99
  end
@@ -113,8 +101,7 @@ module Psych
113
101
  ###
114
102
  # Parse and return an int from +string+
115
103
  def parse_int string
116
- return unless INTEGER === string
117
- Integer(string)
104
+ Integer(string.gsub(/[,_]/, ''))
118
105
  end
119
106
 
120
107
  ###
@@ -1,9 +1,10 @@
1
+
1
2
  # frozen_string_literal: true
2
3
  module Psych
3
- # The version is Psych you're using
4
- VERSION = '3.0.3.pre4'
4
+ # The version of Psych you are using
5
+ VERSION = '3.3.0'
5
6
 
6
7
  if RUBY_ENGINE == 'jruby'
7
- DEFAULT_SNAKEYAML_VERSION = '1.23'.freeze
8
+ DEFAULT_SNAKEYAML_VERSION = '1.26'.freeze
8
9
  end
9
10
  end
@@ -12,34 +12,38 @@ module Psych
12
12
  ###
13
13
  # This class walks a YAML AST, converting each node to Ruby
14
14
  class ToRuby < Psych::Visitors::Visitor
15
- def self.create
15
+ def self.create(symbolize_names: false, freeze: false)
16
16
  class_loader = ClassLoader.new
17
17
  scanner = ScalarScanner.new class_loader
18
- new(scanner, class_loader)
18
+ new(scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze)
19
19
  end
20
20
 
21
21
  attr_reader :class_loader
22
22
 
23
- def initialize ss, class_loader
23
+ def initialize ss, class_loader, symbolize_names: false, freeze: false
24
24
  super()
25
25
  @st = {}
26
26
  @ss = ss
27
27
  @domain_types = Psych.domain_types
28
28
  @class_loader = class_loader
29
+ @symbolize_names = symbolize_names
30
+ @freeze = freeze
29
31
  end
30
32
 
31
33
  def accept target
32
34
  result = super
33
- return result if @domain_types.empty? || !target.tag
34
35
 
35
- key = target.tag.sub(/^[!\/]*/, '').sub(/(,\d+)\//, '\1:')
36
- key = "tag:#{key}" unless key =~ /^(?:tag:|x-private)/
36
+ unless @domain_types.empty? || !target.tag
37
+ key = target.tag.sub(/^[!\/]*/, '').sub(/(,\d+)\//, '\1:')
38
+ key = "tag:#{key}" unless key =~ /^(?:tag:|x-private)/
37
39
 
38
- if @domain_types.key? key
39
- value, block = @domain_types[key]
40
- return block.call value, result
40
+ if @domain_types.key? key
41
+ value, block = @domain_types[key]
42
+ result = block.call value, result
43
+ end
41
44
  end
42
45
 
46
+ result = deduplicate(result).freeze if @freeze
43
47
  result
44
48
  end
45
49
 
@@ -252,6 +256,8 @@ module Psych
252
256
 
253
257
  e = build_exception((resolve_class($1) || class_loader.exception),
254
258
  h.delete('message'))
259
+
260
+ e.set_backtrace h.delete('backtrace') if h.key? 'backtrace'
255
261
  init_with(e, h, o)
256
262
 
257
263
  when '!set', 'tag:yaml.org,2002:set'
@@ -331,13 +337,12 @@ module Psych
331
337
  list
332
338
  end
333
339
 
334
- SHOVEL = '<<'
335
340
  def revive_hash hash, o
336
341
  o.children.each_slice(2) { |k,v|
337
342
  key = accept(k)
338
343
  val = accept(v)
339
344
 
340
- if key == SHOVEL && k.tag != "tag:yaml.org,2002:str"
345
+ if key == '<<' && k.tag != "tag:yaml.org,2002:str"
341
346
  case v
342
347
  when Nodes::Alias, Nodes::Mapping
343
348
  begin
@@ -359,6 +364,12 @@ module Psych
359
364
  hash[key] = val
360
365
  end
361
366
  else
367
+ if @symbolize_names
368
+ key = key.to_sym
369
+ elsif !@freeze
370
+ key = deduplicate(key)
371
+ end
372
+
362
373
  hash[key] = val
363
374
  end
364
375
 
@@ -366,6 +377,26 @@ module Psych
366
377
  hash
367
378
  end
368
379
 
380
+ if RUBY_VERSION < '2.7'
381
+ def deduplicate key
382
+ if key.is_a?(String)
383
+ # It is important to untaint the string, otherwise it won't
384
+ # be deduplicated into an fstring, but simply frozen.
385
+ -(key.untaint)
386
+ else
387
+ key
388
+ end
389
+ end
390
+ else
391
+ def deduplicate key
392
+ if key.is_a?(String)
393
+ -key
394
+ else
395
+ key
396
+ end
397
+ end
398
+ end
399
+
369
400
  def merge_key hash, key, val
370
401
  end
371
402
 
@@ -8,12 +8,26 @@ module Psych
8
8
 
9
9
  private
10
10
 
11
- DISPATCH = Hash.new do |hash, klass|
12
- hash[klass] = "visit_#{klass.name.gsub('::', '_')}"
11
+ # @api private
12
+ def self.dispatch_cache
13
+ Hash.new do |hash, klass|
14
+ hash[klass] = :"visit_#{klass.name.gsub('::', '_')}"
15
+ end.compare_by_identity
16
+ end
17
+
18
+ if defined?(Ractor)
19
+ def dispatch
20
+ Ractor.current[:Psych_Visitors_Visitor] ||= Visitor.dispatch_cache
21
+ end
22
+ else
23
+ DISPATCH = dispatch_cache
24
+ def dispatch
25
+ DISPATCH
26
+ end
13
27
  end
14
28
 
15
29
  def visit target
16
- send DISPATCH[target.class], target
30
+ send dispatch[target.class], target
17
31
  end
18
32
  end
19
33
  end
@@ -80,7 +80,7 @@ module Psych
80
80
  raise(TypeError, "Can't dump #{target.class}") unless method
81
81
 
82
82
  h[klass] = method
83
- end
83
+ end.compare_by_identity
84
84
  end
85
85
 
86
86
  def start encoding = Nodes::Stream::UTF8
@@ -181,41 +181,11 @@ module Psych
181
181
  end
182
182
 
183
183
  def visit_Exception o
184
- tag = ['!ruby/exception', o.class.name].join ':'
185
-
186
- @emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK
187
-
188
- {
189
- 'message' => private_iv_get(o, 'mesg'),
190
- 'backtrace' => private_iv_get(o, 'backtrace'),
191
- }.each do |k,v|
192
- next unless v
193
- @emitter.scalar k, nil, nil, true, false, Nodes::Scalar::ANY
194
- accept v
195
- end
196
-
197
- dump_ivars o
198
-
199
- @emitter.end_mapping
184
+ dump_exception o, o.message.to_s
200
185
  end
201
186
 
202
187
  def visit_NameError o
203
- tag = ['!ruby/exception', o.class.name].join ':'
204
-
205
- @emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK
206
-
207
- {
208
- 'message' => o.message.to_s,
209
- 'backtrace' => private_iv_get(o, 'backtrace'),
210
- }.each do |k,v|
211
- next unless v
212
- @emitter.scalar k, nil, nil, true, false, Nodes::Scalar::ANY
213
- accept v
214
- end
215
-
216
- dump_ivars o
217
-
218
- @emitter.end_mapping
188
+ dump_exception o, o.message.to_s
219
189
  end
220
190
 
221
191
  def visit_Regexp o
@@ -458,15 +428,6 @@ module Psych
458
428
  node = @emitter.start_mapping(nil, tag, false, Psych::Nodes::Mapping::BLOCK)
459
429
  register(o, node)
460
430
 
461
- # Dump the elements
462
- accept 'elements'
463
- @emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
464
- o.each do |k,v|
465
- accept k
466
- accept v
467
- end
468
- @emitter.end_mapping
469
-
470
431
  # Dump the ivars
471
432
  accept 'ivars'
472
433
  @emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
@@ -476,6 +437,15 @@ module Psych
476
437
  end
477
438
  @emitter.end_mapping
478
439
 
440
+ # Dump the elements
441
+ accept 'elements'
442
+ @emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
443
+ o.each do |k,v|
444
+ accept k
445
+ accept v
446
+ end
447
+ @emitter.end_mapping
448
+
479
449
  @emitter.end_mapping
480
450
  else
481
451
  tag = "!ruby/hash:#{o.class}"
@@ -492,6 +462,24 @@ module Psych
492
462
  def dump_list o
493
463
  end
494
464
 
465
+ def dump_exception o, msg
466
+ tag = ['!ruby/exception', o.class.name].join ':'
467
+
468
+ @emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK
469
+
470
+ if msg
471
+ @emitter.scalar 'message', nil, nil, true, false, Nodes::Scalar::ANY
472
+ accept msg
473
+ end
474
+
475
+ @emitter.scalar 'backtrace', nil, nil, true, false, Nodes::Scalar::ANY
476
+ accept o.backtrace
477
+
478
+ dump_ivars o
479
+
480
+ @emitter.end_mapping
481
+ end
482
+
495
483
  def format_time time
496
484
  if time.utc?
497
485
  time.strftime("%Y-%m-%d %H:%M:%S.%9N Z")
@@ -1,14 +1,19 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  # frozen_string_literal: true
3
3
 
4
+ version_module = Module.new do
5
+ version_rb = File.join(__dir__, "lib/psych/versions.rb")
6
+ module_eval(File.read(version_rb), version_rb)
7
+ end
8
+
4
9
  Gem::Specification.new do |s|
5
10
  s.name = "psych"
6
- s.version = "3.0.3.pre4"
11
+ s.version = version_module::Psych::VERSION
7
12
  s.authors = ["Aaron Patterson", "SHIBATA Hiroshi", "Charles Oliver Nutter"]
8
13
  s.email = ["aaron@tenderlovemaking.com", "hsbt@ruby-lang.org", "headius@headius.com"]
9
14
  s.summary = "Psych is a YAML parser and emitter"
10
15
  s.description = <<-DESCRIPTION
11
- Psych is a YAML parser and emitter. Psych leverages libyaml[http://pyyaml.org/wiki/LibYAML]
16
+ Psych is a YAML parser and emitter. Psych leverages libyaml[https://pyyaml.org/wiki/LibYAML]
12
17
  for its YAML parsing and emitting capabilities. In addition to wrapping libyaml,
13
18
  Psych also knows how to serialize and de-serialize most Ruby objects to and from the YAML format.
14
19
  DESCRIPTION
@@ -18,7 +23,7 @@ DESCRIPTION
18
23
 
19
24
  # for ruby core repository. It was generated by `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
25
  s.files = [
21
- ".gitignore", ".travis.yml", "CHANGELOG.rdoc", "Gemfile", "Mavenfile", "README.md", "Rakefile", "bin/console",
26
+ ".gitignore", "Gemfile", "LICENSE", "Mavenfile", "README.md", "Rakefile", "bin/console",
22
27
  "bin/setup", "ext/psych/depend", "ext/psych/extconf.rb", "ext/psych/psych.c", "ext/psych/psych.h",
23
28
  "ext/psych/psych_emitter.c", "ext/psych/psych_emitter.h", "ext/psych/psych_parser.c", "ext/psych/psych_parser.h",
24
29
  "ext/psych/psych_to_ruby.c", "ext/psych/psych_to_ruby.h", "ext/psych/psych_yaml_tree.c", "ext/psych/psych_yaml_tree.h",
@@ -38,26 +43,25 @@ DESCRIPTION
38
43
  ]
39
44
 
40
45
  s.rdoc_options = ["--main", "README.md"]
41
- s.extra_rdoc_files = ["CHANGELOG.rdoc", "README.md"]
46
+ s.extra_rdoc_files = ["README.md"]
42
47
 
43
- s.required_ruby_version = Gem::Requirement.new(">= 2.2.2")
44
- s.rubygems_version = "2.5.1"
48
+ s.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
45
49
  s.required_rubygems_version = Gem::Requirement.new(">= 0")
46
50
 
47
- s.add_development_dependency 'rake-compiler', ">= 0.4.1"
48
- s.add_development_dependency 'minitest', "~> 5.0"
49
-
50
51
  if RUBY_ENGINE == 'jruby'
51
52
  s.platform = 'java'
52
53
  s.files.concat [
53
- "ext/java/PsychEmitter.java", "ext/java/PsychLibrary.java", "ext/java/PsychParser.java", "ext/java/PsychToRuby.java",
54
- "ext/java/PsychYamlTree.java", "lib/psych_jars.rb", "lib/psych.jar"
54
+ "ext/java/org/jruby/ext/psych/PsychEmitter.java",
55
+ "ext/java/org/jruby/ext/psych/PsychLibrary.java",
56
+ "ext/java/org/jruby/ext/psych/PsychParser.java",
57
+ "ext/java/org/jruby/ext/psych/PsychToRuby.java",
58
+ "ext/java/org/jruby/ext/psych/PsychYamlTree.java",
59
+ "lib/psych_jars.rb",
60
+ "lib/psych.jar"
55
61
  ]
56
- s.requirements = "jar org.yaml:snakeyaml, 1.23"
62
+ s.requirements = "jar org.yaml:snakeyaml, #{version_module::Psych::DEFAULT_SNAKEYAML_VERSION}"
57
63
  s.add_dependency 'jar-dependencies', '>= 0.1.7'
58
- s.add_development_dependency 'ruby-maven'
59
64
  else
60
65
  s.extensions = ["ext/psych/extconf.rb"]
61
- s.add_development_dependency 'rake-compiler-dock', ">= 0.6.1"
62
66
  end
63
67
  end