1 /**********************************************************************
6 created at: Mon Aug 9 16:11:34 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/internal/config.h"
26 #ifdef HAVE_SYS_WAIT_H
27 # include <sys/wait.h>
31 # include <AvailabilityMacros.h>
35 #include "internal/class.h"
36 #include "internal/error.h"
37 #include "internal/eval.h"
38 #include "internal/hash.h"
39 #include "internal/io.h"
40 #include "internal/load.h"
41 #include "internal/object.h"
42 #include "internal/process.h"
43 #include "internal/string.h"
44 #include "internal/symbol.h"
45 #include "internal/thread.h"
46 #include "internal/variable.h"
47 #include "ruby/encoding.h"
49 #include "ruby/util.h"
50 #include "ruby_assert.h"
57 * \addtogroup exception
62 #define EXIT_SUCCESS 0
66 #define WIFEXITED(status) 1
70 #define WEXITSTATUS(status) (status)
73 VALUE
rb_iseqw_local_variables(VALUE iseqval
);
74 VALUE
rb_iseqw_new(const rb_iseq_t
*);
75 int rb_str_end_with_asciichar(VALUE str
, int c
);
77 long rb_backtrace_length_limit
= -1;
79 VALUE rb_eEWOULDBLOCK
;
80 VALUE rb_eEINPROGRESS
;
81 static VALUE rb_mWarning
;
82 static VALUE rb_cWarningBuffer
;
85 static ID id_category
;
86 static ID id_deprecated
;
87 static ID id_experimental
;
88 static ID id_performance
;
89 static VALUE sym_category
;
90 static VALUE sym_highlight
;
92 st_table
*id2enum
, *enum2id
;
95 extern const char *rb_dynamic_description
;
100 #define defined_error(name, num) if (err == (num)) return (name);
101 #define undefined_error(name)
102 #include "known_errors.inc"
104 #undef undefined_error
109 err_position_0(char *buf
, long len
, const char *file
, int line
)
114 else if (line
== 0) {
115 return snprintf(buf
, len
, "%s: ", file
);
118 return snprintf(buf
, len
, "%s:%d: ", file
, line
);
122 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 5, 0)
124 err_vcatf(VALUE str
, const char *pre
, const char *file
, int line
,
125 const char *fmt
, va_list args
)
128 rb_str_cat2(str
, file
);
129 if (line
) rb_str_catf(str
, ":%d", line
);
130 rb_str_cat2(str
, ": ");
132 if (pre
) rb_str_cat2(str
, pre
);
133 rb_str_vcatf(str
, fmt
, args
);
137 static VALUE
syntax_error_with_path(VALUE
, VALUE
, VALUE
*, rb_encoding
*);
140 rb_syntax_error_append(VALUE exc
, VALUE file
, int line
, int column
,
141 rb_encoding
*enc
, const char *fmt
, va_list args
)
143 const char *fn
= NIL_P(file
) ? NULL
: RSTRING_PTR(file
);
145 VALUE mesg
= rb_enc_str_new(0, 0, enc
);
146 err_vcatf(mesg
, NULL
, fn
, line
, fmt
, args
);
147 rb_str_cat2(mesg
, "\n");
148 rb_write_error_str(mesg
);
152 exc
= syntax_error_with_path(exc
, file
, &mesg
, enc
);
153 err_vcatf(mesg
, NULL
, fn
, line
, fmt
, args
);
159 static unsigned int warning_disabled_categories
= (
160 (1U << RB_WARN_CATEGORY_DEPRECATED
) |
161 ~RB_WARN_CATEGORY_DEFAULT_BITS
);
164 rb_warning_category_mask(VALUE category
)
166 return 1U << rb_warning_category_from_name(category
);
169 rb_warning_category_t
170 rb_warning_category_from_name(VALUE category
)
174 Check_Type(category
, T_SYMBOL
);
175 if (!(cat_id
= rb_check_id(&category
)) ||
176 !st_lookup(warning_categories
.id2enum
, cat_id
, &cat_value
)) {
177 rb_raise(rb_eArgError
, "unknown category: %"PRIsVALUE
, category
);
179 return (rb_warning_category_t
)cat_value
;
183 rb_warning_category_to_name(rb_warning_category_t category
)
186 if (!st_lookup(warning_categories
.enum2id
, category
, &id
)) {
187 rb_raise(rb_eArgError
, "invalid category: %d", (int)category
);
189 return id
? ID2SYM(id
) : Qnil
;
193 rb_warning_category_update(unsigned int mask
, unsigned int bits
)
195 warning_disabled_categories
&= ~mask
;
196 warning_disabled_categories
|= mask
& ~bits
;
200 rb_warning_category_enabled_p(rb_warning_category_t category
)
202 return !(warning_disabled_categories
& (1U << category
));
207 * Warning[category] -> true or false
209 * Returns the flag to show the warning messages for +category+.
210 * Supported categories are:
213 * deprecation warnings
214 * * assignment of non-nil value to <code>$,</code> and <code>$;</code>
215 * * keyword arguments
219 * experimental features
224 * * Shape variation limit
228 rb_warning_s_aref(VALUE mod
, VALUE category
)
230 rb_warning_category_t cat
= rb_warning_category_from_name(category
);
231 return RBOOL(rb_warning_category_enabled_p(cat
));
236 * Warning[category] = flag -> flag
238 * Sets the warning flags for +category+.
239 * See Warning.[] for the categories.
243 rb_warning_s_aset(VALUE mod
, VALUE category
, VALUE flag
)
245 unsigned int mask
= rb_warning_category_mask(category
);
246 unsigned int disabled
= warning_disabled_categories
;
251 warning_disabled_categories
= disabled
;
257 * categories -> array
259 * Returns a list of the supported category symbols.
263 rb_warning_s_categories(VALUE mod
)
265 st_index_t num
= warning_categories
.id2enum
->num_entries
;
266 ID
*ids
= ALLOCA_N(ID
, num
);
267 num
= st_keys(warning_categories
.id2enum
, ids
, num
);
268 VALUE ary
= rb_ary_new_capa(num
);
269 for (st_index_t i
= 0; i
< num
; ++i
) {
270 rb_ary_push(ary
, ID2SYM(ids
[i
]));
272 return rb_ary_freeze(ary
);
277 * warn(msg, category: nil) -> nil
279 * Writes warning message +msg+ to $stderr. This method is called by
280 * Ruby for all emitted warnings. A +category+ may be included with
283 * See the documentation of the Warning module for how to customize this.
287 rb_warning_s_warn(int argc
, VALUE
*argv
, VALUE mod
)
291 VALUE category
= Qnil
;
293 rb_scan_args(argc
, argv
, "1:", &str
, &opt
);
294 if (!NIL_P(opt
)) rb_get_kwargs(opt
, &id_category
, 0, 1, &category
);
296 Check_Type(str
, T_STRING
);
297 rb_must_asciicompat(str
);
298 if (!NIL_P(category
)) {
299 rb_warning_category_t cat
= rb_warning_category_from_name(category
);
300 if (!rb_warning_category_enabled_p(cat
)) return Qnil
;
302 rb_write_error_str(str
);
307 * Document-module: Warning
309 * The Warning module contains a single method named #warn, and the
310 * module extends itself, making Warning.warn available.
311 * Warning.warn is called for all warnings issued by Ruby.
312 * By default, warnings are printed to $stderr.
314 * Changing the behavior of Warning.warn is useful to customize how warnings are
315 * handled by Ruby, for instance by filtering some warnings, and/or outputting
316 * warnings somewhere other than <tt>$stderr</tt>.
318 * If you want to change the behavior of Warning.warn you should use
319 * <tt>Warning.extend(MyNewModuleWithWarnMethod)</tt> and you can use +super+
320 * to get the default behavior of printing the warning to <tt>$stderr</tt>.
323 * module MyWarningFilter
324 * def warn(message, category: nil, **kwargs)
325 * if /some warning I want to ignore/.match?(message)
332 * Warning.extend MyWarningFilter
334 * You should never redefine Warning#warn (the instance method), as that will
335 * then no longer provide a way to use the default behavior.
337 * The warning[https://rubygems.org/gems/warning] gem provides convenient ways to customize Warning.warn.
341 rb_warning_warn(VALUE mod
, VALUE str
)
343 return rb_funcallv(mod
, id_warn
, 1, &str
);
348 rb_warning_warn_arity(void)
350 const rb_method_entry_t
*me
= rb_method_entry(rb_singleton_class(rb_mWarning
), id_warn
);
351 return me
? rb_method_entry_arity(me
) : 1;
355 rb_warn_category(VALUE str
, VALUE category
)
357 if (RUBY_DEBUG
&& !NIL_P(category
)) {
358 rb_warning_category_from_name(category
);
361 if (rb_warning_warn_arity() == 1) {
362 return rb_warning_warn(rb_mWarning
, str
);
367 args
[1] = rb_hash_new();
368 rb_hash_aset(args
[1], sym_category
, category
);
369 return rb_funcallv_kw(rb_mWarning
, id_warn
, 2, args
, RB_PASS_KEYWORDS
);
374 rb_write_warning_str(VALUE str
)
376 rb_warning_warn(rb_mWarning
, str
);
379 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 4, 0)
381 warn_vsprintf(rb_encoding
*enc
, const char *file
, int line
, const char *fmt
, va_list args
)
383 VALUE str
= rb_enc_str_new(0, 0, enc
);
385 err_vcatf(str
, "warning: ", file
, line
, fmt
, args
);
386 return rb_str_cat2(str
, "\n");
389 #define with_warn_vsprintf(file, line, fmt) \
392 va_start(args, fmt); \
393 str = warn_vsprintf(NULL, file, line, fmt, args); \
397 rb_compile_warn(const char *file
, int line
, const char *fmt
, ...)
399 if (!NIL_P(ruby_verbose
)) {
400 with_warn_vsprintf(file
, line
, fmt
) {
401 rb_write_warning_str(str
);
406 /* rb_compile_warning() reports only in verbose mode */
408 rb_compile_warning(const char *file
, int line
, const char *fmt
, ...)
410 if (RTEST(ruby_verbose
)) {
411 with_warn_vsprintf(file
, line
, fmt
) {
412 rb_write_warning_str(str
);
418 rb_category_compile_warn(rb_warning_category_t category
, const char *file
, int line
, const char *fmt
, ...)
420 if (!NIL_P(ruby_verbose
)) {
421 with_warn_vsprintf(file
, line
, fmt
) {
422 rb_warn_category(str
, rb_warning_category_to_name(category
));
427 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 2, 0)
429 warning_string(rb_encoding
*enc
, const char *fmt
, va_list args
)
432 const char *file
= rb_source_location_cstr(&line
);
433 return warn_vsprintf(enc
, file
, line
, fmt
, args
);
436 #define with_warning_string(mesg, enc, fmt) \
437 with_warning_string_from(mesg, enc, fmt, fmt)
438 #define with_warning_string_from(mesg, enc, fmt, last_arg) \
440 va_list args; va_start(args, last_arg); \
441 mesg = warning_string(enc, fmt, args); \
445 rb_warn(const char *fmt
, ...)
447 if (!NIL_P(ruby_verbose
)) {
448 with_warning_string(mesg
, 0, fmt
) {
449 rb_write_warning_str(mesg
);
455 rb_category_warn(rb_warning_category_t category
, const char *fmt
, ...)
457 if (!NIL_P(ruby_verbose
)) {
458 with_warning_string(mesg
, 0, fmt
) {
459 rb_warn_category(mesg
, rb_warning_category_to_name(category
));
465 rb_enc_warn(rb_encoding
*enc
, const char *fmt
, ...)
467 if (!NIL_P(ruby_verbose
)) {
468 with_warning_string(mesg
, enc
, fmt
) {
469 rb_write_warning_str(mesg
);
474 /* rb_warning() reports only in verbose mode */
476 rb_warning(const char *fmt
, ...)
478 if (RTEST(ruby_verbose
)) {
479 with_warning_string(mesg
, 0, fmt
) {
480 rb_write_warning_str(mesg
);
485 /* rb_category_warning() reports only in verbose mode */
487 rb_category_warning(rb_warning_category_t category
, const char *fmt
, ...)
489 if (RTEST(ruby_verbose
)) {
490 with_warning_string(mesg
, 0, fmt
) {
491 rb_warn_category(mesg
, rb_warning_category_to_name(category
));
497 rb_warning_string(const char *fmt
, ...)
499 with_warning_string(mesg
, 0, fmt
) {
506 rb_enc_warning(rb_encoding
*enc
, const char *fmt
, ...)
508 if (RTEST(ruby_verbose
)) {
509 with_warning_string(mesg
, enc
, fmt
) {
510 rb_write_warning_str(mesg
);
517 deprecation_warning_enabled(void)
519 if (NIL_P(ruby_verbose
)) return false;
520 if (!rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED
)) return false;
525 warn_deprecated(VALUE mesg
, const char *removal
, const char *suggest
)
527 rb_str_set_len(mesg
, RSTRING_LEN(mesg
) - 1);
528 rb_str_cat_cstr(mesg
, " is deprecated");
530 rb_str_catf(mesg
, " and will be removed in Ruby %s", removal
);
532 if (suggest
) rb_str_catf(mesg
, "; use %s instead", suggest
);
533 rb_str_cat_cstr(mesg
, "\n");
534 rb_warn_category(mesg
, ID2SYM(id_deprecated
));
538 rb_warn_deprecated(const char *fmt
, const char *suggest
, ...)
540 if (!deprecation_warning_enabled()) return;
542 with_warning_string_from(mesg
, 0, fmt
, suggest
) {
543 warn_deprecated(mesg
, NULL
, suggest
);
548 rb_warn_deprecated_to_remove(const char *removal
, const char *fmt
, const char *suggest
, ...)
550 if (!deprecation_warning_enabled()) return;
552 with_warning_string_from(mesg
, 0, fmt
, suggest
) {
553 warn_deprecated(mesg
, removal
, suggest
);
558 end_with_asciichar(VALUE str
, int c
)
560 return RB_TYPE_P(str
, T_STRING
) &&
561 rb_str_end_with_asciichar(str
, c
);
566 warning_write(int argc
, VALUE
*argv
, VALUE buf
)
569 rb_str_append(buf
, *argv
++);
574 VALUE
rb_ec_backtrace_location_ary(const rb_execution_context_t
*ec
, long lev
, long n
, bool skip_internal
);
577 rb_warn_m(rb_execution_context_t
*ec
, VALUE exc
, VALUE msgs
, VALUE uplevel
, VALUE category
)
579 VALUE location
= Qnil
;
580 int argc
= RARRAY_LENINT(msgs
);
581 const VALUE
*argv
= RARRAY_CONST_PTR(msgs
);
583 if (!NIL_P(ruby_verbose
) && argc
> 0) {
585 if (!NIL_P(uplevel
)) {
586 long lev
= NUM2LONG(uplevel
);
588 rb_raise(rb_eArgError
, "negative level (%ld)", lev
);
590 location
= rb_ec_backtrace_location_ary(ec
, lev
+ 1, 1, TRUE
);
591 if (!NIL_P(location
)) {
592 location
= rb_ary_entry(location
, 0);
595 if (argc
> 1 || !NIL_P(uplevel
) || !end_with_asciichar(str
, '\n')) {
597 if (NIL_P(uplevel
)) {
598 str
= rb_str_tmp_new(0);
600 else if (NIL_P(location
) ||
601 NIL_P(path
= rb_funcall(location
, rb_intern("path"), 0))) {
602 str
= rb_str_new_cstr("warning: ");
605 str
= rb_sprintf("%s:%ld: warning: ",
606 rb_string_value_ptr(&path
),
607 NUM2LONG(rb_funcall(location
, rb_intern("lineno"), 0)));
609 RBASIC_SET_CLASS(str
, rb_cWarningBuffer
);
610 rb_io_puts(argc
, argv
, str
);
611 RBASIC_SET_CLASS(str
, rb_cString
);
614 if (!NIL_P(category
)) {
615 category
= rb_to_symbol_type(category
);
616 rb_warning_category_from_name(category
);
619 if (exc
== rb_mWarning
) {
620 rb_must_asciicompat(str
);
621 rb_write_error_str(str
);
624 rb_warn_category(str
, category
);
630 #define MAX_BUG_REPORTERS 0x100
632 static struct bug_reporters
{
633 void (*func
)(FILE *out
, void *data
);
635 } bug_reporters
[MAX_BUG_REPORTERS
];
637 static int bug_reporters_size
;
640 rb_bug_reporter_add(void (*func
)(FILE *, void *), void *data
)
642 struct bug_reporters
*reporter
;
643 if (bug_reporters_size
>= MAX_BUG_REPORTERS
) {
644 return 0; /* failed to register */
646 reporter
= &bug_reporters
[bug_reporters_size
++];
647 reporter
->func
= func
;
648 reporter
->data
= data
;
653 /* returns true if x can not be used as file name */
657 #if defined __CYGWIN__ || defined DOSISH
658 # define PATH_SEP_ENCODING 1
659 // Assume that "/" is only the first byte in any encoding.
660 if (x
== ':') return true; // drive letter or ADS
661 if (x
== '\\') return true;
671 static const char PATHSEP_REPLACE
= '!';
674 append_pathname(char *p
, const char *pe
, VALUE str
)
676 #ifdef PATH_SEP_ENCODING
677 rb_encoding
*enc
= rb_enc_get(str
);
679 const char *s
= RSTRING_PTR(str
);
680 const char *const se
= s
+ RSTRING_LEN(str
);
683 --pe
; // for terminator
685 while (p
< pe
&& s
< se
&& (c
= *s
) != '\0') {
687 if (s
== se
|| !*s
) break; // chomp "." basename
688 if (path_sep_p(s
[1])) goto skipsep
; // skip "./"
690 else if (path_sep_p(c
)) {
691 // squeeze successive separators
692 *p
++ = PATHSEP_REPLACE
;
694 while (++s
< se
&& path_sep_p(*s
));
697 const char *const ss
= s
;
698 while (p
< pe
&& s
< se
&& *s
&& !path_sep_p(*s
)) {
699 #ifdef PATH_SEP_ENCODING
700 int n
= rb_enc_mbclen(s
, se
, enc
);
707 if (s
> ss
) memcpy(p
- (s
- ss
), ss
, s
- ss
);
714 append_basename(char *p
, const char *pe
, struct path_string
*path
, VALUE str
)
717 #ifdef PATH_SEP_ENCODING
718 rb_encoding
*enc
= rb_enc_get(str
);
720 const char *const b
= RSTRING_PTR(str
), *const e
= RSTRING_END(str
), *p
= e
;
723 if (path_sep_p(p
[-1])) {
724 #ifdef PATH_SEP_ENCODING
725 const char *t
= rb_enc_prev_char(b
, p
, e
, enc
);
740 size_t n
= path
->len
;
741 if (p
+ n
> pe
) n
= pe
- p
;
742 memcpy(p
, path
->ptr
, n
);
747 finish_report(FILE *out
, rb_pid_t pid
)
749 if (out
!= stdout
&& out
!= stderr
) fclose(out
);
750 #ifdef HAVE_WORKING_FORK
751 if (pid
> 0) waitpid(pid
, NULL
, 0);
755 struct report_expansion
{
756 struct path_string exe
, script
;
762 * Open a bug report file to write. The `RUBY_CRASH_REPORT`
763 * environment variable can be set to define a template that is used
764 * to name bug report files. The template can contain % specifiers
765 * which are substituted by the following values when a bug report
768 * %% A single % character.
769 * %e The base name of the executable filename.
770 * %E Pathname of executable, with slashes ('/') replaced by
771 * exclamation marks ('!').
772 * %f Similar to %e with the main script filename.
773 * %F Similar to %E with the main script filename.
774 * %p PID of dumped process in decimal.
775 * %t Time of dump, expressed as seconds since the Epoch,
776 * 1970-01-01 00:00:00 +0000 (UTC).
777 * %NNN Octal char code, upto 3 digits.
780 expand_report_argument(const char **input_template
, struct report_expansion
*values
,
781 char *buf
, size_t size
, bool word
)
784 char *end
= buf
+ size
;
785 const char *template = *input_template
;
788 if (p
>= end
-1 || !*template) return NULL
;
790 char c
= *template++;
791 if (word
&& ISSPACE(c
)) break;
792 if (!store
) continue;
795 switch (c
= *template++) {
797 p
= append_basename(p
, end
, &values
->exe
, rb_argv0
);
800 p
= append_pathname(p
, end
, rb_argv0
);
803 p
= append_basename(p
, end
, &values
->script
, GET_VM()->orig_progname
);
806 p
= append_pathname(p
, end
, GET_VM()->orig_progname
);
809 if (!values
->pid
) values
->pid
= getpid();
810 snprintf(p
, end
-p
, "%" PRI_PIDT_PREFIX
"d", values
->pid
);
814 if (!values
->time
) values
->time
= time(NULL
);
815 snprintf(p
, end
-p
, "%" PRI_TIMET_PREFIX
"d", values
->time
);
819 if (c
>= '0' && c
<= '7') {
820 c
= (unsigned char)ruby_scan_oct(template-1, 3, &n
);
822 if (!c
) store
= false;
827 if (p
< end
-1) *p
++ = c
;
829 *input_template
= template;
834 FILE *ruby_popen_writer(char *const *argv
, rb_pid_t
*pid
);
837 open_report_path(const char *template, char *buf
, size_t size
, rb_pid_t
*pid
)
839 struct report_expansion values
= {{0}};
841 if (!template) return NULL
;
842 if (0) fprintf(stderr
, "RUBY_CRASH_REPORT=%s\n", buf
);
843 if (*template == '|') {
844 char *argv
[16], *bufend
= buf
+ size
, *p
;
847 for (argc
= 0; argc
< numberof(argv
) - 1; ++argc
) {
848 while (*template && ISSPACE(*template)) template++;
849 p
= expand_report_argument(&template, &values
, buf
, bufend
-buf
, true);
855 if (!p
) return ruby_popen_writer(argv
, pid
);
857 else if (*template) {
858 expand_report_argument(&template, &values
, buf
, size
, false);
859 return fopen(buf
, "w");
864 static const char *crash_report
;
866 /* SIGSEGV handler might have a very small stack. Thus we need to use it carefully. */
867 #define REPORT_BUG_BUFSIZ 256
869 bug_report_file(const char *file
, int line
, rb_pid_t
*pid
)
871 char buf
[REPORT_BUG_BUFSIZ
];
872 const char *report
= crash_report
;
873 if (!report
) report
= getenv("RUBY_CRASH_REPORT");
874 FILE *out
= open_report_path(report
, buf
, sizeof(buf
), pid
);
875 int len
= err_position_0(buf
, sizeof(buf
), file
, line
);
878 if ((ssize_t
)fwrite(buf
, 1, len
, out
) == (ssize_t
)len
) return out
;
881 if ((ssize_t
)fwrite(buf
, 1, len
, stderr
) == (ssize_t
)len
) {
884 if ((ssize_t
)fwrite(buf
, 1, len
, stdout
) == (ssize_t
)len
) {
891 FUNC_MINIMIZED(static void bug_important_message(FILE *out
, const char *const msg
, size_t len
));
894 bug_important_message(FILE *out
, const char *const msg
, size_t len
)
896 const char *const endmsg
= msg
+ len
;
900 if (isatty(fileno(out
))) {
901 static const char red
[] = "\033[;31;1;7m";
902 static const char green
[] = "\033[;32;7m";
903 static const char reset
[] = "\033[m";
904 const char *e
= strchr(p
, '\n');
905 const int w
= (int)(e
- p
);
907 int i
= (int)(e
- p
);
908 fputs(*p
== ' ' ? green
: red
, out
);
909 fwrite(p
, 1, e
- p
, out
);
910 for (; i
< w
; ++i
) fputc(' ', out
);
913 } while ((p
= e
+ 1) < endmsg
&& (e
= strchr(p
, '\n')) != 0 && e
> p
+ 1);
915 fwrite(p
, 1, endmsg
- p
, out
);
918 #undef CRASH_REPORTER_MAY_BE_CREATED
919 #if defined(__APPLE__) && \
920 (!defined(MAC_OS_X_VERSION_10_6) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6 || defined(__POWERPC__)) /* 10.6 PPC case */
921 # define CRASH_REPORTER_MAY_BE_CREATED
924 preface_dump(FILE *out
)
926 #if defined __APPLE__
927 static const char msg
[] = ""
928 "-- Crash Report log information "
929 "--------------------------------------------\n"
930 " See Crash Report log file in one of the following locations:\n"
931 # ifdef CRASH_REPORTER_MAY_BE_CREATED
932 " * ~/Library/Logs/CrashReporter\n"
933 " * /Library/Logs/CrashReporter\n"
935 " * ~/Library/Logs/DiagnosticReports\n"
936 " * /Library/Logs/DiagnosticReports\n"
937 " for more details.\n"
938 "Don't forget to include the above Crash Report log file in bug reports.\n"
940 const size_t msglen
= sizeof(msg
) - 1;
942 const char *msg
= NULL
;
943 const size_t msglen
= 0;
945 bug_important_message(out
, msg
, msglen
);
949 postscript_dump(FILE *out
)
951 #if defined __APPLE__
952 static const char msg
[] = ""
954 /*" ------------------------------------------------"*/
955 "\n""Don't forget to include the Crash Report log file under\n"
956 # ifdef CRASH_REPORTER_MAY_BE_CREATED
959 "DiagnosticReports directory in bug reports.\n"
960 /*"------------------------------------------------------------\n"*/
962 const size_t msglen
= sizeof(msg
) - 1;
964 const char *msg
= NULL
;
965 const size_t msglen
= 0;
967 bug_important_message(out
, msg
, msglen
);
970 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 2, 0)
972 bug_report_begin_valist(FILE *out
, const char *fmt
, va_list args
)
974 char buf
[REPORT_BUG_BUFSIZ
];
976 fputs("[BUG] ", out
);
977 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
979 snprintf(buf
, sizeof(buf
), "\n%s\n\n", rb_dynamic_description
);
984 #define bug_report_begin(out, fmt) do { \
986 va_start(args, fmt); \
987 bug_report_begin_valist(out, fmt, args); \
992 bug_report_end(FILE *out
, rb_pid_t pid
)
994 /* call additional bug reporters */
997 for (i
=0; i
<bug_reporters_size
; i
++) {
998 struct bug_reporters
*reporter
= &bug_reporters
[i
];
999 (*reporter
->func
)(out
, reporter
->data
);
1002 postscript_dump(out
);
1003 finish_report(out
, pid
);
1006 #define report_bug(file, line, fmt, ctx) do { \
1007 rb_pid_t pid = -1; \
1008 FILE *out = bug_report_file(file, line, &pid); \
1010 bug_report_begin(out, fmt); \
1011 rb_vm_bugreport(ctx, out); \
1012 bug_report_end(out, pid); \
1016 #define report_bug_valist(file, line, fmt, ctx, args) do { \
1017 rb_pid_t pid = -1; \
1018 FILE *out = bug_report_file(file, line, &pid); \
1020 bug_report_begin_valist(out, fmt, args); \
1021 rb_vm_bugreport(ctx, out); \
1022 bug_report_end(out, pid); \
1027 ruby_set_crash_report(const char *template)
1029 crash_report
= template;
1032 char buf
[REPORT_BUG_BUFSIZ
];
1033 FILE *out
= open_report_path(template, buf
, sizeof(buf
), &pid
);
1035 time_t t
= time(NULL
);
1036 fprintf(out
, "ruby_test_bug_report: %s", ctime(&t
));
1037 finish_report(out
, pid
);
1042 NORETURN(static void die(void));
1046 #if defined(_WIN32) && defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 80
1047 _set_abort_behavior( 0, _CALL_REPORTFAULT
);
1053 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 1, 0)
1055 rb_bug_without_die(const char *fmt
, va_list args
)
1057 const char *file
= NULL
;
1061 file
= rb_source_location_cstr(&line
);
1064 report_bug_valist(file
, line
, fmt
, NULL
, args
);
1068 rb_bug(const char *fmt
, ...)
1071 va_start(args
, fmt
);
1072 rb_bug_without_die(fmt
, args
);
1078 rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler
, int sig
, const void *ctx
, const char *fmt
, ...)
1080 const char *file
= NULL
;
1084 file
= rb_source_location_cstr(&line
);
1087 report_bug(file
, line
, fmt
, ctx
);
1089 if (default_sighandler
) default_sighandler(sig
);
1091 ruby_default_signal(sig
);
1097 rb_bug_errno(const char *mesg
, int errno_arg
)
1100 rb_bug("%s: errno == 0 (NOERROR)", mesg
);
1102 const char *errno_str
= rb_strerrno(errno_arg
);
1104 rb_bug("%s: %s (%s)", mesg
, strerror(errno_arg
), errno_str
);
1106 rb_bug("%s: %s (%d)", mesg
, strerror(errno_arg
), errno_arg
);
1111 * this is safe to call inside signal handler and timer thread
1112 * (which isn't a Ruby Thread object)
1114 #define write_or_abort(fd, str, len) (write((fd), (str), (len)) < 0 ? abort() : (void)0)
1115 #define WRITE_CONST(fd,str) write_or_abort((fd),(str),sizeof(str) - 1)
1118 rb_async_bug_errno(const char *mesg
, int errno_arg
)
1120 WRITE_CONST(2, "[ASYNC BUG] ");
1121 write_or_abort(2, mesg
, strlen(mesg
));
1122 WRITE_CONST(2, "\n");
1124 if (errno_arg
== 0) {
1125 WRITE_CONST(2, "errno == 0 (NOERROR)\n");
1128 const char *errno_str
= rb_strerrno(errno_arg
);
1131 errno_str
= "undefined errno";
1132 write_or_abort(2, errno_str
, strlen(errno_str
));
1134 WRITE_CONST(2, "\n\n");
1135 write_or_abort(2, rb_dynamic_description
, strlen(rb_dynamic_description
));
1140 rb_report_bug_valist(VALUE file
, int line
, const char *fmt
, va_list args
)
1142 report_bug_valist(RSTRING_PTR(file
), line
, fmt
, NULL
, args
);
1146 rb_assert_failure(const char *file
, int line
, const char *name
, const char *expr
)
1148 rb_assert_failure_detail(file
, line
, name
, expr
, NULL
);
1152 rb_assert_failure_detail(const char *file
, int line
, const char *name
, const char *expr
,
1153 const char *fmt
, ...)
1156 fprintf(out
, "Assertion Failed: %s:%d:", file
, line
);
1157 if (name
) fprintf(out
, "%s:", name
);
1158 fprintf(out
, "%s\n%s\n\n", expr
, rb_dynamic_description
);
1162 va_start(args
, fmt
);
1163 vfprintf(out
, fmt
, args
);
1168 rb_vm_bugreport(NULL
, out
);
1169 bug_report_end(out
, -1);
1173 static const char builtin_types
[][10] = {
1186 "Data", /* internal use: wrapped C pointers */
1187 "MatchData", /* data of $~ */
1194 "Symbol", /* :symbol */
1196 "undef", /* internal use: #undef; should not happen */
1200 "<Memo>", /* internal use: general memo */
1201 "<Node>", /* internal use: syntax tree node */
1202 "<iClass>", /* internal use: mixed-in module holder */
1206 rb_builtin_type_name(int t
)
1209 if ((unsigned int)t
>= numberof(builtin_types
)) return 0;
1210 name
= builtin_types
[t
];
1211 if (*name
) return name
;
1216 displaying_class_of(VALUE x
)
1219 case Qfalse
: return rb_fstring_cstr("false");
1220 case Qnil
: return rb_fstring_cstr("nil");
1221 case Qtrue
: return rb_fstring_cstr("true");
1222 default: return rb_obj_class(x
);
1227 builtin_class_name(VALUE x
)
1234 else if (FIXNUM_P(x
)) {
1237 else if (SYMBOL_P(x
)) {
1240 else if (RB_TYPE_P(x
, T_TRUE
)) {
1243 else if (RB_TYPE_P(x
, T_FALSE
)) {
1253 rb_builtin_class_name(VALUE x
)
1255 const char *etype
= builtin_class_name(x
);
1258 etype
= rb_obj_classname(x
);
1263 COLDFUNC
NORETURN(static void unexpected_type(VALUE
, int, int));
1264 #define UNDEF_LEAKED "undef leaked to the Ruby space"
1267 unexpected_type(VALUE x
, int xt
, int t
)
1269 const char *tname
= rb_builtin_type_name(t
);
1270 VALUE mesg
, exc
= rb_eFatal
;
1273 mesg
= rb_sprintf("wrong argument type %"PRIsVALUE
" (expected %s)",
1274 displaying_class_of(x
), tname
);
1275 exc
= rb_eTypeError
;
1277 else if (xt
> T_MASK
&& xt
<= 0x3f) {
1278 mesg
= rb_sprintf("unknown type 0x%x (0x%x given, probably comes"
1279 " from extension library for ruby 1.8)", t
, xt
);
1282 mesg
= rb_sprintf("unknown type 0x%x (0x%x given)", t
, xt
);
1284 rb_exc_raise(rb_exc_new_str(exc
, mesg
));
1288 rb_check_type(VALUE x
, int t
)
1292 if (RB_UNLIKELY(UNDEF_P(x
))) {
1293 rb_bug(UNDEF_LEAKED
);
1297 if (xt
!= t
|| (xt
== T_DATA
&& rbimpl_rtypeddata_p(x
))) {
1299 * Typed data is not simple `T_DATA`, but in a sense an
1300 * extension of `struct RVALUE`, which are incompatible with
1301 * each other except when inherited.
1303 * So it is not enough to just check `T_DATA`, it must be
1304 * identified by its `type` using `Check_TypedStruct` instead.
1306 unexpected_type(x
, xt
, t
);
1311 rb_unexpected_type(VALUE x
, int t
)
1313 if (RB_UNLIKELY(UNDEF_P(x
))) {
1314 rb_bug(UNDEF_LEAKED
);
1317 unexpected_type(x
, TYPE(x
), t
);
1321 rb_typeddata_inherited_p(const rb_data_type_t
*child
, const rb_data_type_t
*parent
)
1324 if (child
== parent
) return 1;
1325 child
= child
->parent
;
1331 rb_typeddata_is_kind_of(VALUE obj
, const rb_data_type_t
*data_type
)
1333 if (!RB_TYPE_P(obj
, T_DATA
) ||
1334 !RTYPEDDATA_P(obj
) || !rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj
), data_type
)) {
1340 #undef rb_typeddata_is_instance_of
1342 rb_typeddata_is_instance_of(VALUE obj
, const rb_data_type_t
*data_type
)
1344 return rb_typeddata_is_instance_of_inline(obj
, data_type
);
1348 rb_check_typeddata(VALUE obj
, const rb_data_type_t
*data_type
)
1352 if (!RB_TYPE_P(obj
, T_DATA
)) {
1353 actual
= displaying_class_of(obj
);
1355 else if (!RTYPEDDATA_P(obj
)) {
1356 actual
= displaying_class_of(obj
);
1358 else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj
), data_type
)) {
1359 const char *name
= RTYPEDDATA_TYPE(obj
)->wrap_struct_name
;
1360 actual
= rb_str_new_cstr(name
); /* or rb_fstring_cstr? not sure... */
1363 return RTYPEDDATA_GET_DATA(obj
);
1366 const char *expected
= data_type
->wrap_struct_name
;
1367 rb_raise(rb_eTypeError
, "wrong argument type %"PRIsVALUE
" (expected %s)",
1369 UNREACHABLE_RETURN(NULL
);
1372 /* exception classes */
1373 VALUE rb_eException
;
1374 VALUE rb_eSystemExit
;
1375 VALUE rb_eInterrupt
;
1378 VALUE rb_eStandardError
;
1379 VALUE rb_eRuntimeError
;
1380 VALUE rb_eFrozenError
;
1381 VALUE rb_eTypeError
;
1383 VALUE rb_eIndexError
;
1385 VALUE rb_eRangeError
;
1386 VALUE rb_eNameError
;
1387 VALUE rb_eEncodingError
;
1388 VALUE rb_eEncCompatError
;
1389 VALUE rb_eNoMethodError
;
1390 VALUE rb_eSecurityError
;
1391 VALUE rb_eNotImpError
;
1392 VALUE rb_eNoMemError
;
1393 VALUE rb_cNameErrorMesg
;
1394 VALUE rb_eNoMatchingPatternError
;
1395 VALUE rb_eNoMatchingPatternKeyError
;
1397 VALUE rb_eScriptError
;
1398 VALUE rb_eSyntaxError
;
1399 VALUE rb_eLoadError
;
1401 VALUE rb_eSystemCallError
;
1403 static VALUE rb_eNOERROR
;
1405 ID ruby_static_id_cause
;
1406 #define id_cause ruby_static_id_cause
1407 static ID id_message
, id_detailed_message
, id_backtrace
;
1408 static ID id_key
, id_matchee
, id_args
, id_Errno
, id_errno
, id_i_path
;
1409 static ID id_receiver
, id_recv
, id_iseq
, id_local_variables
;
1410 static ID id_private_call_p
, id_top
, id_bottom
;
1412 #define id_bt_locations idBt_locations
1413 #define id_mesg idMesg
1414 #define id_name idName
1416 #undef rb_exc_new_cstr
1419 rb_exc_new(VALUE etype
, const char *ptr
, long len
)
1421 VALUE mesg
= rb_str_new(ptr
, len
);
1422 return rb_class_new_instance(1, &mesg
, etype
);
1426 rb_exc_new_cstr(VALUE etype
, const char *s
)
1428 return rb_exc_new(etype
, s
, strlen(s
));
1432 rb_exc_new_str(VALUE etype
, VALUE str
)
1434 rb_yjit_lazy_push_frame(GET_EC()->cfp
->pc
);
1436 return rb_class_new_instance(1, &str
, etype
);
1440 exc_init(VALUE exc
, VALUE mesg
)
1442 rb_ivar_set(exc
, id_mesg
, mesg
);
1443 rb_ivar_set(exc
, id_bt
, Qnil
);
1450 * Exception.new(msg = nil) -> exception
1451 * Exception.exception(msg = nil) -> exception
1453 * Construct a new Exception object, optionally passing in
1458 exc_initialize(int argc
, VALUE
*argv
, VALUE exc
)
1462 arg
= (!rb_check_arity(argc
, 0, 1) ? Qnil
: argv
[0]);
1463 return exc_init(exc
, arg
);
1467 * Document-method: exception
1470 * exc.exception([string]) -> an_exception or exc
1472 * With no argument, or if the argument is the same as the receiver,
1473 * return the receiver. Otherwise, create a new
1474 * exception object of the same class as the receiver, but with a
1475 * message equal to <code>string.to_str</code>.
1480 exc_exception(int argc
, VALUE
*argv
, VALUE self
)
1484 argc
= rb_check_arity(argc
, 0, 1);
1485 if (argc
== 0) return self
;
1486 if (argc
== 1 && self
== argv
[0]) return self
;
1487 exc
= rb_obj_clone(self
);
1488 rb_ivar_set(exc
, id_mesg
, argv
[0]);
1494 * exception.to_s -> string
1496 * Returns exception's message (or the name of the exception if
1497 * no message is set).
1503 VALUE mesg
= rb_attr_get(exc
, idMesg
);
1505 if (NIL_P(mesg
)) return rb_class_name(CLASS_OF(exc
));
1506 return rb_String(mesg
);
1509 /* FIXME: Include eval_error.c */
1510 void rb_error_write(VALUE errinfo
, VALUE emesg
, VALUE errat
, VALUE str
, VALUE opt
, VALUE highlight
, VALUE reverse
);
1513 rb_get_message(VALUE exc
)
1515 VALUE e
= rb_check_funcall(exc
, id_message
, 0, 0);
1516 if (UNDEF_P(e
)) return Qnil
;
1517 if (!RB_TYPE_P(e
, T_STRING
)) e
= rb_check_string_type(e
);
1522 rb_get_detailed_message(VALUE exc
, VALUE opt
)
1526 e
= rb_check_funcall(exc
, id_detailed_message
, 0, 0);
1529 e
= rb_check_funcall_kw(exc
, id_detailed_message
, 1, &opt
, 1);
1531 if (UNDEF_P(e
)) return Qnil
;
1532 if (!RB_TYPE_P(e
, T_STRING
)) e
= rb_check_string_type(e
);
1538 * Exception.to_tty? -> true or false
1540 * Returns +true+ if exception messages will be sent to a tty.
1543 exc_s_to_tty_p(VALUE self
)
1545 return RBOOL(rb_stderr_tty_p());
1549 check_highlight_keyword(VALUE opt
, int auto_tty_detect
)
1551 VALUE highlight
= Qnil
;
1554 highlight
= rb_hash_lookup(opt
, sym_highlight
);
1556 switch (highlight
) {
1558 rb_bool_expected(highlight
, "highlight", TRUE
);
1560 case Qtrue
: case Qfalse
: case Qnil
: break;
1564 if (NIL_P(highlight
)) {
1565 highlight
= RBOOL(auto_tty_detect
&& rb_stderr_tty_p());
1572 check_order_keyword(VALUE opt
)
1577 static VALUE kw_order
;
1578 if (!kw_order
) kw_order
= ID2SYM(rb_intern_const("order"));
1580 order
= rb_hash_lookup(opt
, kw_order
);
1582 if (order
!= Qnil
) {
1583 ID id
= rb_check_id(&order
);
1584 if (id
== id_bottom
) order
= Qtrue
;
1585 else if (id
== id_top
) order
= Qfalse
;
1587 rb_raise(rb_eArgError
, "expected :top or :bottom as "
1588 "order: %+"PRIsVALUE
, order
);
1593 if (NIL_P(order
)) order
= Qfalse
;
1600 * exception.full_message(highlight: bool, order: [:top or :bottom]) -> string
1602 * Returns formatted string of _exception_.
1603 * The returned string is formatted using the same format that Ruby uses
1604 * when printing an uncaught exceptions to stderr.
1606 * If _highlight_ is +true+ the default error handler will send the
1607 * messages to a tty.
1609 * _order_ must be either of +:top+ or +:bottom+, and places the error
1610 * message and the innermost backtrace come at the top or the bottom.
1612 * The default values of these options depend on <code>$stderr</code>
1613 * and its +tty?+ at the timing of a call.
1617 exc_full_message(int argc
, VALUE
*argv
, VALUE exc
)
1619 VALUE opt
, str
, emesg
, errat
;
1620 VALUE highlight
, order
;
1622 rb_scan_args(argc
, argv
, "0:", &opt
);
1624 highlight
= check_highlight_keyword(opt
, 1);
1625 order
= check_order_keyword(opt
);
1628 if (NIL_P(opt
)) opt
= rb_hash_new();
1629 rb_hash_aset(opt
, sym_highlight
, highlight
);
1632 str
= rb_str_new2("");
1633 errat
= rb_get_backtrace(exc
);
1634 emesg
= rb_get_detailed_message(exc
, opt
);
1636 rb_error_write(exc
, emesg
, errat
, str
, opt
, highlight
, order
);
1642 * exception.message -> string
1644 * Returns the result of invoking <code>exception.to_s</code>.
1645 * Normally this returns the exception's message or name.
1649 exc_message(VALUE exc
)
1651 return rb_funcallv(exc
, idTo_s
, 0, 0);
1656 * exception.detailed_message(highlight: bool, **opt) -> string
1658 * Processes a string returned by #message.
1660 * It may add the class name of the exception to the end of the first line.
1661 * Also, when +highlight+ keyword is true, it adds ANSI escape sequences to
1662 * make the message bold.
1664 * If you override this method, it must be tolerant for unknown keyword
1665 * arguments. All keyword arguments passed to #full_message are delegated
1668 * This method is overridden by did_you_mean and error_highlight to add
1669 * their information.
1671 * A user-defined exception class can also define their own
1672 * +detailed_message+ method to add supplemental information.
1673 * When +highlight+ is true, it can return a string containing escape
1674 * sequences, but use widely-supported ones. It is recommended to limit
1675 * the following codes:
1679 * - Underline (+\e[4m+)
1680 * - Foreground color except white and black
1682 * - Green (+\e[32m+)
1683 * - Yellow (+\e[33m+)
1685 * - Magenta (+\e[35m+)
1688 * Use escape sequences carefully even if +highlight+ is true.
1689 * Do not use escape sequences to express essential information;
1690 * the message should be readable even if all escape sequences are
1695 exc_detailed_message(int argc
, VALUE
*argv
, VALUE exc
)
1699 rb_scan_args(argc
, argv
, "0:", &opt
);
1701 VALUE highlight
= check_highlight_keyword(opt
, 0);
1703 extern VALUE
rb_decorate_message(const VALUE eclass
, VALUE emesg
, int highlight
);
1705 return rb_decorate_message(CLASS_OF(exc
), rb_get_message(exc
), RTEST(highlight
));
1710 * exception.inspect -> string
1712 * Return this exception's class name and message.
1716 exc_inspect(VALUE exc
)
1720 klass
= CLASS_OF(exc
);
1721 exc
= rb_obj_as_string(exc
);
1722 if (RSTRING_LEN(exc
) == 0) {
1723 return rb_class_name(klass
);
1726 str
= rb_str_buf_new2("#<");
1727 klass
= rb_class_name(klass
);
1728 rb_str_buf_append(str
, klass
);
1730 if (RTEST(rb_str_include(exc
, rb_str_new2("\n")))) {
1731 rb_str_catf(str
, ":%+"PRIsVALUE
, exc
);
1734 rb_str_buf_cat(str
, ": ", 2);
1735 rb_str_buf_append(str
, exc
);
1738 rb_str_buf_cat(str
, ">", 1);
1745 * exception.backtrace -> array or nil
1747 * Returns any backtrace associated with the exception. The backtrace
1748 * is an array of strings, each containing either ``filename:lineNo: in
1749 * `method''' or ``filename:lineNo.''
1762 * print detail.backtrace.join("\n")
1765 * <em>produces:</em>
1771 * In the case no backtrace has been set, +nil+ is returned
1773 * ex = StandardError.new
1779 exc_backtrace(VALUE exc
)
1783 obj
= rb_attr_get(exc
, id_bt
);
1785 if (rb_backtrace_p(obj
)) {
1786 obj
= rb_backtrace_to_str_ary(obj
);
1787 /* rb_ivar_set(exc, id_bt, obj); */
1793 static VALUE
rb_check_backtrace(VALUE
);
1796 rb_get_backtrace(VALUE exc
)
1798 ID mid
= id_backtrace
;
1800 if (rb_method_basic_definition_p(CLASS_OF(exc
), id_backtrace
)) {
1801 VALUE klass
= rb_eException
;
1802 rb_execution_context_t
*ec
= GET_EC();
1805 EXEC_EVENT_HOOK(ec
, RUBY_EVENT_C_CALL
, exc
, mid
, mid
, klass
, Qundef
);
1806 info
= exc_backtrace(exc
);
1807 EXEC_EVENT_HOOK(ec
, RUBY_EVENT_C_RETURN
, exc
, mid
, mid
, klass
, info
);
1810 info
= rb_funcallv(exc
, mid
, 0, 0);
1812 if (NIL_P(info
)) return Qnil
;
1813 return rb_check_backtrace(info
);
1818 * exception.backtrace_locations -> array or nil
1820 * Returns any backtrace associated with the exception. This method is
1821 * similar to Exception#backtrace, but the backtrace is an array of
1822 * Thread::Backtrace::Location.
1824 * This method is not affected by Exception#set_backtrace().
1827 exc_backtrace_locations(VALUE exc
)
1831 obj
= rb_attr_get(exc
, id_bt_locations
);
1833 obj
= rb_backtrace_to_location_ary(obj
);
1839 rb_check_backtrace(VALUE bt
)
1842 static const char err
[] = "backtrace must be an Array of String or an Array of Thread::Backtrace::Location";
1845 if (RB_TYPE_P(bt
, T_STRING
)) return rb_ary_new3(1, bt
);
1846 if (rb_backtrace_p(bt
)) return bt
;
1847 if (!RB_TYPE_P(bt
, T_ARRAY
)) {
1848 rb_raise(rb_eTypeError
, err
);
1850 for (i
=0;i
<RARRAY_LEN(bt
);i
++) {
1851 VALUE e
= RARRAY_AREF(bt
, i
);
1852 if (!RB_TYPE_P(e
, T_STRING
)) {
1853 rb_raise(rb_eTypeError
, err
);
1862 * exc.set_backtrace(backtrace) -> array
1864 * Sets the backtrace information associated with +exc+. The +backtrace+ must
1865 * be an array of Thread::Backtrace::Location objects or an array of String objects
1866 * or a single String in the format described in Exception#backtrace.
1871 exc_set_backtrace(VALUE exc
, VALUE bt
)
1873 VALUE btobj
= rb_location_ary_to_backtrace(bt
);
1875 rb_ivar_set(exc
, id_bt
, btobj
);
1876 rb_ivar_set(exc
, id_bt_locations
, btobj
);
1880 return rb_ivar_set(exc
, id_bt
, rb_check_backtrace(bt
));
1885 rb_exc_set_backtrace(VALUE exc
, VALUE bt
)
1887 return exc_set_backtrace(exc
, bt
);
1892 * exception.cause -> an_exception or nil
1894 * Returns the previous exception ($!) at the time this exception was raised.
1895 * This is useful for wrapping exceptions and retaining the original exception
1900 exc_cause(VALUE exc
)
1902 return rb_attr_get(exc
, id_cause
);
1906 try_convert_to_exception(VALUE obj
)
1908 return rb_check_funcall(obj
, idException
, 0, 0);
1913 * exc == obj -> true or false
1915 * Equality---If <i>obj</i> is not an Exception, returns
1916 * <code>false</code>. Otherwise, returns <code>true</code> if <i>exc</i> and
1917 * <i>obj</i> share same class, messages, and backtrace.
1921 exc_equal(VALUE exc
, VALUE obj
)
1923 VALUE mesg
, backtrace
;
1925 if (exc
== obj
) return Qtrue
;
1927 if (rb_obj_class(exc
) != rb_obj_class(obj
)) {
1930 obj
= rb_protect(try_convert_to_exception
, obj
, &state
);
1931 if (state
|| UNDEF_P(obj
)) {
1932 rb_set_errinfo(Qnil
);
1935 if (rb_obj_class(exc
) != rb_obj_class(obj
)) return Qfalse
;
1936 mesg
= rb_check_funcall(obj
, id_message
, 0, 0);
1937 if (UNDEF_P(mesg
)) return Qfalse
;
1938 backtrace
= rb_check_funcall(obj
, id_backtrace
, 0, 0);
1939 if (UNDEF_P(backtrace
)) return Qfalse
;
1942 mesg
= rb_attr_get(obj
, id_mesg
);
1943 backtrace
= exc_backtrace(obj
);
1946 if (!rb_equal(rb_attr_get(exc
, id_mesg
), mesg
))
1948 return rb_equal(exc_backtrace(exc
), backtrace
);
1953 * SystemExit.new -> system_exit
1954 * SystemExit.new(status) -> system_exit
1955 * SystemExit.new(status, msg) -> system_exit
1956 * SystemExit.new(msg) -> system_exit
1958 * Create a new +SystemExit+ exception with the given status and message.
1959 * Status is true, false, or an integer.
1960 * If status is not given, true is used.
1964 exit_initialize(int argc
, VALUE
*argv
, VALUE exc
)
1972 status
= INT2FIX(EXIT_SUCCESS
);
1977 status
= INT2FIX(EXIT_FAILURE
);
1982 status
= rb_check_to_int(status
);
1983 if (NIL_P(status
)) {
1984 status
= INT2FIX(EXIT_SUCCESS
);
1987 #if EXIT_SUCCESS != 0
1988 if (status
== INT2FIX(0))
1989 status
= INT2FIX(EXIT_SUCCESS
);
1998 status
= INT2FIX(EXIT_SUCCESS
);
2000 rb_call_super(argc
, argv
);
2001 rb_ivar_set(exc
, id_status
, status
);
2008 * system_exit.status -> integer
2010 * Return the status value associated with this system exit.
2014 exit_status(VALUE exc
)
2016 return rb_attr_get(exc
, id_status
);
2022 * system_exit.success? -> true or false
2024 * Returns +true+ if exiting successful, +false+ if not.
2028 exit_success_p(VALUE exc
)
2030 VALUE status_val
= rb_attr_get(exc
, id_status
);
2033 if (NIL_P(status_val
))
2035 status
= NUM2INT(status_val
);
2036 return RBOOL(WIFEXITED(status
) && WEXITSTATUS(status
) == EXIT_SUCCESS
);
2040 err_init_recv(VALUE exc
, VALUE recv
)
2042 if (!UNDEF_P(recv
)) rb_ivar_set(exc
, id_recv
, recv
);
2048 * FrozenError.new(msg=nil, receiver: nil) -> frozen_error
2050 * Construct a new FrozenError exception. If given the <i>receiver</i>
2051 * parameter may subsequently be examined using the FrozenError#receiver
2055 * raise FrozenError.new("can't modify frozen array", receiver: a)
2059 frozen_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2062 VALUE values
[numberof(keywords
)], options
;
2064 argc
= rb_scan_args(argc
, argv
, "*:", NULL
, &options
);
2065 keywords
[0] = id_receiver
;
2066 rb_get_kwargs(options
, keywords
, 0, numberof(values
), values
);
2067 rb_call_super(argc
, argv
);
2068 err_init_recv(self
, values
[0]);
2073 * Document-method: FrozenError#receiver
2075 * frozen_error.receiver -> object
2077 * Return the receiver associated with this FrozenError exception.
2080 #define frozen_err_receiver name_err_receiver
2083 rb_name_error(ID id
, const char *fmt
, ...)
2088 va_start(args
, fmt
);
2089 argv
[0] = rb_vsprintf(fmt
, args
);
2092 argv
[1] = ID2SYM(id
);
2093 exc
= rb_class_new_instance(2, argv
, rb_eNameError
);
2098 rb_name_error_str(VALUE str
, const char *fmt
, ...)
2103 va_start(args
, fmt
);
2104 argv
[0] = rb_vsprintf(fmt
, args
);
2108 exc
= rb_class_new_instance(2, argv
, rb_eNameError
);
2113 name_err_init_attr(VALUE exc
, VALUE recv
, VALUE method
)
2115 const rb_execution_context_t
*ec
= GET_EC();
2116 rb_control_frame_t
*cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(ec
->cfp
);
2117 cfp
= rb_vm_get_ruby_level_next_cfp(ec
, cfp
);
2118 rb_ivar_set(exc
, id_name
, method
);
2119 err_init_recv(exc
, recv
);
2120 if (cfp
&& VM_FRAME_TYPE(cfp
) != VM_FRAME_MAGIC_DUMMY
) {
2121 rb_ivar_set(exc
, id_iseq
, rb_iseqw_new(cfp
->iseq
));
2128 * NameError.new(msg=nil, name=nil, receiver: nil) -> name_error
2130 * Construct a new NameError exception. If given the <i>name</i>
2131 * parameter may subsequently be examined using the NameError#name
2132 * method. <i>receiver</i> parameter allows to pass object in
2133 * context of which the error happened. Example:
2135 * [1, 2, 3].method(:rject) # NameError with name "rject" and receiver: Array
2136 * [1, 2, 3].singleton_method(:rject) # NameError with name "rject" and receiver: [1, 2, 3]
2140 name_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2143 VALUE values
[numberof(keywords
)], name
, options
;
2145 argc
= rb_scan_args(argc
, argv
, "*:", NULL
, &options
);
2146 keywords
[0] = id_receiver
;
2147 rb_get_kwargs(options
, keywords
, 0, numberof(values
), values
);
2148 name
= (argc
> 1) ? argv
[--argc
] : Qnil
;
2149 rb_call_super(argc
, argv
);
2150 name_err_init_attr(self
, values
[0], name
);
2154 static VALUE
rb_name_err_mesg_new(VALUE mesg
, VALUE recv
, VALUE method
);
2157 name_err_init(VALUE exc
, VALUE mesg
, VALUE recv
, VALUE method
)
2159 exc_init(exc
, rb_name_err_mesg_new(mesg
, recv
, method
));
2160 return name_err_init_attr(exc
, recv
, method
);
2164 rb_name_err_new(VALUE mesg
, VALUE recv
, VALUE method
)
2166 VALUE exc
= rb_obj_alloc(rb_eNameError
);
2167 return name_err_init(exc
, mesg
, recv
, method
);
2172 * name_error.name -> string or nil
2174 * Return the name associated with this NameError exception.
2178 name_err_name(VALUE self
)
2180 return rb_attr_get(self
, id_name
);
2185 * name_error.local_variables -> array
2187 * Return a list of the local variable names defined where this
2188 * NameError exception was raised.
2190 * Internal use only.
2194 name_err_local_variables(VALUE self
)
2196 VALUE vars
= rb_attr_get(self
, id_local_variables
);
2199 VALUE iseqw
= rb_attr_get(self
, id_iseq
);
2200 if (!NIL_P(iseqw
)) vars
= rb_iseqw_local_variables(iseqw
);
2201 if (NIL_P(vars
)) vars
= rb_ary_new();
2202 rb_ivar_set(self
, id_local_variables
, vars
);
2208 nometh_err_init_attr(VALUE exc
, VALUE args
, int priv
)
2210 rb_ivar_set(exc
, id_args
, args
);
2211 rb_ivar_set(exc
, id_private_call_p
, RBOOL(priv
));
2217 * NoMethodError.new(msg=nil, name=nil, args=nil, private=false, receiver: nil) -> no_method_error
2219 * Construct a NoMethodError exception for a method of the given name
2220 * called with the given arguments. The name may be accessed using
2221 * the <code>#name</code> method on the resulting object, and the
2222 * arguments using the <code>#args</code> method.
2224 * If <i>private</i> argument were passed, it designates method was
2225 * attempted to call in private context, and can be accessed with
2226 * <code>#private_call?</code> method.
2228 * <i>receiver</i> argument stores an object whose method was called.
2232 nometh_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2235 VALUE args
, options
;
2236 argc
= rb_scan_args(argc
, argv
, "*:", NULL
, &options
);
2237 priv
= (argc
> 3) && (--argc
, RTEST(argv
[argc
]));
2238 args
= (argc
> 2) ? argv
[--argc
] : Qnil
;
2239 if (!NIL_P(options
)) argv
[argc
++] = options
;
2240 rb_call_super_kw(argc
, argv
, RB_PASS_CALLED_KEYWORDS
);
2241 return nometh_err_init_attr(self
, args
, priv
);
2245 rb_nomethod_err_new(VALUE mesg
, VALUE recv
, VALUE method
, VALUE args
, int priv
)
2247 VALUE exc
= rb_obj_alloc(rb_eNoMethodError
);
2248 name_err_init(exc
, mesg
, recv
, method
);
2249 return nometh_err_init_attr(exc
, args
, priv
);
2252 typedef struct name_error_message_struct
{
2256 } name_error_message_t
;
2259 name_err_mesg_mark(void *p
)
2261 name_error_message_t
*ptr
= (name_error_message_t
*)p
;
2262 rb_gc_mark_movable(ptr
->mesg
);
2263 rb_gc_mark_movable(ptr
->recv
);
2264 rb_gc_mark_movable(ptr
->name
);
2268 name_err_mesg_update(void *p
)
2270 name_error_message_t
*ptr
= (name_error_message_t
*)p
;
2271 ptr
->mesg
= rb_gc_location(ptr
->mesg
);
2272 ptr
->recv
= rb_gc_location(ptr
->recv
);
2273 ptr
->name
= rb_gc_location(ptr
->name
);
2276 static const rb_data_type_t name_err_mesg_data_type
= {
2280 RUBY_TYPED_DEFAULT_FREE
,
2281 NULL
, // No external memory to report,
2282 name_err_mesg_update
,
2284 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
| RUBY_TYPED_WB_PROTECTED
| RUBY_TYPED_EMBEDDABLE
2289 rb_name_err_mesg_init(VALUE klass
, VALUE mesg
, VALUE recv
, VALUE name
)
2291 name_error_message_t
*message
;
2292 VALUE result
= TypedData_Make_Struct(klass
, name_error_message_t
, &name_err_mesg_data_type
, message
);
2293 RB_OBJ_WRITE(result
, &message
->mesg
, mesg
);
2294 RB_OBJ_WRITE(result
, &message
->recv
, recv
);
2295 RB_OBJ_WRITE(result
, &message
->name
, name
);
2301 rb_name_err_mesg_new(VALUE mesg
, VALUE recv
, VALUE method
)
2303 return rb_name_err_mesg_init(rb_cNameErrorMesg
, mesg
, recv
, method
);
2308 name_err_mesg_alloc(VALUE klass
)
2310 return rb_name_err_mesg_init(klass
, Qnil
, Qnil
, Qnil
);
2315 name_err_mesg_init_copy(VALUE obj1
, VALUE obj2
)
2317 if (obj1
== obj2
) return obj1
;
2318 rb_obj_init_copy(obj1
, obj2
);
2320 name_error_message_t
*ptr1
, *ptr2
;
2321 TypedData_Get_Struct(obj1
, name_error_message_t
, &name_err_mesg_data_type
, ptr1
);
2322 TypedData_Get_Struct(obj2
, name_error_message_t
, &name_err_mesg_data_type
, ptr2
);
2324 RB_OBJ_WRITE(obj1
, &ptr1
->mesg
, ptr2
->mesg
);
2325 RB_OBJ_WRITE(obj1
, &ptr1
->recv
, ptr2
->recv
);
2326 RB_OBJ_WRITE(obj1
, &ptr1
->name
, ptr2
->name
);
2332 name_err_mesg_equal(VALUE obj1
, VALUE obj2
)
2334 if (obj1
== obj2
) return Qtrue
;
2336 if (rb_obj_class(obj2
) != rb_cNameErrorMesg
)
2339 name_error_message_t
*ptr1
, *ptr2
;
2340 TypedData_Get_Struct(obj1
, name_error_message_t
, &name_err_mesg_data_type
, ptr1
);
2341 TypedData_Get_Struct(obj2
, name_error_message_t
, &name_err_mesg_data_type
, ptr2
);
2343 if (!rb_equal(ptr1
->mesg
, ptr2
->mesg
)) return Qfalse
;
2344 if (!rb_equal(ptr1
->recv
, ptr2
->recv
)) return Qfalse
;
2345 if (!rb_equal(ptr1
->name
, ptr2
->name
)) return Qfalse
;
2351 name_err_mesg_receiver_name(VALUE obj
)
2353 if (RB_SPECIAL_CONST_P(obj
)) return Qundef
;
2354 if (RB_BUILTIN_TYPE(obj
) == T_MODULE
|| RB_BUILTIN_TYPE(obj
) == T_CLASS
) {
2355 return rb_check_funcall(obj
, rb_intern("name"), 0, 0);
2362 name_err_mesg_to_str(VALUE obj
)
2364 name_error_message_t
*ptr
;
2365 TypedData_Get_Struct(obj
, name_error_message_t
, &name_err_mesg_data_type
, ptr
);
2367 VALUE mesg
= ptr
->mesg
;
2368 if (NIL_P(mesg
)) return Qnil
;
2370 struct RString s_str
, c_str
, d_str
;
2371 VALUE c
, s
, d
= 0, args
[4], c2
;
2373 rb_encoding
*usascii
= rb_usascii_encoding();
2375 #define FAKE_CSTR(v, str) rb_setup_fake_str((v), (str), rb_strlen_lit(str), usascii)
2376 c
= s
= FAKE_CSTR(&s_str
, "");
2380 c
= d
= FAKE_CSTR(&d_str
, "nil");
2383 c
= d
= FAKE_CSTR(&d_str
, "true");
2386 c
= d
= FAKE_CSTR(&d_str
, "false");
2389 if (strstr(RSTRING_PTR(mesg
), "%2$s")) {
2390 d
= rb_protect(name_err_mesg_receiver_name
, obj
, &state
);
2391 if (state
|| NIL_OR_UNDEF_P(d
))
2392 d
= rb_protect(rb_inspect
, obj
, &state
);
2394 rb_set_errinfo(Qnil
);
2396 d
= rb_check_string_type(d
);
2398 d
= rb_any_to_s(obj
);
2402 if (!RB_SPECIAL_CONST_P(obj
)) {
2403 switch (RB_BUILTIN_TYPE(obj
)) {
2405 s
= FAKE_CSTR(&s_str
, "module ");
2409 s
= FAKE_CSTR(&s_str
, "class ");
2419 klass
= CLASS_OF(obj
);
2420 if (RB_TYPE_P(klass
, T_CLASS
) && RCLASS_SINGLETON_P(klass
)) {
2421 s
= FAKE_CSTR(&s_str
, "");
2422 if (obj
== rb_vm_top_self()) {
2423 c
= FAKE_CSTR(&c_str
, "main");
2426 c
= rb_any_to_s(obj
);
2431 s
= FAKE_CSTR(&s_str
, "an instance of ");
2432 c
= rb_class_real(klass
);
2435 c2
= rb_protect(name_err_mesg_receiver_name
, c
, &state
);
2436 if (state
|| NIL_OR_UNDEF_P(c2
))
2437 c2
= rb_protect(rb_inspect
, c
, &state
);
2439 rb_set_errinfo(Qnil
);
2441 c2
= rb_check_string_type(c2
);
2443 c2
= rb_any_to_s(c
);
2448 args
[0] = rb_obj_as_string(ptr
->name
);
2452 mesg
= rb_str_format(4, args
, mesg
);
2459 name_err_mesg_dump(VALUE obj
, VALUE limit
)
2461 return name_err_mesg_to_str(obj
);
2466 name_err_mesg_load(VALUE klass
, VALUE str
)
2473 * name_error.receiver -> object
2475 * Return the receiver associated with this NameError exception.
2479 name_err_receiver(VALUE self
)
2481 VALUE recv
= rb_ivar_lookup(self
, id_recv
, Qundef
);
2482 if (!UNDEF_P(recv
)) return recv
;
2484 VALUE mesg
= rb_attr_get(self
, id_mesg
);
2485 if (!rb_typeddata_is_kind_of(mesg
, &name_err_mesg_data_type
)) {
2486 rb_raise(rb_eArgError
, "no receiver is available");
2489 name_error_message_t
*ptr
;
2490 TypedData_Get_Struct(mesg
, name_error_message_t
, &name_err_mesg_data_type
, ptr
);
2496 * no_method_error.args -> obj
2498 * Return the arguments passed in as the third parameter to
2503 nometh_err_args(VALUE self
)
2505 return rb_attr_get(self
, id_args
);
2510 * no_method_error.private_call? -> true or false
2512 * Return true if the caused method was called as private.
2516 nometh_err_private_call_p(VALUE self
)
2518 return rb_attr_get(self
, id_private_call_p
);
2522 rb_invalid_str(const char *str
, const char *type
)
2524 VALUE s
= rb_str_new2(str
);
2526 rb_raise(rb_eArgError
, "invalid value for %s: %+"PRIsVALUE
, type
, s
);
2531 * key_error.receiver -> object
2533 * Return the receiver associated with this KeyError exception.
2537 key_err_receiver(VALUE self
)
2541 recv
= rb_ivar_lookup(self
, id_receiver
, Qundef
);
2542 if (!UNDEF_P(recv
)) return recv
;
2543 rb_raise(rb_eArgError
, "no receiver is available");
2548 * key_error.key -> object
2550 * Return the key caused this KeyError exception.
2554 key_err_key(VALUE self
)
2558 key
= rb_ivar_lookup(self
, id_key
, Qundef
);
2559 if (!UNDEF_P(key
)) return key
;
2560 rb_raise(rb_eArgError
, "no key is available");
2564 rb_key_err_new(VALUE mesg
, VALUE recv
, VALUE key
)
2566 VALUE exc
= rb_obj_alloc(rb_eKeyError
);
2567 rb_ivar_set(exc
, id_mesg
, mesg
);
2568 rb_ivar_set(exc
, id_bt
, Qnil
);
2569 rb_ivar_set(exc
, id_key
, key
);
2570 rb_ivar_set(exc
, id_receiver
, recv
);
2576 * KeyError.new(message=nil, receiver: nil, key: nil) -> key_error
2578 * Construct a new +KeyError+ exception with the given message,
2583 key_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2587 rb_call_super(rb_scan_args(argc
, argv
, "01:", NULL
, &options
), argv
);
2589 if (!NIL_P(options
)) {
2591 VALUE values
[numberof(keywords
)];
2593 keywords
[0] = id_receiver
;
2594 keywords
[1] = id_key
;
2595 rb_get_kwargs(options
, keywords
, 0, numberof(values
), values
);
2596 for (i
= 0; i
< numberof(values
); ++i
) {
2597 if (!UNDEF_P(values
[i
])) {
2598 rb_ivar_set(self
, keywords
[i
], values
[i
]);
2608 * no_matching_pattern_key_error.matchee -> object
2610 * Return the matchee associated with this NoMatchingPatternKeyError exception.
2614 no_matching_pattern_key_err_matchee(VALUE self
)
2618 matchee
= rb_ivar_lookup(self
, id_matchee
, Qundef
);
2619 if (!UNDEF_P(matchee
)) return matchee
;
2620 rb_raise(rb_eArgError
, "no matchee is available");
2625 * no_matching_pattern_key_error.key -> object
2627 * Return the key caused this NoMatchingPatternKeyError exception.
2631 no_matching_pattern_key_err_key(VALUE self
)
2635 key
= rb_ivar_lookup(self
, id_key
, Qundef
);
2636 if (!UNDEF_P(key
)) return key
;
2637 rb_raise(rb_eArgError
, "no key is available");
2642 * NoMatchingPatternKeyError.new(message=nil, matchee: nil, key: nil) -> no_matching_pattern_key_error
2644 * Construct a new +NoMatchingPatternKeyError+ exception with the given message,
2649 no_matching_pattern_key_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2653 rb_call_super(rb_scan_args(argc
, argv
, "01:", NULL
, &options
), argv
);
2655 if (!NIL_P(options
)) {
2657 VALUE values
[numberof(keywords
)];
2659 keywords
[0] = id_matchee
;
2660 keywords
[1] = id_key
;
2661 rb_get_kwargs(options
, keywords
, 0, numberof(values
), values
);
2662 for (i
= 0; i
< numberof(values
); ++i
) {
2663 if (!UNDEF_P(values
[i
])) {
2664 rb_ivar_set(self
, keywords
[i
], values
[i
]);
2675 * SyntaxError.new([msg]) -> syntax_error
2677 * Construct a SyntaxError exception.
2681 syntax_error_initialize(int argc
, VALUE
*argv
, VALUE self
)
2685 mesg
= rb_fstring_lit("compile error");
2689 return rb_call_super(argc
, argv
);
2693 syntax_error_with_path(VALUE exc
, VALUE path
, VALUE
*mesg
, rb_encoding
*enc
)
2696 *mesg
= rb_enc_str_new(0, 0, enc
);
2697 exc
= rb_class_new_instance(1, mesg
, rb_eSyntaxError
);
2698 rb_ivar_set(exc
, id_i_path
, path
);
2701 if (rb_attr_get(exc
, id_i_path
) != path
) {
2702 rb_raise(rb_eArgError
, "SyntaxError#path changed");
2704 VALUE s
= *mesg
= rb_attr_get(exc
, idMesg
);
2705 if (RSTRING_LEN(s
) > 0 && *(RSTRING_END(s
)-1) != '\n')
2706 rb_str_cat_cstr(s
, "\n");
2712 * Document-module: Errno
2714 * Ruby exception objects are subclasses of Exception. However,
2715 * operating systems typically report errors using plain
2716 * integers. Module Errno is created dynamically to map these
2717 * operating system errors to Ruby classes, with each error number
2718 * generating its own subclass of SystemCallError. As the subclass
2719 * is created in module Errno, its name will start
2720 * <code>Errno::</code>.
2722 * The names of the <code>Errno::</code> classes depend on the
2723 * environment in which Ruby runs. On a typical Unix or Windows
2724 * platform, there are Errno classes such as Errno::EACCES,
2725 * Errno::EAGAIN, Errno::EINTR, and so on.
2727 * The integer operating system error number corresponding to a
2728 * particular error is available as the class constant
2729 * <code>Errno::</code><em>error</em><code>::Errno</code>.
2731 * Errno::EACCES::Errno #=> 13
2732 * Errno::EAGAIN::Errno #=> 11
2733 * Errno::EINTR::Errno #=> 4
2735 * The full list of operating system errors on your particular platform
2736 * are available as the constants of Errno.
2738 * Errno.constants #=> :E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, ...
2741 static st_table
*syserr_tbl
;
2744 rb_free_warning(void)
2746 st_free_table(warning_categories
.id2enum
);
2747 st_free_table(warning_categories
.enum2id
);
2748 st_free_table(syserr_tbl
);
2752 setup_syserr(int n
, const char *name
)
2754 VALUE error
= rb_define_class_under(rb_mErrno
, name
, rb_eSystemCallError
);
2756 /* capture nonblock errnos for WaitReadable/WaitWritable subclasses */
2761 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
2766 rb_eEWOULDBLOCK
= error
;
2769 rb_eEINPROGRESS
= error
;
2773 rb_define_const(error
, "Errno", INT2NUM(n
));
2774 st_add_direct(syserr_tbl
, n
, (st_data_t
)error
);
2779 set_syserr(int n
, const char *name
)
2783 if (!st_lookup(syserr_tbl
, n
, &error
)) {
2784 return setup_syserr(n
, name
);
2787 VALUE errclass
= (VALUE
)error
;
2788 rb_define_const(rb_mErrno
, name
, errclass
);
2798 if (!st_lookup(syserr_tbl
, n
, &error
)) {
2799 char name
[DECIMAL_SIZE_OF(n
) + sizeof("E-")];
2801 snprintf(name
, sizeof(name
), "E%03d", n
);
2802 return setup_syserr(n
, name
);
2804 return (VALUE
)error
;
2809 * SystemCallError.new(msg, errno) -> system_call_error_subclass
2811 * If _errno_ corresponds to a known system error code, constructs the
2812 * appropriate Errno class for that error, otherwise constructs a
2813 * generic SystemCallError object. The error number is subsequently
2814 * available via the #errno method.
2818 syserr_initialize(int argc
, VALUE
*argv
, VALUE self
)
2821 VALUE mesg
, error
, func
, errmsg
;
2822 VALUE klass
= rb_obj_class(self
);
2824 if (klass
== rb_eSystemCallError
) {
2825 st_data_t data
= (st_data_t
)klass
;
2826 rb_scan_args(argc
, argv
, "12", &mesg
, &error
, &func
);
2827 if (argc
== 1 && FIXNUM_P(mesg
)) {
2828 error
= mesg
; mesg
= Qnil
;
2830 if (!NIL_P(error
) && st_lookup(syserr_tbl
, NUM2LONG(error
), &data
)) {
2831 klass
= (VALUE
)data
;
2833 if (!RB_TYPE_P(self
, T_OBJECT
)) { /* insurance to avoid type crash */
2834 rb_raise(rb_eTypeError
, "invalid instance type");
2836 RBASIC_SET_CLASS(self
, klass
);
2840 rb_scan_args(argc
, argv
, "02", &mesg
, &func
);
2841 error
= rb_const_get(klass
, id_Errno
);
2843 if (!NIL_P(error
)) err
= strerror(NUM2INT(error
));
2844 else err
= "unknown error";
2846 errmsg
= rb_enc_str_new_cstr(err
, rb_locale_encoding());
2848 VALUE str
= StringValue(mesg
);
2850 if (!NIL_P(func
)) rb_str_catf(errmsg
, " @ %"PRIsVALUE
, func
);
2851 rb_str_catf(errmsg
, " - %"PRIsVALUE
, str
);
2855 rb_call_super(1, &mesg
);
2856 rb_ivar_set(self
, id_errno
, error
);
2862 * system_call_error.errno -> integer
2864 * Return this SystemCallError's error number.
2868 syserr_errno(VALUE self
)
2870 return rb_attr_get(self
, id_errno
);
2875 * system_call_error === other -> true or false
2877 * Return +true+ if the receiver is a generic +SystemCallError+, or
2878 * if the error numbers +self+ and _other_ are the same.
2882 syserr_eqq(VALUE self
, VALUE exc
)
2886 if (!rb_obj_is_kind_of(exc
, rb_eSystemCallError
)) {
2887 if (!rb_respond_to(exc
, id_errno
)) return Qfalse
;
2889 else if (self
== rb_eSystemCallError
) return Qtrue
;
2891 num
= rb_attr_get(exc
, id_errno
);
2893 num
= rb_funcallv(exc
, id_errno
, 0, 0);
2895 e
= rb_const_get(self
, id_Errno
);
2896 return RBOOL(FIXNUM_P(num
) ? num
== e
: rb_equal(num
, e
));
2901 * Document-class: StandardError
2903 * The most standard error types are subclasses of StandardError. A
2904 * rescue clause without an explicit Exception class will rescue all
2905 * StandardErrors (and only those).
2910 * foo rescue "Hello" #=> "Hello"
2912 * On the other hand:
2914 * require 'does/not/exist' rescue "Hi"
2916 * <em>raises the exception:</em>
2918 * LoadError: no such file to load -- does/not/exist
2923 * Document-class: SystemExit
2925 * Raised by +exit+ to initiate the termination of the script.
2929 * Document-class: SignalException
2931 * Raised when a signal is received.
2934 * Process.kill('HUP',Process.pid)
2935 * sleep # wait for receiver to handle signal sent by Process.kill
2936 * rescue SignalException => e
2937 * puts "received Exception #{e}"
2940 * <em>produces:</em>
2942 * received Exception SIGHUP
2946 * Document-class: Interrupt
2948 * Raised when the interrupt signal is received, typically because the
2949 * user has pressed Control-C (on most posix platforms). As such, it is a
2950 * subclass of +SignalException+.
2953 * puts "Press ctrl-C when you get bored"
2955 * rescue Interrupt => e
2956 * puts "Note: You will typically use Signal.trap instead."
2959 * <em>produces:</em>
2961 * Press ctrl-C when you get bored
2963 * <em>then waits until it is interrupted with Control-C and then prints:</em>
2965 * Note: You will typically use Signal.trap instead.
2969 * Document-class: TypeError
2971 * Raised when encountering an object that is not of the expected type.
2973 * [1, 2, 3].first("two")
2975 * <em>raises the exception:</em>
2977 * TypeError: no implicit conversion of String into Integer
2982 * Document-class: ArgumentError
2984 * Raised when the arguments are wrong and there isn't a more specific
2987 * Ex: passing the wrong number of arguments
2989 * [1, 2, 3].first(4, 5)
2991 * <em>raises the exception:</em>
2993 * ArgumentError: wrong number of arguments (given 2, expected 1)
2995 * Ex: passing an argument that is not acceptable:
2997 * [1, 2, 3].first(-4)
2999 * <em>raises the exception:</em>
3001 * ArgumentError: negative array size
3005 * Document-class: IndexError
3007 * Raised when the given index is invalid.
3010 * a.fetch(0) #=> :foo
3012 * a.fetch(4) #=> IndexError: index 4 outside of array bounds: -2...2
3017 * Document-class: KeyError
3019 * Raised when the specified key is not found. It is a subclass of
3022 * h = {"foo" => :bar}
3023 * h.fetch("foo") #=> :bar
3024 * h.fetch("baz") #=> KeyError: key not found: "baz"
3029 * Document-class: RangeError
3031 * Raised when a given numerical value is out of range.
3033 * [1, 2, 3].drop(1 << 100)
3035 * <em>raises the exception:</em>
3037 * RangeError: bignum too big to convert into `long'
3041 * Document-class: ScriptError
3043 * ScriptError is the superclass for errors raised when a script
3044 * can not be executed because of a +LoadError+,
3045 * +NotImplementedError+ or a +SyntaxError+. Note these type of
3046 * +ScriptErrors+ are not +StandardError+ and will not be
3047 * rescued unless it is specified explicitly (or its ancestor
3052 * Document-class: SyntaxError
3054 * Raised when encountering Ruby code with an invalid syntax.
3058 * <em>raises the exception:</em>
3060 * SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
3064 * Document-class: LoadError
3066 * Raised when a file required (a Ruby script, extension library, ...)
3069 * require 'this/file/does/not/exist'
3071 * <em>raises the exception:</em>
3073 * LoadError: no such file to load -- this/file/does/not/exist
3077 * Document-class: NotImplementedError
3079 * Raised when a feature is not implemented on the current platform. For
3080 * example, methods depending on the +fsync+ or +fork+ system calls may
3081 * raise this exception if the underlying operating system or Ruby
3082 * runtime does not support them.
3084 * Note that if +fork+ raises a +NotImplementedError+, then
3085 * <code>respond_to?(:fork)</code> returns +false+.
3089 * Document-class: NameError
3091 * Raised when a given name is invalid or undefined.
3095 * <em>raises the exception:</em>
3097 * NameError: undefined local variable or method `foo' for main:Object
3099 * Since constant names must start with a capital:
3101 * Integer.const_set :answer, 42
3103 * <em>raises the exception:</em>
3105 * NameError: wrong constant name answer
3109 * Document-class: NoMethodError
3111 * Raised when a method is called on a receiver which doesn't have it
3112 * defined and also fails to respond with +method_missing+.
3116 * <em>raises the exception:</em>
3118 * NoMethodError: undefined method `to_ary' for an instance of String
3122 * Document-class: FrozenError
3124 * Raised when there is an attempt to modify a frozen object.
3126 * [1, 2, 3].freeze << 4
3128 * <em>raises the exception:</em>
3130 * FrozenError: can't modify frozen Array
3134 * Document-class: RuntimeError
3136 * A generic error class raised when an invalid operation is attempted.
3137 * Kernel#raise will raise a RuntimeError if no Exception class is
3142 * <em>raises the exception:</em>
3144 * RuntimeError: ouch
3148 * Document-class: SecurityError
3150 * No longer used by internal code.
3154 * Document-class: NoMemoryError
3156 * Raised when memory allocation fails.
3160 * Document-class: SystemCallError
3162 * SystemCallError is the base class for all low-level
3163 * platform-dependent errors.
3165 * The errors available on the current platform are subclasses of
3166 * SystemCallError and are defined in the Errno module.
3168 * File.open("does/not/exist")
3170 * <em>raises the exception:</em>
3172 * Errno::ENOENT: No such file or directory - does/not/exist
3176 * Document-class: EncodingError
3178 * EncodingError is the base class for encoding errors.
3182 * Document-class: Encoding::CompatibilityError
3184 * Raised by Encoding and String methods when the source encoding is
3185 * incompatible with the target encoding.
3189 * Document-class: fatal
3191 * +fatal+ is an Exception that is raised when Ruby has encountered a fatal
3192 * error and must exit.
3196 * Document-class: NameError::message
3201 * Document-class: Exception
3203 * \Class Exception and its subclasses are used to communicate between
3204 * Kernel#raise and +rescue+ statements in <code>begin ... end</code> blocks.
3206 * An Exception object carries information about an exception:
3207 * - Its type (the exception's class).
3208 * - An optional descriptive message.
3209 * - Optional backtrace information.
3211 * Some built-in subclasses of Exception have additional methods: e.g., NameError#name.
3215 * Two Ruby statements have default exception classes:
3216 * - +raise+: defaults to RuntimeError.
3217 * - +rescue+: defaults to StandardError.
3219 * == Global Variables
3221 * When an exception has been raised but not yet handled (in +rescue+,
3222 * +ensure+, +at_exit+ and +END+ blocks), two global variables are set:
3223 * - <code>$!</code> contains the current exception.
3224 * - <code>$@</code> contains its backtrace.
3226 * == Custom Exceptions
3228 * To provide additional or alternate information,
3229 * a program may create custom exception classes
3230 * that derive from the built-in exception classes.
3232 * A good practice is for a library to create a single "generic" exception class
3233 * (typically a subclass of StandardError or RuntimeError)
3234 * and have its other exception classes derive from that class.
3235 * This allows the user to rescue the generic exception, thus catching all exceptions
3236 * the library may raise even if future versions of the library add new
3237 * exception subclasses.
3242 * class Error < ::StandardError
3245 * class WidgetError < Error
3248 * class FrobError < Error
3253 * To handle both MyLibrary::WidgetError and MyLibrary::FrobError the library
3254 * user can rescue MyLibrary::Error.
3256 * == Built-In Exception Classes
3258 * The built-in subclasses of Exception are:
3263 * * NotImplementedError
3270 * * UncaughtThrowError
3278 * * ClosedQueueError
3283 * * FloatDomainError
3291 * * ZeroDivisionError
3293 * * SystemStackError
3298 exception_alloc(VALUE klass
)
3300 return rb_class_allocate_instance(klass
);
3304 exception_dumper(VALUE exc
)
3306 // TODO: Currently, the instance variables "bt" and "bt_locations"
3307 // refers to the same object (Array of String). But "bt_locations"
3308 // should have an Array of Thread::Backtrace::Locations.
3314 ivar_copy_i(ID key
, VALUE val
, st_data_t exc
)
3316 rb_ivar_set((VALUE
)exc
, key
, val
);
3320 void rb_exc_check_circular_cause(VALUE exc
);
3323 exception_loader(VALUE exc
, VALUE obj
)
3325 // The loader function of rb_marshal_define_compat seems to be called for two events:
3326 // one is for fixup (r_fixup_compat), the other is for TYPE_USERDEF.
3327 // In the former case, the first argument is an instance of Exception (because
3328 // we pass rb_eException to rb_marshal_define_compat). In the latter case, the first
3329 // argument is a class object (see TYPE_USERDEF case in r_object0).
3330 // We want to copy all instance variables (but "bt_locations") from obj to exc.
3331 // But we do not want to do so in the second case, so the following branch is for that.
3332 if (RB_TYPE_P(exc
, T_CLASS
)) return obj
; // maybe called from Marshal's TYPE_USERDEF
3334 rb_ivar_foreach(obj
, ivar_copy_i
, exc
);
3336 rb_exc_check_circular_cause(exc
);
3338 if (rb_attr_get(exc
, id_bt
) == rb_attr_get(exc
, id_bt_locations
)) {
3339 rb_ivar_set(exc
, id_bt_locations
, Qnil
);
3346 Init_Exception(void)
3348 rb_eException
= rb_define_class("Exception", rb_cObject
);
3349 rb_define_alloc_func(rb_eException
, exception_alloc
);
3350 rb_marshal_define_compat(rb_eException
, rb_eException
, exception_dumper
, exception_loader
);
3351 rb_define_singleton_method(rb_eException
, "exception", rb_class_new_instance
, -1);
3352 rb_define_singleton_method(rb_eException
, "to_tty?", exc_s_to_tty_p
, 0);
3353 rb_define_method(rb_eException
, "exception", exc_exception
, -1);
3354 rb_define_method(rb_eException
, "initialize", exc_initialize
, -1);
3355 rb_define_method(rb_eException
, "==", exc_equal
, 1);
3356 rb_define_method(rb_eException
, "to_s", exc_to_s
, 0);
3357 rb_define_method(rb_eException
, "message", exc_message
, 0);
3358 rb_define_method(rb_eException
, "detailed_message", exc_detailed_message
, -1);
3359 rb_define_method(rb_eException
, "full_message", exc_full_message
, -1);
3360 rb_define_method(rb_eException
, "inspect", exc_inspect
, 0);
3361 rb_define_method(rb_eException
, "backtrace", exc_backtrace
, 0);
3362 rb_define_method(rb_eException
, "backtrace_locations", exc_backtrace_locations
, 0);
3363 rb_define_method(rb_eException
, "set_backtrace", exc_set_backtrace
, 1);
3364 rb_define_method(rb_eException
, "cause", exc_cause
, 0);
3366 rb_eSystemExit
= rb_define_class("SystemExit", rb_eException
);
3367 rb_define_method(rb_eSystemExit
, "initialize", exit_initialize
, -1);
3368 rb_define_method(rb_eSystemExit
, "status", exit_status
, 0);
3369 rb_define_method(rb_eSystemExit
, "success?", exit_success_p
, 0);
3371 rb_eFatal
= rb_define_class("fatal", rb_eException
);
3372 rb_eSignal
= rb_define_class("SignalException", rb_eException
);
3373 rb_eInterrupt
= rb_define_class("Interrupt", rb_eSignal
);
3375 rb_eStandardError
= rb_define_class("StandardError", rb_eException
);
3376 rb_eTypeError
= rb_define_class("TypeError", rb_eStandardError
);
3377 rb_eArgError
= rb_define_class("ArgumentError", rb_eStandardError
);
3378 rb_eIndexError
= rb_define_class("IndexError", rb_eStandardError
);
3379 rb_eKeyError
= rb_define_class("KeyError", rb_eIndexError
);
3380 rb_define_method(rb_eKeyError
, "initialize", key_err_initialize
, -1);
3381 rb_define_method(rb_eKeyError
, "receiver", key_err_receiver
, 0);
3382 rb_define_method(rb_eKeyError
, "key", key_err_key
, 0);
3383 rb_eRangeError
= rb_define_class("RangeError", rb_eStandardError
);
3385 rb_eScriptError
= rb_define_class("ScriptError", rb_eException
);
3386 rb_eSyntaxError
= rb_define_class("SyntaxError", rb_eScriptError
);
3387 rb_define_method(rb_eSyntaxError
, "initialize", syntax_error_initialize
, -1);
3389 /* RDoc will use literal name value while parsing rb_attr,
3390 * and will render `idPath` as an attribute name without this trick */
3393 /* the path failed to parse */
3394 rb_attr(rb_eSyntaxError
, path
, TRUE
, FALSE
, FALSE
);
3396 rb_eLoadError
= rb_define_class("LoadError", rb_eScriptError
);
3397 /* the path failed to load */
3398 rb_attr(rb_eLoadError
, path
, TRUE
, FALSE
, FALSE
);
3400 rb_eNotImpError
= rb_define_class("NotImplementedError", rb_eScriptError
);
3402 rb_eNameError
= rb_define_class("NameError", rb_eStandardError
);
3403 rb_define_method(rb_eNameError
, "initialize", name_err_initialize
, -1);
3404 rb_define_method(rb_eNameError
, "name", name_err_name
, 0);
3405 rb_define_method(rb_eNameError
, "receiver", name_err_receiver
, 0);
3406 rb_define_method(rb_eNameError
, "local_variables", name_err_local_variables
, 0);
3407 rb_cNameErrorMesg
= rb_define_class_under(rb_eNameError
, "message", rb_cObject
);
3408 rb_define_alloc_func(rb_cNameErrorMesg
, name_err_mesg_alloc
);
3409 rb_define_method(rb_cNameErrorMesg
, "initialize_copy", name_err_mesg_init_copy
, 1);
3410 rb_define_method(rb_cNameErrorMesg
, "==", name_err_mesg_equal
, 1);
3411 rb_define_method(rb_cNameErrorMesg
, "to_str", name_err_mesg_to_str
, 0);
3412 rb_define_method(rb_cNameErrorMesg
, "_dump", name_err_mesg_dump
, 1);
3413 rb_define_singleton_method(rb_cNameErrorMesg
, "_load", name_err_mesg_load
, 1);
3414 rb_eNoMethodError
= rb_define_class("NoMethodError", rb_eNameError
);
3415 rb_define_method(rb_eNoMethodError
, "initialize", nometh_err_initialize
, -1);
3416 rb_define_method(rb_eNoMethodError
, "args", nometh_err_args
, 0);
3417 rb_define_method(rb_eNoMethodError
, "private_call?", nometh_err_private_call_p
, 0);
3419 rb_eRuntimeError
= rb_define_class("RuntimeError", rb_eStandardError
);
3420 rb_eFrozenError
= rb_define_class("FrozenError", rb_eRuntimeError
);
3421 rb_define_method(rb_eFrozenError
, "initialize", frozen_err_initialize
, -1);
3422 rb_define_method(rb_eFrozenError
, "receiver", frozen_err_receiver
, 0);
3423 rb_eSecurityError
= rb_define_class("SecurityError", rb_eException
);
3424 rb_eNoMemError
= rb_define_class("NoMemoryError", rb_eException
);
3425 rb_eEncodingError
= rb_define_class("EncodingError", rb_eStandardError
);
3426 rb_eEncCompatError
= rb_define_class_under(rb_cEncoding
, "CompatibilityError", rb_eEncodingError
);
3427 rb_eNoMatchingPatternError
= rb_define_class("NoMatchingPatternError", rb_eStandardError
);
3428 rb_eNoMatchingPatternKeyError
= rb_define_class("NoMatchingPatternKeyError", rb_eNoMatchingPatternError
);
3429 rb_define_method(rb_eNoMatchingPatternKeyError
, "initialize", no_matching_pattern_key_err_initialize
, -1);
3430 rb_define_method(rb_eNoMatchingPatternKeyError
, "matchee", no_matching_pattern_key_err_matchee
, 0);
3431 rb_define_method(rb_eNoMatchingPatternKeyError
, "key", no_matching_pattern_key_err_key
, 0);
3433 syserr_tbl
= st_init_numtable();
3434 rb_eSystemCallError
= rb_define_class("SystemCallError", rb_eStandardError
);
3435 rb_define_method(rb_eSystemCallError
, "initialize", syserr_initialize
, -1);
3436 rb_define_method(rb_eSystemCallError
, "errno", syserr_errno
, 0);
3437 rb_define_singleton_method(rb_eSystemCallError
, "===", syserr_eqq
, 1);
3439 rb_mErrno
= rb_define_module("Errno");
3441 rb_mWarning
= rb_define_module("Warning");
3442 rb_define_singleton_method(rb_mWarning
, "[]", rb_warning_s_aref
, 1);
3443 rb_define_singleton_method(rb_mWarning
, "[]=", rb_warning_s_aset
, 2);
3444 rb_define_singleton_method(rb_mWarning
, "categories", rb_warning_s_categories
, 0);
3445 rb_define_method(rb_mWarning
, "warn", rb_warning_s_warn
, -1);
3446 rb_extend_object(rb_mWarning
, rb_mWarning
);
3449 rb_cWarningBuffer
= rb_define_class_under(rb_mWarning
, "buffer", rb_cString
);
3450 rb_define_method(rb_cWarningBuffer
, "write", warning_write
, -1);
3452 id_cause
= rb_intern_const("cause");
3453 id_message
= rb_intern_const("message");
3454 id_detailed_message
= rb_intern_const("detailed_message");
3455 id_backtrace
= rb_intern_const("backtrace");
3456 id_key
= rb_intern_const("key");
3457 id_matchee
= rb_intern_const("matchee");
3458 id_args
= rb_intern_const("args");
3459 id_receiver
= rb_intern_const("receiver");
3460 id_private_call_p
= rb_intern_const("private_call?");
3461 id_local_variables
= rb_intern_const("local_variables");
3462 id_Errno
= rb_intern_const("Errno");
3463 id_errno
= rb_intern_const("errno");
3464 id_i_path
= rb_intern_const("@path");
3465 id_warn
= rb_intern_const("warn");
3466 id_category
= rb_intern_const("category");
3467 id_deprecated
= rb_intern_const("deprecated");
3468 id_experimental
= rb_intern_const("experimental");
3469 id_performance
= rb_intern_const("performance");
3470 id_top
= rb_intern_const("top");
3471 id_bottom
= rb_intern_const("bottom");
3472 id_iseq
= rb_make_internal_id();
3473 id_recv
= rb_make_internal_id();
3475 sym_category
= ID2SYM(id_category
);
3476 sym_highlight
= ID2SYM(rb_intern_const("highlight"));
3478 warning_categories
.id2enum
= rb_init_identtable();
3479 st_add_direct(warning_categories
.id2enum
, id_deprecated
, RB_WARN_CATEGORY_DEPRECATED
);
3480 st_add_direct(warning_categories
.id2enum
, id_experimental
, RB_WARN_CATEGORY_EXPERIMENTAL
);
3481 st_add_direct(warning_categories
.id2enum
, id_performance
, RB_WARN_CATEGORY_PERFORMANCE
);
3483 warning_categories
.enum2id
= rb_init_identtable();
3484 st_add_direct(warning_categories
.enum2id
, RB_WARN_CATEGORY_NONE
, 0);
3485 st_add_direct(warning_categories
.enum2id
, RB_WARN_CATEGORY_DEPRECATED
, id_deprecated
);
3486 st_add_direct(warning_categories
.enum2id
, RB_WARN_CATEGORY_EXPERIMENTAL
, id_experimental
);
3487 st_add_direct(warning_categories
.enum2id
, RB_WARN_CATEGORY_PERFORMANCE
, id_performance
);
3491 rb_enc_raise(rb_encoding
*enc
, VALUE exc
, const char *fmt
, ...)
3496 va_start(args
, fmt
);
3497 mesg
= rb_enc_vsprintf(enc
, fmt
, args
);
3500 rb_exc_raise(rb_exc_new3(exc
, mesg
));
3504 rb_vraise(VALUE exc
, const char *fmt
, va_list ap
)
3506 rb_exc_raise(rb_exc_new3(exc
, rb_vsprintf(fmt
, ap
)));
3510 rb_raise(VALUE exc
, const char *fmt
, ...)
3513 va_start(args
, fmt
);
3514 rb_vraise(exc
, fmt
, args
);
3518 NORETURN(static void raise_loaderror(VALUE path
, VALUE mesg
));
3521 raise_loaderror(VALUE path
, VALUE mesg
)
3523 VALUE err
= rb_exc_new3(rb_eLoadError
, mesg
);
3524 rb_ivar_set(err
, id_i_path
, path
);
3529 rb_loaderror(const char *fmt
, ...)
3534 va_start(args
, fmt
);
3535 mesg
= rb_enc_vsprintf(rb_locale_encoding(), fmt
, args
);
3537 raise_loaderror(Qnil
, mesg
);
3541 rb_loaderror_with_path(VALUE path
, const char *fmt
, ...)
3546 va_start(args
, fmt
);
3547 mesg
= rb_enc_vsprintf(rb_locale_encoding(), fmt
, args
);
3549 raise_loaderror(path
, mesg
);
3553 rb_notimplement(void)
3555 rb_raise(rb_eNotImpError
,
3556 "%"PRIsVALUE
"() function is unimplemented on this machine",
3557 rb_id2str(rb_frame_this_func()));
3561 rb_fatal(const char *fmt
, ...)
3566 if (! ruby_thread_has_gvl_p()) {
3567 /* The thread has no GVL. Object allocation impossible (cant run GC),
3568 * thus no message can be printed out. */
3569 fprintf(stderr
, "[FATAL] rb_fatal() outside of GVL\n");
3570 rb_print_backtrace(stderr
);
3574 va_start(args
, fmt
);
3575 mesg
= rb_vsprintf(fmt
, args
);
3578 rb_exc_fatal(rb_exc_new3(rb_eFatal
, mesg
));
3582 make_errno_exc(const char *mesg
)
3588 rb_bug("rb_sys_fail(%s) - errno == 0", mesg
? mesg
: "");
3590 return rb_syserr_new(n
, mesg
);
3594 make_errno_exc_str(VALUE mesg
)
3599 if (!mesg
) mesg
= Qnil
;
3601 const char *s
= !NIL_P(mesg
) ? RSTRING_PTR(mesg
) : "";
3602 rb_bug("rb_sys_fail_str(%s) - errno == 0", s
);
3604 return rb_syserr_new_str(n
, mesg
);
3608 rb_syserr_new(int n
, const char *mesg
)
3611 arg
= mesg
? rb_str_new2(mesg
) : Qnil
;
3612 return rb_syserr_new_str(n
, arg
);
3616 rb_syserr_new_str(int n
, VALUE arg
)
3618 return rb_class_new_instance(1, &arg
, get_syserr(n
));
3622 rb_syserr_fail(int e
, const char *mesg
)
3624 rb_exc_raise(rb_syserr_new(e
, mesg
));
3628 rb_syserr_fail_str(int e
, VALUE mesg
)
3630 rb_exc_raise(rb_syserr_new_str(e
, mesg
));
3635 rb_sys_fail(const char *mesg
)
3637 rb_exc_raise(make_errno_exc(mesg
));
3640 #undef rb_sys_fail_str
3642 rb_sys_fail_str(VALUE mesg
)
3644 rb_exc_raise(make_errno_exc_str(mesg
));
3647 #ifdef RUBY_FUNCTION_NAME_STRING
3649 rb_sys_fail_path_in(const char *func_name
, VALUE path
)
3654 rb_syserr_fail_path_in(func_name
, n
, path
);
3658 rb_syserr_fail_path_in(const char *func_name
, int n
, VALUE path
)
3660 rb_exc_raise(rb_syserr_new_path_in(func_name
, n
, path
));
3664 rb_syserr_new_path_in(const char *func_name
, int n
, VALUE path
)
3668 if (!path
) path
= Qnil
;
3670 const char *s
= !NIL_P(path
) ? RSTRING_PTR(path
) : "";
3671 if (!func_name
) func_name
= "(null)";
3672 rb_bug("rb_sys_fail_path_in(%s, %s) - errno == 0",
3676 args
[1] = rb_str_new_cstr(func_name
);
3677 return rb_class_new_instance(2, args
, get_syserr(n
));
3681 NORETURN(static void rb_mod_exc_raise(VALUE exc
, VALUE mod
));
3684 rb_mod_exc_raise(VALUE exc
, VALUE mod
)
3686 rb_extend_object(exc
, mod
);
3691 rb_mod_sys_fail(VALUE mod
, const char *mesg
)
3693 VALUE exc
= make_errno_exc(mesg
);
3694 rb_mod_exc_raise(exc
, mod
);
3698 rb_mod_sys_fail_str(VALUE mod
, VALUE mesg
)
3700 VALUE exc
= make_errno_exc_str(mesg
);
3701 rb_mod_exc_raise(exc
, mod
);
3705 rb_mod_syserr_fail(VALUE mod
, int e
, const char *mesg
)
3707 VALUE exc
= rb_syserr_new(e
, mesg
);
3708 rb_mod_exc_raise(exc
, mod
);
3712 rb_mod_syserr_fail_str(VALUE mod
, int e
, VALUE mesg
)
3714 VALUE exc
= rb_syserr_new_str(e
, mesg
);
3715 rb_mod_exc_raise(exc
, mod
);
3719 syserr_warning(VALUE mesg
, int err
)
3721 rb_str_set_len(mesg
, RSTRING_LEN(mesg
)-1);
3722 rb_str_catf(mesg
, ": %s\n", strerror(err
));
3723 rb_write_warning_str(mesg
);
3728 rb_sys_warn(const char *fmt
, ...)
3730 if (!NIL_P(ruby_verbose
)) {
3731 int errno_save
= errno
;
3732 with_warning_string(mesg
, 0, fmt
) {
3733 syserr_warning(mesg
, errno_save
);
3740 rb_syserr_warn(int err
, const char *fmt
, ...)
3742 if (!NIL_P(ruby_verbose
)) {
3743 with_warning_string(mesg
, 0, fmt
) {
3744 syserr_warning(mesg
, err
);
3750 rb_sys_enc_warn(rb_encoding
*enc
, const char *fmt
, ...)
3752 if (!NIL_P(ruby_verbose
)) {
3753 int errno_save
= errno
;
3754 with_warning_string(mesg
, enc
, fmt
) {
3755 syserr_warning(mesg
, errno_save
);
3762 rb_syserr_enc_warn(int err
, rb_encoding
*enc
, const char *fmt
, ...)
3764 if (!NIL_P(ruby_verbose
)) {
3765 with_warning_string(mesg
, enc
, fmt
) {
3766 syserr_warning(mesg
, err
);
3773 rb_sys_warning(const char *fmt
, ...)
3775 if (RTEST(ruby_verbose
)) {
3776 int errno_save
= errno
;
3777 with_warning_string(mesg
, 0, fmt
) {
3778 syserr_warning(mesg
, errno_save
);
3786 rb_syserr_warning(int err
, const char *fmt
, ...)
3788 if (RTEST(ruby_verbose
)) {
3789 with_warning_string(mesg
, 0, fmt
) {
3790 syserr_warning(mesg
, err
);
3797 rb_sys_enc_warning(rb_encoding
*enc
, const char *fmt
, ...)
3799 if (RTEST(ruby_verbose
)) {
3800 int errno_save
= errno
;
3801 with_warning_string(mesg
, enc
, fmt
) {
3802 syserr_warning(mesg
, errno_save
);
3809 rb_syserr_enc_warning(int err
, rb_encoding
*enc
, const char *fmt
, ...)
3811 if (RTEST(ruby_verbose
)) {
3812 with_warning_string(mesg
, enc
, fmt
) {
3813 syserr_warning(mesg
, err
);
3819 rb_load_fail(VALUE path
, const char *err
)
3821 VALUE mesg
= rb_str_buf_new_cstr(err
);
3822 rb_str_cat2(mesg
, " -- ");
3823 rb_str_append(mesg
, path
); /* should be ASCII compatible */
3824 raise_loaderror(path
, mesg
);
3828 rb_error_frozen(const char *what
)
3830 rb_raise(rb_eFrozenError
, "can't modify frozen %s", what
);
3834 rb_frozen_error_raise(VALUE frozen_obj
, const char *fmt
, ...)
3839 va_start(args
, fmt
);
3840 mesg
= rb_vsprintf(fmt
, args
);
3842 exc
= rb_exc_new3(rb_eFrozenError
, mesg
);
3843 rb_ivar_set(exc
, id_recv
, frozen_obj
);
3848 inspect_frozen_obj(VALUE obj
, VALUE mesg
, int recur
)
3851 rb_str_cat_cstr(mesg
, " ...");
3854 rb_str_append(mesg
, rb_inspect(obj
));
3860 rb_error_frozen_object(VALUE frozen_obj
)
3862 rb_yjit_lazy_push_frame(GET_EC()->cfp
->pc
);
3864 if (CHILLED_STRING_P(frozen_obj
)) {
3865 CHILLED_STRING_MUTATED(frozen_obj
);
3870 const ID created_info
= id_debug_created_info
;
3871 VALUE mesg
= rb_sprintf("can't modify frozen %"PRIsVALUE
": ",
3872 CLASS_OF(frozen_obj
));
3873 VALUE exc
= rb_exc_new_str(rb_eFrozenError
, mesg
);
3875 rb_ivar_set(exc
, id_recv
, frozen_obj
);
3876 rb_exec_recursive(inspect_frozen_obj
, frozen_obj
, mesg
);
3878 if (!NIL_P(debug_info
= rb_attr_get(frozen_obj
, created_info
))) {
3879 VALUE path
= rb_ary_entry(debug_info
, 0);
3880 VALUE line
= rb_ary_entry(debug_info
, 1);
3882 rb_str_catf(mesg
, ", created at %"PRIsVALUE
":%"PRIsVALUE
, path
, line
);
3887 #undef rb_check_frozen
3889 rb_check_frozen(VALUE obj
)
3891 rb_check_frozen_internal(obj
);
3895 rb_check_copyable(VALUE obj
, VALUE orig
)
3897 if (!FL_ABLE(obj
)) return;
3898 rb_check_frozen_internal(obj
);
3899 if (!FL_ABLE(orig
)) return;
3905 rb_eNOERROR
= setup_syserr(0, "NOERROR");
3908 rb_define_const(rb_mErrno
, "NOERROR", rb_eNOERROR
);
3910 #define defined_error(name, num) set_syserr((num), (name));
3911 #define undefined_error(name) rb_define_const(rb_mErrno, (name), rb_eNOERROR);
3912 #include "known_errors.inc"
3913 #undef defined_error
3914 #undef undefined_error
3917 #include "warning.rbinc"