From 075d98c7dc0cee294f03ed0ed26e1e8d8876b2b9 Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 7 Mar 2012 07:30:31 +0000 Subject: * error.c (rb_load_fail): should honor encoding. * load.c (load_failed): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34944 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ error.c | 27 +++++++++++++++++---------- internal.h | 2 +- load.c | 4 +--- ruby.c | 2 +- test/ruby/test_require.rb | 6 ++++-- 6 files changed, 30 insertions(+), 17 deletions(-) diff --git a/ChangeLog b/ChangeLog index 11655a4ab7..c73efc3f79 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Wed Mar 7 16:30:24 2012 Nobuyoshi Nakada + + * error.c (rb_load_fail): should honor encoding. + + * load.c (load_failed): ditto. + Wed Mar 7 12:26:25 2012 Nobuyoshi Nakada * error.c (rb_load_fail): use path as a string, not char*. diff --git a/error.c b/error.c index b8f9f06654..0562de5d84 100644 --- a/error.c +++ b/error.c @@ -1740,19 +1740,26 @@ rb_raise(VALUE exc, const char *fmt, ...) rb_exc_raise(rb_exc_new3(exc, mesg)); } +NORETURN(static void raise_loaderror(VALUE path, VALUE mesg)); + +static void +raise_loaderror(VALUE path, VALUE mesg) +{ + VALUE err = rb_exc_new3(rb_eLoadError, mesg); + rb_ivar_set(err, rb_intern("@path"), path); + rb_exc_raise(err); +} + void rb_loaderror(const char *fmt, ...) { va_list args; VALUE mesg; - VALUE err; va_start(args, fmt); mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args); va_end(args); - err = rb_exc_new3(rb_eLoadError, mesg); - rb_ivar_set(err, rb_intern("@path"), Qnil); - rb_exc_raise(err); + raise_loaderror(Qnil, mesg); } void @@ -1760,14 +1767,11 @@ rb_loaderror_with_path(VALUE path, const char *fmt, ...) { va_list args; VALUE mesg; - VALUE err; va_start(args, fmt); mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args); va_end(args); - err = rb_exc_new3(rb_eLoadError, mesg); - rb_ivar_set(err, rb_intern("@path"), path); - rb_exc_raise(err); + raise_loaderror(path, mesg); } void @@ -1908,9 +1912,12 @@ rb_sys_warning(const char *fmt, ...) } void -rb_load_fail(VALUE path) +rb_load_fail(VALUE path, const char *err) { - rb_loaderror_with_path(path, "%s -- %s", strerror(errno), RSTRING_PTR(path)); + VALUE mesg = rb_str_buf_new_cstr(err); + rb_str_cat2(mesg, " -- "); + rb_str_append(mesg, path); /* should be ASCII compatible */ + raise_loaderror(path, mesg); } void diff --git a/internal.h b/internal.h index e25062bc2f..f5a463e83b 100644 --- a/internal.h +++ b/internal.h @@ -116,7 +116,7 @@ VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase); /* load.c */ VALUE rb_get_load_path(void); -NORETURN(void rb_load_fail(VALUE)); +NORETURN(void rb_load_fail(VALUE, const char*)); /* math.c */ VALUE rb_math_atan2(VALUE, VALUE); diff --git a/load.c b/load.c index 3d877a83c9..18651dd3a2 100644 --- a/load.c +++ b/load.c @@ -588,12 +588,10 @@ search_required(VALUE fname, volatile VALUE *path, int safe_level) return type ? 's' : 'r'; } -void rb_loaderror_with_path(VALUE path, const char *fmt, ...); - static void load_failed(VALUE fname) { - rb_loaderror_with_path(fname, "cannot load such file -- %s", RSTRING_PTR(fname)); + rb_load_fail(fname, "cannot load such file"); } static VALUE diff --git a/ruby.c b/ruby.c index c0e3b030e2..c18787458b 100644 --- a/ruby.c +++ b/ruby.c @@ -1524,7 +1524,7 @@ load_file_internal(VALUE arg) } #endif if ((fd = rb_cloexec_open(fname, mode, 0)) < 0) { - rb_load_fail(fname_v); + rb_load_fail(fname_v, strerror(errno)); } rb_update_max_fd(fd); diff --git a/test/ruby/test_require.rb b/test/ruby/test_require.rb index e0e885dce6..de99fbdff4 100644 --- a/test/ruby/test_require.rb +++ b/test/ruby/test_require.rb @@ -50,8 +50,10 @@ class TestRequire < Test::Unit::TestCase def test_require_nonascii bug3758 = '[ruby-core:31915]' - e = assert_raise(LoadError, bug3758) {require "\u{221e}"} - assert_match(/\u{221e}\z/, e.message, bug3758) + ["\u{221e}", "\x82\xa0".force_encoding("cp932")].each do |path| + e = assert_raise(LoadError, bug3758) {require path} + assert_match(/#{path}\z/, e.message, bug3758) + end end def test_require_path_home_1 -- cgit v1.2.3