Avoid triggering autoload in Module#const_defined?(String)
[Bug #15780]
This commit is contained in:
parent
6786fe44dc
commit
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 (!RTEST(recur)) {
|
||||||
if (!rb_const_defined_at(mod, id))
|
if (!rb_const_defined_at(mod, id))
|
||||||
return Qfalse;
|
return Qfalse;
|
||||||
|
if (p == pend) return Qtrue;
|
||||||
mod = rb_const_get_at(mod, id);
|
mod = rb_const_get_at(mod, id);
|
||||||
}
|
}
|
||||||
else if (beglen == 0) {
|
else if (beglen == 0) {
|
||||||
if (!rb_const_defined(mod, id))
|
if (!rb_const_defined(mod, id))
|
||||||
return Qfalse;
|
return Qfalse;
|
||||||
|
if (p == pend) return Qtrue;
|
||||||
mod = rb_const_get(mod, id);
|
mod = rb_const_get(mod, id);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (!rb_const_defined_from(mod, id))
|
if (!rb_const_defined_from(mod, id))
|
||||||
return Qfalse;
|
return Qfalse;
|
||||||
|
if (p == pend) return Qtrue;
|
||||||
mod = rb_const_get_from(mod, id);
|
mod = rb_const_get_from(mod, id);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -449,6 +449,7 @@ describe "Module#autoload" do
|
|||||||
it "does not load the file when accessing the constants table of the module" do
|
it "does not load the file when accessing the constants table of the module" do
|
||||||
ModuleSpecs::Autoload.autoload :P, @non_existent
|
ModuleSpecs::Autoload.autoload :P, @non_existent
|
||||||
ModuleSpecs::Autoload.const_defined?(:P).should be_true
|
ModuleSpecs::Autoload.const_defined?(:P).should be_true
|
||||||
|
ModuleSpecs::Autoload.const_defined?("P").should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
it "loads the file when opening a module that is the autoloaded constant" do
|
it "loads the file when opening a module that is the autoloaded constant" do
|
||||||
|
@ -42,8 +42,10 @@ p Foo::Bar
|
|||||||
require 'tmpdir'
|
require 'tmpdir'
|
||||||
Dir.mktmpdir('autoload') {|tmpdir|
|
Dir.mktmpdir('autoload') {|tmpdir|
|
||||||
tmpfile = tmpdir + '/foo.rb'
|
tmpfile = tmpdir + '/foo.rb'
|
||||||
|
tmpfile2 = tmpdir + '/bar.rb'
|
||||||
a = Module.new do
|
a = Module.new do
|
||||||
autoload :X, tmpfile
|
autoload :X, tmpfile
|
||||||
|
autoload :Y, tmpfile2
|
||||||
end
|
end
|
||||||
b = Module.new do
|
b = Module.new do
|
||||||
include a
|
include a
|
||||||
@ -52,6 +54,10 @@ p Foo::Bar
|
|||||||
assert_equal(true, b.const_defined?(:X))
|
assert_equal(true, b.const_defined?(:X))
|
||||||
assert_equal(tmpfile, a.autoload?(:X), bug4565)
|
assert_equal(tmpfile, a.autoload?(:X), bug4565)
|
||||||
assert_equal(tmpfile, b.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
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user