psych 3.2.1 → 4.0.1

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: 33ed655876a483c54babe4a2262f347f87833bcbdd2bb611712935b97f0aa13d
4
- data.tar.gz: 7659d9c4c3dc37cc6dc87c82da975d41ac37156cea5baa5361727550110af5f3
3
+ metadata.gz: 2fa803573d39d69c706eec844ab105ce74589c01fd5284340d353e577829c4e1
4
+ data.tar.gz: f5ec2260e1d0d1f2703d6c4cb672235b71754045a95b6a74cc7d12f5b490d0be
5
5
  SHA512:
6
- metadata.gz: e23390eaa06a588135456d1e4f826d04526301a3512acb4f34efc911d97a93f126129ada2a0cd312f8374333106402d10aa7f357417f370ddb0dd5cf2b6ce224
7
- data.tar.gz: 36d08044e2022d89dcea7f6e589167c6f61bd0a9e24010715e03dba117835d6f4c652c52feb139f602317618aed7a267a8f2a0c95147194a518051c99c24daa5
6
+ metadata.gz: 2127235feecf70da3458afd0cf70a916d5f04e0d7bd659f731ec8f18b8d6438ec4f9c81385ba055029825cd4db28df940db4447cff20ca767b9ebe526753e595
7
+ data.tar.gz: 0d8247f8a1cd7a2f9cb260c2dde98abdf7e7d23c6616209e8d06f7245c0eeeb8c17a289d006ae9bf0c5c674f91292c5014b001dfa85d18c92ce85bfc1907bf12
data/Gemfile CHANGED
@@ -4,6 +4,6 @@ gemspec
4
4
 
5
5
  group :development do
6
6
  gem 'rake-compiler', ">= 0.4.1"
7
- gem 'minitest', "~> 5.0"
7
+ gem 'test-unit'
8
8
  gem 'ruby-maven', :platforms => :jruby
9
9
  end
data/Rakefile CHANGED
@@ -3,8 +3,8 @@ Bundler::GemHelper.install_tasks
3
3
 
4
4
  require "rake/testtask"
5
5
  Rake::TestTask.new(:test) do |t|
6
- t.libs << "test"
7
- t.libs << "lib"
6
+ t.libs << "test/lib" << "test"
7
+ t.ruby_opts << "-rhelper"
8
8
  t.test_files = FileList['test/**/test_*.rb']
9
9
  t.verbose = true
10
10
  t.warning = true
@@ -31,4 +31,11 @@ else
31
31
  Rake::ExtensionTask.new("psych")
32
32
  end
33
33
 
34
+ task :sync_tool do
35
+ require 'fileutils'
36
+ FileUtils.cp "../ruby/tool/lib/test/unit/core_assertions.rb", "./test/lib"
37
+ FileUtils.cp "../ruby/tool/lib/envutil.rb", "./test/lib"
38
+ FileUtils.cp "../ruby/tool/lib/find_executable.rb", "./test/lib"
39
+ end
40
+
34
41
  task :default => [:compile, :test]
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);
@@ -541,4 +541,4 @@ yaml_parser_load_mapping_end(yaml_parser_t *parser, yaml_event_t *event,
541
541
  (void)POP(parser, *ctx);
542
542
 
543
543
  return 1;
544
- }
544
+ }
@@ -273,7 +273,7 @@
273
273
  * The tokens BLOCK-SEQUENCE-START and BLOCK-MAPPING-START denote indentation
274
274
  * increase that precedes a block collection (cf. the INDENT token in Python).
275
275
  * The token BLOCK-END denote indentation decrease that ends a block collection
276
- * (cf. the DEDENT token in Python). However YAML has some syntax pecularities
276
+ * (cf. the DEDENT token in Python). However YAML has some syntax peculiarities
277
277
  * that makes detections of these tokens more complex.
278
278
  *
279
279
  * The tokens BLOCK-ENTRY, KEY, and VALUE are used to represent the indicators
@@ -3287,7 +3287,7 @@ yaml_parser_scan_flow_scalar(yaml_parser_t *parser, yaml_token_t *token,
3287
3287
 
3288
3288
  /* Check if we are at the end of the scalar. */
3289
3289
 
3290
- /* Fix for crash unitialized value crash
3290
+ /* Fix for crash uninitialized value crash
3291
3291
  * Credit for the bug and input is to OSS Fuzz
3292
3292
  * Credit for the fix to Alex Gaynor
3293
3293
  */
@@ -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. */
@@ -1095,7 +1095,7 @@ typedef struct yaml_parser_s {
1095
1095
  yaml_error_type_t error;
1096
1096
  /** Error description. */
1097
1097
  const char *problem;
1098
- /** The byte about which the problem occured. */
1098
+ /** The byte about which the problem occurred. */
1099
1099
  size_t problem_offset;
1100
1100
  /** The problematic value (@c -1 is none). */
1101
1101
  int problem_value;
@@ -1335,7 +1335,7 @@ yaml_parser_delete(yaml_parser_t *parser);
1335
1335
  * Set a string input.
1336
1336
  *
1337
1337
  * Note that the @a input pointer must be valid while the @a parser object
1338
- * exists. The application is responsible for destroing @a input after
1338
+ * exists. The application is responsible for destroying @a input after
1339
1339
  * destroying the @a parser.
1340
1340
  *
1341
1341
  * @param[in,out] parser A parser object.
@@ -1734,7 +1734,7 @@ typedef struct yaml_emitter_s {
1734
1734
  size_t length;
1735
1735
  /** Does the scalar contain line breaks? */
1736
1736
  int multiline;
1737
- /** Can the scalar be expessed in the flow plain style? */
1737
+ /** Can the scalar be expressed in the flow plain style? */
1738
1738
  int flow_plain_allowed;
1739
1739
  /** Can the scalar be expressed in the block plain style? */
1740
1740
  int block_plain_allowed;
@@ -1950,7 +1950,7 @@ yaml_emitter_close(yaml_emitter_t *emitter);
1950
1950
  /**
1951
1951
  * Emit a YAML document.
1952
1952
  *
1953
- * The documen object may be generated using the yaml_parser_load() function
1953
+ * The document object may be generated using the yaml_parser_load() function
1954
1954
  * or the yaml_document_initialize() function. The emitter takes the
1955
1955
  * responsibility for the document object and destroys its content after
1956
1956
  * it is emitted. The document object is destroyed even if the function fails.
@@ -2,7 +2,7 @@
2
2
  #include RUBY_EXTCONF_H
3
3
  #endif
4
4
 
5
- #if HAVE_CONFIG_H
5
+ #ifdef HAVE_CONFIG_H
6
6
  #include "config.h"
7
7
  #endif
8
8
 
data/lib/psych.rb CHANGED
@@ -233,10 +233,7 @@ require 'psych/class_loader'
233
233
 
234
234
  module Psych
235
235
  # The version of libyaml Psych is using
236
- LIBYAML_VERSION = Psych.libyaml_version.join '.'
237
- # Deprecation guard
238
- NOT_GIVEN = Object.new
239
- private_constant :NOT_GIVEN
236
+ LIBYAML_VERSION = Psych.libyaml_version.join('.').freeze
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,25 +259,21 @@ 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.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
280
272
  result = parse(yaml, filename: filename)
281
273
  return fallback unless result
282
274
  result.to_ruby(symbolize_names: symbolize_names, freeze: freeze)
283
275
  end
276
+ class << self; alias :load :unsafe_load; end
284
277
 
285
278
  ###
286
279
  # Safely load the yaml string in +yaml+. By default, only the following
@@ -289,7 +282,8 @@ module Psych
289
282
  # * TrueClass
290
283
  # * FalseClass
291
284
  # * NilClass
292
- # * Numeric
285
+ # * Integer
286
+ # * Float
293
287
  # * String
294
288
  # * Array
295
289
  # * Hash
@@ -326,27 +320,7 @@ module Psych
326
320
  # Psych.safe_load("---\n foo: bar") # => {"foo"=>"bar"}
327
321
  # Psych.safe_load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
328
322
  #
329
- 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
330
- if legacy_permitted_classes != NOT_GIVEN
331
- 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
332
- permitted_classes = legacy_permitted_classes
333
- end
334
-
335
- if legacy_permitted_symbols != NOT_GIVEN
336
- 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
337
- permitted_symbols = legacy_permitted_symbols
338
- end
339
-
340
- if legacy_aliases != NOT_GIVEN
341
- 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
342
- aliases = legacy_aliases
343
- end
344
-
345
- if legacy_filename != NOT_GIVEN
346
- 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
347
- filename = legacy_filename
348
- end
349
-
323
+ def self.safe_load yaml, permitted_classes: [], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false
350
324
  result = parse(yaml, filename: filename)
351
325
  return fallback unless result
352
326
 
@@ -362,6 +336,46 @@ module Psych
362
336
  result
363
337
  end
364
338
 
339
+ ###
340
+ # Load +yaml+ in to a Ruby data structure. If multiple documents are
341
+ # provided, the object contained in the first document will be returned.
342
+ # +filename+ will be used in the exception message if any exception
343
+ # is raised while parsing. If +yaml+ is empty, it returns
344
+ # the specified +fallback+ return value, which defaults to +false+.
345
+ #
346
+ # Raises a Psych::SyntaxError when a YAML syntax error is detected.
347
+ #
348
+ # Example:
349
+ #
350
+ # Psych.load("--- a") # => 'a'
351
+ # Psych.load("---\n - a\n - b") # => ['a', 'b']
352
+ #
353
+ # begin
354
+ # Psych.load("--- `", filename: "file.txt")
355
+ # rescue Psych::SyntaxError => ex
356
+ # ex.file # => 'file.txt'
357
+ # ex.message # => "(file.txt): found character that cannot start any token"
358
+ # end
359
+ #
360
+ # When the optional +symbolize_names+ keyword argument is set to a
361
+ # true value, returns symbols for keys in Hash objects (default: strings).
362
+ #
363
+ # Psych.load("---\n foo: bar") # => {"foo"=>"bar"}
364
+ # Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
365
+ #
366
+ # Raises a TypeError when `yaml` parameter is NilClass. This method is
367
+ # similar to `safe_load` except that `Symbol` objects are allowed by default.
368
+ #
369
+ def self.load yaml, permitted_classes: [Symbol], permitted_symbols: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false
370
+ safe_load yaml, permitted_classes: permitted_classes,
371
+ permitted_symbols: permitted_symbols,
372
+ aliases: aliases,
373
+ filename: filename,
374
+ fallback: fallback,
375
+ symbolize_names: symbolize_names,
376
+ freeze: freeze
377
+ end
378
+
365
379
  ###
366
380
  # Parse a YAML string in +yaml+. Returns the Psych::Nodes::Document.
367
381
  # +filename+ is used in the exception message if a Psych::SyntaxError is
@@ -381,22 +395,12 @@ module Psych
381
395
  # end
382
396
  #
383
397
  # See Psych::Nodes for more information about YAML AST.
384
- def self.parse yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: NOT_GIVEN
385
- if legacy_filename != NOT_GIVEN
386
- 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
387
- filename = legacy_filename
388
- end
389
-
398
+ def self.parse yaml, filename: nil
390
399
  parse_stream(yaml, filename: filename) do |node|
391
400
  return node
392
401
  end
393
402
 
394
- if fallback != NOT_GIVEN
395
- warn_with_uplevel 'Passing the `fallback` keyword argument of Psych.parse is deprecated.', uplevel: 1 if $VERBOSE
396
- fallback
397
- else
398
- false
399
- end
403
+ false
400
404
  end
401
405
 
402
406
  ###
@@ -445,12 +449,7 @@ module Psych
445
449
  # Raises a TypeError when NilClass is passed.
446
450
  #
447
451
  # See Psych::Nodes for more information about YAML AST.
448
- def self.parse_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, &block
449
- if legacy_filename != NOT_GIVEN
450
- 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
451
- filename = legacy_filename
452
- end
453
-
452
+ def self.parse_stream yaml, filename: nil, &block
454
453
  if block_given?
455
454
  parser = Psych::Parser.new(Handlers::DocumentStream.new(&block))
456
455
  parser.parse yaml, filename
@@ -514,6 +513,79 @@ module Psych
514
513
  visitor.tree.yaml io, options
515
514
  end
516
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
+
517
589
  ###
518
590
  # Dump a list of objects as separate documents to a document stream.
519
591
  #
@@ -551,12 +623,7 @@ module Psych
551
623
  # end
552
624
  # list # => ['foo', 'bar']
553
625
  #
554
- def self.load_stream yaml, legacy_filename = NOT_GIVEN, filename: nil, fallback: [], **kwargs
555
- if legacy_filename != NOT_GIVEN
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
557
- filename = legacy_filename
558
- end
559
-
626
+ def self.load_stream yaml, filename: nil, fallback: [], **kwargs
560
627
  result = if block_given?
561
628
  parse_stream(yaml, filename: filename) do |node|
562
629
  yield node.to_ruby(**kwargs)
@@ -577,9 +644,9 @@ module Psych
577
644
  # NOTE: This method *should not* be used to parse untrusted documents, such as
578
645
  # YAML documents that are supplied via user input. Instead, please use the
579
646
  # safe_load_file method.
580
- def self.load_file filename, **kwargs
647
+ def self.unsafe_load_file filename, **kwargs
581
648
  File.open(filename, 'r:bom|utf-8') { |f|
582
- self.load f, filename: filename, **kwargs
649
+ self.unsafe_load f, filename: filename, **kwargs
583
650
  }
584
651
  end
585
652
 
@@ -594,29 +661,37 @@ module Psych
594
661
  }
595
662
  end
596
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
+
597
675
  # :stopdoc:
598
- @domain_types = {}
599
676
  def self.add_domain_type domain, type_tag, &block
600
677
  key = ['tag', domain, type_tag].join ':'
601
- @domain_types[key] = [key, block]
602
- @domain_types["tag:#{type_tag}"] = [key, block]
678
+ domain_types[key] = [key, block]
679
+ domain_types["tag:#{type_tag}"] = [key, block]
603
680
  end
604
681
 
605
682
  def self.add_builtin_type type_tag, &block
606
683
  domain = 'yaml.org,2002'
607
684
  key = ['tag', domain, type_tag].join ':'
608
- @domain_types[key] = [key, block]
685
+ domain_types[key] = [key, block]
609
686
  end
610
687
 
611
688
  def self.remove_type type_tag
612
- @domain_types.delete type_tag
689
+ domain_types.delete type_tag
613
690
  end
614
691
 
615
- @load_tags = {}
616
- @dump_tags = {}
617
692
  def self.add_tag tag, klass
618
- @load_tags[tag] = klass.name
619
- @dump_tags[klass] = tag
693
+ load_tags[tag] = klass.name
694
+ dump_tags[klass] = tag
620
695
  end
621
696
 
622
697
  # Workaround for emulating `warn '...', uplevel: 1` in Ruby 2.4 or lower.
@@ -635,9 +710,32 @@ module Psych
635
710
  private_class_method :warn_with_uplevel, :parse_caller
636
711
 
637
712
  class << self
638
- attr_accessor :load_tags
639
- attr_accessor :dump_tags
640
- attr_accessor :domain_types
713
+ if defined?(Ractor)
714
+ require 'forwardable'
715
+ extend Forwardable
716
+
717
+ class Config
718
+ attr_accessor :load_tags, :dump_tags, :domain_types
719
+ def initialize
720
+ @load_tags = {}
721
+ @dump_tags = {}
722
+ @domain_types = {}
723
+ end
724
+ end
725
+
726
+ def config
727
+ Ractor.current[:PsychConfig] ||= Config.new
728
+ end
729
+
730
+ def_delegators :config, :load_tags, :dump_tags, :domain_types, :load_tags=, :dump_tags=, :domain_types=
731
+ else
732
+ attr_accessor :load_tags
733
+ attr_accessor :dump_tags
734
+ attr_accessor :domain_types
735
+ end
641
736
  end
737
+ self.load_tags = {}
738
+ self.dump_tags = {}
739
+ self.domain_types = {}
642
740
  # :startdoc:
643
741
  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
@@ -84,7 +86,7 @@ module Psych
84
86
  if @symbols.include? sym
85
87
  super
86
88
  else
87
- raise DisallowedClass, 'Symbol'
89
+ raise DisallowedClass.new('load', 'Symbol')
88
90
  end
89
91
  end
90
92
 
@@ -94,7 +96,7 @@ module Psych
94
96
  if @classes.include? klassname
95
97
  super
96
98
  else
97
- raise DisallowedClass, klassname
99
+ raise DisallowedClass.new('load', klassname)
98
100
  end
99
101
  end
100
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
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+
@@ -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
  #
@@ -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.2.1'
5
+ VERSION = '4.0.1'
6
6
 
7
7
  if RUBY_ENGINE == 'jruby'
8
- DEFAULT_SNAKEYAML_VERSION = '1.26'.freeze
8
+ DEFAULT_SNAKEYAML_VERSION = '1.28'.freeze
9
9
  end
10
10
  end
@@ -24,6 +24,7 @@ module Psych
24
24
  super()
25
25
  @st = {}
26
26
  @ss = ss
27
+ @load_tags = Psych.load_tags
27
28
  @domain_types = Psych.domain_types
28
29
  @class_loader = class_loader
29
30
  @symbolize_names = symbolize_names
@@ -48,7 +49,7 @@ module Psych
48
49
  end
49
50
 
50
51
  def deserialize o
51
- if klass = resolve_class(Psych.load_tags[o.tag])
52
+ if klass = resolve_class(@load_tags[o.tag])
52
53
  instance = klass.allocate
53
54
 
54
55
  if instance.respond_to?(:init_with)
@@ -128,7 +129,7 @@ module Psych
128
129
  end
129
130
 
130
131
  def visit_Psych_Nodes_Sequence o
131
- if klass = resolve_class(Psych.load_tags[o.tag])
132
+ if klass = resolve_class(@load_tags[o.tag])
132
133
  instance = klass.allocate
133
134
 
134
135
  if instance.respond_to?(:init_with)
@@ -160,8 +161,8 @@ module Psych
160
161
  end
161
162
 
162
163
  def visit_Psych_Nodes_Mapping o
163
- if Psych.load_tags[o.tag]
164
- return revive(resolve_class(Psych.load_tags[o.tag]), o)
164
+ if @load_tags[o.tag]
165
+ return revive(resolve_class(@load_tags[o.tag]), o)
165
166
  end
166
167
  return revive_hash(register(o, {}), o) unless o.tag
167
168
 
@@ -326,6 +327,7 @@ module Psych
326
327
  end
327
328
 
328
329
  private
330
+
329
331
  def register node, object
330
332
  @st[node.anchor] = object if node.anchor
331
333
  object
@@ -337,7 +339,7 @@ module Psych
337
339
  list
338
340
  end
339
341
 
340
- def revive_hash hash, o
342
+ def revive_hash hash, o, tagged= false
341
343
  o.children.each_slice(2) { |k,v|
342
344
  key = accept(k)
343
345
  val = accept(v)
@@ -364,7 +366,7 @@ module Psych
364
366
  hash[key] = val
365
367
  end
366
368
  else
367
- if @symbolize_names
369
+ if !tagged && @symbolize_names && key.is_a?(String)
368
370
  key = key.to_sym
369
371
  elsif !@freeze
370
372
  key = deduplicate(key)
@@ -402,7 +404,7 @@ module Psych
402
404
 
403
405
  def revive klass, node
404
406
  s = register(node, klass.allocate)
405
- init_with(s, revive_hash({}, node), node)
407
+ init_with(s, revive_hash({}, node, true), node)
406
408
  end
407
409
 
408
410
  def init_with o, h, node
@@ -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
+ @dispatch_cache ||= (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
@@ -509,9 +509,9 @@ module Psych
509
509
  def emit_coder c, o
510
510
  case c.type
511
511
  when :scalar
512
- @emitter.scalar c.scalar, nil, c.tag, c.tag.nil?, false, Nodes::Scalar::ANY
512
+ @emitter.scalar c.scalar, nil, c.tag, c.tag.nil?, false, c.style
513
513
  when :seq
514
- @emitter.start_sequence nil, c.tag, c.tag.nil?, Nodes::Sequence::BLOCK
514
+ @emitter.start_sequence nil, c.tag, c.tag.nil?, c.style
515
515
  c.seq.each do |thing|
516
516
  accept thing
517
517
  end
@@ -535,5 +535,51 @@ module Psych
535
535
  end
536
536
  end
537
537
  end
538
+
539
+ class RestrictedYAMLTree < YAMLTree
540
+ DEFAULT_PERMITTED_CLASSES = {
541
+ TrueClass => true,
542
+ FalseClass => true,
543
+ NilClass => true,
544
+ Integer => true,
545
+ Float => true,
546
+ String => true,
547
+ Array => true,
548
+ Hash => true,
549
+ }.compare_by_identity.freeze
550
+
551
+ def initialize emitter, ss, options
552
+ super
553
+ @permitted_classes = DEFAULT_PERMITTED_CLASSES.dup
554
+ Array(options[:permitted_classes]).each do |klass|
555
+ @permitted_classes[klass] = true
556
+ end
557
+ @permitted_symbols = {}.compare_by_identity
558
+ Array(options[:permitted_symbols]).each do |symbol|
559
+ @permitted_symbols[symbol] = true
560
+ end
561
+ @aliases = options.fetch(:aliases, false)
562
+ end
563
+
564
+ def accept target
565
+ if !@aliases && @st.key?(target)
566
+ raise BadAlias, "Tried to dump an aliased object"
567
+ end
568
+
569
+ unless @permitted_classes[target.class]
570
+ raise DisallowedClass.new('dump', target.class.name || target.class.inspect)
571
+ end
572
+
573
+ super
574
+ end
575
+
576
+ def visit_Symbol sym
577
+ unless @permitted_symbols[sym]
578
+ raise DisallowedClass.new('dump', "Symbol(#{sym.inspect})")
579
+ end
580
+
581
+ super
582
+ end
583
+ end
538
584
  end
539
585
  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.2.1
4
+ version: 4.0.1
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-12-14 00:00:00.000000000 Z
13
+ date: 2021-06-07 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
120
+ rubygems_version: 3.2.15
121
121
  signing_key:
122
122
  specification_version: 4
123
123
  summary: Psych is a YAML parser and emitter