Skip to content

Commit

Permalink
merge revision(s) 4329554,b5c74d548872388921402ff2db36be15e924a89b: […
Browse files Browse the repository at this point in the history
…Backport #19985]

	[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`.
	---
	 load.c                    | 14 +++++++++++---
	 test/ruby/test_require.rb | 26 +++++++++++++++++++++-----
	 2 files changed, 32 insertions(+), 8 deletions(-)

	Ease the `Encoding::CompatibilityError` test failure

	At the time this test first started using `assert_raise_with_message`,
	it did not touch `Encoding.default_internal`.
	---
	 test/ruby/test_require.rb | 3 ++-
	 1 file changed, 2 insertions(+), 1 deletion(-)
  • Loading branch information
nagachika committed Nov 9, 2023
1 parent 8bbf909 commit 2aaa9af
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
14 changes: 11 additions & 3 deletions load.c
Expand Up @@ -893,6 +893,7 @@ load_unlock(rb_vm_t *vm, const char *ftptr, int done)
}
}

static VALUE rb_require_string_internal(VALUE fname);

/*
* call-seq:
Expand Down Expand Up @@ -955,7 +956,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);
Expand Down Expand Up @@ -1164,7 +1165,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;
Expand Down Expand Up @@ -1289,6 +1289,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));
Expand All @@ -1306,7 +1312,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);
}

#if EXTSTATIC
Expand Down
29 changes: 23 additions & 6 deletions test/ruby/test_require.rb
Expand Up @@ -6,11 +6,27 @@

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
Expand Down Expand Up @@ -52,7 +68,8 @@ def test_require_too_long_filename
def test_require_nonascii
bug3758 = '[ruby-core:31915]'
["\u{221e}", "\x82\xa0".force_encoding("cp932")].each do |path|
assert_raise_with_message(LoadError, /#{path}\z/, bug3758) {require path}
e = assert_raise(LoadError, bug3758) {require path}
assert_operator(e.message, :end_with?, path, bug3758)
end
end

Expand Down
2 changes: 1 addition & 1 deletion version.h
Expand Up @@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 2
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 127
#define RUBY_PATCHLEVEL 128

#include "ruby/version.h"
#include "ruby/internal/abi.h"
Expand Down

0 comments on commit 2aaa9af

Please sign in to comment.