зеркало из https://github.com/github/ruby.git
[Bug #20342] Consider wrapped load in `main` methods
This commit is contained in:
Родитель
ef19234b10
Коммит
58918788ab
18
eval.c
18
eval.c
|
@ -1782,6 +1782,16 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj)
|
|||
return obj;
|
||||
}
|
||||
|
||||
VALUE
|
||||
rb_top_main_class(const char *method)
|
||||
{
|
||||
VALUE klass = GET_THREAD()->top_wrapper;
|
||||
|
||||
if (!klass) return rb_cObject;
|
||||
rb_warning("main.%s in the wrapped load is effective only in wrapper module", method);
|
||||
return klass;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* include(module, ...) -> self
|
||||
|
@ -1794,13 +1804,7 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj)
|
|||
static VALUE
|
||||
top_include(int argc, VALUE *argv, VALUE self)
|
||||
{
|
||||
rb_thread_t *th = GET_THREAD();
|
||||
|
||||
if (th->top_wrapper) {
|
||||
rb_warning("main.include in the wrapped load is effective only in wrapper module");
|
||||
return rb_mod_include(argc, argv, th->top_wrapper);
|
||||
}
|
||||
return rb_mod_include(argc, argv, rb_cObject);
|
||||
return rb_mod_include(argc, argv, rb_top_main_class("include"));
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -21,6 +21,7 @@ extern ID ruby_static_id_status;
|
|||
VALUE rb_refinement_module_get_refined_class(VALUE module);
|
||||
void rb_class_modify_check(VALUE);
|
||||
NORETURN(VALUE rb_f_raise(int argc, VALUE *argv));
|
||||
VALUE rb_top_main_class(const char *method);
|
||||
|
||||
/* eval_error.c */
|
||||
VALUE rb_get_backtrace(VALUE info);
|
||||
|
|
12
proc.c
12
proc.c
|
@ -2331,17 +2331,7 @@ rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
|
|||
static VALUE
|
||||
top_define_method(int argc, VALUE *argv, VALUE obj)
|
||||
{
|
||||
rb_thread_t *th = GET_THREAD();
|
||||
VALUE klass;
|
||||
|
||||
klass = th->top_wrapper;
|
||||
if (klass) {
|
||||
rb_warning("main.define_method in the wrapped load is effective only in wrapper module");
|
||||
}
|
||||
else {
|
||||
klass = rb_cObject;
|
||||
}
|
||||
return rb_mod_define_method(argc, argv, klass);
|
||||
return rb_mod_define_method(argc, argv, rb_top_main_class("define_method"));
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -370,6 +370,26 @@ class TestRequire < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_public_in_wrapped_load
|
||||
Tempfile.create(["test_public_in_wrapped_load", ".rb"]) do |t|
|
||||
t.puts "def foo; end", "public :foo"
|
||||
t.close
|
||||
assert_warning(/main\.public/) do
|
||||
assert load(t.path, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_private_in_wrapped_load
|
||||
Tempfile.create(["test_private_in_wrapped_load", ".rb"]) do |t|
|
||||
t.puts "def foo; end", "private :foo"
|
||||
t.close
|
||||
assert_warning(/main\.private/) do
|
||||
assert load(t.path, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_load_scope
|
||||
bug1982 = '[ruby-core:25039] [Bug #1982]'
|
||||
Tempfile.create(["test_ruby_test_require", ".rb"]) {|t|
|
||||
|
|
|
@ -2685,7 +2685,7 @@ rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
|
|||
static VALUE
|
||||
top_public(int argc, VALUE *argv, VALUE _)
|
||||
{
|
||||
return rb_mod_public(argc, argv, rb_cObject);
|
||||
return rb_mod_public(argc, argv, rb_top_main_class("public"));
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -2705,7 +2705,7 @@ top_public(int argc, VALUE *argv, VALUE _)
|
|||
static VALUE
|
||||
top_private(int argc, VALUE *argv, VALUE _)
|
||||
{
|
||||
return rb_mod_private(argc, argv, rb_cObject);
|
||||
return rb_mod_private(argc, argv, rb_top_main_class("private"));
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -2718,7 +2718,7 @@ top_private(int argc, VALUE *argv, VALUE _)
|
|||
static VALUE
|
||||
top_ruby2_keywords(int argc, VALUE *argv, VALUE module)
|
||||
{
|
||||
return rb_mod_ruby2_keywords(argc, argv, rb_cObject);
|
||||
return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class("ruby2_keywords"));
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Загрузка…
Ссылка в новой задаче