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

ruby-changes:71241

From: Yusuke <ko1@a...>
Date: Tue, 22 Feb 2022 11:56:10 +0900 (JST)
Subject: [ruby-changes:71241] 3af316fa8c (master): Refactor out rb_decorate_message from print_errinfo

https://git.ruby-lang.org/ruby.git/commit/?id=3af316fa8c

From 3af316fa8c48e33c03159e3b0b3bef329e41dee8 Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Tue, 1 Feb 2022 17:03:58 +0900
Subject: Refactor out rb_decorate_message from print_errinfo

rb_decorate_message adds bold escape sequences to message, and the class
name of exception (like " (RuntimeError)) of "message (RuntimeError)").
---
 eval_error.c | 69 ++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 49 insertions(+), 20 deletions(-)

diff --git a/eval_error.c b/eval_error.c
index 95a3b04b67..7486f2777f 100644
--- a/eval_error.c
+++ b/eval_error.c
@@ -89,24 +89,20 @@ static const char reset[] = CSI_BEGIN""CSI_SGR; https://github.com/ruby/ruby/blob/trunk/eval_error.c#L89
 static void
 print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VALUE str, int highlight)
 {
-    const char *einfo = "";
     long elen = 0;
     VALUE mesg;
 
-    if (emesg != Qundef) {
-	if (NIL_P(errat) || RARRAY_LEN(errat) == 0 ||
-	    NIL_P(mesg = RARRAY_AREF(errat, 0))) {
-	    error_pos(str);
-	}
-	else {
-	    write_warn_str(str, mesg);
-	    write_warn(str, ": ");
-	}
+    if (NIL_P(errat) || RARRAY_LEN(errat) == 0 ||
+        NIL_P(mesg = RARRAY_AREF(errat, 0))) {
+        error_pos(str);
+    }
+    else {
+        write_warn_str(str, mesg);
+        write_warn(str, ": ");
+    }
 
-	if (!NIL_P(emesg)) {
-	    einfo = RSTRING_PTR(emesg);
-            elen = RSTRING_LEN(emesg);
-	}
+    if (!NIL_P(emesg)) {
+        elen = RSTRING_LEN(emesg);
     }
 
     if (eclass == rb_eRuntimeError && elen == 0) {
@@ -125,6 +121,39 @@ print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VA https://github.com/ruby/ruby/blob/trunk/eval_error.c#L121
 	    if (highlight) write_warn(str, reset);
 	    write_warn(str, "\n");
 	}
+	else {
+	    write_warn_str(str, emesg);
+	    write_warn(str, "\n");
+        }
+    }
+}
+
+VALUE
+rb_decorate_message(const VALUE eclass, const VALUE emesg, int highlight)
+{
+    const char *einfo = "";
+    long elen = 0;
+
+    VALUE str = rb_str_new2("");
+
+    if (!NIL_P(emesg)) {
+        einfo = RSTRING_PTR(emesg);
+        elen = RSTRING_LEN(emesg);
+    }
+    if (eclass == rb_eRuntimeError && elen == 0) {
+	if (highlight) write_warn(str, underline);
+	write_warn(str, "unhandled exception");
+	if (highlight) write_warn(str, reset);
+    }
+    else {
+	VALUE epath;
+
+	epath = rb_class_name(eclass);
+	if (elen == 0) {
+	    if (highlight) write_warn(str, underline);
+	    write_warn_str(str, epath);
+	    if (highlight) write_warn(str, reset);
+	}
 	else {
             /* emesg is a String instance */
 	    const char *tail = 0;
@@ -149,25 +178,24 @@ print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VA https://github.com/ruby/ruby/blob/trunk/eval_error.c#L178
 		}
 		write_warn2(str, ")", 1);
 		if (highlight) write_warn(str, reset);
-		write_warn2(str, "\n", 1);
 	    }
 	    if (tail && einfo+elen > tail) {
 		if (!highlight) {
+                    write_warn2(str, "\n", 1);
                     write_warn2(str, tail, einfo+elen-tail);
-		    if (einfo[elen-1] != '\n') write_warn2(str, "\n", 1);
 		}
 		else {
 		    elen -= tail - einfo;
 		    einfo = tail;
                     write_warn2(str, "\n", 1);
 		    while (elen > 0) {
+                        write_warn2(str, "\n", 1);
 			tail = memchr(einfo, '\n', elen);
 			if (!tail || tail > einfo) {
 			    write_warn(str, bold);
                             write_warn2(str, einfo, tail ? tail-einfo : elen);
 			    write_warn(str, reset);
 			    if (!tail) {
-				write_warn2(str, "\n", 1);
 				break;
 			    }
 			}
@@ -180,11 +208,10 @@ print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VA https://github.com/ruby/ruby/blob/trunk/eval_error.c#L208
 		    }
 		}
 	    }
-	    else if (!epath) {
-		write_warn2(str, "\n", 1);
-	    }
 	}
     }
+
+    return str;
 }
 
 static void
@@ -257,6 +284,7 @@ show_cause(VALUE errinfo, VALUE str, VALUE highlight, VALUE reverse, long backtr https://github.com/ruby/ruby/blob/trunk/eval_error.c#L284
         volatile VALUE eclass = CLASS_OF(cause);
         VALUE errat = rb_get_backtrace(cause);
         VALUE emesg = rb_get_message(cause);
+        emesg = rb_decorate_message(eclass, emesg, RTEST(highlight));
         if (reverse) {
             show_cause(cause, str, highlight, reverse, backtrace_limit, shown_causes);
             print_backtrace(eclass, errat, str, TRUE, backtrace_limit);
@@ -284,6 +312,7 @@ rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlig https://github.com/ruby/ruby/blob/trunk/eval_error.c#L312
 	errat = Qnil;
     }
     eclass = CLASS_OF(errinfo);
+    emesg = rb_decorate_message(eclass, emesg, RTEST(highlight));
     if (reverse) {
 	static const char traceback[] = "Traceback "
 	    "(most recent call last):\n";
-- 
cgit v1.2.1


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

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