зеркало из https://github.com/github/ruby.git
Avoid triggering autoload in Module#const_defined?(String)
[Bug #15780]
This commit is contained in:
Родитель
6786fe44dc
Коммит
7d805e67f3
3
object.c
3
object.c
|
@ -2694,16 +2694,19 @@ rb_mod_const_defined(int argc, VALUE *argv, VALUE mod)
|
|||
if (!RTEST(recur)) {
|
||||
if (!rb_const_defined_at(mod, id))
|
||||
return Qfalse;
|
||||
if (p == pend) return Qtrue;
|
||||
mod = rb_const_get_at(mod, id);
|
||||
}
|
||||
else if (beglen == 0) {
|
||||
if (!rb_const_defined(mod, id))
|
||||
return Qfalse;
|
||||
if (p == pend) return Qtrue;
|
||||
mod = rb_const_get(mod, id);
|
||||
}
|
||||
else {
|
||||
if (!rb_const_defined_from(mod, id))
|
||||
return Qfalse;
|
||||
if (p == pend) return Qtrue;
|
||||
mod = rb_const_get_from(mod, id);
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -449,6 +449,7 @@ describe "Module#autoload" do
|
|||
it "does not load the file when accessing the constants table of the module" do
|
||||
ModuleSpecs::Autoload.autoload :P, @non_existent
|
||||
ModuleSpecs::Autoload.const_defined?(:P).should be_true
|
||||
ModuleSpecs::Autoload.const_defined?("P").should be_true
|
||||
end
|
||||
|
||||
it "loads the file when opening a module that is the autoloaded constant" do
|
||||
|
|
|
@ -42,8 +42,10 @@ p Foo::Bar
|
|||
require 'tmpdir'
|
||||
Dir.mktmpdir('autoload') {|tmpdir|
|
||||
tmpfile = tmpdir + '/foo.rb'
|
||||
tmpfile2 = tmpdir + '/bar.rb'
|
||||
a = Module.new do
|
||||
autoload :X, tmpfile
|
||||
autoload :Y, tmpfile2
|
||||
end
|
||||
b = Module.new do
|
||||
include a
|
||||
|
@ -52,6 +54,10 @@ p Foo::Bar
|
|||
assert_equal(true, b.const_defined?(:X))
|
||||
assert_equal(tmpfile, a.autoload?(:X), bug4565)
|
||||
assert_equal(tmpfile, b.autoload?(:X), bug4565)
|
||||
assert_equal(true, a.const_defined?("Y"))
|
||||
assert_equal(true, b.const_defined?("Y"))
|
||||
assert_equal(tmpfile2, a.autoload?("Y"))
|
||||
assert_equal(tmpfile2, b.autoload?("Y"))
|
||||
}
|
||||
end
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче