Skip to content

Commit b4655dd

Browse files
hsbtclaude
andcommitted
Use YAMLSerializer in Specification with Psych fallback
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d81ae0a commit b4655dd

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

lib/rubygems/safe_marshal.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module SafeMarshal
5454
"Gem::NameTuple" => %w[@name @version @platform],
5555
"Gem::Platform" => %w[@os @cpu @version],
5656
"Psych::PrivateType" => %w[@value @type_id],
57+
"YAML::PrivateType" => %w[@value @type_id],
5758
}.freeze
5859
private_constant :PERMITTED_IVARS
5960

lib/rubygems/specification.rb

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ def self._load(str)
12751275
raise unless message.include?("YAML::")
12761276

12771277
unless Object.const_defined?(:YAML)
1278-
Object.const_set "YAML", Psych
1278+
Object.const_set "YAML", Module.new
12791279
yaml_set = true
12801280
end
12811281

@@ -1284,7 +1284,7 @@ def self._load(str)
12841284

12851285
YAML::Syck.const_set "DefaultKey", Class.new if message.include?("YAML::Syck::DefaultKey") && !YAML::Syck.const_defined?(:DefaultKey)
12861286
elsif message.include?("YAML::PrivateType") && !YAML.const_defined?(:PrivateType)
1287-
YAML.const_set "PrivateType", Class.new
1287+
YAML.const_set "PrivateType", Class.new { attr_accessor :type_id, :value }
12881288
end
12891289

12901290
retry_count += 1
@@ -2455,24 +2455,28 @@ def to_spec
24552455
def to_yaml(opts = {}) # :nodoc:
24562456
Gem.load_yaml
24572457

2458-
# Because the user can switch the YAML engine behind our
2459-
# back, we have to check again here to make sure that our
2460-
# psych code was properly loaded, and load it if not.
2461-
unless Gem.const_defined?(:NoAliasYAMLTree)
2462-
require_relative "psych_tree"
2463-
end
2458+
if Gem.use_psych?
2459+
# Because the user can switch the YAML engine behind our
2460+
# back, we have to check again here to make sure that our
2461+
# psych code was properly loaded, and load it if not.
2462+
unless Gem.const_defined?(:NoAliasYAMLTree)
2463+
require_relative "psych_tree"
2464+
end
24642465

2465-
builder = Gem::NoAliasYAMLTree.create
2466-
builder << self
2467-
ast = builder.tree
2466+
builder = Gem::NoAliasYAMLTree.create
2467+
builder << self
2468+
ast = builder.tree
24682469

2469-
require "stringio"
2470-
io = StringIO.new
2471-
io.set_encoding Encoding::UTF_8
2470+
require "stringio"
2471+
io = StringIO.new
2472+
io.set_encoding Encoding::UTF_8
24722473

2473-
Psych::Visitors::Emitter.new(io).accept(ast)
2474+
Psych::Visitors::Emitter.new(io).accept(ast)
24742475

2475-
io.string.gsub(/ !!null \n/, " \n")
2476+
io.string.gsub(/ !!null \n/, " \n")
2477+
else
2478+
Gem::YAMLSerializer.dump(self)
2479+
end
24762480
end
24772481

24782482
##

0 commit comments

Comments
 (0)