[Bug #19985] Raise LoadError with the converted feature name

`Kernel#require` converts feature name objects that have the `to_path`
method such as `Pathname`, but had used the original object on error
and had resulted in an unexpected `TypeError`.
This commit is contained in:
Nobuyoshi Nakada 2023-11-06 16:59:55 +09:00
Родитель 7a6c72e03d
Коммит 4329554f17
2 изменённых файлов: 32 добавлений и 8 удалений

14
load.c
Просмотреть файл

@ -935,6 +935,7 @@ load_unlock(rb_vm_t *vm, const char *ftptr, int done)
}
}
static VALUE rb_require_string_internal(VALUE fname);
/*
* call-seq:
@ -997,7 +998,7 @@ rb_f_require_relative(VALUE obj, VALUE fname)
rb_loaderror("cannot infer basepath");
}
base = rb_file_dirname(base);
return rb_require_string(rb_file_absolute_path(fname, base));
return rb_require_string_internal(rb_file_absolute_path(fname, base));
}
typedef int (*feature_func)(rb_vm_t *vm, const char *feature, const char *ext, int rb, int expanded, const char **fn);
@ -1210,7 +1211,6 @@ require_internal(rb_execution_context_t *ec, VALUE fname, int exception, bool wa
volatile bool reset_ext_config = false;
struct rb_ext_config prev_ext_config;
fname = rb_get_path(fname);
path = rb_str_encode_ospath(fname);
RUBY_DTRACE_HOOK(REQUIRE_ENTRY, RSTRING_PTR(fname));
saved_path = path;
@ -1335,6 +1335,12 @@ ruby_require_internal(const char *fname, unsigned int len)
VALUE
rb_require_string(VALUE fname)
{
return rb_require_string_internal(FilePathValue(fname));
}
static VALUE
rb_require_string_internal(VALUE fname)
{
rb_execution_context_t *ec = GET_EC();
int result = require_internal(ec, fname, 1, RTEST(ruby_verbose));
@ -1352,7 +1358,9 @@ rb_require_string(VALUE fname)
VALUE
rb_require(const char *fname)
{
return rb_require_string(rb_str_new_cstr(fname));
struct RString fake;
VALUE str = rb_setup_fake_str(&fake, fname, strlen(fname), 0);
return rb_require_string_internal(str);
}
static int

Просмотреть файл

@ -6,11 +6,27 @@ require 'tmpdir'
class TestRequire < Test::Unit::TestCase
def test_load_error_path
filename = "should_not_exist"
error = assert_raise(LoadError) do
require filename
end
assert_equal filename, error.path
Tempfile.create(["should_not_exist", ".rb"]) {|t|
filename = t.path
t.close
File.unlink(filename)
error = assert_raise(LoadError) do
require filename
end
assert_equal filename, error.path
# with --disable=gems
assert_separately(["-", filename], "#{<<~"begin;"}\n#{<<~'end;'}")
begin;
filename = ARGV[0]
path = Struct.new(:to_path).new(filename)
error = assert_raise(LoadError) do
require path
end
assert_equal filename, error.path
end;
}
end
def test_require_invalid_shared_object