Skip to content

Commit 36e31b0

Browse files
committed
error.c: Refactoring
Factor out from rb_error_write the responsibility to check if stderr is a tty.
1 parent c53bdb8 commit 36e31b0

File tree

2 files changed

+68
-43
lines changed

2 files changed

+68
-43
lines changed

error.c

+60-32
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,60 @@ exc_s_to_tty_p(VALUE self)
12471247
return RBOOL(rb_stderr_tty_p());
12481248
}
12491249

1250+
static VALUE
1251+
check_highlight_keyword(VALUE opt)
1252+
{
1253+
VALUE highlight = Qnil;
1254+
1255+
if (!NIL_P(opt)) {
1256+
static VALUE kw_highlight;
1257+
if (!kw_highlight) kw_highlight = ID2SYM(rb_intern_const("highlight"));
1258+
1259+
highlight = rb_hash_aref(opt, kw_highlight);
1260+
1261+
switch (highlight) {
1262+
default:
1263+
rb_bool_expected(highlight, "highlight");
1264+
UNREACHABLE;
1265+
case Qundef: highlight = Qnil; break;
1266+
case Qtrue: case Qfalse: case Qnil: break;
1267+
}
1268+
}
1269+
1270+
if (NIL_P(highlight)) {
1271+
highlight = rb_stderr_tty_p() ? Qtrue : Qfalse;
1272+
}
1273+
1274+
return highlight;
1275+
}
1276+
1277+
static VALUE
1278+
check_order_keyword(VALUE opt)
1279+
{
1280+
VALUE order = Qnil;
1281+
1282+
if (!NIL_P(opt)) {
1283+
static VALUE kw_order;
1284+
if (!kw_order) kw_order = ID2SYM(rb_intern_const("order"));
1285+
1286+
order = rb_hash_aref(opt, kw_order);
1287+
1288+
if (order != Qnil) {
1289+
ID id = rb_check_id(&order);
1290+
if (id == id_bottom) order = Qtrue;
1291+
else if (id == id_top) order = Qfalse;
1292+
else {
1293+
rb_raise(rb_eArgError, "expected :top or :bottom as "
1294+
"order: %+"PRIsVALUE, order);
1295+
}
1296+
}
1297+
}
1298+
1299+
if (NIL_P(order)) order = Qfalse;
1300+
1301+
return order;
1302+
}
1303+
12501304
/*
12511305
* call-seq:
12521306
* exception.full_message(highlight: bool, order: [:top or :bottom]) -> string
@@ -1269,44 +1323,18 @@ static VALUE
12691323
exc_full_message(int argc, VALUE *argv, VALUE exc)
12701324
{
12711325
VALUE opt, str, emesg, errat;
1272-
enum {kw_highlight, kw_order, kw_max_};
1273-
static ID kw[kw_max_];
1274-
VALUE args[kw_max_] = {Qnil, Qnil};
1326+
VALUE highlight, order;
12751327

12761328
rb_scan_args(argc, argv, "0:", &opt);
1277-
if (!NIL_P(opt)) {
1278-
if (!kw[0]) {
1279-
#define INIT_KW(n) kw[kw_##n] = rb_intern_const(#n)
1280-
INIT_KW(highlight);
1281-
INIT_KW(order);
1282-
#undef INIT_KW
1283-
}
1284-
rb_get_kwargs(opt, kw, 0, kw_max_, args);
1285-
switch (args[kw_highlight]) {
1286-
default:
1287-
rb_bool_expected(args[kw_highlight], "highlight");
1288-
UNREACHABLE;
1289-
case Qundef: args[kw_highlight] = Qnil; break;
1290-
case Qtrue: case Qfalse: case Qnil: break;
1291-
}
1292-
if (args[kw_order] == Qundef) {
1293-
args[kw_order] = Qnil;
1294-
}
1295-
else {
1296-
ID id = rb_check_id(&args[kw_order]);
1297-
if (id == id_bottom) args[kw_order] = Qtrue;
1298-
else if (id == id_top) args[kw_order] = Qfalse;
1299-
else {
1300-
rb_raise(rb_eArgError, "expected :top or :bottom as "
1301-
"order: %+"PRIsVALUE, args[kw_order]);
1302-
}
1303-
}
1304-
}
1329+
1330+
highlight = check_highlight_keyword(opt);
1331+
order = check_order_keyword(opt);
1332+
13051333
str = rb_str_new2("");
13061334
errat = rb_get_backtrace(exc);
13071335
emesg = rb_get_message(exc);
13081336

1309-
rb_error_write(exc, emesg, errat, str, args[kw_highlight], args[kw_order]);
1337+
rb_error_write(exc, emesg, errat, str, highlight, order);
13101338
return str;
13111339
}
13121340

eval_error.c

+8-11
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VA
200200
else {
201201
elen -= tail - einfo;
202202
einfo = tail;
203+
write_warn2(str, "\n", 1);
203204
while (elen > 0) {
204205
tail = memchr(einfo, '\n', elen);
205206
if (!tail || tail > einfo) {
@@ -300,10 +301,10 @@ show_cause(VALUE errinfo, VALUE str, VALUE highlight, VALUE reverse, long backtr
300301
if (reverse) {
301302
show_cause(cause, str, highlight, reverse, backtrace_limit, shown_causes);
302303
print_backtrace(eclass, errat, str, TRUE, backtrace_limit);
303-
print_errinfo(eclass, errat, emesg, str, highlight!=0);
304+
print_errinfo(eclass, errat, emesg, str, RTEST(highlight));
304305
}
305306
else {
306-
print_errinfo(eclass, errat, emesg, str, highlight!=0);
307+
print_errinfo(eclass, errat, emesg, str, RTEST(highlight));
307308
print_backtrace(eclass, errat, str, FALSE, backtrace_limit);
308309
show_cause(cause, str, highlight, reverse, backtrace_limit, shown_causes);
309310
}
@@ -324,19 +325,14 @@ rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlig
324325
errat = Qnil;
325326
}
326327
eclass = CLASS_OF(errinfo);
327-
if (NIL_P(reverse)) reverse = Qfalse;
328-
if (NIL_P(highlight)) {
329-
VALUE tty = (VALUE)rb_stderr_tty_p();
330-
if (NIL_P(highlight)) highlight = tty;
331-
}
332328
if (reverse) {
333329
static const char traceback[] = "Traceback "
334330
"(most recent call last):\n";
335331
const int bold_part = rb_strlen_lit("Traceback");
336332
char buff[sizeof(traceback)+sizeof(bold)+sizeof(reset)-2], *p = buff;
337333
const char *msg = traceback;
338334
long len = sizeof(traceback) - 1;
339-
if (highlight) {
335+
if (RTEST(highlight)) {
340336
#define APPEND(s, l) (memcpy(p, s, l), p += (l))
341337
APPEND(bold, sizeof(bold)-1);
342338
APPEND(traceback, bold_part);
@@ -348,10 +344,10 @@ rb_error_write(VALUE errinfo, VALUE emesg, VALUE errat, VALUE str, VALUE highlig
348344
write_warn2(str, msg, len);
349345
show_cause(errinfo, str, highlight, reverse, backtrace_limit, &shown_causes);
350346
print_backtrace(eclass, errat, str, TRUE, backtrace_limit);
351-
print_errinfo(eclass, errat, emesg, str, highlight!=0);
347+
print_errinfo(eclass, errat, emesg, str, RTEST(highlight));
352348
}
353349
else {
354-
print_errinfo(eclass, errat, emesg, str, highlight!=0);
350+
print_errinfo(eclass, errat, emesg, str, RTEST(highlight));
355351
print_backtrace(eclass, errat, str, FALSE, backtrace_limit);
356352
show_cause(errinfo, str, highlight, reverse, backtrace_limit, &shown_causes);
357353
}
@@ -380,7 +376,8 @@ rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo)
380376

381377
if (!written) {
382378
written = true;
383-
rb_error_write(errinfo, emesg, errat, Qnil, Qnil, Qfalse);
379+
VALUE highlight = rb_stderr_tty_p() ? Qtrue : Qfalse;
380+
rb_error_write(errinfo, emesg, errat, Qnil, highlight, Qfalse);
384381
}
385382

386383
EC_POP_TAG();

0 commit comments

Comments
 (0)