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(enc, file, line, fmt) \
392 va_start(args, fmt); \
393 str = warn_vsprintf(enc, 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(NULL
, file
, line
, fmt
) {
401 rb_write_warning_str(str
);
407 rb_enc_compile_warn(rb_encoding
*enc
, const char *file
, int line
, const char *fmt
, ...)
409 if (!NIL_P(ruby_verbose
)) {
410 with_warn_vsprintf(enc
, file
, line
, fmt
) {
411 rb_write_warning_str(str
);
416 /* rb_compile_warning() reports only in verbose mode */
418 rb_compile_warning(const char *file
, int line
, const char *fmt
, ...)
420 if (RTEST(ruby_verbose
)) {
421 with_warn_vsprintf(NULL
, file
, line
, fmt
) {
422 rb_write_warning_str(str
);
427 /* rb_enc_compile_warning() reports only in verbose mode */
429 rb_enc_compile_warning(rb_encoding
*enc
, const char *file
, int line
, const char *fmt
, ...)
431 if (RTEST(ruby_verbose
)) {
432 with_warn_vsprintf(enc
, file
, line
, fmt
) {
433 rb_write_warning_str(str
);
439 rb_category_compile_warn(rb_warning_category_t category
, const char *file
, int line
, const char *fmt
, ...)
441 if (!NIL_P(ruby_verbose
)) {
442 with_warn_vsprintf(NULL
, file
, line
, fmt
) {
443 rb_warn_category(str
, rb_warning_category_to_name(category
));
448 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 2, 0)
450 warning_string(rb_encoding
*enc
, const char *fmt
, va_list args
)
453 const char *file
= rb_source_location_cstr(&line
);
454 return warn_vsprintf(enc
, file
, line
, fmt
, args
);
457 #define with_warning_string(mesg, enc, fmt) \
458 with_warning_string_from(mesg, enc, fmt, fmt)
459 #define with_warning_string_from(mesg, enc, fmt, last_arg) \
461 va_list args; va_start(args, last_arg); \
462 mesg = warning_string(enc, fmt, args); \
466 rb_warn(const char *fmt
, ...)
468 if (!NIL_P(ruby_verbose
)) {
469 with_warning_string(mesg
, 0, fmt
) {
470 rb_write_warning_str(mesg
);
476 rb_category_warn(rb_warning_category_t category
, const char *fmt
, ...)
478 if (!NIL_P(ruby_verbose
)) {
479 with_warning_string(mesg
, 0, fmt
) {
480 rb_warn_category(mesg
, rb_warning_category_to_name(category
));
486 rb_enc_warn(rb_encoding
*enc
, const char *fmt
, ...)
488 if (!NIL_P(ruby_verbose
)) {
489 with_warning_string(mesg
, enc
, fmt
) {
490 rb_write_warning_str(mesg
);
495 /* rb_warning() reports only in verbose mode */
497 rb_warning(const char *fmt
, ...)
499 if (RTEST(ruby_verbose
)) {
500 with_warning_string(mesg
, 0, fmt
) {
501 rb_write_warning_str(mesg
);
506 /* rb_category_warning() reports only in verbose mode */
508 rb_category_warning(rb_warning_category_t category
, const char *fmt
, ...)
510 if (RTEST(ruby_verbose
)) {
511 with_warning_string(mesg
, 0, fmt
) {
512 rb_warn_category(mesg
, rb_warning_category_to_name(category
));
518 rb_warning_string(const char *fmt
, ...)
520 with_warning_string(mesg
, 0, fmt
) {
527 rb_enc_warning(rb_encoding
*enc
, const char *fmt
, ...)
529 if (RTEST(ruby_verbose
)) {
530 with_warning_string(mesg
, enc
, fmt
) {
531 rb_write_warning_str(mesg
);
538 deprecation_warning_enabled(void)
540 if (NIL_P(ruby_verbose
)) return false;
541 if (!rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED
)) return false;
546 warn_deprecated(VALUE mesg
, const char *removal
, const char *suggest
)
548 rb_str_set_len(mesg
, RSTRING_LEN(mesg
) - 1);
549 rb_str_cat_cstr(mesg
, " is deprecated");
551 rb_str_catf(mesg
, " and will be removed in Ruby %s", removal
);
553 if (suggest
) rb_str_catf(mesg
, "; use %s instead", suggest
);
554 rb_str_cat_cstr(mesg
, "\n");
555 rb_warn_category(mesg
, ID2SYM(id_deprecated
));
559 rb_warn_deprecated(const char *fmt
, const char *suggest
, ...)
561 if (!deprecation_warning_enabled()) return;
563 with_warning_string_from(mesg
, 0, fmt
, suggest
) {
564 warn_deprecated(mesg
, NULL
, suggest
);
569 rb_warn_deprecated_to_remove(const char *removal
, const char *fmt
, const char *suggest
, ...)
571 if (!deprecation_warning_enabled()) return;
573 with_warning_string_from(mesg
, 0, fmt
, suggest
) {
574 warn_deprecated(mesg
, removal
, suggest
);
579 end_with_asciichar(VALUE str
, int c
)
581 return RB_TYPE_P(str
, T_STRING
) &&
582 rb_str_end_with_asciichar(str
, c
);
587 warning_write(int argc
, VALUE
*argv
, VALUE buf
)
590 rb_str_append(buf
, *argv
++);
595 VALUE
rb_ec_backtrace_location_ary(const rb_execution_context_t
*ec
, long lev
, long n
, bool skip_internal
);
598 rb_warn_m(rb_execution_context_t
*ec
, VALUE exc
, VALUE msgs
, VALUE uplevel
, VALUE category
)
600 VALUE location
= Qnil
;
601 int argc
= RARRAY_LENINT(msgs
);
602 const VALUE
*argv
= RARRAY_CONST_PTR(msgs
);
604 if (!NIL_P(ruby_verbose
) && argc
> 0) {
606 if (!NIL_P(uplevel
)) {
607 long lev
= NUM2LONG(uplevel
);
609 rb_raise(rb_eArgError
, "negative level (%ld)", lev
);
611 location
= rb_ec_backtrace_location_ary(ec
, lev
+ 1, 1, TRUE
);
612 if (!NIL_P(location
)) {
613 location
= rb_ary_entry(location
, 0);
616 if (argc
> 1 || !NIL_P(uplevel
) || !end_with_asciichar(str
, '\n')) {
618 if (NIL_P(uplevel
)) {
619 str
= rb_str_tmp_new(0);
621 else if (NIL_P(location
) ||
622 NIL_P(path
= rb_funcall(location
, rb_intern("path"), 0))) {
623 str
= rb_str_new_cstr("warning: ");
626 str
= rb_sprintf("%s:%ld: warning: ",
627 rb_string_value_ptr(&path
),
628 NUM2LONG(rb_funcall(location
, rb_intern("lineno"), 0)));
630 RBASIC_SET_CLASS(str
, rb_cWarningBuffer
);
631 rb_io_puts(argc
, argv
, str
);
632 RBASIC_SET_CLASS(str
, rb_cString
);
635 if (!NIL_P(category
)) {
636 category
= rb_to_symbol_type(category
);
637 rb_warning_category_from_name(category
);
640 if (exc
== rb_mWarning
) {
641 rb_must_asciicompat(str
);
642 rb_write_error_str(str
);
645 rb_warn_category(str
, category
);
651 #define MAX_BUG_REPORTERS 0x100
653 static struct bug_reporters
{
654 void (*func
)(FILE *out
, void *data
);
656 } bug_reporters
[MAX_BUG_REPORTERS
];
658 static int bug_reporters_size
;
661 rb_bug_reporter_add(void (*func
)(FILE *, void *), void *data
)
663 struct bug_reporters
*reporter
;
664 if (bug_reporters_size
>= MAX_BUG_REPORTERS
) {
665 return 0; /* failed to register */
667 reporter
= &bug_reporters
[bug_reporters_size
++];
668 reporter
->func
= func
;
669 reporter
->data
= data
;
674 /* returns true if x can not be used as file name */
678 #if defined __CYGWIN__ || defined DOSISH
679 # define PATH_SEP_ENCODING 1
680 // Assume that "/" is only the first byte in any encoding.
681 if (x
== ':') return true; // drive letter or ADS
682 if (x
== '\\') return true;
692 static const char PATHSEP_REPLACE
= '!';
695 append_pathname(char *p
, const char *pe
, VALUE str
)
697 #ifdef PATH_SEP_ENCODING
698 rb_encoding
*enc
= rb_enc_get(str
);
700 const char *s
= RSTRING_PTR(str
);
701 const char *const se
= s
+ RSTRING_LEN(str
);
704 --pe
; // for terminator
706 while (p
< pe
&& s
< se
&& (c
= *s
) != '\0') {
708 if (s
== se
|| !*s
) break; // chomp "." basename
709 if (path_sep_p(s
[1])) goto skipsep
; // skip "./"
711 else if (path_sep_p(c
)) {
712 // squeeze successive separators
713 *p
++ = PATHSEP_REPLACE
;
715 while (++s
< se
&& path_sep_p(*s
));
718 const char *const ss
= s
;
719 while (p
< pe
&& s
< se
&& *s
&& !path_sep_p(*s
)) {
720 #ifdef PATH_SEP_ENCODING
721 int n
= rb_enc_mbclen(s
, se
, enc
);
728 if (s
> ss
) memcpy(p
- (s
- ss
), ss
, s
- ss
);
735 append_basename(char *p
, const char *pe
, struct path_string
*path
, VALUE str
)
738 #ifdef PATH_SEP_ENCODING
739 rb_encoding
*enc
= rb_enc_get(str
);
741 const char *const b
= RSTRING_PTR(str
), *const e
= RSTRING_END(str
), *p
= e
;
744 if (path_sep_p(p
[-1])) {
745 #ifdef PATH_SEP_ENCODING
746 const char *t
= rb_enc_prev_char(b
, p
, e
, enc
);
761 size_t n
= path
->len
;
762 if (p
+ n
> pe
) n
= pe
- p
;
763 memcpy(p
, path
->ptr
, n
);
768 finish_report(FILE *out
, rb_pid_t pid
)
770 if (out
!= stdout
&& out
!= stderr
) fclose(out
);
771 #ifdef HAVE_WORKING_FORK
772 if (pid
> 0) waitpid(pid
, NULL
, 0);
776 struct report_expansion
{
777 struct path_string exe
, script
;
783 * Open a bug report file to write. The `RUBY_CRASH_REPORT`
784 * environment variable can be set to define a template that is used
785 * to name bug report files. The template can contain % specifiers
786 * which are substituted by the following values when a bug report
789 * %% A single % character.
790 * %e The base name of the executable filename.
791 * %E Pathname of executable, with slashes ('/') replaced by
792 * exclamation marks ('!').
793 * %f Similar to %e with the main script filename.
794 * %F Similar to %E with the main script filename.
795 * %p PID of dumped process in decimal.
796 * %t Time of dump, expressed as seconds since the Epoch,
797 * 1970-01-01 00:00:00 +0000 (UTC).
798 * %NNN Octal char code, upto 3 digits.
801 expand_report_argument(const char **input_template
, struct report_expansion
*values
,
802 char *buf
, size_t size
, bool word
)
805 char *end
= buf
+ size
;
806 const char *template = *input_template
;
809 if (p
>= end
-1 || !*template) return NULL
;
811 char c
= *template++;
812 if (word
&& ISSPACE(c
)) break;
813 if (!store
) continue;
816 switch (c
= *template++) {
818 p
= append_basename(p
, end
, &values
->exe
, rb_argv0
);
821 p
= append_pathname(p
, end
, rb_argv0
);
824 p
= append_basename(p
, end
, &values
->script
, GET_VM()->orig_progname
);
827 p
= append_pathname(p
, end
, GET_VM()->orig_progname
);
830 if (!values
->pid
) values
->pid
= getpid();
831 snprintf(p
, end
-p
, "%" PRI_PIDT_PREFIX
"d", values
->pid
);
835 if (!values
->time
) values
->time
= time(NULL
);
836 snprintf(p
, end
-p
, "%" PRI_TIMET_PREFIX
"d", values
->time
);
840 if (c
>= '0' && c
<= '7') {
841 c
= (unsigned char)ruby_scan_oct(template-1, 3, &n
);
843 if (!c
) store
= false;
848 if (p
< end
-1) *p
++ = c
;
850 *input_template
= template;
855 FILE *ruby_popen_writer(char *const *argv
, rb_pid_t
*pid
);
858 open_report_path(const char *template, char *buf
, size_t size
, rb_pid_t
*pid
)
860 struct report_expansion values
= {{0}};
862 if (!template) return NULL
;
863 if (0) fprintf(stderr
, "RUBY_CRASH_REPORT=%s\n", buf
);
864 if (*template == '|') {
865 char *argv
[16], *bufend
= buf
+ size
, *p
;
868 for (argc
= 0; argc
< numberof(argv
) - 1; ++argc
) {
869 while (*template && ISSPACE(*template)) template++;
870 p
= expand_report_argument(&template, &values
, buf
, bufend
-buf
, true);
876 if (!p
) return ruby_popen_writer(argv
, pid
);
878 else if (*template) {
879 expand_report_argument(&template, &values
, buf
, size
, false);
880 return fopen(buf
, "w");
885 static const char *crash_report
;
887 /* SIGSEGV handler might have a very small stack. Thus we need to use it carefully. */
888 #define REPORT_BUG_BUFSIZ 256
890 bug_report_file(const char *file
, int line
, rb_pid_t
*pid
)
892 char buf
[REPORT_BUG_BUFSIZ
];
893 const char *report
= crash_report
;
894 if (!report
) report
= getenv("RUBY_CRASH_REPORT");
895 FILE *out
= open_report_path(report
, buf
, sizeof(buf
), pid
);
896 int len
= err_position_0(buf
, sizeof(buf
), file
, line
);
899 if ((ssize_t
)fwrite(buf
, 1, len
, out
) == (ssize_t
)len
) return out
;
902 if ((ssize_t
)fwrite(buf
, 1, len
, stderr
) == (ssize_t
)len
) {
905 if ((ssize_t
)fwrite(buf
, 1, len
, stdout
) == (ssize_t
)len
) {
912 FUNC_MINIMIZED(static void bug_important_message(FILE *out
, const char *const msg
, size_t len
));
915 bug_important_message(FILE *out
, const char *const msg
, size_t len
)
917 const char *const endmsg
= msg
+ len
;
921 if (isatty(fileno(out
))) {
922 static const char red
[] = "\033[;31;1;7m";
923 static const char green
[] = "\033[;32;7m";
924 static const char reset
[] = "\033[m";
925 const char *e
= strchr(p
, '\n');
926 const int w
= (int)(e
- p
);
928 int i
= (int)(e
- p
);
929 fputs(*p
== ' ' ? green
: red
, out
);
930 fwrite(p
, 1, e
- p
, out
);
931 for (; i
< w
; ++i
) fputc(' ', out
);
934 } while ((p
= e
+ 1) < endmsg
&& (e
= strchr(p
, '\n')) != 0 && e
> p
+ 1);
936 fwrite(p
, 1, endmsg
- p
, out
);
939 #undef CRASH_REPORTER_MAY_BE_CREATED
940 #if defined(__APPLE__) && \
941 (!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 */
942 # define CRASH_REPORTER_MAY_BE_CREATED
945 preface_dump(FILE *out
)
947 #if defined __APPLE__
948 static const char msg
[] = ""
949 "-- Crash Report log information "
950 "--------------------------------------------\n"
951 " See Crash Report log file in one of the following locations:\n"
952 # ifdef CRASH_REPORTER_MAY_BE_CREATED
953 " * ~/Library/Logs/CrashReporter\n"
954 " * /Library/Logs/CrashReporter\n"
956 " * ~/Library/Logs/DiagnosticReports\n"
957 " * /Library/Logs/DiagnosticReports\n"
958 " for more details.\n"
959 "Don't forget to include the above Crash Report log file in bug reports.\n"
961 const size_t msglen
= sizeof(msg
) - 1;
963 const char *msg
= NULL
;
964 const size_t msglen
= 0;
966 bug_important_message(out
, msg
, msglen
);
970 postscript_dump(FILE *out
)
972 #if defined __APPLE__
973 static const char msg
[] = ""
975 /*" ------------------------------------------------"*/
976 "\n""Don't forget to include the Crash Report log file under\n"
977 # ifdef CRASH_REPORTER_MAY_BE_CREATED
980 "DiagnosticReports directory in bug reports.\n"
981 /*"------------------------------------------------------------\n"*/
983 const size_t msglen
= sizeof(msg
) - 1;
985 const char *msg
= NULL
;
986 const size_t msglen
= 0;
988 bug_important_message(out
, msg
, msglen
);
991 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 2, 0)
993 bug_report_begin_valist(FILE *out
, const char *fmt
, va_list args
)
995 char buf
[REPORT_BUG_BUFSIZ
];
997 fputs("[BUG] ", out
);
998 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
1000 snprintf(buf
, sizeof(buf
), "\n%s\n\n", rb_dynamic_description
);
1005 #define bug_report_begin(out, fmt) do { \
1007 va_start(args, fmt); \
1008 bug_report_begin_valist(out, fmt, args); \
1013 bug_report_end(FILE *out
, rb_pid_t pid
)
1015 /* call additional bug reporters */
1018 for (i
=0; i
<bug_reporters_size
; i
++) {
1019 struct bug_reporters
*reporter
= &bug_reporters
[i
];
1020 (*reporter
->func
)(out
, reporter
->data
);
1023 postscript_dump(out
);
1024 finish_report(out
, pid
);
1027 #define report_bug(file, line, fmt, ctx) do { \
1028 rb_pid_t pid = -1; \
1029 FILE *out = bug_report_file(file, line, &pid); \
1031 bug_report_begin(out, fmt); \
1032 rb_vm_bugreport(ctx, out); \
1033 bug_report_end(out, pid); \
1037 #define report_bug_valist(file, line, fmt, ctx, args) do { \
1038 rb_pid_t pid = -1; \
1039 FILE *out = bug_report_file(file, line, &pid); \
1041 bug_report_begin_valist(out, fmt, args); \
1042 rb_vm_bugreport(ctx, out); \
1043 bug_report_end(out, pid); \
1048 ruby_set_crash_report(const char *template)
1050 crash_report
= template;
1053 char buf
[REPORT_BUG_BUFSIZ
];
1054 FILE *out
= open_report_path(template, buf
, sizeof(buf
), &pid
);
1056 time_t t
= time(NULL
);
1057 fprintf(out
, "ruby_test_bug_report: %s", ctime(&t
));
1058 finish_report(out
, pid
);
1063 NORETURN(static void die(void));
1067 #if defined(_WIN32) && defined(RUBY_MSVCRT_VERSION) && RUBY_MSVCRT_VERSION >= 80
1068 _set_abort_behavior( 0, _CALL_REPORTFAULT
);
1074 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT
, 1, 0)
1076 rb_bug_without_die(const char *fmt
, va_list args
)
1078 const char *file
= NULL
;
1082 file
= rb_source_location_cstr(&line
);
1085 report_bug_valist(file
, line
, fmt
, NULL
, args
);
1089 rb_bug(const char *fmt
, ...)
1092 va_start(args
, fmt
);
1093 rb_bug_without_die(fmt
, args
);
1099 rb_bug_for_fatal_signal(ruby_sighandler_t default_sighandler
, int sig
, const void *ctx
, const char *fmt
, ...)
1101 const char *file
= NULL
;
1105 file
= rb_source_location_cstr(&line
);
1108 report_bug(file
, line
, fmt
, ctx
);
1110 if (default_sighandler
) default_sighandler(sig
);
1112 ruby_default_signal(sig
);
1118 rb_bug_errno(const char *mesg
, int errno_arg
)
1121 rb_bug("%s: errno == 0 (NOERROR)", mesg
);
1123 const char *errno_str
= rb_strerrno(errno_arg
);
1125 rb_bug("%s: %s (%s)", mesg
, strerror(errno_arg
), errno_str
);
1127 rb_bug("%s: %s (%d)", mesg
, strerror(errno_arg
), errno_arg
);
1132 * this is safe to call inside signal handler and timer thread
1133 * (which isn't a Ruby Thread object)
1135 #define write_or_abort(fd, str, len) (write((fd), (str), (len)) < 0 ? abort() : (void)0)
1136 #define WRITE_CONST(fd,str) write_or_abort((fd),(str),sizeof(str) - 1)
1139 rb_async_bug_errno(const char *mesg
, int errno_arg
)
1141 WRITE_CONST(2, "[ASYNC BUG] ");
1142 write_or_abort(2, mesg
, strlen(mesg
));
1143 WRITE_CONST(2, "\n");
1145 if (errno_arg
== 0) {
1146 WRITE_CONST(2, "errno == 0 (NOERROR)\n");
1149 const char *errno_str
= rb_strerrno(errno_arg
);
1152 errno_str
= "undefined errno";
1153 write_or_abort(2, errno_str
, strlen(errno_str
));
1155 WRITE_CONST(2, "\n\n");
1156 write_or_abort(2, rb_dynamic_description
, strlen(rb_dynamic_description
));
1161 rb_report_bug_valist(VALUE file
, int line
, const char *fmt
, va_list args
)
1163 report_bug_valist(RSTRING_PTR(file
), line
, fmt
, NULL
, args
);
1167 rb_assert_failure(const char *file
, int line
, const char *name
, const char *expr
)
1169 rb_assert_failure_detail(file
, line
, name
, expr
, NULL
);
1173 rb_assert_failure_detail(const char *file
, int line
, const char *name
, const char *expr
,
1174 const char *fmt
, ...)
1177 fprintf(out
, "Assertion Failed: %s:%d:", file
, line
);
1178 if (name
) fprintf(out
, "%s:", name
);
1179 fprintf(out
, "%s\n%s\n\n", expr
, rb_dynamic_description
);
1183 va_start(args
, fmt
);
1184 vfprintf(out
, fmt
, args
);
1189 rb_vm_bugreport(NULL
, out
);
1190 bug_report_end(out
, -1);
1194 static const char builtin_types
[][10] = {
1207 "Data", /* internal use: wrapped C pointers */
1208 "MatchData", /* data of $~ */
1215 "Symbol", /* :symbol */
1217 "undef", /* internal use: #undef; should not happen */
1221 "<Memo>", /* internal use: general memo */
1222 "<Node>", /* internal use: syntax tree node */
1223 "<iClass>", /* internal use: mixed-in module holder */
1227 rb_builtin_type_name(int t
)
1230 if ((unsigned int)t
>= numberof(builtin_types
)) return 0;
1231 name
= builtin_types
[t
];
1232 if (*name
) return name
;
1237 displaying_class_of(VALUE x
)
1240 case Qfalse
: return rb_fstring_cstr("false");
1241 case Qnil
: return rb_fstring_cstr("nil");
1242 case Qtrue
: return rb_fstring_cstr("true");
1243 default: return rb_obj_class(x
);
1248 builtin_class_name(VALUE x
)
1255 else if (FIXNUM_P(x
)) {
1258 else if (SYMBOL_P(x
)) {
1261 else if (RB_TYPE_P(x
, T_TRUE
)) {
1264 else if (RB_TYPE_P(x
, T_FALSE
)) {
1274 rb_builtin_class_name(VALUE x
)
1276 const char *etype
= builtin_class_name(x
);
1279 etype
= rb_obj_classname(x
);
1284 COLDFUNC
NORETURN(static void unexpected_type(VALUE
, int, int));
1285 #define UNDEF_LEAKED "undef leaked to the Ruby space"
1288 unexpected_type(VALUE x
, int xt
, int t
)
1290 const char *tname
= rb_builtin_type_name(t
);
1291 VALUE mesg
, exc
= rb_eFatal
;
1294 mesg
= rb_sprintf("wrong argument type %"PRIsVALUE
" (expected %s)",
1295 displaying_class_of(x
), tname
);
1296 exc
= rb_eTypeError
;
1298 else if (xt
> T_MASK
&& xt
<= 0x3f) {
1299 mesg
= rb_sprintf("unknown type 0x%x (0x%x given, probably comes"
1300 " from extension library for ruby 1.8)", t
, xt
);
1303 mesg
= rb_sprintf("unknown type 0x%x (0x%x given)", t
, xt
);
1305 rb_exc_raise(rb_exc_new_str(exc
, mesg
));
1309 rb_check_type(VALUE x
, int t
)
1313 if (RB_UNLIKELY(UNDEF_P(x
))) {
1314 rb_bug(UNDEF_LEAKED
);
1318 if (xt
!= t
|| (xt
== T_DATA
&& rbimpl_rtypeddata_p(x
))) {
1320 * Typed data is not simple `T_DATA`, but in a sense an
1321 * extension of `struct RVALUE`, which are incompatible with
1322 * each other except when inherited.
1324 * So it is not enough to just check `T_DATA`, it must be
1325 * identified by its `type` using `Check_TypedStruct` instead.
1327 unexpected_type(x
, xt
, t
);
1332 rb_unexpected_type(VALUE x
, int t
)
1334 if (RB_UNLIKELY(UNDEF_P(x
))) {
1335 rb_bug(UNDEF_LEAKED
);
1338 unexpected_type(x
, TYPE(x
), t
);
1342 rb_typeddata_inherited_p(const rb_data_type_t
*child
, const rb_data_type_t
*parent
)
1345 if (child
== parent
) return 1;
1346 child
= child
->parent
;
1352 rb_typeddata_is_kind_of(VALUE obj
, const rb_data_type_t
*data_type
)
1354 if (!RB_TYPE_P(obj
, T_DATA
) ||
1355 !RTYPEDDATA_P(obj
) || !rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj
), data_type
)) {
1361 #undef rb_typeddata_is_instance_of
1363 rb_typeddata_is_instance_of(VALUE obj
, const rb_data_type_t
*data_type
)
1365 return rb_typeddata_is_instance_of_inline(obj
, data_type
);
1369 rb_check_typeddata(VALUE obj
, const rb_data_type_t
*data_type
)
1373 if (!RB_TYPE_P(obj
, T_DATA
)) {
1374 actual
= displaying_class_of(obj
);
1376 else if (!RTYPEDDATA_P(obj
)) {
1377 actual
= displaying_class_of(obj
);
1379 else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj
), data_type
)) {
1380 const char *name
= RTYPEDDATA_TYPE(obj
)->wrap_struct_name
;
1381 actual
= rb_str_new_cstr(name
); /* or rb_fstring_cstr? not sure... */
1384 return RTYPEDDATA_GET_DATA(obj
);
1387 const char *expected
= data_type
->wrap_struct_name
;
1388 rb_raise(rb_eTypeError
, "wrong argument type %"PRIsVALUE
" (expected %s)",
1390 UNREACHABLE_RETURN(NULL
);
1393 /* exception classes */
1394 VALUE rb_eException
;
1395 VALUE rb_eSystemExit
;
1396 VALUE rb_eInterrupt
;
1399 VALUE rb_eStandardError
;
1400 VALUE rb_eRuntimeError
;
1401 VALUE rb_eFrozenError
;
1402 VALUE rb_eTypeError
;
1404 VALUE rb_eIndexError
;
1406 VALUE rb_eRangeError
;
1407 VALUE rb_eNameError
;
1408 VALUE rb_eEncodingError
;
1409 VALUE rb_eEncCompatError
;
1410 VALUE rb_eNoMethodError
;
1411 VALUE rb_eSecurityError
;
1412 VALUE rb_eNotImpError
;
1413 VALUE rb_eNoMemError
;
1414 VALUE rb_cNameErrorMesg
;
1415 VALUE rb_eNoMatchingPatternError
;
1416 VALUE rb_eNoMatchingPatternKeyError
;
1418 VALUE rb_eScriptError
;
1419 VALUE rb_eSyntaxError
;
1420 VALUE rb_eLoadError
;
1422 VALUE rb_eSystemCallError
;
1424 static VALUE rb_eNOERROR
;
1426 ID ruby_static_id_cause
;
1427 #define id_cause ruby_static_id_cause
1428 static ID id_message
, id_detailed_message
, id_backtrace
;
1429 static ID id_key
, id_matchee
, id_args
, id_Errno
, id_errno
, id_i_path
;
1430 static ID id_receiver
, id_recv
, id_iseq
, id_local_variables
;
1431 static ID id_private_call_p
, id_top
, id_bottom
;
1433 #define id_bt_locations idBt_locations
1434 #define id_mesg idMesg
1435 #define id_name idName
1437 #undef rb_exc_new_cstr
1440 rb_exc_new(VALUE etype
, const char *ptr
, long len
)
1442 VALUE mesg
= rb_str_new(ptr
, len
);
1443 return rb_class_new_instance(1, &mesg
, etype
);
1447 rb_exc_new_cstr(VALUE etype
, const char *s
)
1449 return rb_exc_new(etype
, s
, strlen(s
));
1453 rb_exc_new_str(VALUE etype
, VALUE str
)
1455 rb_yjit_lazy_push_frame(GET_EC()->cfp
->pc
);
1457 return rb_class_new_instance(1, &str
, etype
);
1461 exc_init(VALUE exc
, VALUE mesg
)
1463 rb_ivar_set(exc
, id_mesg
, mesg
);
1464 rb_ivar_set(exc
, id_bt
, Qnil
);
1471 * Exception.new(msg = nil) -> exception
1472 * Exception.exception(msg = nil) -> exception
1474 * Construct a new Exception object, optionally passing in
1479 exc_initialize(int argc
, VALUE
*argv
, VALUE exc
)
1483 arg
= (!rb_check_arity(argc
, 0, 1) ? Qnil
: argv
[0]);
1484 return exc_init(exc
, arg
);
1488 * Document-method: exception
1491 * exc.exception([string]) -> an_exception or exc
1493 * With no argument, or if the argument is the same as the receiver,
1494 * return the receiver. Otherwise, create a new
1495 * exception object of the same class as the receiver, but with a
1496 * message equal to <code>string.to_str</code>.
1501 exc_exception(int argc
, VALUE
*argv
, VALUE self
)
1505 argc
= rb_check_arity(argc
, 0, 1);
1506 if (argc
== 0) return self
;
1507 if (argc
== 1 && self
== argv
[0]) return self
;
1508 exc
= rb_obj_clone(self
);
1509 rb_ivar_set(exc
, id_mesg
, argv
[0]);
1515 * exception.to_s -> string
1517 * Returns exception's message (or the name of the exception if
1518 * no message is set).
1524 VALUE mesg
= rb_attr_get(exc
, idMesg
);
1526 if (NIL_P(mesg
)) return rb_class_name(CLASS_OF(exc
));
1527 return rb_String(mesg
);
1530 /* FIXME: Include eval_error.c */
1531 void rb_error_write(VALUE errinfo
, VALUE emesg
, VALUE errat
, VALUE str
, VALUE opt
, VALUE highlight
, VALUE reverse
);
1534 rb_get_message(VALUE exc
)
1536 VALUE e
= rb_check_funcall(exc
, id_message
, 0, 0);
1537 if (UNDEF_P(e
)) return Qnil
;
1538 if (!RB_TYPE_P(e
, T_STRING
)) e
= rb_check_string_type(e
);
1543 rb_get_detailed_message(VALUE exc
, VALUE opt
)
1547 e
= rb_check_funcall(exc
, id_detailed_message
, 0, 0);
1550 e
= rb_check_funcall_kw(exc
, id_detailed_message
, 1, &opt
, 1);
1552 if (UNDEF_P(e
)) return Qnil
;
1553 if (!RB_TYPE_P(e
, T_STRING
)) e
= rb_check_string_type(e
);
1559 * Exception.to_tty? -> true or false
1561 * Returns +true+ if exception messages will be sent to a tty.
1564 exc_s_to_tty_p(VALUE self
)
1566 return RBOOL(rb_stderr_tty_p());
1570 check_highlight_keyword(VALUE opt
, int auto_tty_detect
)
1572 VALUE highlight
= Qnil
;
1575 highlight
= rb_hash_lookup(opt
, sym_highlight
);
1577 switch (highlight
) {
1579 rb_bool_expected(highlight
, "highlight", TRUE
);
1581 case Qtrue
: case Qfalse
: case Qnil
: break;
1585 if (NIL_P(highlight
)) {
1586 highlight
= RBOOL(auto_tty_detect
&& rb_stderr_tty_p());
1593 check_order_keyword(VALUE opt
)
1598 static VALUE kw_order
;
1599 if (!kw_order
) kw_order
= ID2SYM(rb_intern_const("order"));
1601 order
= rb_hash_lookup(opt
, kw_order
);
1603 if (order
!= Qnil
) {
1604 ID id
= rb_check_id(&order
);
1605 if (id
== id_bottom
) order
= Qtrue
;
1606 else if (id
== id_top
) order
= Qfalse
;
1608 rb_raise(rb_eArgError
, "expected :top or :bottom as "
1609 "order: %+"PRIsVALUE
, order
);
1614 if (NIL_P(order
)) order
= Qfalse
;
1621 * exception.full_message(highlight: bool, order: [:top or :bottom]) -> string
1623 * Returns formatted string of _exception_.
1624 * The returned string is formatted using the same format that Ruby uses
1625 * when printing an uncaught exceptions to stderr.
1627 * If _highlight_ is +true+ the default error handler will send the
1628 * messages to a tty.
1630 * _order_ must be either of +:top+ or +:bottom+, and places the error
1631 * message and the innermost backtrace come at the top or the bottom.
1633 * The default values of these options depend on <code>$stderr</code>
1634 * and its +tty?+ at the timing of a call.
1638 exc_full_message(int argc
, VALUE
*argv
, VALUE exc
)
1640 VALUE opt
, str
, emesg
, errat
;
1641 VALUE highlight
, order
;
1643 rb_scan_args(argc
, argv
, "0:", &opt
);
1645 highlight
= check_highlight_keyword(opt
, 1);
1646 order
= check_order_keyword(opt
);
1649 if (NIL_P(opt
)) opt
= rb_hash_new();
1650 rb_hash_aset(opt
, sym_highlight
, highlight
);
1653 str
= rb_str_new2("");
1654 errat
= rb_get_backtrace(exc
);
1655 emesg
= rb_get_detailed_message(exc
, opt
);
1657 rb_error_write(exc
, emesg
, errat
, str
, opt
, highlight
, order
);
1663 * exception.message -> string
1665 * Returns the result of invoking <code>exception.to_s</code>.
1666 * Normally this returns the exception's message or name.
1670 exc_message(VALUE exc
)
1672 return rb_funcallv(exc
, idTo_s
, 0, 0);
1677 * exception.detailed_message(highlight: bool, **opt) -> string
1679 * Processes a string returned by #message.
1681 * It may add the class name of the exception to the end of the first line.
1682 * Also, when +highlight+ keyword is true, it adds ANSI escape sequences to
1683 * make the message bold.
1685 * If you override this method, it must be tolerant for unknown keyword
1686 * arguments. All keyword arguments passed to #full_message are delegated
1689 * This method is overridden by did_you_mean and error_highlight to add
1690 * their information.
1692 * A user-defined exception class can also define their own
1693 * +detailed_message+ method to add supplemental information.
1694 * When +highlight+ is true, it can return a string containing escape
1695 * sequences, but use widely-supported ones. It is recommended to limit
1696 * the following codes:
1700 * - Underline (+\e[4m+)
1701 * - Foreground color except white and black
1703 * - Green (+\e[32m+)
1704 * - Yellow (+\e[33m+)
1706 * - Magenta (+\e[35m+)
1709 * Use escape sequences carefully even if +highlight+ is true.
1710 * Do not use escape sequences to express essential information;
1711 * the message should be readable even if all escape sequences are
1716 exc_detailed_message(int argc
, VALUE
*argv
, VALUE exc
)
1720 rb_scan_args(argc
, argv
, "0:", &opt
);
1722 VALUE highlight
= check_highlight_keyword(opt
, 0);
1724 extern VALUE
rb_decorate_message(const VALUE eclass
, VALUE emesg
, int highlight
);
1726 return rb_decorate_message(CLASS_OF(exc
), rb_get_message(exc
), RTEST(highlight
));
1731 * exception.inspect -> string
1733 * Return this exception's class name and message.
1737 exc_inspect(VALUE exc
)
1741 klass
= CLASS_OF(exc
);
1742 exc
= rb_obj_as_string(exc
);
1743 if (RSTRING_LEN(exc
) == 0) {
1744 return rb_class_name(klass
);
1747 str
= rb_str_buf_new2("#<");
1748 klass
= rb_class_name(klass
);
1749 rb_str_buf_append(str
, klass
);
1751 if (RTEST(rb_str_include(exc
, rb_str_new2("\n")))) {
1752 rb_str_catf(str
, ":%+"PRIsVALUE
, exc
);
1755 rb_str_buf_cat(str
, ": ", 2);
1756 rb_str_buf_append(str
, exc
);
1759 rb_str_buf_cat(str
, ">", 1);
1766 * exception.backtrace -> array or nil
1768 * Returns any backtrace associated with the exception. The backtrace
1769 * is an array of strings, each containing either ``filename:lineNo: in
1770 * `method''' or ``filename:lineNo.''
1783 * print detail.backtrace.join("\n")
1786 * <em>produces:</em>
1792 * In the case no backtrace has been set, +nil+ is returned
1794 * ex = StandardError.new
1800 exc_backtrace(VALUE exc
)
1804 obj
= rb_attr_get(exc
, id_bt
);
1806 if (rb_backtrace_p(obj
)) {
1807 obj
= rb_backtrace_to_str_ary(obj
);
1808 /* rb_ivar_set(exc, id_bt, obj); */
1814 static VALUE
rb_check_backtrace(VALUE
);
1817 rb_get_backtrace(VALUE exc
)
1819 ID mid
= id_backtrace
;
1821 if (rb_method_basic_definition_p(CLASS_OF(exc
), id_backtrace
)) {
1822 VALUE klass
= rb_eException
;
1823 rb_execution_context_t
*ec
= GET_EC();
1826 EXEC_EVENT_HOOK(ec
, RUBY_EVENT_C_CALL
, exc
, mid
, mid
, klass
, Qundef
);
1827 info
= exc_backtrace(exc
);
1828 EXEC_EVENT_HOOK(ec
, RUBY_EVENT_C_RETURN
, exc
, mid
, mid
, klass
, info
);
1831 info
= rb_funcallv(exc
, mid
, 0, 0);
1833 if (NIL_P(info
)) return Qnil
;
1834 return rb_check_backtrace(info
);
1839 * exception.backtrace_locations -> array or nil
1841 * Returns any backtrace associated with the exception. This method is
1842 * similar to Exception#backtrace, but the backtrace is an array of
1843 * Thread::Backtrace::Location.
1845 * This method is not affected by Exception#set_backtrace().
1848 exc_backtrace_locations(VALUE exc
)
1852 obj
= rb_attr_get(exc
, id_bt_locations
);
1854 obj
= rb_backtrace_to_location_ary(obj
);
1860 rb_check_backtrace(VALUE bt
)
1863 static const char err
[] = "backtrace must be an Array of String or an Array of Thread::Backtrace::Location";
1866 if (RB_TYPE_P(bt
, T_STRING
)) return rb_ary_new3(1, bt
);
1867 if (rb_backtrace_p(bt
)) return bt
;
1868 if (!RB_TYPE_P(bt
, T_ARRAY
)) {
1869 rb_raise(rb_eTypeError
, err
);
1871 for (i
=0;i
<RARRAY_LEN(bt
);i
++) {
1872 VALUE e
= RARRAY_AREF(bt
, i
);
1873 if (!RB_TYPE_P(e
, T_STRING
)) {
1874 rb_raise(rb_eTypeError
, err
);
1883 * exc.set_backtrace(backtrace) -> array
1885 * Sets the backtrace information associated with +exc+. The +backtrace+ must
1886 * be an array of Thread::Backtrace::Location objects or an array of String objects
1887 * or a single String in the format described in Exception#backtrace.
1892 exc_set_backtrace(VALUE exc
, VALUE bt
)
1894 VALUE btobj
= rb_location_ary_to_backtrace(bt
);
1896 rb_ivar_set(exc
, id_bt
, btobj
);
1897 rb_ivar_set(exc
, id_bt_locations
, btobj
);
1901 return rb_ivar_set(exc
, id_bt
, rb_check_backtrace(bt
));
1906 rb_exc_set_backtrace(VALUE exc
, VALUE bt
)
1908 return exc_set_backtrace(exc
, bt
);
1913 * exception.cause -> an_exception or nil
1915 * Returns the previous exception ($!) at the time this exception was raised.
1916 * This is useful for wrapping exceptions and retaining the original exception
1921 exc_cause(VALUE exc
)
1923 return rb_attr_get(exc
, id_cause
);
1927 try_convert_to_exception(VALUE obj
)
1929 return rb_check_funcall(obj
, idException
, 0, 0);
1934 * exc == obj -> true or false
1936 * Equality---If <i>obj</i> is not an Exception, returns
1937 * <code>false</code>. Otherwise, returns <code>true</code> if <i>exc</i> and
1938 * <i>obj</i> share same class, messages, and backtrace.
1942 exc_equal(VALUE exc
, VALUE obj
)
1944 VALUE mesg
, backtrace
;
1946 if (exc
== obj
) return Qtrue
;
1948 if (rb_obj_class(exc
) != rb_obj_class(obj
)) {
1951 obj
= rb_protect(try_convert_to_exception
, obj
, &state
);
1952 if (state
|| UNDEF_P(obj
)) {
1953 rb_set_errinfo(Qnil
);
1956 if (rb_obj_class(exc
) != rb_obj_class(obj
)) return Qfalse
;
1957 mesg
= rb_check_funcall(obj
, id_message
, 0, 0);
1958 if (UNDEF_P(mesg
)) return Qfalse
;
1959 backtrace
= rb_check_funcall(obj
, id_backtrace
, 0, 0);
1960 if (UNDEF_P(backtrace
)) return Qfalse
;
1963 mesg
= rb_attr_get(obj
, id_mesg
);
1964 backtrace
= exc_backtrace(obj
);
1967 if (!rb_equal(rb_attr_get(exc
, id_mesg
), mesg
))
1969 return rb_equal(exc_backtrace(exc
), backtrace
);
1974 * SystemExit.new -> system_exit
1975 * SystemExit.new(status) -> system_exit
1976 * SystemExit.new(status, msg) -> system_exit
1977 * SystemExit.new(msg) -> system_exit
1979 * Create a new +SystemExit+ exception with the given status and message.
1980 * Status is true, false, or an integer.
1981 * If status is not given, true is used.
1985 exit_initialize(int argc
, VALUE
*argv
, VALUE exc
)
1993 status
= INT2FIX(EXIT_SUCCESS
);
1998 status
= INT2FIX(EXIT_FAILURE
);
2003 status
= rb_check_to_int(status
);
2004 if (NIL_P(status
)) {
2005 status
= INT2FIX(EXIT_SUCCESS
);
2008 #if EXIT_SUCCESS != 0
2009 if (status
== INT2FIX(0))
2010 status
= INT2FIX(EXIT_SUCCESS
);
2019 status
= INT2FIX(EXIT_SUCCESS
);
2021 rb_call_super(argc
, argv
);
2022 rb_ivar_set(exc
, id_status
, status
);
2029 * system_exit.status -> integer
2031 * Return the status value associated with this system exit.
2035 exit_status(VALUE exc
)
2037 return rb_attr_get(exc
, id_status
);
2043 * system_exit.success? -> true or false
2045 * Returns +true+ if exiting successful, +false+ if not.
2049 exit_success_p(VALUE exc
)
2051 VALUE status_val
= rb_attr_get(exc
, id_status
);
2054 if (NIL_P(status_val
))
2056 status
= NUM2INT(status_val
);
2057 return RBOOL(WIFEXITED(status
) && WEXITSTATUS(status
) == EXIT_SUCCESS
);
2061 err_init_recv(VALUE exc
, VALUE recv
)
2063 if (!UNDEF_P(recv
)) rb_ivar_set(exc
, id_recv
, recv
);
2069 * FrozenError.new(msg=nil, receiver: nil) -> frozen_error
2071 * Construct a new FrozenError exception. If given the <i>receiver</i>
2072 * parameter may subsequently be examined using the FrozenError#receiver
2076 * raise FrozenError.new("can't modify frozen array", receiver: a)
2080 frozen_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2083 VALUE values
[numberof(keywords
)], options
;
2085 argc
= rb_scan_args(argc
, argv
, "*:", NULL
, &options
);
2086 keywords
[0] = id_receiver
;
2087 rb_get_kwargs(options
, keywords
, 0, numberof(values
), values
);
2088 rb_call_super(argc
, argv
);
2089 err_init_recv(self
, values
[0]);
2094 * Document-method: FrozenError#receiver
2096 * frozen_error.receiver -> object
2098 * Return the receiver associated with this FrozenError exception.
2101 #define frozen_err_receiver name_err_receiver
2104 rb_name_error(ID id
, const char *fmt
, ...)
2109 va_start(args
, fmt
);
2110 argv
[0] = rb_vsprintf(fmt
, args
);
2113 argv
[1] = ID2SYM(id
);
2114 exc
= rb_class_new_instance(2, argv
, rb_eNameError
);
2119 rb_name_error_str(VALUE str
, const char *fmt
, ...)
2124 va_start(args
, fmt
);
2125 argv
[0] = rb_vsprintf(fmt
, args
);
2129 exc
= rb_class_new_instance(2, argv
, rb_eNameError
);
2134 name_err_init_attr(VALUE exc
, VALUE recv
, VALUE method
)
2136 const rb_execution_context_t
*ec
= GET_EC();
2137 rb_control_frame_t
*cfp
= RUBY_VM_PREVIOUS_CONTROL_FRAME(ec
->cfp
);
2138 cfp
= rb_vm_get_ruby_level_next_cfp(ec
, cfp
);
2139 rb_ivar_set(exc
, id_name
, method
);
2140 err_init_recv(exc
, recv
);
2141 if (cfp
&& VM_FRAME_TYPE(cfp
) != VM_FRAME_MAGIC_DUMMY
) {
2142 rb_ivar_set(exc
, id_iseq
, rb_iseqw_new(cfp
->iseq
));
2149 * NameError.new(msg=nil, name=nil, receiver: nil) -> name_error
2151 * Construct a new NameError exception. If given the <i>name</i>
2152 * parameter may subsequently be examined using the NameError#name
2153 * method. <i>receiver</i> parameter allows to pass object in
2154 * context of which the error happened. Example:
2156 * [1, 2, 3].method(:rject) # NameError with name "rject" and receiver: Array
2157 * [1, 2, 3].singleton_method(:rject) # NameError with name "rject" and receiver: [1, 2, 3]
2161 name_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2164 VALUE values
[numberof(keywords
)], name
, options
;
2166 argc
= rb_scan_args(argc
, argv
, "*:", NULL
, &options
);
2167 keywords
[0] = id_receiver
;
2168 rb_get_kwargs(options
, keywords
, 0, numberof(values
), values
);
2169 name
= (argc
> 1) ? argv
[--argc
] : Qnil
;
2170 rb_call_super(argc
, argv
);
2171 name_err_init_attr(self
, values
[0], name
);
2175 static VALUE
rb_name_err_mesg_new(VALUE mesg
, VALUE recv
, VALUE method
);
2178 name_err_init(VALUE exc
, VALUE mesg
, VALUE recv
, VALUE method
)
2180 exc_init(exc
, rb_name_err_mesg_new(mesg
, recv
, method
));
2181 return name_err_init_attr(exc
, recv
, method
);
2185 rb_name_err_new(VALUE mesg
, VALUE recv
, VALUE method
)
2187 VALUE exc
= rb_obj_alloc(rb_eNameError
);
2188 return name_err_init(exc
, mesg
, recv
, method
);
2193 * name_error.name -> string or nil
2195 * Return the name associated with this NameError exception.
2199 name_err_name(VALUE self
)
2201 return rb_attr_get(self
, id_name
);
2206 * name_error.local_variables -> array
2208 * Return a list of the local variable names defined where this
2209 * NameError exception was raised.
2211 * Internal use only.
2215 name_err_local_variables(VALUE self
)
2217 VALUE vars
= rb_attr_get(self
, id_local_variables
);
2220 VALUE iseqw
= rb_attr_get(self
, id_iseq
);
2221 if (!NIL_P(iseqw
)) vars
= rb_iseqw_local_variables(iseqw
);
2222 if (NIL_P(vars
)) vars
= rb_ary_new();
2223 rb_ivar_set(self
, id_local_variables
, vars
);
2229 nometh_err_init_attr(VALUE exc
, VALUE args
, int priv
)
2231 rb_ivar_set(exc
, id_args
, args
);
2232 rb_ivar_set(exc
, id_private_call_p
, RBOOL(priv
));
2238 * NoMethodError.new(msg=nil, name=nil, args=nil, private=false, receiver: nil) -> no_method_error
2240 * Construct a NoMethodError exception for a method of the given name
2241 * called with the given arguments. The name may be accessed using
2242 * the <code>#name</code> method on the resulting object, and the
2243 * arguments using the <code>#args</code> method.
2245 * If <i>private</i> argument were passed, it designates method was
2246 * attempted to call in private context, and can be accessed with
2247 * <code>#private_call?</code> method.
2249 * <i>receiver</i> argument stores an object whose method was called.
2253 nometh_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2256 VALUE args
, options
;
2257 argc
= rb_scan_args(argc
, argv
, "*:", NULL
, &options
);
2258 priv
= (argc
> 3) && (--argc
, RTEST(argv
[argc
]));
2259 args
= (argc
> 2) ? argv
[--argc
] : Qnil
;
2260 if (!NIL_P(options
)) argv
[argc
++] = options
;
2261 rb_call_super_kw(argc
, argv
, RB_PASS_CALLED_KEYWORDS
);
2262 return nometh_err_init_attr(self
, args
, priv
);
2266 rb_nomethod_err_new(VALUE mesg
, VALUE recv
, VALUE method
, VALUE args
, int priv
)
2268 VALUE exc
= rb_obj_alloc(rb_eNoMethodError
);
2269 name_err_init(exc
, mesg
, recv
, method
);
2270 return nometh_err_init_attr(exc
, args
, priv
);
2273 typedef struct name_error_message_struct
{
2277 } name_error_message_t
;
2280 name_err_mesg_mark(void *p
)
2282 name_error_message_t
*ptr
= (name_error_message_t
*)p
;
2283 rb_gc_mark_movable(ptr
->mesg
);
2284 rb_gc_mark_movable(ptr
->recv
);
2285 rb_gc_mark_movable(ptr
->name
);
2289 name_err_mesg_update(void *p
)
2291 name_error_message_t
*ptr
= (name_error_message_t
*)p
;
2292 ptr
->mesg
= rb_gc_location(ptr
->mesg
);
2293 ptr
->recv
= rb_gc_location(ptr
->recv
);
2294 ptr
->name
= rb_gc_location(ptr
->name
);
2297 static const rb_data_type_t name_err_mesg_data_type
= {
2301 RUBY_TYPED_DEFAULT_FREE
,
2302 NULL
, // No external memory to report,
2303 name_err_mesg_update
,
2305 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
| RUBY_TYPED_WB_PROTECTED
| RUBY_TYPED_EMBEDDABLE
2310 rb_name_err_mesg_init(VALUE klass
, VALUE mesg
, VALUE recv
, VALUE name
)
2312 name_error_message_t
*message
;
2313 VALUE result
= TypedData_Make_Struct(klass
, name_error_message_t
, &name_err_mesg_data_type
, message
);
2314 RB_OBJ_WRITE(result
, &message
->mesg
, mesg
);
2315 RB_OBJ_WRITE(result
, &message
->recv
, recv
);
2316 RB_OBJ_WRITE(result
, &message
->name
, name
);
2322 rb_name_err_mesg_new(VALUE mesg
, VALUE recv
, VALUE method
)
2324 return rb_name_err_mesg_init(rb_cNameErrorMesg
, mesg
, recv
, method
);
2329 name_err_mesg_alloc(VALUE klass
)
2331 return rb_name_err_mesg_init(klass
, Qnil
, Qnil
, Qnil
);
2336 name_err_mesg_init_copy(VALUE obj1
, VALUE obj2
)
2338 if (obj1
== obj2
) return obj1
;
2339 rb_obj_init_copy(obj1
, obj2
);
2341 name_error_message_t
*ptr1
, *ptr2
;
2342 TypedData_Get_Struct(obj1
, name_error_message_t
, &name_err_mesg_data_type
, ptr1
);
2343 TypedData_Get_Struct(obj2
, name_error_message_t
, &name_err_mesg_data_type
, ptr2
);
2345 RB_OBJ_WRITE(obj1
, &ptr1
->mesg
, ptr2
->mesg
);
2346 RB_OBJ_WRITE(obj1
, &ptr1
->recv
, ptr2
->recv
);
2347 RB_OBJ_WRITE(obj1
, &ptr1
->name
, ptr2
->name
);
2353 name_err_mesg_equal(VALUE obj1
, VALUE obj2
)
2355 if (obj1
== obj2
) return Qtrue
;
2357 if (rb_obj_class(obj2
) != rb_cNameErrorMesg
)
2360 name_error_message_t
*ptr1
, *ptr2
;
2361 TypedData_Get_Struct(obj1
, name_error_message_t
, &name_err_mesg_data_type
, ptr1
);
2362 TypedData_Get_Struct(obj2
, name_error_message_t
, &name_err_mesg_data_type
, ptr2
);
2364 if (!rb_equal(ptr1
->mesg
, ptr2
->mesg
)) return Qfalse
;
2365 if (!rb_equal(ptr1
->recv
, ptr2
->recv
)) return Qfalse
;
2366 if (!rb_equal(ptr1
->name
, ptr2
->name
)) return Qfalse
;
2372 name_err_mesg_receiver_name(VALUE obj
)
2374 if (RB_SPECIAL_CONST_P(obj
)) return Qundef
;
2375 if (RB_BUILTIN_TYPE(obj
) == T_MODULE
|| RB_BUILTIN_TYPE(obj
) == T_CLASS
) {
2376 return rb_check_funcall(obj
, rb_intern("name"), 0, 0);
2383 name_err_mesg_to_str(VALUE obj
)
2385 name_error_message_t
*ptr
;
2386 TypedData_Get_Struct(obj
, name_error_message_t
, &name_err_mesg_data_type
, ptr
);
2388 VALUE mesg
= ptr
->mesg
;
2389 if (NIL_P(mesg
)) return Qnil
;
2391 struct RString s_str
, c_str
, d_str
;
2392 VALUE c
, s
, d
= 0, args
[4], c2
;
2394 rb_encoding
*usascii
= rb_usascii_encoding();
2396 #define FAKE_CSTR(v, str) rb_setup_fake_str((v), (str), rb_strlen_lit(str), usascii)
2397 c
= s
= FAKE_CSTR(&s_str
, "");
2401 c
= d
= FAKE_CSTR(&d_str
, "nil");
2404 c
= d
= FAKE_CSTR(&d_str
, "true");
2407 c
= d
= FAKE_CSTR(&d_str
, "false");
2410 if (strstr(RSTRING_PTR(mesg
), "%2$s")) {
2411 d
= rb_protect(name_err_mesg_receiver_name
, obj
, &state
);
2412 if (state
|| NIL_OR_UNDEF_P(d
))
2413 d
= rb_protect(rb_inspect
, obj
, &state
);
2415 rb_set_errinfo(Qnil
);
2417 d
= rb_check_string_type(d
);
2419 d
= rb_any_to_s(obj
);
2423 if (!RB_SPECIAL_CONST_P(obj
)) {
2424 switch (RB_BUILTIN_TYPE(obj
)) {
2426 s
= FAKE_CSTR(&s_str
, "module ");
2430 s
= FAKE_CSTR(&s_str
, "class ");
2440 klass
= CLASS_OF(obj
);
2441 if (RB_TYPE_P(klass
, T_CLASS
) && RCLASS_SINGLETON_P(klass
)) {
2442 s
= FAKE_CSTR(&s_str
, "");
2443 if (obj
== rb_vm_top_self()) {
2444 c
= FAKE_CSTR(&c_str
, "main");
2447 c
= rb_any_to_s(obj
);
2452 s
= FAKE_CSTR(&s_str
, "an instance of ");
2453 c
= rb_class_real(klass
);
2456 c2
= rb_protect(name_err_mesg_receiver_name
, c
, &state
);
2457 if (state
|| NIL_OR_UNDEF_P(c2
))
2458 c2
= rb_protect(rb_inspect
, c
, &state
);
2460 rb_set_errinfo(Qnil
);
2462 c2
= rb_check_string_type(c2
);
2464 c2
= rb_any_to_s(c
);
2469 args
[0] = rb_obj_as_string(ptr
->name
);
2473 mesg
= rb_str_format(4, args
, mesg
);
2480 name_err_mesg_dump(VALUE obj
, VALUE limit
)
2482 return name_err_mesg_to_str(obj
);
2487 name_err_mesg_load(VALUE klass
, VALUE str
)
2494 * name_error.receiver -> object
2496 * Return the receiver associated with this NameError exception.
2500 name_err_receiver(VALUE self
)
2502 VALUE recv
= rb_ivar_lookup(self
, id_recv
, Qundef
);
2503 if (!UNDEF_P(recv
)) return recv
;
2505 VALUE mesg
= rb_attr_get(self
, id_mesg
);
2506 if (!rb_typeddata_is_kind_of(mesg
, &name_err_mesg_data_type
)) {
2507 rb_raise(rb_eArgError
, "no receiver is available");
2510 name_error_message_t
*ptr
;
2511 TypedData_Get_Struct(mesg
, name_error_message_t
, &name_err_mesg_data_type
, ptr
);
2517 * no_method_error.args -> obj
2519 * Return the arguments passed in as the third parameter to
2524 nometh_err_args(VALUE self
)
2526 return rb_attr_get(self
, id_args
);
2531 * no_method_error.private_call? -> true or false
2533 * Return true if the caused method was called as private.
2537 nometh_err_private_call_p(VALUE self
)
2539 return rb_attr_get(self
, id_private_call_p
);
2543 rb_invalid_str(const char *str
, const char *type
)
2545 VALUE s
= rb_str_new2(str
);
2547 rb_raise(rb_eArgError
, "invalid value for %s: %+"PRIsVALUE
, type
, s
);
2552 * key_error.receiver -> object
2554 * Return the receiver associated with this KeyError exception.
2558 key_err_receiver(VALUE self
)
2562 recv
= rb_ivar_lookup(self
, id_receiver
, Qundef
);
2563 if (!UNDEF_P(recv
)) return recv
;
2564 rb_raise(rb_eArgError
, "no receiver is available");
2569 * key_error.key -> object
2571 * Return the key caused this KeyError exception.
2575 key_err_key(VALUE self
)
2579 key
= rb_ivar_lookup(self
, id_key
, Qundef
);
2580 if (!UNDEF_P(key
)) return key
;
2581 rb_raise(rb_eArgError
, "no key is available");
2585 rb_key_err_new(VALUE mesg
, VALUE recv
, VALUE key
)
2587 VALUE exc
= rb_obj_alloc(rb_eKeyError
);
2588 rb_ivar_set(exc
, id_mesg
, mesg
);
2589 rb_ivar_set(exc
, id_bt
, Qnil
);
2590 rb_ivar_set(exc
, id_key
, key
);
2591 rb_ivar_set(exc
, id_receiver
, recv
);
2597 * KeyError.new(message=nil, receiver: nil, key: nil) -> key_error
2599 * Construct a new +KeyError+ exception with the given message,
2604 key_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2608 rb_call_super(rb_scan_args(argc
, argv
, "01:", NULL
, &options
), argv
);
2610 if (!NIL_P(options
)) {
2612 VALUE values
[numberof(keywords
)];
2614 keywords
[0] = id_receiver
;
2615 keywords
[1] = id_key
;
2616 rb_get_kwargs(options
, keywords
, 0, numberof(values
), values
);
2617 for (i
= 0; i
< numberof(values
); ++i
) {
2618 if (!UNDEF_P(values
[i
])) {
2619 rb_ivar_set(self
, keywords
[i
], values
[i
]);
2629 * no_matching_pattern_key_error.matchee -> object
2631 * Return the matchee associated with this NoMatchingPatternKeyError exception.
2635 no_matching_pattern_key_err_matchee(VALUE self
)
2639 matchee
= rb_ivar_lookup(self
, id_matchee
, Qundef
);
2640 if (!UNDEF_P(matchee
)) return matchee
;
2641 rb_raise(rb_eArgError
, "no matchee is available");
2646 * no_matching_pattern_key_error.key -> object
2648 * Return the key caused this NoMatchingPatternKeyError exception.
2652 no_matching_pattern_key_err_key(VALUE self
)
2656 key
= rb_ivar_lookup(self
, id_key
, Qundef
);
2657 if (!UNDEF_P(key
)) return key
;
2658 rb_raise(rb_eArgError
, "no key is available");
2663 * NoMatchingPatternKeyError.new(message=nil, matchee: nil, key: nil) -> no_matching_pattern_key_error
2665 * Construct a new +NoMatchingPatternKeyError+ exception with the given message,
2670 no_matching_pattern_key_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
2674 rb_call_super(rb_scan_args(argc
, argv
, "01:", NULL
, &options
), argv
);
2676 if (!NIL_P(options
)) {
2678 VALUE values
[numberof(keywords
)];
2680 keywords
[0] = id_matchee
;
2681 keywords
[1] = id_key
;
2682 rb_get_kwargs(options
, keywords
, 0, numberof(values
), values
);
2683 for (i
= 0; i
< numberof(values
); ++i
) {
2684 if (!UNDEF_P(values
[i
])) {
2685 rb_ivar_set(self
, keywords
[i
], values
[i
]);
2696 * SyntaxError.new([msg]) -> syntax_error
2698 * Construct a SyntaxError exception.
2702 syntax_error_initialize(int argc
, VALUE
*argv
, VALUE self
)
2706 mesg
= rb_fstring_lit("compile error");
2710 return rb_call_super(argc
, argv
);
2714 syntax_error_with_path(VALUE exc
, VALUE path
, VALUE
*mesg
, rb_encoding
*enc
)
2717 *mesg
= rb_enc_str_new(0, 0, enc
);
2718 exc
= rb_class_new_instance(1, mesg
, rb_eSyntaxError
);
2719 rb_ivar_set(exc
, id_i_path
, path
);
2722 VALUE old_path
= rb_attr_get(exc
, id_i_path
);
2723 if (old_path
!= path
) {
2724 if (rb_str_equal(path
, old_path
)) {
2725 rb_raise(rb_eArgError
, "SyntaxError#path changed: %+"PRIsVALUE
" (%p->%p)",
2726 old_path
, (void *)old_path
, (void *)path
);
2729 rb_raise(rb_eArgError
, "SyntaxError#path changed: %+"PRIsVALUE
"(%s%s)->%+"PRIsVALUE
"(%s)",
2730 old_path
, rb_enc_name(rb_enc_get(old_path
)),
2731 (FL_TEST(old_path
, RSTRING_FSTR
) ? ":FSTR" : ""),
2732 path
, rb_enc_name(rb_enc_get(path
)));
2735 VALUE s
= *mesg
= rb_attr_get(exc
, idMesg
);
2736 if (RSTRING_LEN(s
) > 0 && *(RSTRING_END(s
)-1) != '\n')
2737 rb_str_cat_cstr(s
, "\n");
2743 * Document-module: Errno
2745 * Ruby exception objects are subclasses of Exception. However,
2746 * operating systems typically report errors using plain
2747 * integers. Module Errno is created dynamically to map these
2748 * operating system errors to Ruby classes, with each error number
2749 * generating its own subclass of SystemCallError. As the subclass
2750 * is created in module Errno, its name will start
2751 * <code>Errno::</code>.
2753 * The names of the <code>Errno::</code> classes depend on the
2754 * environment in which Ruby runs. On a typical Unix or Windows
2755 * platform, there are Errno classes such as Errno::EACCES,
2756 * Errno::EAGAIN, Errno::EINTR, and so on.
2758 * The integer operating system error number corresponding to a
2759 * particular error is available as the class constant
2760 * <code>Errno::</code><em>error</em><code>::Errno</code>.
2762 * Errno::EACCES::Errno #=> 13
2763 * Errno::EAGAIN::Errno #=> 11
2764 * Errno::EINTR::Errno #=> 4
2766 * The full list of operating system errors on your particular platform
2767 * are available as the constants of Errno.
2769 * Errno.constants #=> :E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, ...
2772 static st_table
*syserr_tbl
;
2775 rb_free_warning(void)
2777 st_free_table(warning_categories
.id2enum
);
2778 st_free_table(warning_categories
.enum2id
);
2779 st_free_table(syserr_tbl
);
2783 setup_syserr(int n
, const char *name
)
2785 VALUE error
= rb_define_class_under(rb_mErrno
, name
, rb_eSystemCallError
);
2787 /* capture nonblock errnos for WaitReadable/WaitWritable subclasses */
2792 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
2797 rb_eEWOULDBLOCK
= error
;
2800 rb_eEINPROGRESS
= error
;
2804 rb_define_const(error
, "Errno", INT2NUM(n
));
2805 st_add_direct(syserr_tbl
, n
, (st_data_t
)error
);
2810 set_syserr(int n
, const char *name
)
2814 if (!st_lookup(syserr_tbl
, n
, &error
)) {
2815 return setup_syserr(n
, name
);
2818 VALUE errclass
= (VALUE
)error
;
2819 rb_define_const(rb_mErrno
, name
, errclass
);
2829 if (!st_lookup(syserr_tbl
, n
, &error
)) {
2830 char name
[DECIMAL_SIZE_OF(n
) + sizeof("E-")];
2832 snprintf(name
, sizeof(name
), "E%03d", n
);
2833 return setup_syserr(n
, name
);
2835 return (VALUE
)error
;
2840 * SystemCallError.new(msg, errno) -> system_call_error_subclass
2842 * If _errno_ corresponds to a known system error code, constructs the
2843 * appropriate Errno class for that error, otherwise constructs a
2844 * generic SystemCallError object. The error number is subsequently
2845 * available via the #errno method.
2849 syserr_initialize(int argc
, VALUE
*argv
, VALUE self
)
2852 VALUE mesg
, error
, func
, errmsg
;
2853 VALUE klass
= rb_obj_class(self
);
2855 if (klass
== rb_eSystemCallError
) {
2856 st_data_t data
= (st_data_t
)klass
;
2857 rb_scan_args(argc
, argv
, "12", &mesg
, &error
, &func
);
2858 if (argc
== 1 && FIXNUM_P(mesg
)) {
2859 error
= mesg
; mesg
= Qnil
;
2861 if (!NIL_P(error
) && st_lookup(syserr_tbl
, NUM2LONG(error
), &data
)) {
2862 klass
= (VALUE
)data
;
2864 if (!RB_TYPE_P(self
, T_OBJECT
)) { /* insurance to avoid type crash */
2865 rb_raise(rb_eTypeError
, "invalid instance type");
2867 RBASIC_SET_CLASS(self
, klass
);
2871 rb_scan_args(argc
, argv
, "02", &mesg
, &func
);
2872 error
= rb_const_get(klass
, id_Errno
);
2874 if (!NIL_P(error
)) err
= strerror(NUM2INT(error
));
2875 else err
= "unknown error";
2877 errmsg
= rb_enc_str_new_cstr(err
, rb_locale_encoding());
2879 VALUE str
= StringValue(mesg
);
2881 if (!NIL_P(func
)) rb_str_catf(errmsg
, " @ %"PRIsVALUE
, func
);
2882 rb_str_catf(errmsg
, " - %"PRIsVALUE
, str
);
2886 rb_call_super(1, &mesg
);
2887 rb_ivar_set(self
, id_errno
, error
);
2893 * system_call_error.errno -> integer
2895 * Return this SystemCallError's error number.
2899 syserr_errno(VALUE self
)
2901 return rb_attr_get(self
, id_errno
);
2906 * system_call_error === other -> true or false
2908 * Return +true+ if the receiver is a generic +SystemCallError+, or
2909 * if the error numbers +self+ and _other_ are the same.
2913 syserr_eqq(VALUE self
, VALUE exc
)
2917 if (!rb_obj_is_kind_of(exc
, rb_eSystemCallError
)) {
2918 if (!rb_respond_to(exc
, id_errno
)) return Qfalse
;
2920 else if (self
== rb_eSystemCallError
) return Qtrue
;
2922 num
= rb_attr_get(exc
, id_errno
);
2924 num
= rb_funcallv(exc
, id_errno
, 0, 0);
2926 e
= rb_const_get(self
, id_Errno
);
2927 return RBOOL(FIXNUM_P(num
) ? num
== e
: rb_equal(num
, e
));
2932 * Document-class: StandardError
2934 * The most standard error types are subclasses of StandardError. A
2935 * rescue clause without an explicit Exception class will rescue all
2936 * StandardErrors (and only those).
2941 * foo rescue "Hello" #=> "Hello"
2943 * On the other hand:
2945 * require 'does/not/exist' rescue "Hi"
2947 * <em>raises the exception:</em>
2949 * LoadError: no such file to load -- does/not/exist
2954 * Document-class: SystemExit
2956 * Raised by +exit+ to initiate the termination of the script.
2960 * Document-class: SignalException
2962 * Raised when a signal is received.
2965 * Process.kill('HUP',Process.pid)
2966 * sleep # wait for receiver to handle signal sent by Process.kill
2967 * rescue SignalException => e
2968 * puts "received Exception #{e}"
2971 * <em>produces:</em>
2973 * received Exception SIGHUP
2977 * Document-class: Interrupt
2979 * Raised when the interrupt signal is received, typically because the
2980 * user has pressed Control-C (on most posix platforms). As such, it is a
2981 * subclass of +SignalException+.
2984 * puts "Press ctrl-C when you get bored"
2986 * rescue Interrupt => e
2987 * puts "Note: You will typically use Signal.trap instead."
2990 * <em>produces:</em>
2992 * Press ctrl-C when you get bored
2994 * <em>then waits until it is interrupted with Control-C and then prints:</em>
2996 * Note: You will typically use Signal.trap instead.
3000 * Document-class: TypeError
3002 * Raised when encountering an object that is not of the expected type.
3004 * [1, 2, 3].first("two")
3006 * <em>raises the exception:</em>
3008 * TypeError: no implicit conversion of String into Integer
3013 * Document-class: ArgumentError
3015 * Raised when the arguments are wrong and there isn't a more specific
3018 * Ex: passing the wrong number of arguments
3020 * [1, 2, 3].first(4, 5)
3022 * <em>raises the exception:</em>
3024 * ArgumentError: wrong number of arguments (given 2, expected 1)
3026 * Ex: passing an argument that is not acceptable:
3028 * [1, 2, 3].first(-4)
3030 * <em>raises the exception:</em>
3032 * ArgumentError: negative array size
3036 * Document-class: IndexError
3038 * Raised when the given index is invalid.
3041 * a.fetch(0) #=> :foo
3043 * a.fetch(4) #=> IndexError: index 4 outside of array bounds: -2...2
3048 * Document-class: KeyError
3050 * Raised when the specified key is not found. It is a subclass of
3053 * h = {"foo" => :bar}
3054 * h.fetch("foo") #=> :bar
3055 * h.fetch("baz") #=> KeyError: key not found: "baz"
3060 * Document-class: RangeError
3062 * Raised when a given numerical value is out of range.
3064 * [1, 2, 3].drop(1 << 100)
3066 * <em>raises the exception:</em>
3068 * RangeError: bignum too big to convert into `long'
3072 * Document-class: ScriptError
3074 * ScriptError is the superclass for errors raised when a script
3075 * can not be executed because of a +LoadError+,
3076 * +NotImplementedError+ or a +SyntaxError+. Note these type of
3077 * +ScriptErrors+ are not +StandardError+ and will not be
3078 * rescued unless it is specified explicitly (or its ancestor
3083 * Document-class: SyntaxError
3085 * Raised when encountering Ruby code with an invalid syntax.
3089 * <em>raises the exception:</em>
3091 * SyntaxError: (eval):1: syntax error, unexpected '=', expecting $end
3095 * Document-class: LoadError
3097 * Raised when a file required (a Ruby script, extension library, ...)
3100 * require 'this/file/does/not/exist'
3102 * <em>raises the exception:</em>
3104 * LoadError: no such file to load -- this/file/does/not/exist
3108 * Document-class: NotImplementedError
3110 * Raised when a feature is not implemented on the current platform. For
3111 * example, methods depending on the +fsync+ or +fork+ system calls may
3112 * raise this exception if the underlying operating system or Ruby
3113 * runtime does not support them.
3115 * Note that if +fork+ raises a +NotImplementedError+, then
3116 * <code>respond_to?(:fork)</code> returns +false+.
3120 * Document-class: NameError
3122 * Raised when a given name is invalid or undefined.
3126 * <em>raises the exception:</em>
3128 * NameError: undefined local variable or method `foo' for main:Object
3130 * Since constant names must start with a capital:
3132 * Integer.const_set :answer, 42
3134 * <em>raises the exception:</em>
3136 * NameError: wrong constant name answer
3140 * Document-class: NoMethodError
3142 * Raised when a method is called on a receiver which doesn't have it
3143 * defined and also fails to respond with +method_missing+.
3147 * <em>raises the exception:</em>
3149 * NoMethodError: undefined method `to_ary' for an instance of String
3153 * Document-class: FrozenError
3155 * Raised when there is an attempt to modify a frozen object.
3157 * [1, 2, 3].freeze << 4
3159 * <em>raises the exception:</em>
3161 * FrozenError: can't modify frozen Array
3165 * Document-class: RuntimeError
3167 * A generic error class raised when an invalid operation is attempted.
3168 * Kernel#raise will raise a RuntimeError if no Exception class is
3173 * <em>raises the exception:</em>
3175 * RuntimeError: ouch
3179 * Document-class: SecurityError
3181 * No longer used by internal code.
3185 * Document-class: NoMemoryError
3187 * Raised when memory allocation fails.
3191 * Document-class: SystemCallError
3193 * SystemCallError is the base class for all low-level
3194 * platform-dependent errors.
3196 * The errors available on the current platform are subclasses of
3197 * SystemCallError and are defined in the Errno module.
3199 * File.open("does/not/exist")
3201 * <em>raises the exception:</em>
3203 * Errno::ENOENT: No such file or directory - does/not/exist
3207 * Document-class: EncodingError
3209 * EncodingError is the base class for encoding errors.
3213 * Document-class: Encoding::CompatibilityError
3215 * Raised by Encoding and String methods when the source encoding is
3216 * incompatible with the target encoding.
3220 * Document-class: fatal
3222 * +fatal+ is an Exception that is raised when Ruby has encountered a fatal
3223 * error and must exit.
3227 * Document-class: NameError::message
3232 * Document-class: Exception
3234 * \Class Exception and its subclasses are used to communicate between
3235 * Kernel#raise and +rescue+ statements in <code>begin ... end</code> blocks.
3237 * An Exception object carries information about an exception:
3238 * - Its type (the exception's class).
3239 * - An optional descriptive message.
3240 * - Optional backtrace information.
3242 * Some built-in subclasses of Exception have additional methods: e.g., NameError#name.
3246 * Two Ruby statements have default exception classes:
3247 * - +raise+: defaults to RuntimeError.
3248 * - +rescue+: defaults to StandardError.
3250 * == Global Variables
3252 * When an exception has been raised but not yet handled (in +rescue+,
3253 * +ensure+, +at_exit+ and +END+ blocks), two global variables are set:
3254 * - <code>$!</code> contains the current exception.
3255 * - <code>$@</code> contains its backtrace.
3257 * == Custom Exceptions
3259 * To provide additional or alternate information,
3260 * a program may create custom exception classes
3261 * that derive from the built-in exception classes.
3263 * A good practice is for a library to create a single "generic" exception class
3264 * (typically a subclass of StandardError or RuntimeError)
3265 * and have its other exception classes derive from that class.
3266 * This allows the user to rescue the generic exception, thus catching all exceptions
3267 * the library may raise even if future versions of the library add new
3268 * exception subclasses.
3273 * class Error < ::StandardError
3276 * class WidgetError < Error
3279 * class FrobError < Error
3284 * To handle both MyLibrary::WidgetError and MyLibrary::FrobError the library
3285 * user can rescue MyLibrary::Error.
3287 * == Built-In Exception Classes
3289 * The built-in subclasses of Exception are:
3294 * * NotImplementedError
3301 * * UncaughtThrowError
3309 * * ClosedQueueError
3314 * * FloatDomainError
3322 * * ZeroDivisionError
3324 * * SystemStackError
3329 exception_alloc(VALUE klass
)
3331 return rb_class_allocate_instance(klass
);
3335 exception_dumper(VALUE exc
)
3337 // TODO: Currently, the instance variables "bt" and "bt_locations"
3338 // refers to the same object (Array of String). But "bt_locations"
3339 // should have an Array of Thread::Backtrace::Locations.
3345 ivar_copy_i(ID key
, VALUE val
, st_data_t exc
)
3347 rb_ivar_set((VALUE
)exc
, key
, val
);
3351 void rb_exc_check_circular_cause(VALUE exc
);
3354 exception_loader(VALUE exc
, VALUE obj
)
3356 // The loader function of rb_marshal_define_compat seems to be called for two events:
3357 // one is for fixup (r_fixup_compat), the other is for TYPE_USERDEF.
3358 // In the former case, the first argument is an instance of Exception (because
3359 // we pass rb_eException to rb_marshal_define_compat). In the latter case, the first
3360 // argument is a class object (see TYPE_USERDEF case in r_object0).
3361 // We want to copy all instance variables (but "bt_locations") from obj to exc.
3362 // But we do not want to do so in the second case, so the following branch is for that.
3363 if (RB_TYPE_P(exc
, T_CLASS
)) return obj
; // maybe called from Marshal's TYPE_USERDEF
3365 rb_ivar_foreach(obj
, ivar_copy_i
, exc
);
3367 rb_exc_check_circular_cause(exc
);
3369 if (rb_attr_get(exc
, id_bt
) == rb_attr_get(exc
, id_bt_locations
)) {
3370 rb_ivar_set(exc
, id_bt_locations
, Qnil
);
3377 Init_Exception(void)
3379 rb_eException
= rb_define_class("Exception", rb_cObject
);
3380 rb_define_alloc_func(rb_eException
, exception_alloc
);
3381 rb_marshal_define_compat(rb_eException
, rb_eException
, exception_dumper
, exception_loader
);
3382 rb_define_singleton_method(rb_eException
, "exception", rb_class_new_instance
, -1);
3383 rb_define_singleton_method(rb_eException
, "to_tty?", exc_s_to_tty_p
, 0);
3384 rb_define_method(rb_eException
, "exception", exc_exception
, -1);
3385 rb_define_method(rb_eException
, "initialize", exc_initialize
, -1);
3386 rb_define_method(rb_eException
, "==", exc_equal
, 1);
3387 rb_define_method(rb_eException
, "to_s", exc_to_s
, 0);
3388 rb_define_method(rb_eException
, "message", exc_message
, 0);
3389 rb_define_method(rb_eException
, "detailed_message", exc_detailed_message
, -1);
3390 rb_define_method(rb_eException
, "full_message", exc_full_message
, -1);
3391 rb_define_method(rb_eException
, "inspect", exc_inspect
, 0);
3392 rb_define_method(rb_eException
, "backtrace", exc_backtrace
, 0);
3393 rb_define_method(rb_eException
, "backtrace_locations", exc_backtrace_locations
, 0);
3394 rb_define_method(rb_eException
, "set_backtrace", exc_set_backtrace
, 1);
3395 rb_define_method(rb_eException
, "cause", exc_cause
, 0);
3397 rb_eSystemExit
= rb_define_class("SystemExit", rb_eException
);
3398 rb_define_method(rb_eSystemExit
, "initialize", exit_initialize
, -1);
3399 rb_define_method(rb_eSystemExit
, "status", exit_status
, 0);
3400 rb_define_method(rb_eSystemExit
, "success?", exit_success_p
, 0);
3402 rb_eFatal
= rb_define_class("fatal", rb_eException
);
3403 rb_eSignal
= rb_define_class("SignalException", rb_eException
);
3404 rb_eInterrupt
= rb_define_class("Interrupt", rb_eSignal
);
3406 rb_eStandardError
= rb_define_class("StandardError", rb_eException
);
3407 rb_eTypeError
= rb_define_class("TypeError", rb_eStandardError
);
3408 rb_eArgError
= rb_define_class("ArgumentError", rb_eStandardError
);
3409 rb_eIndexError
= rb_define_class("IndexError", rb_eStandardError
);
3410 rb_eKeyError
= rb_define_class("KeyError", rb_eIndexError
);
3411 rb_define_method(rb_eKeyError
, "initialize", key_err_initialize
, -1);
3412 rb_define_method(rb_eKeyError
, "receiver", key_err_receiver
, 0);
3413 rb_define_method(rb_eKeyError
, "key", key_err_key
, 0);
3414 rb_eRangeError
= rb_define_class("RangeError", rb_eStandardError
);
3416 rb_eScriptError
= rb_define_class("ScriptError", rb_eException
);
3417 rb_eSyntaxError
= rb_define_class("SyntaxError", rb_eScriptError
);
3418 rb_define_method(rb_eSyntaxError
, "initialize", syntax_error_initialize
, -1);
3420 /* RDoc will use literal name value while parsing rb_attr,
3421 * and will render `idPath` as an attribute name without this trick */
3424 /* the path failed to parse */
3425 rb_attr(rb_eSyntaxError
, path
, TRUE
, FALSE
, FALSE
);
3427 rb_eLoadError
= rb_define_class("LoadError", rb_eScriptError
);
3428 /* the path failed to load */
3429 rb_attr(rb_eLoadError
, path
, TRUE
, FALSE
, FALSE
);
3431 rb_eNotImpError
= rb_define_class("NotImplementedError", rb_eScriptError
);
3433 rb_eNameError
= rb_define_class("NameError", rb_eStandardError
);
3434 rb_define_method(rb_eNameError
, "initialize", name_err_initialize
, -1);
3435 rb_define_method(rb_eNameError
, "name", name_err_name
, 0);
3436 rb_define_method(rb_eNameError
, "receiver", name_err_receiver
, 0);
3437 rb_define_method(rb_eNameError
, "local_variables", name_err_local_variables
, 0);
3438 rb_cNameErrorMesg
= rb_define_class_under(rb_eNameError
, "message", rb_cObject
);
3439 rb_define_alloc_func(rb_cNameErrorMesg
, name_err_mesg_alloc
);
3440 rb_define_method(rb_cNameErrorMesg
, "initialize_copy", name_err_mesg_init_copy
, 1);
3441 rb_define_method(rb_cNameErrorMesg
, "==", name_err_mesg_equal
, 1);
3442 rb_define_method(rb_cNameErrorMesg
, "to_str", name_err_mesg_to_str
, 0);
3443 rb_define_method(rb_cNameErrorMesg
, "_dump", name_err_mesg_dump
, 1);
3444 rb_define_singleton_method(rb_cNameErrorMesg
, "_load", name_err_mesg_load
, 1);
3445 rb_eNoMethodError
= rb_define_class("NoMethodError", rb_eNameError
);
3446 rb_define_method(rb_eNoMethodError
, "initialize", nometh_err_initialize
, -1);
3447 rb_define_method(rb_eNoMethodError
, "args", nometh_err_args
, 0);
3448 rb_define_method(rb_eNoMethodError
, "private_call?", nometh_err_private_call_p
, 0);
3450 rb_eRuntimeError
= rb_define_class("RuntimeError", rb_eStandardError
);
3451 rb_eFrozenError
= rb_define_class("FrozenError", rb_eRuntimeError
);
3452 rb_define_method(rb_eFrozenError
, "initialize", frozen_err_initialize
, -1);
3453 rb_define_method(rb_eFrozenError
, "receiver", frozen_err_receiver
, 0);
3454 rb_eSecurityError
= rb_define_class("SecurityError", rb_eException
);
3455 rb_eNoMemError
= rb_define_class("NoMemoryError", rb_eException
);
3456 rb_eEncodingError
= rb_define_class("EncodingError", rb_eStandardError
);
3457 rb_eEncCompatError
= rb_define_class_under(rb_cEncoding
, "CompatibilityError", rb_eEncodingError
);
3458 rb_eNoMatchingPatternError
= rb_define_class("NoMatchingPatternError", rb_eStandardError
);
3459 rb_eNoMatchingPatternKeyError
= rb_define_class("NoMatchingPatternKeyError", rb_eNoMatchingPatternError
);
3460 rb_define_method(rb_eNoMatchingPatternKeyError
, "initialize", no_matching_pattern_key_err_initialize
, -1);
3461 rb_define_method(rb_eNoMatchingPatternKeyError
, "matchee", no_matching_pattern_key_err_matchee
, 0);
3462 rb_define_method(rb_eNoMatchingPatternKeyError
, "key", no_matching_pattern_key_err_key
, 0);
3464 syserr_tbl
= st_init_numtable();
3465 rb_eSystemCallError
= rb_define_class("SystemCallError", rb_eStandardError
);
3466 rb_define_method(rb_eSystemCallError
, "initialize", syserr_initialize
, -1);
3467 rb_define_method(rb_eSystemCallError
, "errno", syserr_errno
, 0);
3468 rb_define_singleton_method(rb_eSystemCallError
, "===", syserr_eqq
, 1);
3470 rb_mErrno
= rb_define_module("Errno");
3472 rb_mWarning
= rb_define_module("Warning");
3473 rb_define_singleton_method(rb_mWarning
, "[]", rb_warning_s_aref
, 1);
3474 rb_define_singleton_method(rb_mWarning
, "[]=", rb_warning_s_aset
, 2);
3475 rb_define_singleton_method(rb_mWarning
, "categories", rb_warning_s_categories
, 0);
3476 rb_define_method(rb_mWarning
, "warn", rb_warning_s_warn
, -1);
3477 rb_extend_object(rb_mWarning
, rb_mWarning
);
3480 rb_cWarningBuffer
= rb_define_class_under(rb_mWarning
, "buffer", rb_cString
);
3481 rb_define_method(rb_cWarningBuffer
, "write", warning_write
, -1);
3483 id_cause
= rb_intern_const("cause");
3484 id_message
= rb_intern_const("message");
3485 id_detailed_message
= rb_intern_const("detailed_message");
3486 id_backtrace
= rb_intern_const("backtrace");
3487 id_key
= rb_intern_const("key");
3488 id_matchee
= rb_intern_const("matchee");
3489 id_args
= rb_intern_const("args");
3490 id_receiver
= rb_intern_const("receiver");
3491 id_private_call_p
= rb_intern_const("private_call?");
3492 id_local_variables
= rb_intern_const("local_variables");
3493 id_Errno
= rb_intern_const("Errno");
3494 id_errno
= rb_intern_const("errno");
3495 id_i_path
= rb_intern_const("@path");
3496 id_warn
= rb_intern_const("warn");
3497 id_category
= rb_intern_const("category");
3498 id_deprecated
= rb_intern_const("deprecated");
3499 id_experimental
= rb_intern_const("experimental");
3500 id_performance
= rb_intern_const("performance");
3501 id_top
= rb_intern_const("top");
3502 id_bottom
= rb_intern_const("bottom");
3503 id_iseq
= rb_make_internal_id();
3504 id_recv
= rb_make_internal_id();
3506 sym_category
= ID2SYM(id_category
);
3507 sym_highlight
= ID2SYM(rb_intern_const("highlight"));
3509 warning_categories
.id2enum
= rb_init_identtable();
3510 st_add_direct(warning_categories
.id2enum
, id_deprecated
, RB_WARN_CATEGORY_DEPRECATED
);
3511 st_add_direct(warning_categories
.id2enum
, id_experimental
, RB_WARN_CATEGORY_EXPERIMENTAL
);
3512 st_add_direct(warning_categories
.id2enum
, id_performance
, RB_WARN_CATEGORY_PERFORMANCE
);
3514 warning_categories
.enum2id
= rb_init_identtable();
3515 st_add_direct(warning_categories
.enum2id
, RB_WARN_CATEGORY_NONE
, 0);
3516 st_add_direct(warning_categories
.enum2id
, RB_WARN_CATEGORY_DEPRECATED
, id_deprecated
);
3517 st_add_direct(warning_categories
.enum2id
, RB_WARN_CATEGORY_EXPERIMENTAL
, id_experimental
);
3518 st_add_direct(warning_categories
.enum2id
, RB_WARN_CATEGORY_PERFORMANCE
, id_performance
);
3522 rb_enc_raise(rb_encoding
*enc
, VALUE exc
, const char *fmt
, ...)
3527 va_start(args
, fmt
);
3528 mesg
= rb_enc_vsprintf(enc
, fmt
, args
);
3531 rb_exc_raise(rb_exc_new3(exc
, mesg
));
3535 rb_vraise(VALUE exc
, const char *fmt
, va_list ap
)
3537 rb_exc_raise(rb_exc_new3(exc
, rb_vsprintf(fmt
, ap
)));
3541 rb_raise(VALUE exc
, const char *fmt
, ...)
3544 va_start(args
, fmt
);
3545 rb_vraise(exc
, fmt
, args
);
3549 NORETURN(static void raise_loaderror(VALUE path
, VALUE mesg
));
3552 raise_loaderror(VALUE path
, VALUE mesg
)
3554 VALUE err
= rb_exc_new3(rb_eLoadError
, mesg
);
3555 rb_ivar_set(err
, id_i_path
, path
);
3560 rb_loaderror(const char *fmt
, ...)
3565 va_start(args
, fmt
);
3566 mesg
= rb_enc_vsprintf(rb_locale_encoding(), fmt
, args
);
3568 raise_loaderror(Qnil
, mesg
);
3572 rb_loaderror_with_path(VALUE path
, const char *fmt
, ...)
3577 va_start(args
, fmt
);
3578 mesg
= rb_enc_vsprintf(rb_locale_encoding(), fmt
, args
);
3580 raise_loaderror(path
, mesg
);
3584 rb_notimplement(void)
3586 rb_raise(rb_eNotImpError
,
3587 "%"PRIsVALUE
"() function is unimplemented on this machine",
3588 rb_id2str(rb_frame_this_func()));
3592 rb_fatal(const char *fmt
, ...)
3597 if (! ruby_thread_has_gvl_p()) {
3598 /* The thread has no GVL. Object allocation impossible (cant run GC),
3599 * thus no message can be printed out. */
3600 fprintf(stderr
, "[FATAL] rb_fatal() outside of GVL\n");
3601 rb_print_backtrace(stderr
);
3605 va_start(args
, fmt
);
3606 mesg
= rb_vsprintf(fmt
, args
);
3609 rb_exc_fatal(rb_exc_new3(rb_eFatal
, mesg
));
3613 make_errno_exc(const char *mesg
)
3619 rb_bug("rb_sys_fail(%s) - errno == 0", mesg
? mesg
: "");
3621 return rb_syserr_new(n
, mesg
);
3625 make_errno_exc_str(VALUE mesg
)
3630 if (!mesg
) mesg
= Qnil
;
3632 const char *s
= !NIL_P(mesg
) ? RSTRING_PTR(mesg
) : "";
3633 rb_bug("rb_sys_fail_str(%s) - errno == 0", s
);
3635 return rb_syserr_new_str(n
, mesg
);
3639 rb_syserr_new(int n
, const char *mesg
)
3642 arg
= mesg
? rb_str_new2(mesg
) : Qnil
;
3643 return rb_syserr_new_str(n
, arg
);
3647 rb_syserr_new_str(int n
, VALUE arg
)
3649 return rb_class_new_instance(1, &arg
, get_syserr(n
));
3653 rb_syserr_fail(int e
, const char *mesg
)
3655 rb_exc_raise(rb_syserr_new(e
, mesg
));
3659 rb_syserr_fail_str(int e
, VALUE mesg
)
3661 rb_exc_raise(rb_syserr_new_str(e
, mesg
));
3666 rb_sys_fail(const char *mesg
)
3668 rb_exc_raise(make_errno_exc(mesg
));
3671 #undef rb_sys_fail_str
3673 rb_sys_fail_str(VALUE mesg
)
3675 rb_exc_raise(make_errno_exc_str(mesg
));
3678 #ifdef RUBY_FUNCTION_NAME_STRING
3680 rb_sys_fail_path_in(const char *func_name
, VALUE path
)
3685 rb_syserr_fail_path_in(func_name
, n
, path
);
3689 rb_syserr_fail_path_in(const char *func_name
, int n
, VALUE path
)
3691 rb_exc_raise(rb_syserr_new_path_in(func_name
, n
, path
));
3695 rb_syserr_new_path_in(const char *func_name
, int n
, VALUE path
)
3699 if (!path
) path
= Qnil
;
3701 const char *s
= !NIL_P(path
) ? RSTRING_PTR(path
) : "";
3702 if (!func_name
) func_name
= "(null)";
3703 rb_bug("rb_sys_fail_path_in(%s, %s) - errno == 0",
3707 args
[1] = rb_str_new_cstr(func_name
);
3708 return rb_class_new_instance(2, args
, get_syserr(n
));
3712 NORETURN(static void rb_mod_exc_raise(VALUE exc
, VALUE mod
));
3715 rb_mod_exc_raise(VALUE exc
, VALUE mod
)
3717 rb_extend_object(exc
, mod
);
3722 rb_mod_sys_fail(VALUE mod
, const char *mesg
)
3724 VALUE exc
= make_errno_exc(mesg
);
3725 rb_mod_exc_raise(exc
, mod
);
3729 rb_mod_sys_fail_str(VALUE mod
, VALUE mesg
)
3731 VALUE exc
= make_errno_exc_str(mesg
);
3732 rb_mod_exc_raise(exc
, mod
);
3736 rb_mod_syserr_fail(VALUE mod
, int e
, const char *mesg
)
3738 VALUE exc
= rb_syserr_new(e
, mesg
);
3739 rb_mod_exc_raise(exc
, mod
);
3743 rb_mod_syserr_fail_str(VALUE mod
, int e
, VALUE mesg
)
3745 VALUE exc
= rb_syserr_new_str(e
, mesg
);
3746 rb_mod_exc_raise(exc
, mod
);
3750 syserr_warning(VALUE mesg
, int err
)
3752 rb_str_set_len(mesg
, RSTRING_LEN(mesg
)-1);
3753 rb_str_catf(mesg
, ": %s\n", strerror(err
));
3754 rb_write_warning_str(mesg
);
3759 rb_sys_warn(const char *fmt
, ...)
3761 if (!NIL_P(ruby_verbose
)) {
3762 int errno_save
= errno
;
3763 with_warning_string(mesg
, 0, fmt
) {
3764 syserr_warning(mesg
, errno_save
);
3771 rb_syserr_warn(int err
, const char *fmt
, ...)
3773 if (!NIL_P(ruby_verbose
)) {
3774 with_warning_string(mesg
, 0, fmt
) {
3775 syserr_warning(mesg
, err
);
3781 rb_sys_enc_warn(rb_encoding
*enc
, const char *fmt
, ...)
3783 if (!NIL_P(ruby_verbose
)) {
3784 int errno_save
= errno
;
3785 with_warning_string(mesg
, enc
, fmt
) {
3786 syserr_warning(mesg
, errno_save
);
3793 rb_syserr_enc_warn(int err
, rb_encoding
*enc
, const char *fmt
, ...)
3795 if (!NIL_P(ruby_verbose
)) {
3796 with_warning_string(mesg
, enc
, fmt
) {
3797 syserr_warning(mesg
, err
);
3804 rb_sys_warning(const char *fmt
, ...)
3806 if (RTEST(ruby_verbose
)) {
3807 int errno_save
= errno
;
3808 with_warning_string(mesg
, 0, fmt
) {
3809 syserr_warning(mesg
, errno_save
);
3817 rb_syserr_warning(int err
, const char *fmt
, ...)
3819 if (RTEST(ruby_verbose
)) {
3820 with_warning_string(mesg
, 0, fmt
) {
3821 syserr_warning(mesg
, err
);
3828 rb_sys_enc_warning(rb_encoding
*enc
, const char *fmt
, ...)
3830 if (RTEST(ruby_verbose
)) {
3831 int errno_save
= errno
;
3832 with_warning_string(mesg
, enc
, fmt
) {
3833 syserr_warning(mesg
, errno_save
);
3840 rb_syserr_enc_warning(int err
, rb_encoding
*enc
, const char *fmt
, ...)
3842 if (RTEST(ruby_verbose
)) {
3843 with_warning_string(mesg
, enc
, fmt
) {
3844 syserr_warning(mesg
, err
);
3850 rb_load_fail(VALUE path
, const char *err
)
3852 VALUE mesg
= rb_str_buf_new_cstr(err
);
3853 rb_str_cat2(mesg
, " -- ");
3854 rb_str_append(mesg
, path
); /* should be ASCII compatible */
3855 raise_loaderror(path
, mesg
);
3859 rb_error_frozen(const char *what
)
3861 rb_raise(rb_eFrozenError
, "can't modify frozen %s", what
);
3865 rb_frozen_error_raise(VALUE frozen_obj
, const char *fmt
, ...)
3870 va_start(args
, fmt
);
3871 mesg
= rb_vsprintf(fmt
, args
);
3873 exc
= rb_exc_new3(rb_eFrozenError
, mesg
);
3874 rb_ivar_set(exc
, id_recv
, frozen_obj
);
3879 inspect_frozen_obj(VALUE obj
, VALUE mesg
, int recur
)
3882 rb_str_cat_cstr(mesg
, " ...");
3885 rb_str_append(mesg
, rb_inspect(obj
));
3891 rb_error_frozen_object(VALUE frozen_obj
)
3893 rb_yjit_lazy_push_frame(GET_EC()->cfp
->pc
);
3896 const ID created_info
= id_debug_created_info
;
3897 VALUE mesg
= rb_sprintf("can't modify frozen %"PRIsVALUE
": ",
3898 CLASS_OF(frozen_obj
));
3899 VALUE exc
= rb_exc_new_str(rb_eFrozenError
, mesg
);
3901 rb_ivar_set(exc
, id_recv
, frozen_obj
);
3902 rb_exec_recursive(inspect_frozen_obj
, frozen_obj
, mesg
);
3904 if (!NIL_P(debug_info
= rb_attr_get(frozen_obj
, created_info
))) {
3905 VALUE path
= rb_ary_entry(debug_info
, 0);
3906 VALUE line
= rb_ary_entry(debug_info
, 1);
3908 rb_str_catf(mesg
, ", created at %"PRIsVALUE
":%"PRIsVALUE
, path
, line
);
3913 #undef rb_check_frozen
3915 rb_check_frozen(VALUE obj
)
3917 rb_check_frozen_internal(obj
);
3921 rb_check_copyable(VALUE obj
, VALUE orig
)
3923 if (!FL_ABLE(obj
)) return;
3924 rb_check_frozen_internal(obj
);
3925 if (!FL_ABLE(orig
)) return;
3931 rb_eNOERROR
= setup_syserr(0, "NOERROR");
3934 rb_define_const(rb_mErrno
, "NOERROR", rb_eNOERROR
);
3936 #define defined_error(name, num) set_syserr((num), (name));
3937 #define undefined_error(name) rb_define_const(rb_mErrno, (name), rb_eNOERROR);
3938 #include "known_errors.inc"
3939 #undef defined_error
3940 #undef undefined_error
3943 #include "warning.rbinc"