psych 5.1.2 → 5.2.4

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.
@@ -11,18 +11,18 @@ module Psych
11
11
  # Base 60, [-+]inf and NaN are handled separately
12
12
  FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10))$/x
13
13
 
14
- # Taken from http://yaml.org/type/int.html
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
14
+ # Taken from http://yaml.org/type/int.html and modified to ensure at least one numerical symbol exists
15
+ INTEGER_STRICT = /^(?:[-+]?0b[_]*[0-1][0-1_]* (?# base 2)
16
+ |[-+]?0[_]*[0-7][0-7_]* (?# base 8)
17
+ |[-+]?(0|[1-9][0-9_]*) (?# base 10)
18
+ |[-+]?0x[_]*[0-9a-fA-F][0-9a-fA-F_]* (?# base 16))$/x
19
19
 
20
20
  # Same as above, but allows commas.
21
21
  # Not to YML spec, but kept for backwards compatibility
22
- INTEGER_LEGACY = /^(?:[-+]?0b[0-1_,]+ (?# base 2)
23
- |[-+]?0[0-7_,]+ (?# base 8)
22
+ INTEGER_LEGACY = /^(?:[-+]?0b[_,]*[0-1][0-1_,]* (?# base 2)
23
+ |[-+]?0[_,]*[0-7][0-7_,]* (?# base 8)
24
24
  |[-+]?(?:0|[1-9](?:[0-9]|,[0-9]|_[0-9])*) (?# base 10)
25
- |[-+]?0x[0-9a-fA-F_,]+ (?# base 16))$/x
25
+ |[-+]?0x[_,]*[0-9a-fA-F][0-9a-fA-F_,]* (?# base 16))$/x
26
26
 
27
27
  attr_reader :class_loader
28
28
 
@@ -61,7 +61,6 @@ module Psych
61
61
  string
62
62
  end
63
63
  elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/)
64
- require 'date'
65
64
  begin
66
65
  class_loader.date.strptime(string, '%F', Date::GREGORIAN)
67
66
  rescue ArgumentError
@@ -2,9 +2,9 @@
2
2
 
3
3
  module Psych
4
4
  # The version of Psych you are using
5
- VERSION = '5.1.2'
5
+ VERSION = '5.2.4'
6
6
 
7
7
  if RUBY_ENGINE == 'jruby'
8
- DEFAULT_SNAKEYAML_VERSION = '2.7'.freeze
8
+ DEFAULT_SNAKEYAML_VERSION = '2.9'.freeze
9
9
  end
10
10
  end
@@ -36,7 +36,7 @@ module Psych
36
36
 
37
37
  unless @domain_types.empty? || !target.tag
38
38
  key = target.tag.sub(/^[!\/]*/, '').sub(/(,\d+)\//, '\1:')
39
- key = "tag:#{key}" unless key =~ /^(?:tag:|x-private)/
39
+ key = "tag:#{key}" unless key.match?(/^(?:tag:|x-private)/)
40
40
 
41
41
  if @domain_types.key? key
42
42
  value, block = @domain_types[key]
@@ -79,7 +79,6 @@ module Psych
79
79
  class_loader.big_decimal._load o.value
80
80
  when "!ruby/object:DateTime"
81
81
  class_loader.date_time
82
- require 'date' unless defined? DateTime
83
82
  t = @ss.parse_time(o.value)
84
83
  DateTime.civil(*t.to_a[0, 6].reverse, Rational(t.utc_offset, 86400)) +
85
84
  (t.subsec/86400)
@@ -17,19 +17,15 @@ module Psych
17
17
  def initialize
18
18
  @obj_to_id = {}.compare_by_identity
19
19
  @obj_to_node = {}.compare_by_identity
20
- @targets = []
21
20
  @counter = 0
22
21
  end
23
22
 
24
23
  def register target, node
25
- @targets << target
26
24
  @obj_to_node[target] = node
27
25
  end
28
26
 
29
27
  def key? target
30
28
  @obj_to_node.key? target
31
- rescue NoMethodError
32
- false
33
29
  end
34
30
 
35
31
  def id_for target
@@ -69,6 +65,7 @@ module Psych
69
65
  fail(ArgumentError, "Invalid line_width #{@line_width}, must be non-negative or -1 for unlimited.")
70
66
  end
71
67
  end
68
+ @stringify_names = options[:stringify_names]
72
69
  @coders = []
73
70
 
74
71
  @dispatch_cache = Hash.new do |h,klass|
@@ -192,7 +189,8 @@ module Psych
192
189
  end
193
190
 
194
191
  def visit_Date o
195
- register o, visit_Integer(o.gregorian)
192
+ formatted = format_date o
193
+ register o, @emitter.scalar(formatted, nil, nil, true, false, Nodes::Scalar::ANY)
196
194
  end
197
195
 
198
196
  def visit_DateTime o
@@ -264,20 +262,20 @@ module Psych
264
262
  style = Nodes::Scalar::LITERAL
265
263
  plain = false
266
264
  quote = false
267
- elsif o =~ /\n(?!\Z)/ # match \n except blank line at the end of string
265
+ elsif o.match?(/\n(?!\Z)/) # match \n except blank line at the end of string
268
266
  style = Nodes::Scalar::LITERAL
269
267
  elsif o == '<<'
270
268
  style = Nodes::Scalar::SINGLE_QUOTED
271
269
  tag = 'tag:yaml.org,2002:str'
272
270
  plain = false
273
271
  quote = false
274
- elsif o == 'y' || o == 'n'
272
+ elsif o == 'y' || o == 'Y' || o == 'n' || o == 'N'
275
273
  style = Nodes::Scalar::DOUBLE_QUOTED
276
274
  elsif @line_width && o.length > @line_width
277
275
  style = Nodes::Scalar::FOLDED
278
- elsif o =~ /^[^[:word:]][^"]*$/
276
+ elsif o.match?(/^[^[:word:]][^"]*$/)
279
277
  style = Nodes::Scalar::DOUBLE_QUOTED
280
- elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/ =~ o
278
+ elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/.match?(o)
281
279
  style = Nodes::Scalar::SINGLE_QUOTED
282
280
  end
283
281
 
@@ -327,7 +325,7 @@ module Psych
327
325
  if o.class == ::Hash
328
326
  register(o, @emitter.start_mapping(nil, nil, true, Psych::Nodes::Mapping::BLOCK))
329
327
  o.each do |k,v|
330
- accept k
328
+ accept(@stringify_names && Symbol === k ? k.to_s : k)
331
329
  accept v
332
330
  end
333
331
  @emitter.end_mapping
@@ -340,7 +338,7 @@ module Psych
340
338
  register(o, @emitter.start_mapping(nil, '!set', false, Psych::Nodes::Mapping::BLOCK))
341
339
 
342
340
  o.each do |k,v|
343
- accept k
341
+ accept(@stringify_names && Symbol === k ? k.to_s : k)
344
342
  accept v
345
343
  end
346
344
 
@@ -489,6 +487,10 @@ module Psych
489
487
  end
490
488
  end
491
489
 
490
+ def format_date date
491
+ date.strftime("%Y-%m-%d")
492
+ end
493
+
492
494
  def register target, yaml_obj
493
495
  @st.register target, yaml_obj
494
496
  yaml_obj
data/lib/psych.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  # frozen_string_literal: true
2
+ require 'date'
3
+
2
4
  require_relative 'psych/versions'
3
5
  case RUBY_ENGINE
4
6
  when 'jruby'
@@ -84,7 +86,7 @@ require_relative 'psych/class_loader'
84
86
  # Psych.safe_load_file("data.yml", permitted_classes: [Date])
85
87
  # Psych.load_file("trusted_database.yml")
86
88
  #
87
- # ==== Exception handling
89
+ # ==== \Exception handling
88
90
  #
89
91
  # begin
90
92
  # # The second argument changes only the exception contents
@@ -148,7 +150,7 @@ require_relative 'psych/class_loader'
148
150
  # # Returns Psych::Nodes::Document
149
151
  # Psych.parse_file('database.yml')
150
152
  #
151
- # ==== Exception handling
153
+ # ==== \Exception handling
152
154
  #
153
155
  # begin
154
156
  # # The second argument changes only the exception contents
@@ -340,7 +342,7 @@ module Psych
340
342
  # provided, the object contained in the first document will be returned.
341
343
  # +filename+ will be used in the exception message if any exception
342
344
  # is raised while parsing. If +yaml+ is empty, it returns
343
- # the specified +fallback+ return value, which defaults to +false+.
345
+ # the specified +fallback+ return value, which defaults to +nil+.
344
346
  #
345
347
  # Raises a Psych::SyntaxError when a YAML syntax error is detected.
346
348
  #
@@ -479,6 +481,7 @@ module Psych
479
481
  #
480
482
  # Default: <tt>2</tt>.
481
483
  # [<tt>:line_width</tt>] Max character to wrap line at.
484
+ # For unlimited line width use <tt>-1</tt>.
482
485
  #
483
486
  # Default: <tt>0</tt> (meaning "wrap at 81").
484
487
  # [<tt>:canonical</tt>] Write "canonical" YAML form (very verbose, yet
@@ -489,6 +492,10 @@ module Psych
489
492
  #
490
493
  # Default: <tt>false</tt>.
491
494
  #
495
+ # [<tt>:stringify_names</tt>] Dump symbol keys in Hash objects as string.
496
+ #
497
+ # Default: <tt>false</tt>.
498
+ #
492
499
  # Example:
493
500
  #
494
501
  # # Dump an array, get back a YAML string
@@ -502,6 +509,9 @@ module Psych
502
509
  #
503
510
  # # Dump an array to an IO with indentation set
504
511
  # Psych.dump(['a', ['b']], StringIO.new, indentation: 3)
512
+ #
513
+ # # Dump hash with symbol keys as string
514
+ # Psych.dump({a: "b"}, stringify_names: true) # => "---\na: b\n"
505
515
  def self.dump o, io = nil, options = {}
506
516
  if Hash === io
507
517
  options = io
@@ -552,6 +562,7 @@ module Psych
552
562
  #
553
563
  # Default: <tt>2</tt>.
554
564
  # [<tt>:line_width</tt>] Max character to wrap line at.
565
+ # For unlimited line width use <tt>-1</tt>.
555
566
  #
556
567
  # Default: <tt>0</tt> (meaning "wrap at 81").
557
568
  # [<tt>:canonical</tt>] Write "canonical" YAML form (very verbose, yet
@@ -562,6 +573,10 @@ module Psych
562
573
  #
563
574
  # Default: <tt>false</tt>.
564
575
  #
576
+ # [<tt>:stringify_names</tt>] Dump symbol keys in Hash objects as string.
577
+ #
578
+ # Default: <tt>false</tt>.
579
+ #
565
580
  # Example:
566
581
  #
567
582
  # # Dump an array, get back a YAML string
@@ -575,6 +590,9 @@ module Psych
575
590
  #
576
591
  # # Dump an array to an IO with indentation set
577
592
  # Psych.safe_dump(['a', ['b']], StringIO.new, indentation: 3)
593
+ #
594
+ # # Dump hash with symbol keys as string
595
+ # Psych.dump({a: "b"}, stringify_names: true) # => "---\na: b\n"
578
596
  def self.safe_dump o, io = nil, options = {}
579
597
  if Hash === io
580
598
  options = io
@@ -653,7 +671,7 @@ module Psych
653
671
  ###
654
672
  # Safely loads the document contained in +filename+. Returns the yaml contained in
655
673
  # +filename+ as a Ruby object, or if the file is empty, it returns
656
- # the specified +fallback+ return value, which defaults to +false+.
674
+ # the specified +fallback+ return value, which defaults to +nil+.
657
675
  # See safe_load for options.
658
676
  def self.safe_load_file filename, **kwargs
659
677
  File.open(filename, 'r:bom|utf-8') { |f|
@@ -664,7 +682,7 @@ module Psych
664
682
  ###
665
683
  # Loads the document contained in +filename+. Returns the yaml contained in
666
684
  # +filename+ as a Ruby object, or if the file is empty, it returns
667
- # the specified +fallback+ return value, which defaults to +false+.
685
+ # the specified +fallback+ return value, which defaults to +nil+.
668
686
  # See load for options.
669
687
  def self.load_file filename, **kwargs
670
688
  File.open(filename, 'r:bom|utf-8') { |f|
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: 5.1.2
4
+ version: 5.2.4
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: 2023-12-19 00:00:00.000000000 Z
13
+ date: 2025-05-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: stringio
@@ -26,6 +26,20 @@ dependencies:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: '0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: date
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
29
43
  description: |
30
44
  Psych is a YAML parser and emitter. Psych leverages libyaml[https://pyyaml.org/wiki/LibYAML]
31
45
  for its YAML parsing and emitting capabilities. In addition to wrapping libyaml,
@@ -97,6 +111,7 @@ licenses:
97
111
  - MIT
98
112
  metadata:
99
113
  msys2_mingw_dependencies: libyaml
114
+ changelog_uri: https://github.com/ruby/psych/releases
100
115
  post_install_message:
101
116
  rdoc_options:
102
117
  - "--main"
@@ -114,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
114
129
  - !ruby/object:Gem::Version
115
130
  version: '0'
116
131
  requirements: []
117
- rubygems_version: 3.5.1
132
+ rubygems_version: 3.5.11
118
133
  signing_key:
119
134
  specification_version: 4
120
135
  summary: Psych is a YAML parser and emitter