From cd465d552c3a00341f4cb7f1d7a793d0ebcb6cde Mon Sep 17 00:00:00 2001 From: tenderlove Date: Thu, 14 May 2015 21:57:33 +0000 Subject: [PATCH] * variable.c: Change autoload to call `require` through Ruby rather than directly calling `rb_require_safe`. This allows things like RubyGems to intercept file loading done though `autoload`. [Feature #11140] * test/ruby/test_autoload.rb: Test for change. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50494 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++++++ test/ruby/test_autoload.rb | 25 +++++++++++++++++++++++++ variable.c | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 3a12005f91..91ef2d4206 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Fri May 15 06:54:19 2015 Aaron Patterson + + * variable.c: Change autoload to call `require` through Ruby rather + than directly calling `rb_require_safe`. This allows things like + RubyGems to intercept file loading done though `autoload`. + [Feature #11140] + + * test/ruby/test_autoload.rb: Test for change. + Wed Apr 8 19:18:02 2015 Shota Fukumori (sora_h) * enum.c (enum_grep_v, grep_i, grep_iter_i, Init_enum): diff --git a/test/ruby/test_autoload.rb b/test/ruby/test_autoload.rb index a95c27451b..5b1bbc0e0f 100644 --- a/test/ruby/test_autoload.rb +++ b/test/ruby/test_autoload.rb @@ -161,6 +161,31 @@ p Foo::Bar } end + def test_require_implemented_in_ruby_is_called + Kernel.module_eval do; alias :old_require :require; end + + called_with = [] + Kernel.send :define_method, :require do |path| + called_with << path + old_require path + end + + Tempfile.create(['autoload', '.rb']) {|file| + file.puts 'class AutoloadTest; end' + file.close + add_autoload(file.path) + begin + assert(Object::AutoloadTest) + ensure + remove_autoload_constant + end + assert_equal [file.path], called_with + } + + ensure + Kernel.module_eval do; alias :require :old_require; undef :old_require; end + end + def add_autoload(path) (@autoload_paths ||= []) << path eval <<-END diff --git a/variable.c b/variable.c index c835350a4c..9b5a326492 100644 --- a/variable.c +++ b/variable.c @@ -1803,7 +1803,7 @@ static VALUE autoload_require(VALUE arg) { struct autoload_data_i *ele = (struct autoload_data_i *)arg; - return rb_require_safe(ele->feature, ele->safe_level); + return rb_funcall(rb_vm_top_self(), rb_intern("require"), 1, ele->feature); } static VALUE