1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
ryan 2011-01-14 02:02:12 +00:00
parent 8409156e24
commit f52c2cc24d
10 changed files with 40 additions and 277 deletions

View file

@ -1,3 +1,15 @@
Fri Jan 14 10:40:11 2011 Ryan Davis <ryan@lust.local>
* 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 <nobu@ruby-lang.org> Fri Jan 14 07:30:47 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* io.c (argf_next_argv): go advance when the next file cannot be * io.c (argf_next_argv): go advance when the next file cannot be

View file

@ -1,239 +1 @@
# depends on: array.rb dir.rb env.rb file.rb hash.rb module.rb regexp.rb require 'rubygems' unless $disable_rubygems
# 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

View file

@ -5,13 +5,6 @@
# See LICENSE.txt for permissions. # See LICENSE.txt for permissions.
#++ #++
gem_disabled = !defined? Gem
unless gem_disabled
# Nuke the Quickloader stuff
Gem::QuickLoader.remove
end
require 'rubygems/defaults' require 'rubygems/defaults'
require 'thread' require 'thread'
require 'etc' require 'etc'
@ -584,9 +577,9 @@ module Gem
end end
def self.remove_prelude_paths def self.remove_prelude_paths
Gem::QuickLoader::GemLoadPaths.each do |path| # Gem::QuickLoader::GemLoadPaths.each do |path|
$LOAD_PATH.delete(path) # $LOAD_PATH.delete(path)
end # end
end end
## ##
@ -1120,15 +1113,7 @@ class << Gem
end end
end end
## require 'rubygems/custom_require'
# 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
Gem.clear_paths Gem.clear_paths

6
ruby.c
View file

@ -1103,9 +1103,9 @@ proc_options(long argc, char **argv, struct cmdline_options *opt, int envopt)
void Init_prelude(void); void Init_prelude(void);
static 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(); Init_prelude();
rb_const_remove(rb_cObject, rb_intern_const("TMP_RUBY_PREFIX")); 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); 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); ruby_set_argv(argc, argv);
process_sflag(&opt->sflag); process_sflag(&opt->sflag);

View file

@ -1210,8 +1210,8 @@ class TestProcess < Test::Unit::TestCase
Dir.chdir("vd") { Dir.chdir("vd") {
dir = "#{d}/vd" dir = "#{d}/vd"
# OpenSolaris cannot remove the current directory. # OpenSolaris cannot remove the current directory.
system(RUBY, "-e", "Dir.chdir '..'; Dir.rmdir #{dir.dump}") system(RUBY, "--disable-gems", "-e", "Dir.chdir '..'; Dir.rmdir #{dir.dump}")
system({"RUBYLIB"=>nil}, RUBY, "-e", "exit true") system({"RUBYLIB"=>nil}, RUBY, "--disable-gems", "-e", "exit true")
status = $? status = $?
} }
assert(status.success?, "[ruby-dev:38105]") assert(status.success?, "[ruby-dev:38105]")

View file

@ -57,13 +57,14 @@ class TestRubyOptions < Test::Unit::TestCase
end end
def test_debug 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 end
def test_verbose 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_match(/^ruby #{RUBY_VERSION}(?:[p ]|dev).*? \[#{RUBY_PLATFORM}\]$/, r.join)
assert_equal RUBY_DESCRIPTION, r.join.chomp assert_equal RUBY_DESCRIPTION, r.join.chomp
assert_equal([], e) assert_equal([], e)
@ -209,10 +210,10 @@ class TestRubyOptions < Test::Unit::TestCase
assert_in_out_err([], "", [], /invalid switch in RUBYOPT: -e \(RuntimeError\)/) assert_in_out_err([], "", [], /invalid switch in RUBYOPT: -e \(RuntimeError\)/)
ENV['RUBYOPT'] = '-T1' 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' 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' ENV['RUBYOPT'] = '-Eus-ascii -KN'
assert_in_out_err(%w(-Eutf-8 -KU), "p '\u3042'") do |r, e| assert_in_out_err(%w(-Eutf-8 -KU), "p '\u3042'") do |r, e|

View file

@ -271,7 +271,7 @@ class TestThread < Test::Unit::TestCase
end end
INPUT 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 p Thread.abort_on_exception
begin begin
Thread.new { raise } Thread.new { raise }

View file

@ -2,11 +2,7 @@ at_exit { $SAFE = 1 }
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
if RUBY_VERSION > '1.9' then require 'rubygems'
Gem::QuickLoader.load_full_rubygems_library
else
require 'rubygems'
end
require 'fileutils' require 'fileutils'
require 'minitest/autorun' require 'minitest/autorun'
require 'tmpdir' require 'tmpdir'

View file

@ -5,16 +5,16 @@ class TestTracer < Test::Unit::TestCase
include EnvUtil include EnvUtil
def test_work_with_e 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 case lines.size
when 2 when 2
assert_match %r[#0:<internal:lib/rubygems/custom_require>:\d+:Kernel:<: -], lines[0] assert_match %r[#0:<internal:lib/rubygems/custom_require>:\d+:Kernel:<: -], lines[0]
when 1 when 1
# do nothing # do nothing
else else
flunk 'unexpected output from `ruby -rtracer -e 1`' flunk "unexpected output from `ruby -rtracer -e 1`"
end end
assert_equal "#0:-e:1::-: 1", lines[1] assert_equal "#0:-e:1::-: 1", lines.last
end end
end end
end end

View file

@ -241,6 +241,13 @@ class WEBrick::TestFileHandler < Test::Unit::TestCase
:CGIInterpreter => TestWEBrick::RubyBin, :CGIInterpreter => TestWEBrick::RubyBin,
:DocumentRoot => File.dirname(__FILE__), :DocumentRoot => File.dirname(__FILE__),
:CGIPathEnv => ENV['PATH'], :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| TestWEBrick.start_httpserver(config) do |server, addr, port, log|
http = Net::HTTP.new(addr, port) http = Net::HTTP.new(addr, port)