[前][次][番号順一覧][スレッド一覧]

ruby-changes:27711

From: nobu <ko1@a...>
Date: Fri, 15 Mar 2013 20:20:39 +0900 (JST)
Subject: [ruby-changes:27711] nobu:r39763 (trunk): error.c: rb_sys_fail_path_in

nobu	2013-03-15 20:19:56 +0900 (Fri, 15 Mar 2013)

  New Revision: 39763

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39763

  Log:
    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.

  Modified files:
    trunk/error.c
    trunk/file.c
    trunk/internal.h

Index: error.c
===================================================================
--- error.c	(revision 39762)
+++ error.c	(revision 39763)
@@ -1224,12 +1224,12 @@ syserr_initialize(int argc, VALUE *argv, https://github.com/ruby/ruby/blob/trunk/error.c#L1224
     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, https://github.com/ruby/ruby/blob/trunk/error.c#L1243
 	}
     }
     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, https://github.com/ruby/ruby/blob/trunk/error.c#L1253
 	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) https://github.com/ruby/ruby/blob/trunk/error.c#L1910
     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)
 {
Index: internal.h
===================================================================
--- internal.h	(revision 39762)
+++ internal.h	(revision 39763)
@@ -126,11 +126,11 @@ void Init_File(void); https://github.com/ruby/ruby/blob/trunk/internal.h#L126
 # 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
Index: file.c
===================================================================
--- file.c	(revision 39762)
+++ file.c	(revision 39763)
@@ -102,22 +102,6 @@ int flock(int, int); https://github.com/ruby/ruby/blob/trunk/file.c#L102
 #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)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]