зеркало из https://github.com/github/ruby.git
Fix test code and extension to avoid using gvars and Kernel methods
This commit is contained in:
Родитель
ac5ac48a36
Коммит
50a0552bd7
|
@ -1,5 +1,7 @@
|
|||
#include "ruby.h"
|
||||
|
||||
static VALUE rb_mEnsureAndCallcc;
|
||||
|
||||
struct require_data {
|
||||
VALUE obj;
|
||||
VALUE fname;
|
||||
|
@ -16,9 +18,9 @@ call_require(VALUE arg)
|
|||
static VALUE
|
||||
call_ensure(VALUE _)
|
||||
{
|
||||
VALUE v = rb_gv_get("$ensure_called");
|
||||
VALUE v = rb_iv_get(rb_mEnsureAndCallcc, "@ensure_called");
|
||||
int called = FIX2INT(v) + 1;
|
||||
rb_gv_set("$ensure_called", INT2FIX(called));
|
||||
rb_iv_set(rb_mEnsureAndCallcc, "@ensure_called", INT2FIX(called));
|
||||
return Qnil;
|
||||
}
|
||||
|
||||
|
@ -32,8 +34,17 @@ require_with_ensure(VALUE self, VALUE fname)
|
|||
return rb_ensure(call_require, (VALUE)&data, call_ensure, Qnil);
|
||||
}
|
||||
|
||||
static VALUE
|
||||
ensure_called(VALUE self)
|
||||
{
|
||||
return rb_iv_get(rb_mEnsureAndCallcc, "@ensure_called");
|
||||
}
|
||||
|
||||
void
|
||||
Init_ensure_and_callcc(void)
|
||||
{
|
||||
rb_define_method(rb_mKernel, "require_with_ensure", require_with_ensure, 1);
|
||||
rb_mEnsureAndCallcc = rb_define_module("EnsureAndCallcc");
|
||||
rb_iv_set(rb_mEnsureAndCallcc, "@ensure_called", INT2FIX(0));
|
||||
rb_define_singleton_method(rb_mEnsureAndCallcc, "ensure_called", ensure_called, 0);
|
||||
rb_define_singleton_method(rb_mEnsureAndCallcc, "require_with_ensure", require_with_ensure, 1);
|
||||
}
|
||||
|
|
|
@ -3,8 +3,6 @@
|
|||
require 'test/unit'
|
||||
|
||||
class TestEnsureAndCallcc < Test::Unit::TestCase
|
||||
require '-test-/ensure_and_callcc'
|
||||
|
||||
def test_bug20655_dir_chdir_using_rb_ensure
|
||||
need_continuation
|
||||
called = 0
|
||||
|
@ -21,9 +19,11 @@ class TestEnsureAndCallcc < Test::Unit::TestCase
|
|||
|
||||
def test_bug20655_extension_using_rb_ensure
|
||||
need_continuation
|
||||
$ensure_called = 0
|
||||
require_with_ensure(File.join(__dir__, 'required'))
|
||||
assert_equal(1, $ensure_called, "BUG #20655: ensure called unexpectedly in the required script even without exceptions")
|
||||
require '-test-/ensure_and_callcc'
|
||||
assert_equal(0, EnsureAndCallcc.ensure_called)
|
||||
EnsureAndCallcc.require_with_ensure(File.join(__dir__, 'required'))
|
||||
assert_equal(1, EnsureAndCallcc.ensure_called,
|
||||
"BUG #20655: ensure called unexpectedly in the required script even without exceptions")
|
||||
end
|
||||
|
||||
private
|
||||
|
|
Загрузка…
Ссылка в новой задаче