Project

General

Profile

Feature #14141 » 2.patch

sorah (Sorah Fukumori), 11/29/2017 01:04 PM

View differences:

NEWS
* Now deprecated [Feature #3072]
* Exception
* Exception#display [Feature #14141]
* Dir
* Dir.glob provides new optional keyword argument, :base.
error.c
/*
* call-seq:
* exception.display(port=$stderr) -> nil
*
* Prints formatted <i>exception</i> on the given port (default to <code>$stderr</code>).
* Format includes backtrace, exception class and exception message.
*
* This method is useful to log caught exceptions.
*
* begin
* some_work
* rescue => e
* e.display
* retry
* end
*/
static VALUE
exc_display(int argc, VALUE *argv, VALUE exc)
{
VALUE out;
if (argc == 0) {
out = rb_stderr;
}
else {
rb_scan_args(argc, argv, "01", &out);
}
rb_ec_error_write(exc, Qundef, out);
return Qnil;
}
/*
* call-seq:
* exception.message -> string
*
* Returns the result of invoking <code>exception.to_s</code>.
......
rb_define_method(rb_eException, "initialize", exc_initialize, -1);
rb_define_method(rb_eException, "==", exc_equal, 1);
rb_define_method(rb_eException, "to_s", exc_to_s, 0);
rb_define_method(rb_eException, "display", exc_display, -1);
rb_define_method(rb_eException, "message", exc_message, 0);
rb_define_method(rb_eException, "inspect", exc_inspect, 0);
rb_define_method(rb_eException, "backtrace", exc_backtrace, 0);
eval_error.c
*/
#ifdef HAVE_BUILTIN___BUILTIN_CONSTANT_P
#define warn_print_io(io, x) RB_GNUC_EXTENSION_BLOCK( \
NIL_P(io) ? \
warn_print(x) : ( \
(__builtin_constant_p(x)) ? \
rb_io_write((io), rb_str_new((x), (long)strlen(x))) : \
rb_io_write((io), rb_str_new2(x)) \
) \
)
#define warn_print(x) RB_GNUC_EXTENSION_BLOCK( \
(__builtin_constant_p(x)) ? \
rb_write_error2((x), (long)strlen(x)) : \
rb_write_error(x) \
)
#else
#define warn_print_io(io, x) NIL_P(io) ? rb_write_error((x)) : rb_io_write((io), rb_str_new2(x))
#define warn_print(x) rb_write_error(x)
#endif
#define warn_print2_io(io,x,l) NIL_P(io) ? warn_print2(x,l) : rb_io_write((io), rb_str_new((x),(l)))
#define warn_print2(x,l) rb_write_error2((x),(l))
#define warn_print_str_io(io,x) NIL_P(io) ? rb_write_error_str(x) : rb_io_write((io), (x))
#define warn_print_str(x) rb_write_error_str(x)
static VALUE error_pos_str(void);
......
}
static void
print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, int colored)
print_errinfo(const VALUE eclass, const VALUE errat, const VALUE emesg, const VALUE io, int colored)
{
static const char underline[] = "\033[4;1m";
static const char bold[] = "\033[1m";
......
error_pos();
}
else {
warn_print_str(mesg);
warn_print(": ");
warn_print_str_io(io, mesg);
warn_print_io(io, ": ");
}
if (colored) warn_print(bold);
if (colored) warn_print_io(io, bold);
if (!NIL_P(emesg)) {
einfo = RSTRING_PTR(emesg);
......
}
if (eclass == rb_eRuntimeError && elen == 0) {
if (colored) warn_print(underline);
warn_print("unhandled exception\n");
if (colored) warn_print_io(io, underline);
warn_print_io(io, "unhandled exception\n");
}
else {
VALUE epath;
epath = rb_class_name(eclass);
if (elen == 0) {
if (colored) warn_print(underline);
warn_print_str(epath);
warn_print("\n");
if (colored) warn_print_io(io, underline);
warn_print_str_io(io, epath);
warn_print_io(io, "\n");
}
else {
const char *tail = 0;
......
len = tail - einfo;
tail++; /* skip newline */
}
warn_print_str(tail ? rb_str_subseq(emesg, 0, len) : emesg);
warn_print_str_io(io, tail ? rb_str_subseq(emesg, 0, len) : emesg);
if (epath) {
warn_print(" (");
if (colored) warn_print(underline);
warn_print_str(epath);
if (colored) warn_print(reset);
if (colored) warn_print(bold);
warn_print(")\n");
warn_print_io(io, " (");
if (colored) warn_print_io(io, underline);
warn_print_str_io(io, epath);
if (colored) warn_print_io(io, reset);
if (colored) warn_print_io(io, bold);
warn_print_io(io, ")\n");
}
if (tail) {
warn_print_str(rb_str_subseq(emesg, tail - einfo, elen - len - 1));
warn_print_str_io(io, rb_str_subseq(emesg, tail - einfo, elen - len - 1));
}
if (tail ? einfo[elen-1] != '\n' : !epath) warn_print2("\n", 1);
if (tail ? einfo[elen-1] != '\n' : !epath) warn_print2_io(io, "\n", 1);
}
}
if (colored) warn_print(reset);
if (colored) warn_print_io(io, reset);
}
static void
print_backtrace(const VALUE eclass, const VALUE errat, int reverse)
print_backtrace(const VALUE eclass, const VALUE errat, const VALUE io, int reverse)
{
if (!NIL_P(errat)) {
long i;
......
if (RB_TYPE_P(line, T_STRING)) {
VALUE str = rb_str_new_cstr("\t");
if (reverse) rb_str_catf(str, "%*ld: ", width, len - i);
warn_print_str(rb_str_catf(str, "from %"PRIsVALUE"\n", line));
warn_print_str_io(io, rb_str_catf(str, "from %"PRIsVALUE"\n", line));
}
if (skip && i == TRACE_HEAD && len > TRACE_MAX) {
warn_print_str(rb_sprintf("\t ... %ld levels...\n",
warn_print_str_io(io, rb_sprintf("\t ... %ld levels...\n",
len - TRACE_HEAD - TRACE_TAIL));
i = len - TRACE_TAIL;
}
......
}
void
rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo)
rb_ec_error_write(volatile VALUE errinfo, volatile VALUE errat, volatile VALUE io)
{
volatile VALUE errat = Qundef;
volatile int raised_flag = ec->raised_flag;
volatile VALUE eclass = Qundef, emesg = Qundef;
if (NIL_P(errinfo))
return;
rb_ec_raised_clear(ec);
EC_PUSH_TAG(ec);
if (EC_EXEC_TAG() == TAG_NONE) {
errat = rb_get_backtrace(errinfo);
}
else if (errat == Qundef) {
if (errat == Qundef) {
errat = Qnil;
}
else if (eclass == Qundef || emesg != Qundef) {
goto error;
}
if ((eclass = CLASS_OF(errinfo)) != Qundef) {
VALUE e = rb_check_funcall(errinfo, rb_intern("message"), 0, 0);
if (e != Qundef) {
......
}
}
if (rb_stderr_tty_p()) {
warn_print("\033[1mTraceback \033[m(most recent call last):\n");
print_backtrace(eclass, errat, TRUE);
print_errinfo(eclass, errat, emesg, TRUE);
warn_print_io(io, "\033[1mTraceback \033[m(most recent call last):\n");
print_backtrace(eclass, errat, io, TRUE);
print_errinfo(eclass, errat, emesg, io, TRUE);
}
else {
print_errinfo(eclass, errat, emesg, FALSE);
print_backtrace(eclass, errat, FALSE);
print_errinfo(eclass, errat, emesg, io, FALSE);
print_backtrace(eclass, errat, io, FALSE);
}
error:
}
void
rb_ec_error_print(rb_execution_context_t * volatile ec, volatile VALUE errinfo)
{
volatile int raised_flag = ec->raised_flag;
volatile VALUE errat;
if (NIL_P(errinfo))
return;
rb_ec_raised_clear(ec);
EC_PUSH_TAG(ec);
if (EC_EXEC_TAG() == TAG_NONE) {
errat = rb_get_backtrace(errinfo);
}
rb_ec_error_write(errinfo, errat, Qnil);
error:
EC_POP_TAG();
ec->errinfo = errinfo;
rb_ec_raised_set(ec, raised_flag);
(1-1/3)