psych 3.2.0 → 3.3.0

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: 8d2864bd6b2ce0aa8d9c7a1793c6531d582ce5591f61fa77a5147b495a0eacff
4
- data.tar.gz: fb30e5a5a677c813e058366c8ee1339c986c65f9bc51f69e94771d31199ae7e8
3
+ metadata.gz: bb86b57322de81bdd56451df223fe27f61a67b4f85caeb0d28fe9b4cda6cd5ae
4
+ data.tar.gz: 762106ad2df9213bf93e777513d23df05c9bd25fb0dd511c8275172e5eeef118
5
5
  SHA512:
6
- metadata.gz: 7c2fcf1411c11ff9e7479b79564fc1114c76f12a7b6c6a9ad983f8d565691bcc5b012f0db28529974f1fd274929452b99366af885d094b596da625568149a456
7
- data.tar.gz: 433269e6942d6b1e8812bee88844816aafe7b67c2e58ad51ecf4ddfa45e3c8a99f7265b0a7bb48e4dfc758142fa6503c1b17a2e35036c4bad56dc28fe73414f5
6
+ metadata.gz: fa022cc3afcc02867d9528ac55f9b165011e5555ed37a24eed3116b211bb72247bb5afa73136f42232d649d7c6cab3c7af44e44558719d841edb4ad5f3d6d611
7
+ data.tar.gz: d2fbc5ff9683197cbc76abbee9fbdec01622fb70f262c72567342f111549ad3686ca1b8e4839705e43c63d64305aa62bb464819a118d83cf3b050ffb652c8ef7
data/README.md CHANGED
@@ -12,8 +12,8 @@ serialize and de-serialize most Ruby objects to and from the YAML format.
12
12
  ## Examples
13
13
 
14
14
  ```ruby
15
- # Load YAML in to a Ruby object
16
- Psych.load('--- foo') # => 'foo'
15
+ # Safely load YAML in to a Ruby object
16
+ Psych.safe_load('--- foo') # => 'foo'
17
17
 
18
18
  # Emit YAML from a Ruby object
19
19
  Psych.dump("foo") # => "--- foo\n...\n"
data/ext/psych/psych.c CHANGED
@@ -22,6 +22,9 @@ VALUE mPsych;
22
22
 
23
23
  void Init_psych(void)
24
24
  {
25
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
26
+ RB_EXT_RACTOR_SAFE(true);
27
+ #endif
25
28
  mPsych = rb_define_module("Psych");
26
29
 
27
30
  rb_define_singleton_method(mPsych, "libyaml_version", libyaml_version, 0);
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * @file yaml.h
3
3
  * @brief Public interface for libyaml.
4
- *
4
+ *
5
5
  * Include the header file with the code:
6
6
  * @code
7
7
  * #include <yaml.h>
@@ -390,7 +390,7 @@ typedef struct yaml_event_s {
390
390
 
391
391
  /** The event data. */
392
392
  union {
393
-
393
+
394
394
  /** The stream parameters (for @c YAML_STREAM_START_EVENT). */
395
395
  struct {
396
396
  /** The document encoding. */
@@ -726,7 +726,7 @@ struct yaml_node_s {
726
726
 
727
727
  /** The node data. */
728
728
  union {
729
-
729
+
730
730
  /** The scalar parameters (for @c YAML_SCALAR_NODE). */
731
731
  struct {
732
732
  /** The scalar value. */
data/lib/psych.rb CHANGED
@@ -74,12 +74,15 @@ require 'psych/class_loader'
74
74
  #
75
75
  # ==== Reading from a string
76
76
  #
77
- # Psych.load("--- a") # => 'a'
78
- # 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
79
81
  #
80
82
  # ==== Reading from a file
81
83
  #
82
- # Psych.load_file("database.yml")
84
+ # Psych.safe_load_file("data.yml", permitted_classes: [Date])
85
+ # Psych.load_file("trusted_database.yml")
83
86
  #
84
87
  # ==== Exception handling
85
88
  #
@@ -230,9 +233,9 @@ require 'psych/class_loader'
230
233
 
231
234
  module Psych
232
235
  # The version of libyaml Psych is using
233
- LIBYAML_VERSION = Psych.libyaml_version.join '.'
236
+ LIBYAML_VERSION = Psych.libyaml_version.join('.').freeze
234
237
  # Deprecation guard
235
- NOT_GIVEN = Object.new
238
+ NOT_GIVEN = Object.new.freeze
236
239
  private_constant :NOT_GIVEN
237
240
 
238
241
  ###
@@ -276,8 +279,7 @@ module Psych
276
279
 
277
280
  result = parse(yaml, filename: filename)
278
281
  return fallback unless result
279
- result = result.to_ruby(symbolize_names: symbolize_names, freeze: freeze) if result
280
- result
282
+ result.to_ruby(symbolize_names: symbolize_names, freeze: freeze)
281
283
  end
282
284
 
283
285
  ###
@@ -549,7 +551,7 @@ module Psych
549
551
  # end
550
552
  # list # => ['foo', 'bar']
551
553
  #
552
- def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: []
554
+ def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: [], **kwargs
553
555
  if legacy_filename != NOT_GIVEN
554
556
  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
555
557
  filename = legacy_filename
@@ -557,10 +559,10 @@ module Psych
557
559
 
558
560
  result = if block_given?
559
561
  parse_stream(yaml, filename: filename) do |node|
560
- yield node.to_ruby
562
+ yield node.to_ruby(**kwargs)
561
563
  end
562
564
  else
563
- parse_stream(yaml, filename: filename).children.map(&:to_ruby)
565
+ parse_stream(yaml, filename: filename).children.map { |node| node.to_ruby(**kwargs) }
564
566
  end
565
567
 
566
568
  return fallback if result.is_a?(Array) && result.empty?
@@ -571,35 +573,47 @@ module Psych
571
573
  # Load the document contained in +filename+. Returns the yaml contained in
572
574
  # +filename+ as a Ruby object, or if the file is empty, it returns
573
575
  # the specified +fallback+ return value, which defaults to +false+.
574
- def self.load_file filename, fallback: false
576
+ #
577
+ # NOTE: This method *should not* be used to parse untrusted documents, such as
578
+ # YAML documents that are supplied via user input. Instead, please use the
579
+ # safe_load_file method.
580
+ def self.load_file filename, **kwargs
575
581
  File.open(filename, 'r:bom|utf-8') { |f|
576
- self.load f, filename: filename, fallback: fallback
582
+ self.load f, filename: filename, **kwargs
583
+ }
584
+ end
585
+
586
+ ###
587
+ # Safely loads the document contained in +filename+. Returns the yaml contained in
588
+ # +filename+ as a Ruby object, or if the file is empty, it returns
589
+ # the specified +fallback+ return value, which defaults to +false+.
590
+ # See safe_load for options.
591
+ def self.safe_load_file filename, **kwargs
592
+ File.open(filename, 'r:bom|utf-8') { |f|
593
+ self.safe_load f, filename: filename, **kwargs
577
594
  }
578
595
  end
579
596
 
580
597
  # :stopdoc:
581
- @domain_types = {}
582
598
  def self.add_domain_type domain, type_tag, &block
583
599
  key = ['tag', domain, type_tag].join ':'
584
- @domain_types[key] = [key, block]
585
- @domain_types["tag:#{type_tag}"] = [key, block]
600
+ domain_types[key] = [key, block]
601
+ domain_types["tag:#{type_tag}"] = [key, block]
586
602
  end
587
603
 
588
604
  def self.add_builtin_type type_tag, &block
589
605
  domain = 'yaml.org,2002'
590
606
  key = ['tag', domain, type_tag].join ':'
591
- @domain_types[key] = [key, block]
607
+ domain_types[key] = [key, block]
592
608
  end
593
609
 
594
610
  def self.remove_type type_tag
595
- @domain_types.delete type_tag
611
+ domain_types.delete type_tag
596
612
  end
597
613
 
598
- @load_tags = {}
599
- @dump_tags = {}
600
614
  def self.add_tag tag, klass
601
- @load_tags[tag] = klass.name
602
- @dump_tags[klass] = tag
615
+ load_tags[tag] = klass.name
616
+ dump_tags[klass] = tag
603
617
  end
604
618
 
605
619
  # Workaround for emulating `warn '...', uplevel: 1` in Ruby 2.4 or lower.
@@ -618,9 +632,32 @@ module Psych
618
632
  private_class_method :warn_with_uplevel, :parse_caller
619
633
 
620
634
  class << self
621
- attr_accessor :load_tags
622
- attr_accessor :dump_tags
623
- attr_accessor :domain_types
635
+ if defined?(Ractor)
636
+ require 'forwardable'
637
+ extend Forwardable
638
+
639
+ class Config
640
+ attr_accessor :load_tags, :dump_tags, :domain_types
641
+ def initialize
642
+ @load_tags = {}
643
+ @dump_tags = {}
644
+ @domain_types = {}
645
+ end
646
+ end
647
+
648
+ def config
649
+ Ractor.current[:PsychConfig] ||= Config.new
650
+ end
651
+
652
+ def_delegators :config, :load_tags, :dump_tags, :domain_types, :load_tags=, :dump_tags=, :domain_types=
653
+ else
654
+ attr_accessor :load_tags
655
+ attr_accessor :dump_tags
656
+ attr_accessor :domain_types
657
+ end
624
658
  end
659
+ self.load_tags = {}
660
+ self.dump_tags = {}
661
+ self.domain_types = {}
625
662
  # :startdoc:
626
663
  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
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
  module Psych
4
4
  # The version of Psych you are using
5
- VERSION = '3.2.0'
5
+ VERSION = '3.3.0'
6
6
 
7
7
  if RUBY_ENGINE == 'jruby'
8
8
  DEFAULT_SNAKEYAML_VERSION = '1.26'.freeze
@@ -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
data/psych.gemspec CHANGED
@@ -46,7 +46,6 @@ DESCRIPTION
46
46
  s.extra_rdoc_files = ["README.md"]
47
47
 
48
48
  s.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
49
- s.rubygems_version = "2.5.1"
50
49
  s.required_rubygems_version = Gem::Requirement.new(">= 0")
51
50
 
52
51
  if RUBY_ENGINE == 'jruby'
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.2.0
4
+ version: 3.3.0
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: 2020-07-17 00:00:00.000000000 Z
13
+ date: 2020-12-23 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]
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  - !ruby/object:Gem::Version
118
118
  version: '0'
119
119
  requirements: []
120
- rubygems_version: 3.2.0.pre1
120
+ rubygems_version: 3.2.2
121
121
  signing_key:
122
122
  specification_version: 4
123
123
  summary: Psych is a YAML parser and emitter