psych 4.0.0 → 4.0.2

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: d17b99168b9a2274379d31e2e75b667e8cbe0d4b56cf9bc7dace2e8af07d8c3d
4
- data.tar.gz: cf0e77e91cd7f3b9dc4390f1fac70734daad2d747336cf5a4e5e9062035c1f84
3
+ metadata.gz: 1a4075fe0c4db3e6dcc7e5b025be7b155d634502c706e55c69493ce5464e8e58
4
+ data.tar.gz: 2cc4c986617d151391906db8d8ec11c10d27a374142f4eff0dfc6d52939fa42d
5
5
  SHA512:
6
- metadata.gz: 92813d4ba7e393f2d1800ee7fe77b65cc9f56c6d0cc324440c367a71ef087d9d1a0ce2c3cbddaec238c05f50a205e62b8bd95a2bb88564ba03d729b473a31fcf
7
- data.tar.gz: ee68420c75330f9a0671b1e595962fb971940e55b6693d17b41b7b06b8d75c6aa5541625371f3f81e8505c6b7f2d84426dd4fd610b5968e1b17581ca7f5d4bfb
6
+ metadata.gz: b06f0866f9967812901502e12afc81ffcc297597396f25a31184aca58eafbcad635bf39b495ef04e9ea5dca1312c5afb207fd835dbe5de97cb810f504ee37534
7
+ data.tar.gz: 47546ca4c3785b6eaffd73ccc1008ec4414c8d7187503cd4e9c927b98d65a9bee84c07aa7defd55b3e671f30f660aed826ce57245cd0fffc24c5ff7bf3c2912f
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
@@ -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
@@ -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
@@ -9,15 +9,14 @@ module Psych
9
9
  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
10
 
11
11
  # 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
12
+ # Base 60, [-+]inf and NaN are handled separately
13
+ FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10))$/x
15
14
 
16
15
  # 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
16
+ INTEGER = /^(?:[-+]?0b[0-1_,]+ (?# base 2)
17
+ |[-+]?0[0-7_,]+ (?# base 8)
18
+ |[-+]?(?:0|[1-9](?:[0-9]|,[0-9]|_[0-9])*) (?# base 10)
19
+ |[-+]?0x[0-9a-fA-F_,]+ (?# base 16))$/x
21
20
 
22
21
  attr_reader :class_loader
23
22
 
@@ -34,7 +33,7 @@ module Psych
34
33
 
35
34
  # Check for a String type, being careful not to get caught by hash keys, hex values, and
36
35
  # special floats (e.g., -.inf).
37
- if string.match?(/^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/) || string.match?(/\n/)
36
+ if string.match?(%r{^[^\d.:-]?[[:alpha:]_\s!@#$%\^&*(){}<>|/\\~;=]+}) || string.match?(/\n/)
38
37
  return string if string.length > 5
39
38
 
40
39
  if string.match?(/^[^ytonf~]/i)
@@ -61,7 +60,7 @@ module Psych
61
60
  rescue ArgumentError
62
61
  string
63
62
  end
64
- elsif string.match?(/^\.inf$/i)
63
+ elsif string.match?(/^\+?\.inf$/i)
65
64
  Float::INFINITY
66
65
  elsif string.match?(/^-\.inf$/i)
67
66
  -Float::INFINITY
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Psych
4
4
  # The version of Psych you are using
5
- VERSION = '4.0.0'
5
+ VERSION = '4.0.2'
6
6
 
7
7
  if RUBY_ENGINE == 'jruby'
8
8
  DEFAULT_SNAKEYAML_VERSION = '1.28'.freeze
@@ -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
data/lib/psych.rb CHANGED
@@ -33,7 +33,7 @@ require 'psych/class_loader'
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.
@@ -282,7 +282,8 @@ module Psych
282
282
  # * TrueClass
283
283
  # * FalseClass
284
284
  # * NilClass
285
- # * Numeric
285
+ # * Integer
286
+ # * Float
286
287
  # * String
287
288
  # * Array
288
289
  # * Hash
@@ -512,6 +513,79 @@ module Psych
512
513
  visitor.tree.yaml io, options
513
514
  end
514
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
+
515
589
  ###
516
590
  # Dump a list of objects as separate documents to a document stream.
517
591
  #
@@ -575,7 +649,6 @@ module Psych
575
649
  self.unsafe_load f, filename: filename, **kwargs
576
650
  }
577
651
  end
578
- class << self; alias :load_file :unsafe_load_file; end
579
652
 
580
653
  ###
581
654
  # Safely loads the document contained in +filename+. Returns the yaml contained in
@@ -587,7 +660,17 @@ module Psych
587
660
  self.safe_load f, filename: filename, **kwargs
588
661
  }
589
662
  end
590
- class << self; alias load_file safe_load_file end
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
591
674
 
592
675
  # :stopdoc:
593
676
  def self.add_domain_type domain, type_tag, &block
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: 4.0.0
4
+ version: 4.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Patterson
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-05-13 00:00:00.000000000 Z
13
+ date: 2021-10-21 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: |
16
16
  Psych is a YAML parser and emitter. Psych leverages libyaml[https://pyyaml.org/wiki/LibYAML]