psych 3.1.0-java → 3.3.2-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,8 +2,8 @@
2
2
  #include RUBY_EXTCONF_H
3
3
  #endif
4
4
 
5
- #if HAVE_CONFIG_H
6
- #include <config.h>
5
+ #ifdef HAVE_CONFIG_H
6
+ #include "config.h"
7
7
  #endif
8
8
 
9
9
  #include <yaml.h>
@@ -175,14 +175,14 @@ yaml_string_join(
175
175
  * Check the octet at the specified position.
176
176
  */
177
177
 
178
- #define CHECK_AT(string,octet,offset) \
178
+ #define CHECK_AT(string,octet,offset) \
179
179
  ((string).pointer[offset] == (yaml_char_t)(octet))
180
180
 
181
181
  /*
182
182
  * Check the current octet in the buffer.
183
183
  */
184
184
 
185
- #define CHECK(string,octet) CHECK_AT((string),(octet),0)
185
+ #define CHECK(string,octet) (CHECK_AT((string),(octet),0))
186
186
 
187
187
  /*
188
188
  * Check if the character at the specified position is an alphabetical
data/lib/psych.rb CHANGED
@@ -10,11 +10,7 @@ when 'jruby'
10
10
  org.jruby.ext.psych.PsychLibrary.new.load(JRuby.runtime, false)
11
11
  end
12
12
  else
13
- begin
14
- require "#{RUBY_VERSION[/\d+\.\d+/]}/psych.so"
15
- rescue LoadError
16
- require 'psych.so'
17
- end
13
+ require 'psych.so'
18
14
  end
19
15
  require 'psych/nodes'
20
16
  require 'psych/streaming'
@@ -78,12 +74,15 @@ require 'psych/class_loader'
78
74
  #
79
75
  # ==== Reading from a string
80
76
  #
81
- # Psych.load("--- a") # => 'a'
82
- # Psych.load("---\n - a\n - b") # => ['a', 'b']
77
+ # Psych.safe_load("--- a") # => 'a'
78
+ # Psych.safe_load("---\n - a\n - b") # => ['a', 'b']
79
+ # # From a trusted string:
80
+ # Psych.load("--- !ruby/range\nbegin: 0\nend: 42\nexcl: false\n") # => 0..42
83
81
  #
84
82
  # ==== Reading from a file
85
83
  #
86
- # Psych.load_file("database.yml")
84
+ # Psych.safe_load_file("data.yml", permitted_classes: [Date])
85
+ # Psych.load_file("trusted_database.yml")
87
86
  #
88
87
  # ==== Exception handling
89
88
  #
@@ -234,9 +233,9 @@ require 'psych/class_loader'
234
233
 
235
234
  module Psych
236
235
  # The version of libyaml Psych is using
237
- LIBYAML_VERSION = Psych.libyaml_version.join '.'
236
+ LIBYAML_VERSION = Psych.libyaml_version.join('.').freeze
238
237
  # Deprecation guard
239
- NOT_GIVEN = Object.new
238
+ NOT_GIVEN = Object.new.freeze
240
239
  private_constant :NOT_GIVEN
241
240
 
242
241
  ###
@@ -268,7 +267,11 @@ module Psych
268
267
  #
269
268
  # Raises a TypeError when `yaml` parameter is NilClass
270
269
  #
271
- def self.load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false, symbolize_names: false
270
+ # NOTE: This method *should not* be used to parse untrusted documents, such as
271
+ # YAML documents that are supplied via user input. Instead, please use the
272
+ # safe_load method.
273
+ #
274
+ def self.unsafe_load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false, symbolize_names: false, freeze: false
272
275
  if legacy_filename != NOT_GIVEN
273
276
  warn_with_uplevel 'Passing filename with the 2nd argument of Psych.load is deprecated. Use keyword argument like Psych.load(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
274
277
  filename = legacy_filename
@@ -276,10 +279,9 @@ module Psych
276
279
 
277
280
  result = parse(yaml, filename: filename)
278
281
  return fallback unless result
279
- result = result.to_ruby if result
280
- symbolize_names!(result) if symbolize_names
281
- result
282
+ result.to_ruby(symbolize_names: symbolize_names, freeze: freeze)
282
283
  end
284
+ class << self; alias :load :unsafe_load; end
283
285
 
284
286
  ###
285
287
  # Safely load the yaml string in +yaml+. By default, only the following
@@ -325,7 +327,7 @@ module Psych
325
327
  # Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
326
328
  # Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
327
329
  #
328
- def self.safe_load yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false
330
+ def self.safe_load yaml, legacy_permitted_classes = NOT_GIVEN, legacy_permitted_symbols = NOT_GIVEN, legacy_aliases = NOT_GIVEN, legacy_filename = NOT_GIVEN, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false
329
331
  if legacy_permitted_classes != NOT_GIVEN
330
332
  warn_with_uplevel 'Passing permitted_classes with the 2nd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_classes: ...) instead.', uplevel: 1 if $VERBOSE
331
333
  permitted_classes = legacy_permitted_classes
@@ -353,12 +355,11 @@ module Psych
353
355
  permitted_symbols.map(&:to_s))
354
356
  scanner = ScalarScanner.new class_loader
355
357
  visitor = if aliases
356
- Visitors::ToRuby.new scanner, class_loader
358
+ Visitors::ToRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze
357
359
  else
358
- Visitors::NoAliasRuby.new scanner, class_loader
360
+ Visitors::NoAliasRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze
359
361
  end
360
362
  result = visitor.accept result
361
- symbolize_names!(result) if symbolize_names
362
363
  result
363
364
  end
364
365
 
@@ -551,7 +552,7 @@ module Psych
551
552
  # end
552
553
  # list # => ['foo', 'bar']
553
554
  #
554
- def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: []
555
+ def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: [], **kwargs
555
556
  if legacy_filename != NOT_GIVEN
556
557
  warn_with_uplevel 'Passing filename with the 2nd argument of Psych.load_stream is deprecated. Use keyword argument like Psych.load_stream(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
557
558
  filename = legacy_filename
@@ -559,10 +560,10 @@ module Psych
559
560
 
560
561
  result = if block_given?
561
562
  parse_stream(yaml, filename: filename) do |node|
562
- yield node.to_ruby
563
+ yield node.to_ruby(**kwargs)
563
564
  end
564
565
  else
565
- parse_stream(yaml, filename: filename).children.map(&:to_ruby)
566
+ parse_stream(yaml, filename: filename).children.map { |node| node.to_ruby(**kwargs) }
566
567
  end
567
568
 
568
569
  return fallback if result.is_a?(Array) && result.empty?
@@ -573,50 +574,50 @@ module Psych
573
574
  # Load the document contained in +filename+. Returns the yaml contained in
574
575
  # +filename+ as a Ruby object, or if the file is empty, it returns
575
576
  # the specified +fallback+ return value, which defaults to +false+.
576
- def self.load_file filename, fallback: false
577
+ #
578
+ # NOTE: This method *should not* be used to parse untrusted documents, such as
579
+ # YAML documents that are supplied via user input. Instead, please use the
580
+ # safe_load_file method.
581
+ def self.unsafe_load_file filename, **kwargs
582
+ File.open(filename, 'r:bom|utf-8') { |f|
583
+ self.unsafe_load f, filename: filename, **kwargs
584
+ }
585
+ end
586
+ class << self; alias :load_file :unsafe_load_file; end
587
+
588
+ ###
589
+ # Safely loads the document contained in +filename+. Returns the yaml contained in
590
+ # +filename+ as a Ruby object, or if the file is empty, it returns
591
+ # the specified +fallback+ return value, which defaults to +false+.
592
+ # See safe_load for options.
593
+ def self.safe_load_file filename, **kwargs
577
594
  File.open(filename, 'r:bom|utf-8') { |f|
578
- self.load f, filename: filename, fallback: fallback
595
+ self.safe_load f, filename: filename, **kwargs
579
596
  }
580
597
  end
581
598
 
582
599
  # :stopdoc:
583
- @domain_types = {}
584
600
  def self.add_domain_type domain, type_tag, &block
585
601
  key = ['tag', domain, type_tag].join ':'
586
- @domain_types[key] = [key, block]
587
- @domain_types["tag:#{type_tag}"] = [key, block]
602
+ domain_types[key] = [key, block]
603
+ domain_types["tag:#{type_tag}"] = [key, block]
588
604
  end
589
605
 
590
606
  def self.add_builtin_type type_tag, &block
591
607
  domain = 'yaml.org,2002'
592
608
  key = ['tag', domain, type_tag].join ':'
593
- @domain_types[key] = [key, block]
609
+ domain_types[key] = [key, block]
594
610
  end
595
611
 
596
612
  def self.remove_type type_tag
597
- @domain_types.delete type_tag
613
+ domain_types.delete type_tag
598
614
  end
599
615
 
600
- @load_tags = {}
601
- @dump_tags = {}
602
616
  def self.add_tag tag, klass
603
- @load_tags[tag] = klass.name
604
- @dump_tags[klass] = tag
617
+ load_tags[tag] = klass.name
618
+ dump_tags[klass] = tag
605
619
  end
606
620
 
607
- def self.symbolize_names!(result)
608
- case result
609
- when Hash
610
- result.keys.each do |key|
611
- result[key.to_sym] = symbolize_names!(result.delete(key))
612
- end
613
- when Array
614
- result.map! { |r| symbolize_names!(r) }
615
- end
616
- result
617
- end
618
- private_class_method :symbolize_names!
619
-
620
621
  # Workaround for emulating `warn '...', uplevel: 1` in Ruby 2.4 or lower.
621
622
  def self.warn_with_uplevel(message, uplevel: 1)
622
623
  at = parse_caller(caller[uplevel]).join(':')
@@ -633,9 +634,32 @@ module Psych
633
634
  private_class_method :warn_with_uplevel, :parse_caller
634
635
 
635
636
  class << self
636
- attr_accessor :load_tags
637
- attr_accessor :dump_tags
638
- attr_accessor :domain_types
637
+ if defined?(Ractor)
638
+ require 'forwardable'
639
+ extend Forwardable
640
+
641
+ class Config
642
+ attr_accessor :load_tags, :dump_tags, :domain_types
643
+ def initialize
644
+ @load_tags = {}
645
+ @dump_tags = {}
646
+ @domain_types = {}
647
+ end
648
+ end
649
+
650
+ def config
651
+ Ractor.current[:PsychConfig] ||= Config.new
652
+ end
653
+
654
+ def_delegators :config, :load_tags, :dump_tags, :domain_types, :load_tags=, :dump_tags=, :domain_types=
655
+ else
656
+ attr_accessor :load_tags
657
+ attr_accessor :dump_tags
658
+ attr_accessor :domain_types
659
+ end
639
660
  end
661
+ self.load_tags = {}
662
+ self.dump_tags = {}
663
+ self.domain_types = {}
640
664
  # :startdoc:
641
665
  end
@@ -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
data/lib/psych/handler.rb CHANGED
@@ -119,7 +119,7 @@ module Psych
119
119
  # +tag+ is an associated tag or nil
120
120
  # +plain+ is a boolean value
121
121
  # +quoted+ is a boolean value
122
- # +style+ is an integer idicating the string style
122
+ # +style+ is an integer indicating the string style
123
123
  #
124
124
  # See the constants in Psych::Nodes::Scalar for the possible values of
125
125
  # +style+
@@ -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
 
@@ -50,7 +50,7 @@ module Psych
50
50
  # +tag+ is an associated tag or nil
51
51
  # +plain+ is a boolean value
52
52
  # +quoted+ is a boolean value
53
- # +style+ is an integer idicating the string style
53
+ # +style+ is an integer indicating the string style
54
54
  #
55
55
  # == See Also
56
56
  #
@@ -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,10 +1,10 @@
1
-
2
1
  # frozen_string_literal: true
2
+
3
3
  module Psych
4
4
  # The version of Psych you are using
5
- VERSION = '3.1.0' unless defined?(::Psych::VERSION)
5
+ VERSION = '3.3.2'
6
6
 
7
7
  if RUBY_ENGINE == 'jruby'
8
- DEFAULT_SNAKEYAML_VERSION = '1.23'.freeze
8
+ DEFAULT_SNAKEYAML_VERSION = '1.28'.freeze
9
9
  end
10
10
  end