diff --git a/lib/rdoc.rb b/lib/rdoc.rb index 043d342..b3d15e9 100644 --- a/lib/rdoc.rb +++ b/lib/rdoc.rb @@ -391,5 +391,15 @@ module RDoc METHOD_MODIFIERS = GENERAL_MODIFIERS + %w[arg args yield yields notnew not-new not_new doc] + def self.yamler # :nodoc: + @yamler ||= + begin + require 'psych' + Psych + rescue LoadError + require 'yaml' + YAML + end + end end diff --git a/lib/rdoc/ri/descriptions.rb b/lib/rdoc/ri/descriptions.rb index 467b7de..529dee6 100644 --- a/lib/rdoc/ri/descriptions.rb +++ b/lib/rdoc/ri/descriptions.rb @@ -1,4 +1,9 @@ -require 'yaml' +begin + require 'psych' +rescue LoadError + require 'yaml' +end + require 'rdoc/markup/fragments' require 'rdoc/ri' @@ -66,7 +71,7 @@ class RDoc::RI::Description end def self.deserialize(from) - YAML.load(from) + RDoc.yamler.load(from) end def <=>(other) diff --git a/lib/rdoc/ri/driver.rb b/lib/rdoc/ri/driver.rb index 89534a5..e134e5e 100644 --- a/lib/rdoc/ri/driver.rb +++ b/lib/rdoc/ri/driver.rb @@ -1,5 +1,4 @@ require 'optparse' -require 'yaml' require 'rdoc/ri' require 'rdoc/ri/paths' diff --git a/lib/rubygems.rb b/lib/rubygems.rb index 95cdee8..a5e1b05 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -988,6 +988,16 @@ module Gem YAML_SPEC_DIR = 'quick/' + def self.yamler # :nodoc: + @yamler ||= + begin + require 'psych' + Psych + rescue LoadError + require 'yaml' + YAML + end + end end module Kernel diff --git a/lib/rubygems/builder.rb b/lib/rubygems/builder.rb index 9e7f262..ff0fe0c 100644 --- a/lib/rubygems/builder.rb +++ b/lib/rubygems/builder.rb @@ -18,7 +18,6 @@ class Gem::Builder # spec:: [Gem::Specification] The specification instance def initialize(spec) - require "yaml" require "rubygems/package" require "rubygems/security" @@ -70,7 +69,7 @@ EOM def write_package open @spec.file_name, 'wb' do |gem_io| Gem::Package.open gem_io, 'w', @signer do |pkg| - pkg.metadata = @spec.to_yaml + pkg.metadata = Gem.yamler.dump @spec @spec.files.each do |file| next if File.directory? file diff --git a/lib/rubygems/commands/mirror_command.rb b/lib/rubygems/commands/mirror_command.rb index 7cb8583..2658777 100644 --- a/lib/rubygems/commands/mirror_command.rb +++ b/lib/rubygems/commands/mirror_command.rb @@ -1,4 +1,3 @@ -require 'yaml' require 'zlib' require 'rubygems/command' @@ -29,7 +28,7 @@ Multiple sources and destinations may be specified. raise "Config file #{config_file} not found" unless File.exist? config_file - mirrors = YAML.load_file config_file + mirrors = Gem.yamler.load_file config_file raise "Invalid config file #{config_file}" unless mirrors.respond_to? :each diff --git a/lib/rubygems/commands/owner_command.rb b/lib/rubygems/commands/owner_command.rb index e88734e..5ca0152 100644 --- a/lib/rubygems/commands/owner_command.rb +++ b/lib/rubygems/commands/owner_command.rb @@ -43,7 +43,7 @@ class Gem::Commands::OwnerCommand < Gem::Command end with_response response do |resp| - owners = YAML.load resp.body + owners = Gem.yamler.load resp.body say "Owners for gem: #{name}" owners.each do |owner| diff --git a/lib/rubygems/commands/specification_command.rb b/lib/rubygems/commands/specification_command.rb index 07d2c2c..9c8eba8 100644 --- a/lib/rubygems/commands/specification_command.rb +++ b/lib/rubygems/commands/specification_command.rb @@ -1,4 +1,3 @@ -require 'yaml' require 'rubygems/command' require 'rubygems/local_remote_options' require 'rubygems/version_option' @@ -106,7 +105,7 @@ FIELD name of gemspec field to show say case options[:format] when :ruby then s.to_ruby when :marshal then Marshal.dump s - else s.to_yaml + else Gem.yamler.dump s end say "\n" diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb index 3498d06..eed744e 100644 --- a/lib/rubygems/config_file.rb +++ b/lib/rubygems/config_file.rb @@ -199,10 +199,8 @@ class Gem::ConfigFile dirname = File.dirname(credentials_path) Dir.mkdir(dirname) unless File.exists?(dirname) - require 'yaml' - File.open(credentials_path, 'w') do |f| - f.write config.to_yaml + f.write Gem.yamler.dump config end @rubygems_api_key = api_key @@ -211,8 +209,7 @@ class Gem::ConfigFile def load_file(filename) return {} unless filename and File.exists?(filename) begin - require 'yaml' - YAML.load(File.read(filename)) + Gem.yamler.load(File.read(filename)) rescue ArgumentError warn "Failed to load #{config_file_name}" rescue Errno::EACCES @@ -301,12 +298,11 @@ class Gem::ConfigFile yaml_hash[key.to_s] = value end - yaml_hash.to_yaml + Gem.yamler.dump yaml_hash end # Writes out this config file, replacing its source. def write - require 'yaml' open config_file_name, 'w' do |io| io.write to_yaml end diff --git a/lib/rubygems/indexer.rb b/lib/rubygems/indexer.rb index baa5824..a48e34e 100644 --- a/lib/rubygems/indexer.rb +++ b/lib/rubygems/indexer.rb @@ -145,7 +145,7 @@ class Gem::Indexer spec_file_name = "#{original_name}.gemspec.rz" yaml_name = File.join @quick_dir, spec_file_name - yaml_zipped = Gem.deflate spec.to_yaml + yaml_zipped = Gem.deflate Gem.yamler.dump spec open yaml_name, 'wb' do |io| io.write yaml_zipped end progress.updated original_name @@ -191,7 +191,7 @@ class Gem::Indexer gems = index.sort_by { |name, gemspec| gemspec.sort_obj } gems.each do |original_name, gemspec| - yaml = gemspec.to_yaml.gsub(/^/, ' ') + yaml = Gem.yamler.dump(gemspec).gsub(/^/, ' ') yaml = yaml.sub(/\A ---/, '') # there's a needed extra ' ' here io.print " #{original_name}:" io.puts yaml diff --git a/lib/rubygems/old_format.rb b/lib/rubygems/old_format.rb index 43967db..c3b7226 100644 --- a/lib/rubygems/old_format.rb +++ b/lib/rubygems/old_format.rb @@ -6,7 +6,6 @@ require 'rubygems' require 'fileutils' -require 'yaml' require 'zlib' ## @@ -133,7 +132,7 @@ class Gem::OldFormat self.read_until_dashes(gem_file) do |line| header_yaml << line end - header = YAML.load(header_yaml) + header = Gem.yamler.load(header_yaml) raise Gem::Exception, errstr unless header header.each do |entry| diff --git a/lib/rubygems/package.rb b/lib/rubygems/package.rb index 0fd6402..c73e7ae 100644 --- a/lib/rubygems/package.rb +++ b/lib/rubygems/package.rb @@ -7,7 +7,6 @@ require 'fileutils' require 'find' require 'stringio' -require 'yaml' require 'zlib' require 'rubygems/security' diff --git a/lib/rubygems/server.rb b/lib/rubygems/server.rb index 24c9e04..1800a27 100644 --- a/lib/rubygems/server.rb +++ b/lib/rubygems/server.rb @@ -1,5 +1,4 @@ require 'webrick' -require 'yaml' require 'zlib' require 'erb' @@ -577,7 +576,7 @@ div.method-source-code pre { color: #ffdead; overflow: hidden; } res.body << Gem.deflate(Marshal.dump(specs.first)) else # deprecated YAML format res['content-type'] = 'application/x-deflate' - res.body << Gem.deflate(specs.first.to_yaml) + res.body << Gem.deflate(Gem.yamler.dump(specs.first)) end else raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." @@ -823,7 +822,7 @@ div.method-source-code pre { color: #ffdead; overflow: hidden; } res['date'] = File.stat(@spec_dir).mtime - index = @source_index.to_yaml + index = Gem.yamler.dump @source_index if req.path =~ /Z$/ then res['content-type'] = 'application/x-deflate' diff --git a/lib/rubygems/source_index.rb b/lib/rubygems/source_index.rb index ac5d8ad..b44f3ca 100644 --- a/lib/rubygems/source_index.rb +++ b/lib/rubygems/source_index.rb @@ -449,7 +449,7 @@ class Gem::SourceIndex if name =~ /Marshal/ then return Marshal.load(spec_data) else - return YAML.load(spec_data) + return Gem.yamler.load(spec_data) end rescue => e if Gem.configuration.really_verbose then @@ -549,7 +549,7 @@ class Gem::SourceIndex begin yaml_uri = source_uri + "quick/#{spec_name}.gemspec.rz" zipped = fetcher.fetch_path yaml_uri - return YAML.load(unzip(zipped)) + return Gem.yamler.load(unzip(zipped)) rescue => ex @fetch_error = ex if Gem.configuration.really_verbose then diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb index 472d608..31ef356 100644 --- a/lib/rubygems/specification.rb +++ b/lib/rubygems/specification.rb @@ -477,7 +477,7 @@ class Gem::Specification def self.from_yaml(input) input = normalize_yaml_input input - spec = YAML.load input + spec = Gem.yamler.load input if spec && spec.class == FalseClass then raise Gem::EndOfYAMLException @@ -677,33 +677,44 @@ class Gem::Specification } end - def to_yaml(opts = {}) # :nodoc: + def encode_with coder # :nodoc: mark_version attributes = @@attributes.map { |name,| name.to_s }.sort attributes = attributes - %w[name version platform] - yaml = YAML.quick_emit object_id, opts do |out| - out.map taguri, to_yaml_style do |map| - map.add 'name', @name - map.add 'version', @version - platform = case @original_platform - when nil, '' then - 'ruby' - when String then - @original_platform - else - @original_platform.to_s - end - map.add 'platform', platform + coder.add 'name', @name + coder.add 'version', @version - attributes.each do |name| - map.add name, instance_variable_get("@#{name}") - end + platform = case @original_platform + when nil, '' then + 'ruby' + when String then + @original_platform + else + @original_platform.to_s + end + coder.add 'platform', platform + + attributes.each do |name| + coder.add name, instance_variable_get("@#{name}") + end + end + + def to_yaml(opts = {}) # :nodoc: + return super unless 'YAML' == Gem.yamler.name + + yaml = Gem.yamler.quick_emit object_id, opts do |out| + out.map taguri, to_yaml_style do |map| + encode_with map end end end + def init_with coder # :nodoc: + yaml_initialize coder.tag, coder.map + end + def yaml_initialize(tag, vals) # :nodoc: vals.each do |ivar, val| instance_variable_set "@#{ivar}", val diff --git a/test/rubygems/gemutilities.rb b/test/rubygems/gemutilities.rb index 965d898..e9d876f 100644 --- a/test/rubygems/gemutilities.rb +++ b/test/rubygems/gemutilities.rb @@ -8,19 +8,12 @@ else require 'rubygems' end require 'fileutils' -begin - gem 'minitest', '>= 1.3.1' - require 'minitest/unit' -rescue Gem::LoadError - warn "Install minitest gem >= 1.3.1" - raise -end +require 'minitest/autorun' require 'tmpdir' require 'uri' require 'rubygems/package' require 'rubygems/test_utilities' require 'pp' -require 'yaml' begin gem 'rdoc' @@ -137,25 +130,9 @@ class RubyGemTestCase < MiniTest::Unit::TestCase Gem.pre_uninstall do |uninstaller| @pre_uninstall_hook_arg = uninstaller end - - Object.class_eval %q{ - alias tmp_to_yaml to_yaml - def to_yaml( opts = {} ) - YAML::quick_emit( self, opts ) do |out| - out.map( taguri, to_yaml_style ) do |map| - to_yaml_properties.each do |m| - map.add( m[1..-1], instance_variable_get( m ) ) - end - end - end - end - } end def teardown - Object.class_eval %q{ - alias to_yaml tmp_to_yaml - } Gem::ConfigMap[:BASERUBY] = @orig_BASERUBY Gem::ConfigMap[:arch] = @orig_arch diff --git a/test/rubygems/test_gem_commands_specification_command.rb b/test/rubygems/test_gem_commands_specification_command.rb index 6a43439..4e4988b 100644 --- a/test/rubygems/test_gem_commands_specification_command.rb +++ b/test/rubygems/test_gem_commands_specification_command.rb @@ -80,7 +80,7 @@ class TestGemCommandsSpecificationCommand < RubyGemTestCase @cmd.execute end - assert_equal "foo", YAML.load(@ui.output) + assert_equal "foo", Gem.yamler.load(@ui.output) end def test_execute_marshal diff --git a/test/rubygems/test_gem_gemcutter_utilities.rb b/test/rubygems/test_gem_gemcutter_utilities.rb index f8306d5..ec10698 100644 --- a/test/rubygems/test_gem_gemcutter_utilities.rb +++ b/test/rubygems/test_gem_gemcutter_utilities.rb @@ -22,7 +22,7 @@ class TestGemGemcutterUtilities < RubyGemTestCase assert @fetcher.last_request["authorization"] assert_match %r{Signed in.}, @sign_in_ui.output - credentials = YAML.load_file Gem.configuration.credentials_path + credentials = Gem.yamler.load_file Gem.configuration.credentials_path assert_equal api_key, credentials[:rubygems_api_key] end @@ -34,7 +34,7 @@ class TestGemGemcutterUtilities < RubyGemTestCase assert @fetcher.last_request["authorization"] assert_match %r{Signed in.}, @sign_in_ui.output - credentials = YAML.load_file Gem.configuration.credentials_path + credentials = Gem.yamler.load_file Gem.configuration.credentials_path assert_equal api_key, credentials[:rubygems_api_key] end @@ -60,7 +60,7 @@ class TestGemGemcutterUtilities < RubyGemTestCase assert_match %r{Enter your RubyGems.org credentials.}, @sign_in_ui.output assert_match %r{Signed in.}, @sign_in_ui.output - credentials = YAML.load_file Gem.configuration.credentials_path + credentials = Gem.yamler.load_file Gem.configuration.credentials_path assert_equal api_key, credentials[:rubygems_api_key] assert_equal other_api_key, credentials[:other_api_key] end diff --git a/test/rubygems/test_gem_indexer.rb b/test/rubygems/test_gem_indexer.rb index 7bc4862..8ae187a 100644 --- a/test/rubygems/test_gem_indexer.rb +++ b/test/rubygems/test_gem_indexer.rb @@ -521,7 +521,7 @@ eighty characters.</pre> yaml_path = File.join @tempdir, 'yaml' dump_path = File.join @tempdir, "Marshal.#{@marshal_version}" - yaml_index = YAML.load_file yaml_path + yaml_index = Gem.yamler.load_file yaml_path dump_index = Marshal.load Gem.read_binary(dump_path) dump_index.each do |_,gem| diff --git a/test/rubygems/test_gem_server.rb b/test/rubygems/test_gem_server.rb index 41c25fe..dbb53e3 100644 --- a/test/rubygems/test_gem_server.rb +++ b/test/rubygems/test_gem_server.rb @@ -108,7 +108,7 @@ class TestGemServer < RubyGemTestCase assert @res['date'] assert_equal 'application/x-deflate', @res['content-type'] - spec = YAML.load Gem.inflate(@res.body) + spec = Gem.yamler.load Gem.inflate(@res.body) assert_equal 'a', spec.name assert_equal Gem::Version.new(1), spec.version end @@ -125,7 +125,7 @@ class TestGemServer < RubyGemTestCase assert @res['date'] assert_equal 'application/x-deflate', @res['content-type'] - spec = YAML.load Gem.inflate(@res.body) + spec = Gem.yamler.load Gem.inflate(@res.body) assert_equal 'a', spec.name assert_equal Gem::Version.new(1), spec.version assert_equal Gem::Platform.local, spec.platform @@ -143,7 +143,7 @@ class TestGemServer < RubyGemTestCase assert @res['date'] assert_equal 'application/x-deflate', @res['content-type'] - spec = YAML.load Gem.inflate(@res.body) + spec = Gem.yamler.load Gem.inflate(@res.body) assert_equal 'a', spec.name assert_equal Gem::Version.new(1), spec.version end @@ -307,7 +307,7 @@ class TestGemServer < RubyGemTestCase si = Gem::SourceIndex.new si.add_specs @a1, @a2 - assert_equal si, YAML.load(@res.body) + assert_equal si, Gem.yamler.load(@res.body) end def test_yaml_Z @@ -323,7 +323,7 @@ class TestGemServer < RubyGemTestCase si = Gem::SourceIndex.new si.add_specs @a1, @a2 - assert_equal si, YAML.load(Gem.inflate(@res.body)) + assert_equal si, Gem.yamler.load(Gem.inflate(@res.body)) end def util_listen diff --git a/test/rubygems/test_gem_specification.rb b/test/rubygems/test_gem_specification.rb index 5979890..0718149 100644 --- a/test/rubygems/test_gem_specification.rb +++ b/test/rubygems/test_gem_specification.rb @@ -1,6 +1,7 @@ require_relative 'gemutilities' require 'stringio' require 'rubygems/specification' +require 'date' class TestGemSpecification < RubyGemTestCase @@ -855,7 +856,7 @@ end def test_to_yaml yaml_str = @a1.to_yaml - same_spec = YAML.load(yaml_str) + same_spec = Gem.yamler.load(yaml_str) assert_equal @a1, same_spec end @@ -864,7 +865,7 @@ end @a1.platform = Gem::Platform.local yaml_str = @a1.to_yaml - same_spec = YAML.load(yaml_str) + same_spec = Gem.yamler.load(yaml_str) assert_equal Gem::Platform.local, same_spec.platform @@ -883,7 +884,7 @@ end yaml_str = @a1.to_yaml - same_spec = YAML.load(yaml_str) + same_spec = Gem.yamler.load(yaml_str) assert_equal Gem::Platform.new('powerpc-darwin7'), same_spec.platform assert_equal 'powerpc-darwin7.9.0', same_spec.original_platform