From f52c2cc24d1070f1997921c1d870cdd55327ba2a Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 14 Jan 2011 02:02:12 +0000 Subject: Reduced gem_prelude to just require rubygems. Reviewed by Evan Phoenix git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30538 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 12 ++ gem_prelude.rb | 240 +-------------------------------------- lib/rubygems.rb | 23 +--- ruby.c | 6 +- test/ruby/test_process.rb | 4 +- test/ruby/test_rubyoptions.rb | 11 +- test/ruby/test_thread.rb | 2 +- test/rubygems/gemutilities.rb | 6 +- test/test_tracer.rb | 6 +- test/webrick/test_filehandler.rb | 7 ++ 10 files changed, 40 insertions(+), 277 deletions(-) diff --git a/ChangeLog b/ChangeLog index 23f4e71924..f6f4011d2e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +Fri Jan 14 10:40:11 2011 Ryan Davis + + * gem_prelude.rb: Just require rubygems. Fixes rubygems 1.4. + * lib/rubygems.rb: removed all Gem::Quickloader code. + * ruby.c: renamed ruby_init_gems to ruby_init_prelude. Set + $disable_rubygems since there is no fine grained mechanism to + skip parts of the prelude. Open to suggestions on how to do this + better. + * test/*.rb: Load path isn't set up correctly, so add + --disable-gems as needed to failing tests that are explicitly + testing stderr w/ ==. + Fri Jan 14 07:30:47 2011 Nobuyoshi Nakada * io.c (argf_next_argv): go advance when the next file cannot be diff --git a/gem_prelude.rb b/gem_prelude.rb index ffb3e9a63d..c96514ada8 100644 --- a/gem_prelude.rb +++ b/gem_prelude.rb @@ -1,239 +1 @@ -# depends on: array.rb dir.rb env.rb file.rb hash.rb module.rb regexp.rb -# vim: filetype=ruby - -# NOTICE: Ruby is during initialization here. -# * Encoding.default_external does not reflects -E. -# * Should not expect Encoding.default_internal. -# * Locale encoding is available. - -if defined?(Gem) then - - # :stopdoc: - - module Kernel - - def gem(gem_name, *version_requirements) - Gem::QuickLoader.load_full_rubygems_library - gem gem_name, *version_requirements - end - private :gem - end - - module Gem - - ConfigMap = { - :EXEEXT => RbConfig::CONFIG["EXEEXT"], - :RUBY_SO_NAME => RbConfig::CONFIG["RUBY_SO_NAME"], - :arch => RbConfig::CONFIG["arch"], - :bindir => RbConfig::CONFIG["bindir"], - :libdir => RbConfig::CONFIG["libdir"], - :ruby_install_name => RbConfig::CONFIG["ruby_install_name"], - :ruby_version => RbConfig::CONFIG["ruby_version"], - :rubylibprefix => RbConfig::CONFIG["rubylibprefix"], - :sitedir => RbConfig::CONFIG["sitedir"], - :sitelibdir => RbConfig::CONFIG["sitelibdir"], - } - - def self.suffixes - ['', '.rb', ".#{RbConfig::CONFIG["DLEXT"]}"] - end - - def self.dir - @gem_home ||= nil - set_home(ENV['GEM_HOME'] || default_dir) unless @gem_home - @gem_home - end - - def self.path - @gem_path ||= nil - unless @gem_path - paths = [ENV['GEM_PATH'] || default_path] - paths << APPLE_GEM_HOME if defined? APPLE_GEM_HOME - set_paths(paths.compact.join(File::PATH_SEPARATOR)) - end - @gem_path - end - - def self.post_install(&hook) - @post_install_hooks << hook - end - - def self.post_uninstall(&hook) - @post_uninstall_hooks << hook - end - - def self.pre_install(&hook) - @pre_install_hooks << hook - end - - def self.pre_uninstall(&hook) - @pre_uninstall_hooks << hook - end - - def self.set_home(home) - home = home.dup.force_encoding(Encoding.find('filesystem')) - home.gsub!(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR - @gem_home = home - end - - def self.set_paths(gpaths) - if gpaths - @gem_path = gpaths.split(File::PATH_SEPARATOR) - - if File::ALT_SEPARATOR then - @gem_path.map! do |path| - path.gsub File::ALT_SEPARATOR, File::SEPARATOR - end - end - - @gem_path << Gem.dir - else - # TODO: should this be Gem.default_path instead? - @gem_path = [Gem.dir] - end - - @gem_path.uniq! - @gem_path.map!{|x|x.force_encoding(Encoding.find('filesystem'))} - end - - def self.user_home - @user_home ||= File.expand_path("~").force_encoding(Encoding.find('filesystem')) - rescue - if File::ALT_SEPARATOR then - "C:/" - else - "/" - end - end - - # begin rubygems/defaults - # NOTE: this require will be replaced with in-place eval before compilation. - require 'lib/rubygems/defaults.rb' - # end rubygems/defaults - - - ## - # Methods before this line will be removed when QuickLoader is replaced - # with the real RubyGems - - GEM_PRELUDE_METHODS = Gem.methods(false) - - begin - verbose, debug = $VERBOSE, $DEBUG - $VERBOSE = $DEBUG = nil - - begin - require 'rubygems/defaults/operating_system' - rescue ::LoadError - end - - if defined?(RUBY_ENGINE) then - begin - require "rubygems/defaults/#{RUBY_ENGINE}" - rescue ::LoadError - end - end - ensure - $VERBOSE, $DEBUG = verbose, debug - end - - module QuickLoader - - @loaded_full_rubygems_library = false - - def self.remove - return if @loaded_full_rubygems_library - - @loaded_full_rubygems_library = true - - class << Gem - undef_method(*Gem::GEM_PRELUDE_METHODS) - end - - remove_method :const_missing - remove_method :method_missing - - Kernel.module_eval do - undef_method :gem if method_defined? :gem - end - end - - def self.load_full_rubygems_library - return false if @loaded_full_rubygems_library - - remove - - $".delete path_to_full_rubygems_library - if $".any? {|path| path.end_with?('/rubygems.rb')} - raise LoadError, "another rubygems is already loaded from #{path}" - end - - require 'rubygems' - - return true - end - - def self.path_to_full_rubygems_library - installed_path = File.join(Gem::ConfigMap[:rubylibprefix], Gem::ConfigMap[:ruby_version]) - if $:.include?(installed_path) - return File.join(installed_path, 'rubygems.rb') - else # e.g., on test-all - $:.each do |dir| - if File.exist?( path = File.join(dir, 'rubygems.rb') ) - return path - end - end - raise LoadError, 'rubygems.rb' - end - end - - def const_missing(constant) - QuickLoader.load_full_rubygems_library - - if Gem.const_defined?(constant) then - Gem.const_get constant - else - super - end - end - - def method_missing(method, *args, &block) - QuickLoader.load_full_rubygems_library - super unless Gem.respond_to?(method) - Gem.send(method, *args, &block) - end - end - - extend QuickLoader - - def self.try_activate(path) - # This method is only hit when the custom require is hit the first time. - # So we go off and dutifully load all of rubygems and retry the call - # to Gem.try_activate. We retry because full rubygems replaces this - # method with one that actually tries to find a gem for +path+ and load it. - # - # This is conditional because in the course of loading rubygems, the custom - # require will call back into here before all of rubygems is loaded. So - # we must not always retry the call. We only redo the call when - # load_full_rubygems_library returns true, which it only does the first - # time it's called. - # - if QuickLoader.load_full_rubygems_library - return Gem.try_activate(path) - end - - return false - end - - end - - begin - require 'lib/rubygems/custom_require.rb' - rescue Exception => e - puts "Error loading gem paths on load path in gem_prelude" - puts e - puts e.backtrace.join("\n") - end - -end - +require 'rubygems' unless $disable_rubygems diff --git a/lib/rubygems.rb b/lib/rubygems.rb index 6c13f7bce2..9f72b8aa45 100644 --- a/lib/rubygems.rb +++ b/lib/rubygems.rb @@ -5,13 +5,6 @@ # See LICENSE.txt for permissions. #++ -gem_disabled = !defined? Gem - -unless gem_disabled - # Nuke the Quickloader stuff - Gem::QuickLoader.remove -end - require 'rubygems/defaults' require 'thread' require 'etc' @@ -584,9 +577,9 @@ module Gem end def self.remove_prelude_paths - Gem::QuickLoader::GemLoadPaths.each do |path| - $LOAD_PATH.delete(path) - end + # Gem::QuickLoader::GemLoadPaths.each do |path| + # $LOAD_PATH.delete(path) + # end end ## @@ -1120,15 +1113,7 @@ class << Gem end end -## -# Enables the require hook for RubyGems. -# -# if --disable-rubygems was used, then the prelude wasn't loaded, so -# we need to load the custom_require now. - -if gem_disabled - require 'rubygems/custom_require' -end +require 'rubygems/custom_require' Gem.clear_paths diff --git a/ruby.c b/ruby.c index 6e3d791255..079afd8d34 100644 --- a/ruby.c +++ b/ruby.c @@ -1103,9 +1103,9 @@ proc_options(long argc, char **argv, struct cmdline_options *opt, int envopt) void Init_prelude(void); static void -ruby_init_gems(int enable) +ruby_init_prelude(int enable_gems) { - if (enable) rb_define_module("Gem"); + rb_gv_set("$disable_rubygems", enable_gems ? Qfalse : Qtrue); Init_prelude(); rb_const_remove(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX")); } @@ -1365,7 +1365,7 @@ process_options(int argc, char **argv, struct cmdline_options *opt) rb_enc_associate(RARRAY_PTR(load_path)[i], lenc); } } - ruby_init_gems(!(opt->disable & DISABLE_BIT(gems))); + ruby_init_prelude(!(opt->disable & DISABLE_BIT(gems))); ruby_set_argv(argc, argv); process_sflag(&opt->sflag); diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb index 7e96b4062a..55bae0650b 100644 --- a/test/ruby/test_process.rb +++ b/test/ruby/test_process.rb @@ -1210,8 +1210,8 @@ class TestProcess < Test::Unit::TestCase Dir.chdir("vd") { dir = "#{d}/vd" # OpenSolaris cannot remove the current directory. - system(RUBY, "-e", "Dir.chdir '..'; Dir.rmdir #{dir.dump}") - system({"RUBYLIB"=>nil}, RUBY, "-e", "exit true") + system(RUBY, "--disable-gems", "-e", "Dir.chdir '..'; Dir.rmdir #{dir.dump}") + system({"RUBYLIB"=>nil}, RUBY, "--disable-gems", "-e", "exit true") status = $? } assert(status.success?, "[ruby-dev:38105]") diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb index d125b2c078..c483d1f216 100644 --- a/test/ruby/test_rubyoptions.rb +++ b/test/ruby/test_rubyoptions.rb @@ -57,13 +57,14 @@ class TestRubyOptions < Test::Unit::TestCase end def test_debug - assert_in_out_err(%w(-de) + ["p $DEBUG"], "", %w(true), []) + assert_in_out_err(["--disable-gems", "-de", "p $DEBUG"], "", %w(true), []) - assert_in_out_err(%w(--debug -e) + ["p $DEBUG"], "", %w(true), []) + assert_in_out_err(["--disable-gems", "--debug", "-e", "p $DEBUG"], + "", %w(true), []) end def test_verbose - assert_in_out_err(%w(-vve) + [""]) do |r, e| + assert_in_out_err(["-vve", ""]) do |r, e| assert_match(/^ruby #{RUBY_VERSION}(?:[p ]|dev).*? \[#{RUBY_PLATFORM}\]$/, r.join) assert_equal RUBY_DESCRIPTION, r.join.chomp assert_equal([], e) @@ -209,10 +210,10 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err([], "", [], /invalid switch in RUBYOPT: -e \(RuntimeError\)/) ENV['RUBYOPT'] = '-T1' - assert_in_out_err([], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/) + assert_in_out_err(["--disable-gems"], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/) ENV['RUBYOPT'] = '-T4' - assert_in_out_err([], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/) + assert_in_out_err(["--disable-gems"], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/) ENV['RUBYOPT'] = '-Eus-ascii -KN' assert_in_out_err(%w(-Eutf-8 -KU), "p '\u3042'") do |r, e| diff --git a/test/ruby/test_thread.rb b/test/ruby/test_thread.rb index 0322448c71..bc0be1e4d2 100644 --- a/test/ruby/test_thread.rb +++ b/test/ruby/test_thread.rb @@ -271,7 +271,7 @@ class TestThread < Test::Unit::TestCase end INPUT - assert_in_out_err(%w(-d), <<-INPUT, %w(false 2), /.+/) + assert_in_out_err(%w(--disable-gems -d), <<-INPUT, %w(false 2), /.+/) p Thread.abort_on_exception begin Thread.new { raise } diff --git a/test/rubygems/gemutilities.rb b/test/rubygems/gemutilities.rb index 803cd26c96..66914d0c0e 100644 --- a/test/rubygems/gemutilities.rb +++ b/test/rubygems/gemutilities.rb @@ -2,11 +2,7 @@ at_exit { $SAFE = 1 } $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) -if RUBY_VERSION > '1.9' then - Gem::QuickLoader.load_full_rubygems_library -else - require 'rubygems' -end +require 'rubygems' require 'fileutils' require 'minitest/autorun' require 'tmpdir' diff --git a/test/test_tracer.rb b/test/test_tracer.rb index 2580205338..8f542e6e84 100644 --- a/test/test_tracer.rb +++ b/test/test_tracer.rb @@ -5,16 +5,16 @@ class TestTracer < Test::Unit::TestCase include EnvUtil def test_work_with_e - assert_in_out_err(%w[-rtracer -e 1]) do |(*lines),| + assert_in_out_err(%w[--disable-gems -rtracer -e 1]) do |(*lines),| case lines.size when 2 assert_match %r[#0::\d+:Kernel:<: -], lines[0] when 1 # do nothing else - flunk 'unexpected output from `ruby -rtracer -e 1`' + flunk "unexpected output from `ruby -rtracer -e 1`" end - assert_equal "#0:-e:1::-: 1", lines[1] + assert_equal "#0:-e:1::-: 1", lines.last end end end diff --git a/test/webrick/test_filehandler.rb b/test/webrick/test_filehandler.rb index 7ab86a8fb7..e4b672af8d 100644 --- a/test/webrick/test_filehandler.rb +++ b/test/webrick/test_filehandler.rb @@ -241,6 +241,13 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase :CGIInterpreter => TestWEBrick::RubyBin, :DocumentRoot => File.dirname(__FILE__), :CGIPathEnv => ENV['PATH'], + :RequestCallback => Proc.new{|req, res| + def req.meta_vars + meta = super + meta["RUBYLIB"] = $:.join(File::PATH_SEPARATOR) + return meta + end + }, } TestWEBrick.start_httpserver(config) do |server, addr, port, log| http = Net::HTTP.new(addr, port) -- cgit v1.2.3