psych 5.2.3 → 5.2.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8c48b70d7c4ff53b1d536997a6e63173cb7e123e3f6e70f82c5de8596d93c4c
4
- data.tar.gz: c49d610314f1628a1e84267010dcfac5a076276eac2cad8dbbba9079ede9e08f
3
+ metadata.gz: b96aafb2908bc47d791319d79b9dbf2ae5fa68001abf115afbe6366bcf27597e
4
+ data.tar.gz: c9d309b97bdc0355bbe43b9bee36baa06f8562ada6d906e1e5706606cb077a30
5
5
  SHA512:
6
- metadata.gz: 2d3d710995bfd6eb317994dd13024e5e3fd2967dbd379a1d510f86f7517177d2cc5e292a7d3b14c999071cc35ce625715ee3549fab90ce966567ba6e91f2475c
7
- data.tar.gz: f1570e565d5dd8c9e5984155dfbc1418a594aaa871fab7b7fabe62f05bc0a3e72ab141dd9f39c09689f8f4949a5c6e58fa9b1ca99c26db3552b0c9866a6032ad
6
+ metadata.gz: '0988012dbedddbf185d5760497e3bf5b283aa0cf57967b67104c1719fe260371139200ee826e6ee78d6b18f5dc9f3a9f5c76f5d4201db4e15e45ae1deee44082'
7
+ data.tar.gz: 6d2ed9e723c945078cf75aa7862d833086e9161336379d5a636da385955c8106e147f68a4bf76ee590cea4be9ed48e1b2ba9a08f02ff2aac6c04f113abce4ae9
@@ -24,6 +24,15 @@ static VALUE path2class(VALUE self, VALUE path)
24
24
  return rb_path_to_class(path);
25
25
  }
26
26
 
27
+ static VALUE init_struct(VALUE self, VALUE data, VALUE attrs)
28
+ {
29
+ VALUE args = rb_ary_new2(1);
30
+ rb_ary_push(args, attrs);
31
+ rb_struct_initialize(data, args);
32
+
33
+ return data;
34
+ }
35
+
27
36
  void Init_psych_to_ruby(void)
28
37
  {
29
38
  VALUE psych = rb_define_module("Psych");
@@ -33,6 +42,7 @@ void Init_psych_to_ruby(void)
33
42
  VALUE visitor = rb_define_class_under(visitors, "Visitor", rb_cObject);
34
43
  cPsychVisitorsToRuby = rb_define_class_under(visitors, "ToRuby", visitor);
35
44
 
45
+ rb_define_private_method(cPsychVisitorsToRuby, "init_struct", init_struct, 2);
36
46
  rb_define_private_method(cPsychVisitorsToRuby, "build_exception", build_exception, 2);
37
47
  rb_define_private_method(class_loader, "path2class", path2class, 1);
38
48
  }
@@ -6,6 +6,7 @@ module Psych
6
6
  class ClassLoader # :nodoc:
7
7
  BIG_DECIMAL = 'BigDecimal'
8
8
  COMPLEX = 'Complex'
9
+ DATA = 'Data' unless RUBY_VERSION < "3.2"
9
10
  DATE = 'Date'
10
11
  DATE_TIME = 'DateTime'
11
12
  EXCEPTION = 'Exception'
@@ -17,3 +17,24 @@ end
17
17
  if defined?(::IRB)
18
18
  require_relative 'y'
19
19
  end
20
+
21
+ # Up to Ruby 3.4, Set was a regular object and was dumped as such
22
+ # by Pysch.
23
+ # Starting from Ruby 3.5 it's a core class written in C, so we have to implement
24
+ # #encode_with / #init_with to preserve backward compatibility.
25
+ if defined?(::Set) && Set.new.instance_variables.empty?
26
+ class Set
27
+ def encode_with(coder)
28
+ hash = {}
29
+ each do |m|
30
+ hash[m] = true
31
+ end
32
+ coder["hash"] = hash
33
+ coder
34
+ end
35
+
36
+ def init_with(coder)
37
+ replace(coder["hash"].keys)
38
+ end
39
+ end
40
+ end
@@ -55,7 +55,8 @@ module Psych
55
55
  #
56
56
  # See also Psych::Visitors::Emitter
57
57
  def yaml io = nil, options = {}
58
- require "stringio"
58
+ require "stringio" unless defined?(StringIO)
59
+
59
60
  real_io = io || StringIO.new(''.encode('utf-8'))
60
61
 
61
62
  Visitors::Emitter.new(real_io, options).accept self
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Psych
4
4
  # The version of Psych you are using
5
- VERSION = '5.2.3'
5
+ VERSION = '5.2.5'
6
6
 
7
7
  if RUBY_ENGINE == 'jruby'
8
8
  DEFAULT_SNAKEYAML_VERSION = '2.9'.freeze
@@ -96,11 +96,11 @@ module Psych
96
96
  Float(@ss.tokenize(o.value))
97
97
  when "!ruby/regexp"
98
98
  klass = class_loader.regexp
99
- o.value =~ /^\/(.*)\/([mixn]*)$/m
100
- source = $1
99
+ matches = /^\/(?<string>.*)\/(?<options>[mixn]*)$/m.match(o.value)
100
+ source = matches[:string].gsub('\/', '/')
101
101
  options = 0
102
102
  lang = nil
103
- $2&.each_char do |option|
103
+ matches[:options].each_char do |option|
104
104
  case option
105
105
  when 'x' then options |= Regexp::EXTENDED
106
106
  when 'i' then options |= Regexp::IGNORECASE
@@ -197,6 +197,32 @@ module Psych
197
197
  s
198
198
  end
199
199
 
200
+ when /^!ruby\/data(-with-ivars)?(?::(.*))?$/
201
+ data = register(o, resolve_class($2).allocate) if $2
202
+ members = {}
203
+
204
+ if $1 # data-with-ivars
205
+ ivars = {}
206
+ o.children.each_slice(2) do |type, vars|
207
+ case accept(type)
208
+ when 'members'
209
+ revive_data_members(members, vars)
210
+ data ||= allocate_anon_data(o, members)
211
+ when 'ivars'
212
+ revive_hash(ivars, vars)
213
+ end
214
+ end
215
+ ivars.each do |ivar, v|
216
+ data.instance_variable_set ivar, v
217
+ end
218
+ else
219
+ revive_data_members(members, o)
220
+ end
221
+ data ||= allocate_anon_data(o, members)
222
+ init_struct(data, **members)
223
+ data.freeze
224
+ data
225
+
200
226
  when /^!ruby\/object:?(.*)?$/
201
227
  name = $1 || 'Object'
202
228
 
@@ -340,6 +366,20 @@ module Psych
340
366
  list
341
367
  end
342
368
 
369
+ def allocate_anon_data node, members
370
+ klass = class_loader.data.define(*members.keys)
371
+ register(node, klass.allocate)
372
+ end
373
+
374
+ def revive_data_members hash, o
375
+ o.children.each_slice(2) do |k,v|
376
+ name = accept(k)
377
+ value = accept(v)
378
+ hash[class_loader.symbolize(name)] = value
379
+ end
380
+ hash
381
+ end
382
+
343
383
  def revive_hash hash, o, tagged= false
344
384
  o.children.each_slice(2) { |k,v|
345
385
  key = accept(k)
@@ -73,7 +73,7 @@ module Psych
73
73
 
74
74
  method = respond_to?(method) ? method : h[klass.superclass]
75
75
 
76
- raise(TypeError, "Can't dump #{target.class}") unless method
76
+ raise(TypeError, "can't dump #{klass.name}") unless method
77
77
 
78
78
  h[klass] = method
79
79
  end.compare_by_identity
@@ -162,6 +162,44 @@ module Psych
162
162
 
163
163
  alias :visit_Delegator :visit_Object
164
164
 
165
+ def visit_Data o
166
+ ivars = o.instance_variables
167
+ if ivars.empty?
168
+ tag = ['!ruby/data', o.class.name].compact.join(':')
169
+ register o, @emitter.start_mapping(nil, tag, false, Nodes::Mapping::BLOCK)
170
+ o.members.each do |member|
171
+ @emitter.scalar member.to_s, nil, nil, true, false, Nodes::Scalar::ANY
172
+ accept o.send member
173
+ end
174
+ @emitter.end_mapping
175
+
176
+ else
177
+ tag = ['!ruby/data-with-ivars', o.class.name].compact.join(':')
178
+ node = @emitter.start_mapping(nil, tag, false, Psych::Nodes::Mapping::BLOCK)
179
+ register(o, node)
180
+
181
+ # Dump the members
182
+ accept 'members'
183
+ @emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
184
+ o.members.each do |member|
185
+ @emitter.scalar member.to_s, nil, nil, true, false, Nodes::Scalar::ANY
186
+ accept o.send member
187
+ end
188
+ @emitter.end_mapping
189
+
190
+ # Dump the ivars
191
+ accept 'ivars'
192
+ @emitter.start_mapping nil, nil, true, Nodes::Mapping::BLOCK
193
+ ivars.each do |ivar|
194
+ accept ivar.to_s
195
+ accept o.instance_variable_get ivar
196
+ end
197
+ @emitter.end_mapping
198
+
199
+ @emitter.end_mapping
200
+ end
201
+ end
202
+
165
203
  def visit_Struct o
166
204
  tag = ['!ruby/struct', o.class.name].compact.join(':')
167
205
 
@@ -189,7 +227,8 @@ module Psych
189
227
  end
190
228
 
191
229
  def visit_Date o
192
- register o, visit_Integer(o.gregorian)
230
+ formatted = format_date o
231
+ register o, @emitter.scalar(formatted, nil, nil, true, false, Nodes::Scalar::ANY)
193
232
  end
194
233
 
195
234
  def visit_DateTime o
@@ -486,6 +525,10 @@ module Psych
486
525
  end
487
526
  end
488
527
 
528
+ def format_date date
529
+ date.strftime("%Y-%m-%d")
530
+ end
531
+
489
532
  def register target, yaml_obj
490
533
  @st.register target, yaml_obj
491
534
  yaml_obj
data/lib/psych.rb CHANGED
@@ -23,7 +23,6 @@ require_relative 'psych/parser'
23
23
  require_relative 'psych/omap'
24
24
  require_relative 'psych/set'
25
25
  require_relative 'psych/coder'
26
- require_relative 'psych/core_ext'
27
26
  require_relative 'psych/stream'
28
27
  require_relative 'psych/json/tree_builder'
29
28
  require_relative 'psych/json/stream'
@@ -654,6 +653,35 @@ module Psych
654
653
  result
655
654
  end
656
655
 
656
+ ###
657
+ # Load multiple documents given in +yaml+. Returns the parsed documents
658
+ # as a list.
659
+ #
660
+ # Example:
661
+ #
662
+ # Psych.safe_load_stream("--- foo\n...\n--- bar\n...") # => ['foo', 'bar']
663
+ #
664
+ # list = []
665
+ # Psych.safe_load_stream("--- foo\n...\n--- bar\n...") do |ruby|
666
+ # list << ruby
667
+ # end
668
+ # list # => ['foo', 'bar']
669
+ #
670
+ def self.safe_load_stream yaml, filename: nil, permitted_classes: [], aliases: false
671
+ documents = parse_stream(yaml, filename: filename).children.map do |child|
672
+ stream = Psych::Nodes::Stream.new
673
+ stream.children << child
674
+ safe_load(stream.to_yaml, permitted_classes: permitted_classes, aliases: aliases)
675
+ end
676
+
677
+ if block_given?
678
+ documents.each { |doc| yield doc }
679
+ nil
680
+ else
681
+ documents
682
+ end
683
+ end
684
+
657
685
  ###
658
686
  # Load the document contained in +filename+. Returns the yaml contained in
659
687
  # +filename+ as a Ruby object, or if the file is empty, it returns
@@ -761,3 +789,5 @@ module Psych
761
789
  self.domain_types = {}
762
790
  # :startdoc:
763
791
  end
792
+
793
+ require_relative 'psych/core_ext'
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.2.3
4
+ version: 5.2.5
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: 2025-01-17 00:00:00.000000000 Z
13
+ date: 2025-05-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: stringio
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
131
  requirements: []
132
- rubygems_version: 3.5.11
132
+ rubygems_version: 3.5.22
133
133
  signing_key:
134
134
  specification_version: 4
135
135
  summary: Psych is a YAML parser and emitter