psych 3.3.2 → 4.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 816fda8fc436476e4117450ad248554d12b2d41c25439e457c171583b0ad3e6a
4
- data.tar.gz: 83ce5b7ccd045a76f430d7a13b30c356d05be0014d753945f412ab0e939fe0da
3
+ metadata.gz: c7feb4df24f58d2d6f2c8fb8d7ea5ab70f2c42a60b9abb8d3fb9196d0591ffe1
4
+ data.tar.gz: 2f20dbfc2a70e7ee08d220a029c729e51aa4245318680cc65ec08ddba639894f
5
5
  SHA512:
6
- metadata.gz: 855d085016ed2c1f30bc3a3433247ad34ce06220d3871b7a92b922d89fde0a645471b8512f41b9d43812de407dcfaf01b23458475dfc1e0c37b9bbb6ff8d91f6
7
- data.tar.gz: 1191a34c8cf1c82687fbf94a25fdcce854c585718ef69aaedf97eb5a2cc1b07225ec44769a00b58124f67d58e33f138f657ea2233094f76bad1d2479d7a1b400
6
+ metadata.gz: 517352bc229d00418119d8123457f692063f48a72663db1ec4daad04161dc84ad3ae6b15722ac63129f4b6cee2d324fc77c522ac463a4bd9ba664286ccdbb62f
7
+ data.tar.gz: 87411fceb103e660919a5a58844e83e88209f805abd2109c8e3d6145736c41b27d85808ca9d454c51380c7d0cf1a6428937f85c81ef1cb2d47b10b7ef9720c7a
data/Rakefile CHANGED
@@ -33,7 +33,7 @@ end
33
33
 
34
34
  task :sync_tool do
35
35
  require 'fileutils'
36
- FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
36
+ FileUtils.cp "../ruby/tool/lib/core_assertions.rb", "./test/lib"
37
37
  FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
38
38
  FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
39
39
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/omap'
3
- require 'psych/set'
2
+ require_relative 'omap'
3
+ require_relative 'set'
4
4
 
5
5
  module Psych
6
6
  class ClassLoader # :nodoc:
@@ -86,7 +86,7 @@ module Psych
86
86
  if @symbols.include? sym
87
87
  super
88
88
  else
89
- raise DisallowedClass, 'Symbol'
89
+ raise DisallowedClass.new('load', 'Symbol')
90
90
  end
91
91
  end
92
92
 
@@ -96,7 +96,7 @@ module Psych
96
96
  if @classes.include? klassname
97
97
  super
98
98
  else
99
- raise DisallowedClass, klassname
99
+ raise DisallowedClass.new('load', klassname)
100
100
  end
101
101
  end
102
102
  end
@@ -15,5 +15,5 @@ class Object
15
15
  end
16
16
 
17
17
  if defined?(::IRB)
18
- require 'psych/y'
18
+ require_relative 'y'
19
19
  end
@@ -7,8 +7,8 @@ module Psych
7
7
  end
8
8
 
9
9
  class DisallowedClass < Exception
10
- def initialize klass_name
11
- super "Tried to load unspecified class: #{klass_name}"
10
+ def initialize action, klass_name
11
+ super "Tried to #{action} unspecified class: #{klass_name}"
12
12
  end
13
13
  end
14
14
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/tree_builder'
2
+ require_relative '../tree_builder'
3
3
 
4
4
  module Psych
5
5
  module Handlers
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/handler'
2
+ require_relative '../handler'
3
3
 
4
4
  module Psych
5
5
  module Handlers
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/json/ruby_events'
3
- require 'psych/json/yaml_events'
2
+ require_relative 'ruby_events'
3
+ require_relative 'yaml_events'
4
4
 
5
5
  module Psych
6
6
  module JSON
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/json/yaml_events'
2
+ require_relative 'yaml_events'
3
3
 
4
4
  module Psych
5
5
  module JSON
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  require 'stringio'
3
- require 'psych/class_loader'
4
- require 'psych/scalar_scanner'
3
+ require_relative '../class_loader'
4
+ require_relative '../scalar_scanner'
5
5
 
6
6
  module Psych
7
7
  module Nodes
@@ -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(symbolize_names: false, freeze: false)
50
- Visitors::ToRuby.create(symbolize_names: symbolize_names, freeze: freeze).accept(self)
49
+ def to_ruby(symbolize_names: false, freeze: false, strict_integer: false)
50
+ Visitors::ToRuby.create(symbolize_names: symbolize_names, freeze: freeze, strict_integer: strict_integer).accept(self)
51
51
  end
52
52
  alias :transform :to_ruby
53
53
 
data/lib/psych/nodes.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/nodes/node'
3
- require 'psych/nodes/stream'
4
- require 'psych/nodes/document'
5
- require 'psych/nodes/sequence'
6
- require 'psych/nodes/scalar'
7
- require 'psych/nodes/mapping'
8
- require 'psych/nodes/alias'
2
+ require_relative 'nodes/node'
3
+ require_relative 'nodes/stream'
4
+ require_relative 'nodes/document'
5
+ require_relative 'nodes/sequence'
6
+ require_relative 'nodes/scalar'
7
+ require_relative 'nodes/mapping'
8
+ require_relative 'nodes/alias'
9
9
 
10
10
  module Psych
11
11
  ###
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- require 'strscan'
3
2
 
4
3
  module Psych
5
4
  ###
@@ -9,32 +8,39 @@ module Psych
9
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)?))?$/
10
9
 
11
10
  # Taken from http://yaml.org/type/float.html
12
- FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10)
13
- |[-+]?\.(inf|Inf|INF)(?# infinity)
14
- |\.(nan|NaN|NAN)(?# not a number))$/x
11
+ # Base 60, [-+]inf and NaN are handled separately
12
+ FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10))$/x
15
13
 
16
14
  # 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
15
+ INTEGER_STRICT = /^(?:[-+]?0b[0-1_]+ (?# base 2)
16
+ |[-+]?0[0-7_]+ (?# base 8)
17
+ |[-+]?(0|[1-9][0-9_]*) (?# base 10)
18
+ |[-+]?0x[0-9a-fA-F_]+ (?# base 16))$/x
19
+
20
+ # Same as above, but allows commas.
21
+ # Not to YML spec, but kept for backwards compatibility
22
+ INTEGER_LEGACY = /^(?:[-+]?0b[0-1_,]+ (?# base 2)
23
+ |[-+]?0[0-7_,]+ (?# base 8)
24
+ |[-+]?(?:0|[1-9](?:[0-9]|,[0-9]|_[0-9])*) (?# base 10)
25
+ |[-+]?0x[0-9a-fA-F_,]+ (?# base 16))$/x
21
26
 
22
27
  attr_reader :class_loader
23
28
 
24
29
  # Create a new scanner
25
- def initialize class_loader
30
+ def initialize class_loader, strict_integer: false
26
31
  @symbol_cache = {}
27
32
  @class_loader = class_loader
33
+ @strict_integer = strict_integer
28
34
  end
29
35
 
30
36
  # Tokenize +string+ returning the Ruby object
31
37
  def tokenize string
32
38
  return nil if string.empty?
33
39
  return @symbol_cache[string] if @symbol_cache.key?(string)
34
-
40
+ integer_regex = @strict_integer ? INTEGER_STRICT : INTEGER_LEGACY
35
41
  # Check for a String type, being careful not to get caught by hash keys, hex values, and
36
42
  # special floats (e.g., -.inf).
37
- if string.match?(/^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/) || string.match?(/\n/)
43
+ if string.match?(%r{^[^\d.:-]?[[:alpha:]_\s!@#$%\^&*(){}<>|/\\~;=]+}) || string.match?(/\n/)
38
44
  return string if string.length > 5
39
45
 
40
46
  if string.match?(/^[^ytonf~]/i)
@@ -61,7 +67,7 @@ module Psych
61
67
  rescue ArgumentError
62
68
  string
63
69
  end
64
- elsif string.match?(/^\.inf$/i)
70
+ elsif string.match?(/^\+?\.inf$/i)
65
71
  Float::INFINITY
66
72
  elsif string.match?(/^-\.inf$/i)
67
73
  -Float::INFINITY
@@ -89,9 +95,9 @@ module Psych
89
95
  if string.match?(/\A[-+]?\.\Z/)
90
96
  string
91
97
  else
92
- Float(string.gsub(/[,_]|\.([Ee]|$)/, '\1'))
98
+ Float(string.delete(',_').gsub(/\.([Ee]|$)/, '\1'))
93
99
  end
94
- elsif string.match?(INTEGER)
100
+ elsif string.match?(integer_regex)
95
101
  parse_int string
96
102
  else
97
103
  string
@@ -101,7 +107,7 @@ module Psych
101
107
  ###
102
108
  # Parse and return an int from +string+
103
109
  def parse_int string
104
- Integer(string.gsub(/[,_]/, ''))
110
+ Integer(string.delete(',_'))
105
111
  end
106
112
 
107
113
  ###
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/exception'
2
+ require_relative 'exception'
3
3
 
4
4
  module Psych
5
5
  class SyntaxError < Psych::Exception
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/handler'
2
+ require_relative 'handler'
3
3
 
4
4
  module Psych
5
5
  ###
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Psych
4
4
  # The version of Psych you are using
5
- VERSION = '3.3.2'
5
+ VERSION = '4.0.4'
6
6
 
7
7
  if RUBY_ENGINE == 'jruby'
8
8
  DEFAULT_SNAKEYAML_VERSION = '1.28'.freeze
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/json/ruby_events'
2
+ require_relative '../json/ruby_events'
3
3
 
4
4
  module Psych
5
5
  module Visitors
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/scalar_scanner'
3
- require 'psych/class_loader'
4
- require 'psych/exception'
2
+ require_relative '../scalar_scanner'
3
+ require_relative '../class_loader'
4
+ require_relative '../exception'
5
5
 
6
6
  unless defined?(Regexp::NOENCODING)
7
7
  Regexp::NOENCODING = 32
@@ -12,9 +12,9 @@ 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(symbolize_names: false, freeze: false)
15
+ def self.create(symbolize_names: false, freeze: false, strict_integer: false)
16
16
  class_loader = ClassLoader.new
17
- scanner = ScalarScanner.new class_loader
17
+ scanner = ScalarScanner.new class_loader, strict_integer: strict_integer
18
18
  new(scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze)
19
19
  end
20
20
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/tree_builder'
3
- require 'psych/scalar_scanner'
4
- require 'psych/class_loader'
2
+ require_relative '../tree_builder'
3
+ require_relative '../scalar_scanner'
4
+ require_relative '../class_loader'
5
5
 
6
6
  module Psych
7
7
  module Visitors
@@ -272,6 +272,8 @@ module Psych
272
272
  tag = 'tag:yaml.org,2002:str'
273
273
  plain = false
274
274
  quote = false
275
+ elsif o == 'y' || o == 'n'
276
+ style = Nodes::Scalar::DOUBLE_QUOTED
275
277
  elsif @line_width && o.length > @line_width
276
278
  style = Nodes::Scalar::FOLDED
277
279
  elsif o =~ /^[^[:word:]][^"]*$/
@@ -535,5 +537,51 @@ module Psych
535
537
  end
536
538
  end
537
539
  end
540
+
541
+ class RestrictedYAMLTree < YAMLTree
542
+ DEFAULT_PERMITTED_CLASSES = {
543
+ TrueClass => true,
544
+ FalseClass => true,
545
+ NilClass => true,
546
+ Integer => true,
547
+ Float => true,
548
+ String => true,
549
+ Array => true,
550
+ Hash => true,
551
+ }.compare_by_identity.freeze
552
+
553
+ def initialize emitter, ss, options
554
+ super
555
+ @permitted_classes = DEFAULT_PERMITTED_CLASSES.dup
556
+ Array(options[:permitted_classes]).each do |klass|
557
+ @permitted_classes[klass] = true
558
+ end
559
+ @permitted_symbols = {}.compare_by_identity
560
+ Array(options[:permitted_symbols]).each do |symbol|
561
+ @permitted_symbols[symbol] = true
562
+ end
563
+ @aliases = options.fetch(:aliases, false)
564
+ end
565
+
566
+ def accept target
567
+ if !@aliases && @st.key?(target)
568
+ raise BadAlias, "Tried to dump an aliased object"
569
+ end
570
+
571
+ unless @permitted_classes[target.class]
572
+ raise DisallowedClass.new('dump', target.class.name || target.class.inspect)
573
+ end
574
+
575
+ super
576
+ end
577
+
578
+ def visit_Symbol sym
579
+ unless @permitted_symbols[sym]
580
+ raise DisallowedClass.new('dump', "Symbol(#{sym.inspect})")
581
+ end
582
+
583
+ super
584
+ end
585
+ end
538
586
  end
539
587
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/visitors/visitor'
3
- require 'psych/visitors/to_ruby'
4
- require 'psych/visitors/emitter'
5
- require 'psych/visitors/yaml_tree'
6
- require 'psych/visitors/json_tree'
7
- require 'psych/visitors/depth_first'
2
+ require_relative 'visitors/visitor'
3
+ require_relative 'visitors/to_ruby'
4
+ require_relative 'visitors/emitter'
5
+ require_relative 'visitors/yaml_tree'
6
+ require_relative 'visitors/json_tree'
7
+ require_relative 'visitors/depth_first'
data/lib/psych.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
- require 'psych/versions'
2
+ require_relative 'psych/versions'
3
3
  case RUBY_ENGINE
4
4
  when 'jruby'
5
- require 'psych_jars'
5
+ require_relative 'psych_jars'
6
6
  if JRuby::Util.respond_to?(:load_ext)
7
7
  JRuby::Util.load_ext('org.jruby.ext.psych.PsychLibrary')
8
8
  else
@@ -12,28 +12,28 @@ when 'jruby'
12
12
  else
13
13
  require 'psych.so'
14
14
  end
15
- require 'psych/nodes'
16
- require 'psych/streaming'
17
- require 'psych/visitors'
18
- require 'psych/handler'
19
- require 'psych/tree_builder'
20
- require 'psych/parser'
21
- require 'psych/omap'
22
- require 'psych/set'
23
- require 'psych/coder'
24
- require 'psych/core_ext'
25
- require 'psych/stream'
26
- require 'psych/json/tree_builder'
27
- require 'psych/json/stream'
28
- require 'psych/handlers/document_stream'
29
- require 'psych/class_loader'
15
+ require_relative 'psych/nodes'
16
+ require_relative 'psych/streaming'
17
+ require_relative 'psych/visitors'
18
+ require_relative 'psych/handler'
19
+ require_relative 'psych/tree_builder'
20
+ require_relative 'psych/parser'
21
+ require_relative 'psych/omap'
22
+ require_relative 'psych/set'
23
+ require_relative 'psych/coder'
24
+ require_relative 'psych/core_ext'
25
+ require_relative 'psych/stream'
26
+ require_relative 'psych/json/tree_builder'
27
+ require_relative 'psych/json/stream'
28
+ require_relative 'psych/handlers/document_stream'
29
+ require_relative 'psych/class_loader'
30
30
 
31
31
  ###
32
32
  # = Overview
33
33
  #
34
34
  # Psych is a YAML parser and emitter.
35
35
  # Psych leverages libyaml [Home page: https://pyyaml.org/wiki/LibYAML]
36
- # or [HG repo: https://bitbucket.org/xi/libyaml] for its YAML parsing
36
+ # or [git repo: https://github.com/yaml/libyaml] for its YAML parsing
37
37
  # and emitting capabilities. In addition to wrapping libyaml, Psych also
38
38
  # knows how to serialize and de-serialize most Ruby objects to and from
39
39
  # the YAML format.
@@ -234,9 +234,6 @@ require 'psych/class_loader'
234
234
  module Psych
235
235
  # The version of libyaml Psych is using
236
236
  LIBYAML_VERSION = Psych.libyaml_version.join('.').freeze
237
- # Deprecation guard
238
- NOT_GIVEN = Object.new.freeze
239
- private_constant :NOT_GIVEN
240
237
 
241
238
  ###
242
239
  # Load +yaml+ in to a Ruby data structure. If multiple documents are
@@ -249,11 +246,11 @@ module Psych
249
246
  #
250
247
  # Example:
251
248
  #
252
- # Psych.load("--- a") # => 'a'
253
- # Psych.load("---\n - a\n - b") # => ['a', 'b']
249
+ # Psych.unsafe_load("--- a") # => 'a'
250
+ # Psych.unsafe_load("---\n - a\n - b") # => ['a', 'b']
254
251
  #
255
252
  # begin
256
- # Psych.load("--- `", filename: "file.txt")
253
+ # Psych.unsafe_load("--- `", filename: "file.txt")
257
254
  # rescue Psych::SyntaxError => ex
258
255
  # ex.file # => 'file.txt'
259
256
  # ex.message # => "(file.txt): found character that cannot start any token"
@@ -262,26 +259,20 @@ module Psych
262
259
  # When the optional +symbolize_names+ keyword argument is set to a
263
260
  # true value, returns symbols for keys in Hash objects (default: strings).
264
261
  #
265
- # Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
266
- # Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
262
+ # Psych.unsafe_load("---\n foo: bar") # => {"foo"=>"bar"}
263
+ # Psych.unsafe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
267
264
  #
268
265
  # Raises a TypeError when `yaml` parameter is NilClass
269
266
  #
270
267
  # NOTE: This method *should not* be used to parse untrusted documents, such as
271
268
  # YAML documents that are supplied via user input. Instead, please use the
272
- # safe_load method.
269
+ # load method or the safe_load method.
273
270
  #
274
- def self.unsafe_load yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: false, symbolize_names: false, freeze: false
275
- if legacy_filename != NOT_GIVEN
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
277
- filename = legacy_filename
278
- end
279
-
271
+ def self.unsafe_load yaml, filename: nil, fallback: false, symbolize_names: false, freeze: false, strict_integer: false
280
272
  result = parse(yaml, filename: filename)
281
273
  return fallback unless result
282
- result.to_ruby(symbolize_names: symbolize_names, freeze: freeze)
274
+ result.to_ruby(symbolize_names: symbolize_names, freeze: freeze, strict_integer: strict_integer)
283
275
  end
284
- class << self; alias :load :unsafe_load; end
285
276
 
286
277
  ###
287
278
  # Safely load the yaml string in +yaml+. By default, only the following
@@ -290,7 +281,8 @@ module Psych
290
281
  # * TrueClass
291
282
  # * FalseClass
292
283
  # * NilClass
293
- # * Numeric
284
+ # * Integer
285
+ # * Float
294
286
  # * String
295
287
  # * Array
296
288
  # * Hash
@@ -327,33 +319,13 @@ module Psych
327
319
  # Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
328
320
  # Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
329
321
  #
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
331
- if legacy_permitted_classes != NOT_GIVEN
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
333
- permitted_classes = legacy_permitted_classes
334
- end
335
-
336
- if legacy_permitted_symbols != NOT_GIVEN
337
- warn_with_uplevel 'Passing permitted_symbols with the 3rd argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, permitted_symbols: ...) instead.', uplevel: 1 if $VERBOSE
338
- permitted_symbols = legacy_permitted_symbols
339
- end
340
-
341
- if legacy_aliases != NOT_GIVEN
342
- warn_with_uplevel 'Passing aliases with the 4th argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, aliases: ...) instead.', uplevel: 1 if $VERBOSE
343
- aliases = legacy_aliases
344
- end
345
-
346
- if legacy_filename != NOT_GIVEN
347
- warn_with_uplevel 'Passing filename with the 5th argument of Psych.safe_load is deprecated. Use keyword argument like Psych.safe_load(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
348
- filename = legacy_filename
349
- end
350
-
322
+ def self.safe_load yaml, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false, strict_integer: false
351
323
  result = parse(yaml, filename: filename)
352
324
  return fallback unless result
353
325
 
354
326
  class_loader = ClassLoader::Restricted.new(permitted_classes.map(&:to_s),
355
327
  permitted_symbols.map(&:to_s))
356
- scanner = ScalarScanner.new class_loader
328
+ scanner = ScalarScanner.new class_loader, strict_integer: strict_integer
357
329
  visitor = if aliases
358
330
  Visitors::ToRuby.new scanner, class_loader, symbolize_names: symbolize_names, freeze: freeze
359
331
  else
@@ -363,6 +335,47 @@ module Psych
363
335
  result
364
336
  end
365
337
 
338
+ ###
339
+ # Load +yaml+ in to a Ruby data structure. If multiple documents are
340
+ # provided, the object contained in the first document will be returned.
341
+ # +filename+ will be used in the exception message if any exception
342
+ # is raised while parsing. If +yaml+ is empty, it returns
343
+ # the specified +fallback+ return value, which defaults to +false+.
344
+ #
345
+ # Raises a Psych::SyntaxError when a YAML syntax error is detected.
346
+ #
347
+ # Example:
348
+ #
349
+ # Psych.load("--- a") # => 'a'
350
+ # Psych.load("---\n - a\n - b") # => ['a', 'b']
351
+ #
352
+ # begin
353
+ # Psych.load("--- `", filename: "file.txt")
354
+ # rescue Psych::SyntaxError => ex
355
+ # ex.file # => 'file.txt'
356
+ # ex.message # => "(file.txt): found character that cannot start any token"
357
+ # end
358
+ #
359
+ # When the optional +symbolize_names+ keyword argument is set to a
360
+ # true value, returns symbols for keys in Hash objects (default: strings).
361
+ #
362
+ # Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
363
+ # Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
364
+ #
365
+ # Raises a TypeError when `yaml` parameter is NilClass. This method is
366
+ # similar to `safe_load` except that `Symbol` objects are allowed by default.
367
+ #
368
+ def self.load yaml, permitted_classes: [Symbol], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false, strict_integer: false
369
+ safe_load yaml, permitted_classes: permitted_classes,
370
+ permitted_symbols: permitted_symbols,
371
+ aliases: aliases,
372
+ filename: filename,
373
+ fallback: fallback,
374
+ symbolize_names: symbolize_names,
375
+ freeze: freeze,
376
+ strict_integer: strict_integer
377
+ end
378
+
366
379
  ###
367
380
  # Parse a YAML string in +yaml+. Returns the Psych::Nodes::Document.
368
381
  # +filename+ is used in the exception message if a Psych::SyntaxError is
@@ -382,22 +395,12 @@ module Psych
382
395
  # end
383
396
  #
384
397
  # See Psych::Nodes for more information about YAML AST.
385
- def self.parse yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: NOT_GIVEN
386
- if legacy_filename != NOT_GIVEN
387
- warn_with_uplevel 'Passing filename with the 2nd argument of Psych.parse is deprecated. Use keyword argument like Psych.parse(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
388
- filename = legacy_filename
389
- end
390
-
398
+ def self.parse yaml, filename: nil
391
399
  parse_stream(yaml, filename: filename) do |node|
392
400
  return node
393
401
  end
394
402
 
395
- if fallback != NOT_GIVEN
396
- warn_with_uplevel 'Passing the `fallback` keyword argument of Psych.parse is deprecated.', uplevel: 1 if $VERBOSE
397
- fallback
398
- else
399
- false
400
- end
403
+ false
401
404
  end
402
405
 
403
406
  ###
@@ -446,12 +449,7 @@ module Psych
446
449
  # Raises a TypeError when NilClass is passed.
447
450
  #
448
451
  # See Psych::Nodes for more information about YAML AST.
449
- def self.parse_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, &block
450
- if legacy_filename != NOT_GIVEN
451
- warn_with_uplevel 'Passing filename with the 2nd argument of Psych.parse_stream is deprecated. Use keyword argument like Psych.parse_stream(yaml, filename: ...) instead.', uplevel: 1 if $VERBOSE
452
- filename = legacy_filename
453
- end
454
-
452
+ def self.parse_stream yaml, filename: nil, &block
455
453
  if block_given?
456
454
  parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
457
455
  parser.parse yaml, filename
@@ -515,6 +513,79 @@ module Psych
515
513
  visitor.tree.yaml io, options
516
514
  end
517
515
 
516
+ ###
517
+ # call-seq:
518
+ # Psych.safe_dump(o) -> string of yaml
519
+ # Psych.safe_dump(o, options) -> string of yaml
520
+ # Psych.safe_dump(o, io) -> io object passed in
521
+ # Psych.safe_dump(o, io, options) -> io object passed in
522
+ #
523
+ # Safely dump Ruby object +o+ to a YAML string. Optional +options+ may be passed in
524
+ # to control the output format. If an IO object is passed in, the YAML will
525
+ # be dumped to that IO object. By default, only the following
526
+ # classes are allowed to be serialized:
527
+ #
528
+ # * TrueClass
529
+ # * FalseClass
530
+ # * NilClass
531
+ # * Integer
532
+ # * Float
533
+ # * String
534
+ # * Array
535
+ # * Hash
536
+ #
537
+ # Arbitrary classes can be allowed by adding those classes to the +permitted_classes+
538
+ # keyword argument. They are additive. For example, to allow Date serialization:
539
+ #
540
+ # Psych.safe_dump(yaml, permitted_classes: [Date])
541
+ #
542
+ # Now the Date class can be dumped in addition to the classes listed above.
543
+ #
544
+ # A Psych::DisallowedClass exception will be raised if the object contains a
545
+ # class that isn't in the +permitted_classes+ list.
546
+ #
547
+ # Currently supported options are:
548
+ #
549
+ # [<tt>:indentation</tt>] Number of space characters used to indent.
550
+ # Acceptable value should be in <tt>0..9</tt> range,
551
+ # otherwise option is ignored.
552
+ #
553
+ # Default: <tt>2</tt>.
554
+ # [<tt>:line_width</tt>] Max character to wrap line at.
555
+ #
556
+ # Default: <tt>0</tt> (meaning "wrap at 81").
557
+ # [<tt>:canonical</tt>] Write "canonical" YAML form (very verbose, yet
558
+ # strictly formal).
559
+ #
560
+ # Default: <tt>false</tt>.
561
+ # [<tt>:header</tt>] Write <tt>%YAML [version]</tt> at the beginning of document.
562
+ #
563
+ # Default: <tt>false</tt>.
564
+ #
565
+ # Example:
566
+ #
567
+ # # Dump an array, get back a YAML string
568
+ # Psych.safe_dump(['a', 'b']) # => "---\n- a\n- b\n"
569
+ #
570
+ # # Dump an array to an IO object
571
+ # Psych.safe_dump(['a', 'b'], StringIO.new) # => #<StringIO:0x000001009d0890>
572
+ #
573
+ # # Dump an array with indentation set
574
+ # Psych.safe_dump(['a', ['b']], indentation: 3) # => "---\n- a\n- - b\n"
575
+ #
576
+ # # Dump an array to an IO with indentation set
577
+ # Psych.safe_dump(['a', ['b']], StringIO.new, indentation: 3)
578
+ def self.safe_dump o, io = nil, options = {}
579
+ if Hash === io
580
+ options = io
581
+ io = nil
582
+ end
583
+
584
+ visitor = Psych::Visitors::RestrictedYAMLTree.create options
585
+ visitor << o
586
+ visitor.tree.yaml io, options
587
+ end
588
+
518
589
  ###
519
590
  # Dump a list of objects as separate documents to a document stream.
520
591
  #
@@ -552,12 +623,7 @@ module Psych
552
623
  # end
553
624
  # list # => ['foo', 'bar']
554
625
  #
555
- def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: [], **kwargs
556
- if legacy_filename != NOT_GIVEN
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
558
- filename = legacy_filename
559
- end
560
-
626
+ def self.load_stream yaml, filename: nil, fallback: [], **kwargs
561
627
  result = if block_given?
562
628
  parse_stream(yaml, filename: filename) do |node|
563
629
  yield node.to_ruby(**kwargs)
@@ -583,7 +649,6 @@ module Psych
583
649
  self.unsafe_load f, filename: filename, **kwargs
584
650
  }
585
651
  end
586
- class << self; alias :load_file :unsafe_load_file; end
587
652
 
588
653
  ###
589
654
  # Safely loads the document contained in +filename+. Returns the yaml contained in
@@ -596,6 +661,17 @@ module Psych
596
661
  }
597
662
  end
598
663
 
664
+ ###
665
+ # Loads the document contained in +filename+. Returns the yaml contained in
666
+ # +filename+ as a Ruby object, or if the file is empty, it returns
667
+ # the specified +fallback+ return value, which defaults to +false+.
668
+ # See load for options.
669
+ def self.load_file filename, **kwargs
670
+ File.open(filename, 'r:bom|utf-8') { |f|
671
+ self.load f, filename: filename, **kwargs
672
+ }
673
+ end
674
+
599
675
  # :stopdoc:
600
676
  def self.add_domain_type domain, type_tag, &block
601
677
  key = ['tag', domain, type_tag].join ':'
data/psych.gemspec CHANGED
@@ -63,5 +63,7 @@ DESCRIPTION
63
63
  s.add_dependency 'jar-dependencies', '>= 0.1.7'
64
64
  else
65
65
  s.extensions = ["ext/psych/extconf.rb"]
66
+ s.add_dependency 'stringio'
66
67
  end
68
+
67
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: psych
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.2
4
+ version: 4.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Patterson
@@ -10,8 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-05-13 00:00:00.000000000 Z
14
- dependencies: []
13
+ date: 2022-05-16 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: stringio
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
15
29
  description: |
16
30
  Psych is a YAML parser and emitter. Psych leverages libyaml[https://pyyaml.org/wiki/LibYAML]
17
31
  for its YAML parsing and emitting capabilities. In addition to wrapping libyaml,
@@ -117,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
131
  - !ruby/object:Gem::Version
118
132
  version: '0'
119
133
  requirements: []
120
- rubygems_version: 3.3.0.dev
134
+ rubygems_version: 3.4.0.dev
121
135
  signing_key:
122
136
  specification_version: 4
123
137
  summary: Psych is a YAML parser and emitter