diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-03-15 11:19:56 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-03-15 11:19:56 +0000 |
commit | c7c3a275f33ca1f2485a0a496fac8b076f38beed (patch) | |
tree | 5239f55ac4e7d21d09e75ee7c403a15c793903e3 | |
parent | b5a9cd8fb82c33d03e8487d24825690de1697cc0 (diff) | |
download | ruby-c7c3a275f33ca1f2485a0a496fac8b076f38beed.tar.gz |
error.c: rb_sys_fail_path_in
* error.c (syserr_initialize): add optional function name.
* error.c (rb_sys_fail_path_in): rename and move from file.c, and pass
func_name to SystemCallError#initialize.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39763 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | error.c | 32 | ||||
-rw-r--r-- | file.c | 16 | ||||
-rw-r--r-- | internal.h | 4 |
3 files changed, 30 insertions, 22 deletions
@@ -1224,12 +1224,12 @@ syserr_initialize(int argc, VALUE *argv, VALUE self) char *strerror(); #endif const char *err; - VALUE mesg, error; + VALUE mesg, error, func; VALUE klass = rb_obj_class(self); if (klass == rb_eSystemCallError) { st_data_t data = (st_data_t)klass; - rb_scan_args(argc, argv, "11", &mesg, &error); + rb_scan_args(argc, argv, "12", &mesg, &error, &func); if (argc == 1 && FIXNUM_P(mesg)) { error = mesg; mesg = Qnil; } @@ -1243,7 +1243,7 @@ syserr_initialize(int argc, VALUE *argv, VALUE self) } } else { - rb_scan_args(argc, argv, "01", &mesg); + rb_scan_args(argc, argv, "02", &mesg, &func); error = rb_const_get(klass, rb_intern("Errno")); } if (!NIL_P(error)) err = strerror(NUM2INT(error)); @@ -1253,7 +1253,10 @@ syserr_initialize(int argc, VALUE *argv, VALUE self) VALUE str = StringValue(mesg); rb_encoding *me = rb_enc_get(mesg); - mesg = rb_sprintf("%s - %"PRIsVALUE, err, mesg); + if (NIL_P(func)) + mesg = rb_sprintf("%s - %"PRIsVALUE, err, mesg); + else + mesg = rb_sprintf("%s @ %"PRIsVALUE" - %"PRIsVALUE, err, func, mesg); if (le != me && rb_enc_asciicompat(me)) { le = me; }/* else assume err is non ASCII string. */ @@ -1907,6 +1910,27 @@ rb_sys_fail_str(VALUE mesg) rb_exc_raise(make_errno_exc_str(mesg)); } +#ifdef RUBY_FUNCTION_NAME_STRING +void +rb_sys_fail_path_in(const char *func_name, VALUE path) +{ + int n = errno; + VALUE args[2]; + + errno = 0; + if (!path) path = Qnil; + if (n == 0) { + const char *s = !NIL_P(path) ? RSTRING_PTR(path) : ""; + if (!func_name) func_name = "(null)"; + rb_bug("rb_sys_fail_path_in(%s, %s) - errno == 0", + func_name, s); + } + args[0] = path; + args[1] = rb_str_new_cstr(func_name); + rb_exc_raise(rb_class_new_instance(2, args, get_syserr(n))); +} +#endif + void rb_mod_sys_fail(VALUE mod, const char *mesg) { @@ -102,22 +102,6 @@ int flock(int, int); #define STAT(p, s) stat((p), (s)) #endif -#ifdef RUBY_FUNCTION_NAME_STRING -void -rb_sys_fail_path_with_func(const char *func_name, VALUE path) -{ - VALUE mesg = rb_str_new_cstr(func_name); - if (!NIL_P(path)) { - /* RUBY_FUNCTION_NAME_STRING, aka __func__/__FUNCTION__ is not a - * preprocessor macro but a static constant array, so string - * literal concatenation is not allowed */ - rb_str_buf_cat2(mesg, ": "); - rb_str_buf_append(mesg, path); - } - rb_sys_fail_str(mesg); -} -#endif - #if defined(__BEOS__) || defined(__HAIKU__) /* should not change ID if -1 */ static int be_chown(const char *path, uid_t owner, gid_t group) diff --git a/internal.h b/internal.h index 56c096a009..b099f24352 100644 --- a/internal.h +++ b/internal.h @@ -126,11 +126,11 @@ void Init_File(void); # if defined __GNUC__ && __GNUC__ >= 4 # pragma GCC visibility push(default) # endif -NORETURN(void rb_sys_fail_path_with_func(const char *func_name, VALUE path)); +NORETURN(void rb_sys_fail_path_in(const char *func_name, VALUE path)); # if defined __GNUC__ && __GNUC__ >= 4 # pragma GCC visibility pop # endif -# define rb_sys_fail_path(path) rb_sys_fail_path_with_func(RUBY_FUNCTION_NAME_STRING, path) +# define rb_sys_fail_path(path) rb_sys_fail_path_in(RUBY_FUNCTION_NAME_STRING, path) #else # define rb_sys_fail_path(path) rb_sys_fail_str(path) #endif |