1 /**********************************************************************
6 created at: Mon Aug 9 16:11:34 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
22 #define EXIT_SUCCESS 0
25 extern const char ruby_description
[];
28 err_position_0(char *buf
, long len
, const char *file
, int line
)
34 return snprintf(buf
, len
, "%s: ", file
);
37 return snprintf(buf
, len
, "%s:%d: ", file
, line
);
42 err_position(char *buf
, long len
)
44 return err_position_0(buf
, len
, rb_sourcefile(), rb_sourceline());
48 err_snprintf(char *buf
, long len
, const char *fmt
, va_list args
)
52 n
= err_position(buf
, len
);
54 vsnprintf((char*)buf
+n
, len
-n
, fmt
, args
);
59 compile_snprintf(char *buf
, long len
, const char *file
, int line
, const char *fmt
, va_list args
)
63 n
= err_position_0(buf
, len
, file
, line
);
65 vsnprintf((char*)buf
+n
, len
-n
, fmt
, args
);
69 static void err_append(const char*);
72 rb_compile_error(const char *file
, int line
, const char *fmt
, ...)
78 compile_snprintf(buf
, BUFSIZ
, file
, line
, fmt
, args
);
84 rb_compile_error_append(const char *fmt
, ...)
90 vsnprintf(buf
, BUFSIZ
, fmt
, args
);
96 compile_warn_print(const char *file
, int line
, const char *fmt
, va_list args
)
101 compile_snprintf(buf
, BUFSIZ
, file
, line
, fmt
, args
);
104 rb_write_error2(buf
, len
);
108 rb_compile_warn(const char *file
, int line
, const char *fmt
, ...)
113 if (NIL_P(ruby_verbose
)) return;
115 snprintf(buf
, BUFSIZ
, "warning: %s", fmt
);
118 compile_warn_print(file
, line
, buf
, args
);
122 /* rb_compile_warning() reports only in verbose mode */
124 rb_compile_warning(const char *file
, int line
, const char *fmt
, ...)
129 if (!RTEST(ruby_verbose
)) return;
131 snprintf(buf
, BUFSIZ
, "warning: %s", fmt
);
134 compile_warn_print(file
, line
, buf
, args
);
139 warn_print(const char *fmt
, va_list args
)
144 err_snprintf(buf
, BUFSIZ
, fmt
, args
);
147 rb_write_error2(buf
, len
);
151 rb_warn(const char *fmt
, ...)
156 if (NIL_P(ruby_verbose
)) return;
158 snprintf(buf
, BUFSIZ
, "warning: %s", fmt
);
161 warn_print(buf
, args
);
165 /* rb_warning() reports only in verbose mode */
167 rb_warning(const char *fmt
, ...)
172 if (!RTEST(ruby_verbose
)) return;
174 snprintf(buf
, BUFSIZ
, "warning: %s", fmt
);
177 warn_print(buf
, args
);
185 * Display the given message (followed by a newline) on STDERR unless
186 * warnings are disabled (for example with the <code>-W0</code> flag).
190 rb_warn_m(VALUE self
, VALUE mesg
)
192 if (!NIL_P(ruby_verbose
)) {
193 rb_io_write(rb_stderr
, mesg
);
194 rb_io_write(rb_stderr
, rb_default_rs
);
199 void rb_vm_bugreport(void);
202 report_bug(const char *file
, int line
, const char *fmt
, va_list args
)
206 int len
= err_position_0(buf
, BUFSIZ
, file
, line
);
208 if (fwrite(buf
, 1, len
, out
) == len
||
209 fwrite(buf
, 1, len
, (out
= stdout
)) == len
) {
210 fputs("[BUG] ", out
);
211 vfprintf(out
, fmt
, args
);
212 fprintf(out
, "\n%s\n\n", ruby_description
);
218 rb_bug(const char *fmt
, ...)
223 report_bug(rb_sourcefile(), rb_sourceline(), fmt
, args
);
230 rb_compile_bug(const char *file
, int line
, const char *fmt
, ...)
235 report_bug(file
, line
, fmt
, args
);
241 static const struct types
{
244 } builtin_types
[] = {
246 {T_OBJECT
, "Object"},
248 {T_ICLASS
, "iClass"}, /* internal use: mixed-in module holder */
249 {T_MODULE
, "Module"},
251 {T_STRING
, "String"},
252 {T_REGEXP
, "Regexp"},
254 {T_FIXNUM
, "Fixnum"},
256 {T_STRUCT
, "Struct"},
257 {T_BIGNUM
, "Bignum"},
259 {T_RATIONAL
,"Rational"},
260 {T_COMPLEX
, "Complex"},
263 {T_SYMBOL
, "Symbol"}, /* :symbol */
264 {T_DATA
, "Data"}, /* internal use: wrapped C pointers */
265 {T_MATCH
, "MatchData"}, /* data of $~ */
266 {T_NODE
, "Node"}, /* internal use: syntax tree node */
267 {T_UNDEF
, "undef"}, /* internal use: #undef; should not happen */
271 rb_check_type(VALUE x
, int t
)
273 const struct types
*type
= builtin_types
;
274 const struct types
*const typeend
= builtin_types
+
275 sizeof(builtin_types
) / sizeof(builtin_types
[0]);
278 rb_bug("undef leaked to the Ruby space");
282 while (type
< typeend
) {
283 if (type
->type
== t
) {
289 else if (FIXNUM_P(x
)) {
292 else if (SYMBOL_P(x
)) {
295 else if (rb_special_const_p(x
)) {
296 etype
= RSTRING_PTR(rb_obj_as_string(x
));
299 etype
= rb_obj_classname(x
);
301 rb_raise(rb_eTypeError
, "wrong argument type %s (expected %s)",
306 rb_bug("unknown type 0x%x (0x%x given)", t
, TYPE(x
));
310 /* exception classes */
314 VALUE rb_eSystemExit
;
318 VALUE rb_eStandardError
;
319 VALUE rb_eRuntimeError
;
322 VALUE rb_eIndexError
;
324 VALUE rb_eRangeError
;
326 VALUE rb_eNoMethodError
;
327 VALUE rb_eSecurityError
;
328 VALUE rb_eNotImpError
;
329 VALUE rb_eNoMemError
;
330 VALUE rb_cNameErrorMesg
;
332 VALUE rb_eScriptError
;
333 VALUE rb_eSyntaxError
;
336 VALUE rb_eSystemCallError
;
338 static VALUE rb_eNOERROR
;
341 rb_exc_new(VALUE etype
, const char *ptr
, long len
)
343 return rb_funcall(etype
, rb_intern("new"), 1, rb_str_new(ptr
, len
));
347 rb_exc_new2(VALUE etype
, const char *s
)
349 return rb_exc_new(etype
, s
, strlen(s
));
353 rb_exc_new3(VALUE etype
, VALUE str
)
356 return rb_funcall(etype
, rb_intern("new"), 1, str
);
361 * Exception.new(msg = nil) => exception
363 * Construct a new Exception object, optionally passing in
368 exc_initialize(int argc
, VALUE
*argv
, VALUE exc
)
372 rb_scan_args(argc
, argv
, "01", &arg
);
373 rb_iv_set(exc
, "mesg", arg
);
374 rb_iv_set(exc
, "bt", Qnil
);
380 * Document-method: exception
383 * exc.exception(string) -> an_exception or exc
385 * With no argument, or if the argument is the same as the receiver,
386 * return the receiver. Otherwise, create a new
387 * exception object of the same class as the receiver, but with a
388 * message equal to <code>string.to_str</code>.
393 exc_exception(int argc
, VALUE
*argv
, VALUE self
)
397 if (argc
== 0) return self
;
398 if (argc
== 1 && self
== argv
[0]) return self
;
399 exc
= rb_obj_clone(self
);
400 exc_initialize(argc
, argv
, exc
);
407 * exception.to_s => string
409 * Returns exception's message (or the name of the exception if
410 * no message is set).
416 VALUE mesg
= rb_attr_get(exc
, rb_intern("mesg"));
418 if (NIL_P(mesg
)) return rb_class_name(CLASS_OF(exc
));
419 if (OBJ_TAINTED(exc
)) OBJ_TAINT(mesg
);
425 * exception.message => string
427 * Returns the result of invoking <code>exception.to_s</code>.
428 * Normally this returns the exception's message or name. By
429 * supplying a to_str method, exceptions are agreeing to
430 * be used where Strings are expected.
434 exc_message(VALUE exc
)
436 return rb_funcall(exc
, rb_intern("to_s"), 0, 0);
441 * exception.inspect => string
443 * Return this exception's class name an message
447 exc_inspect(VALUE exc
)
451 klass
= CLASS_OF(exc
);
452 exc
= rb_obj_as_string(exc
);
453 if (RSTRING_LEN(exc
) == 0) {
454 return rb_str_dup(rb_class_name(klass
));
457 str
= rb_str_buf_new2("#<");
458 klass
= rb_class_name(klass
);
459 rb_str_buf_append(str
, klass
);
460 rb_str_buf_cat(str
, ": ", 2);
461 rb_str_buf_append(str
, exc
);
462 rb_str_buf_cat(str
, ">", 1);
469 * exception.backtrace => array
471 * Returns any backtrace associated with the exception. The backtrace
472 * is an array of strings, each containing either ``filename:lineNo: in
473 * `method''' or ``filename:lineNo.''
486 * print detail.backtrace.join("\n")
497 exc_backtrace(VALUE exc
)
501 if (!bt
) bt
= rb_intern("bt");
502 return rb_attr_get(exc
, bt
);
506 rb_check_backtrace(VALUE bt
)
509 static const char *err
= "backtrace must be Array of String";
514 if (t
== T_STRING
) return rb_ary_new3(1, bt
);
516 rb_raise(rb_eTypeError
, err
);
518 for (i
=0;i
<RARRAY_LEN(bt
);i
++) {
519 if (TYPE(RARRAY_PTR(bt
)[i
]) != T_STRING
) {
520 rb_raise(rb_eTypeError
, err
);
529 * exc.set_backtrace(array) => array
531 * Sets the backtrace information associated with <i>exc</i>. The
532 * argument must be an array of <code>String</code> objects in the
533 * format described in <code>Exception#backtrace</code>.
538 exc_set_backtrace(VALUE exc
, VALUE bt
)
540 return rb_iv_set(exc
, "bt", rb_check_backtrace(bt
));
545 * exc == obj => true or false
547 * Equality---If <i>obj</i> is not an <code>Exception</code>, returns
548 * <code>false</code>. Otherwise, returns <code>true</code> if <i>exc</i> and
549 * <i>obj</i> share same class, messages, and backtrace.
553 exc_equal(VALUE exc
, VALUE obj
)
555 ID id_mesg
= rb_intern("mesg");
557 if (exc
== obj
) return Qtrue
;
558 if (rb_obj_class(exc
) != rb_obj_class(obj
))
560 if (!rb_equal(rb_attr_get(exc
, id_mesg
), rb_attr_get(obj
, id_mesg
)))
562 if (!rb_equal(exc_backtrace(exc
), exc_backtrace(obj
)))
569 * SystemExit.new(status=0) => system_exit
571 * Create a new +SystemExit+ exception with the given status.
575 exit_initialize(int argc
, VALUE
*argv
, VALUE exc
)
577 VALUE status
= INT2FIX(EXIT_SUCCESS
);
578 if (argc
> 0 && FIXNUM_P(argv
[0])) {
582 rb_call_super(argc
, argv
);
583 rb_iv_set(exc
, "status", status
);
590 * system_exit.status => fixnum
592 * Return the status value associated with this system exit.
596 exit_status(VALUE exc
)
598 return rb_attr_get(exc
, rb_intern("status"));
604 * system_exit.success? => true or false
606 * Returns +true+ if exiting successful, +false+ if not.
610 exit_success_p(VALUE exc
)
612 VALUE status
= rb_attr_get(exc
, rb_intern("status"));
613 if (NIL_P(status
)) return Qtrue
;
614 if (status
== INT2FIX(EXIT_SUCCESS
)) return Qtrue
;
619 rb_name_error(ID id
, const char *fmt
, ...)
626 vsnprintf(buf
, BUFSIZ
, fmt
, args
);
629 argv
[0] = rb_str_new2(buf
);
630 argv
[1] = ID2SYM(id
);
631 exc
= rb_class_new_instance(2, argv
, rb_eNameError
);
637 * NameError.new(msg [, name]) => name_error
639 * Construct a new NameError exception. If given the <i>name</i>
640 * parameter may subsequently be examined using the <code>NameError.name</code>
645 name_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
649 name
= (argc
> 1) ? argv
[--argc
] : Qnil
;
650 rb_call_super(argc
, argv
);
651 rb_iv_set(self
, "name", name
);
657 * name_error.name => string or nil
659 * Return the name associated with this NameError exception.
663 name_err_name(VALUE self
)
665 return rb_attr_get(self
, rb_intern("name"));
670 * name_error.to_s => string
672 * Produce a nicely-formatted string representing the +NameError+.
676 name_err_to_s(VALUE exc
)
678 VALUE mesg
= rb_attr_get(exc
, rb_intern("mesg"));
681 if (NIL_P(mesg
)) return rb_class_name(CLASS_OF(exc
));
684 rb_iv_set(exc
, "mesg", mesg
= str
);
686 if (OBJ_TAINTED(exc
)) OBJ_TAINT(mesg
);
692 * NoMethodError.new(msg, name [, args]) => no_method_error
694 * Construct a NoMethodError exception for a method of the given name
695 * called with the given arguments. The name may be accessed using
696 * the <code>#name</code> method on the resulting object, and the
697 * arguments using the <code>#args</code> method.
701 nometh_err_initialize(int argc
, VALUE
*argv
, VALUE self
)
703 VALUE args
= (argc
> 2) ? argv
[--argc
] : Qnil
;
704 name_err_initialize(argc
, argv
, self
);
705 rb_iv_set(self
, "args", args
);
711 name_err_mesg_mark(VALUE
*ptr
)
713 rb_gc_mark_locations(ptr
, ptr
+3);
718 name_err_mesg_new(VALUE obj
, VALUE mesg
, VALUE recv
, VALUE method
)
720 VALUE
*ptr
= ALLOC_N(VALUE
, 3);
725 return Data_Wrap_Struct(rb_cNameErrorMesg
, name_err_mesg_mark
, -1, ptr
);
730 name_err_mesg_equal(VALUE obj1
, VALUE obj2
)
735 if (obj1
== obj2
) return Qtrue
;
736 if (rb_obj_class(obj2
) != rb_cNameErrorMesg
)
739 Data_Get_Struct(obj1
, VALUE
, ptr1
);
740 Data_Get_Struct(obj2
, VALUE
, ptr2
);
741 for (i
=0; i
<3; i
++) {
742 if (!rb_equal(ptr1
[i
], ptr2
[i
]))
750 name_err_mesg_to_str(VALUE obj
)
753 Data_Get_Struct(obj
, VALUE
, ptr
);
756 if (NIL_P(mesg
)) return Qnil
;
758 const char *desc
= 0;
759 VALUE d
= 0, args
[3];
773 d
= rb_protect(rb_inspect
, obj
, 0);
774 if (NIL_P(d
) || RSTRING_LEN(d
) > 65) {
775 d
= rb_any_to_s(obj
);
777 desc
= RSTRING_PTR(d
);
780 if (desc
&& desc
[0] != '#') {
781 d
= rb_str_new2(desc
);
783 rb_str_cat2(d
, rb_obj_classname(obj
));
788 mesg
= rb_f_sprintf(3, args
);
790 if (OBJ_TAINTED(obj
)) OBJ_TAINT(mesg
);
796 name_err_mesg_load(VALUE klass
, VALUE str
)
803 * no_method_error.args => obj
805 * Return the arguments passed in as the third parameter to
810 nometh_err_args(VALUE self
)
812 return rb_attr_get(self
, rb_intern("args"));
816 rb_invalid_str(const char *str
, const char *type
)
818 VALUE s
= rb_str_inspect(rb_str_new2(str
));
820 rb_raise(rb_eArgError
, "invalid value for %s: %s", type
, RSTRING_PTR(s
));
824 * Document-module: Errno
826 * Ruby exception objects are subclasses of <code>Exception</code>.
827 * However, operating systems typically report errors using plain
828 * integers. Module <code>Errno</code> is created dynamically to map
829 * these operating system errors to Ruby classes, with each error
830 * number generating its own subclass of <code>SystemCallError</code>.
831 * As the subclass is created in module <code>Errno</code>, its name
832 * will start <code>Errno::</code>.
834 * The names of the <code>Errno::</code> classes depend on
835 * the environment in which Ruby runs. On a typical Unix or Windows
836 * platform, there are <code>Errno</code> classes such as
837 * <code>Errno::EACCES</code>, <code>Errno::EAGAIN</code>,
838 * <code>Errno::EINTR</code>, and so on.
840 * The integer operating system error number corresponding to a
841 * particular error is available as the class constant
842 * <code>Errno::</code><em>error</em><code>::Errno</code>.
844 * Errno::EACCES::Errno #=> 13
845 * Errno::EAGAIN::Errno #=> 11
846 * Errno::EINTR::Errno #=> 4
848 * The full list of operating system errors on your particular platform
849 * are available as the constants of <code>Errno</code>.
851 * Errno.constants #=> :E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, ...
854 static st_table
*syserr_tbl
;
857 set_syserr(int n
, const char *name
)
861 if (!st_lookup(syserr_tbl
, n
, &error
)) {
862 error
= rb_define_class_under(rb_mErrno
, name
, rb_eSystemCallError
);
863 rb_define_const(error
, "Errno", INT2NUM(n
));
864 st_add_direct(syserr_tbl
, n
, error
);
867 rb_define_const(rb_mErrno
, name
, error
);
877 if (!st_lookup(syserr_tbl
, n
, &error
)) {
878 char name
[8]; /* some Windows' errno have 5 digits. */
880 snprintf(name
, sizeof(name
), "E%03d", n
);
881 error
= set_syserr(n
, name
);
888 * SystemCallError.new(msg, errno) => system_call_error_subclass
890 * If _errno_ corresponds to a known system error code, constructs
891 * the appropriate <code>Errno</code> class for that error, otherwise
892 * constructs a generic <code>SystemCallError</code> object. The
893 * error number is subsequently available via the <code>errno</code>
898 syserr_initialize(int argc
, VALUE
*argv
, VALUE self
)
900 #if !defined(_WIN32) && !defined(__VMS)
905 VALUE klass
= rb_obj_class(self
);
907 if (klass
== rb_eSystemCallError
) {
908 rb_scan_args(argc
, argv
, "11", &mesg
, &error
);
909 if (argc
== 1 && FIXNUM_P(mesg
)) {
910 error
= mesg
; mesg
= Qnil
;
912 if (!NIL_P(error
) && st_lookup(syserr_tbl
, NUM2LONG(error
), &klass
)) {
914 if (TYPE(self
) != T_OBJECT
) { /* insurance to avoid type crash */
915 rb_raise(rb_eTypeError
, "invalid instance type");
917 RBASIC(self
)->klass
= klass
;
921 rb_scan_args(argc
, argv
, "01", &mesg
);
922 error
= rb_const_get(klass
, rb_intern("Errno"));
924 if (!NIL_P(error
)) err
= strerror(NUM2LONG(error
));
925 else err
= "unknown error";
930 mesg
= rb_sprintf("%s - %.*s", err
,
931 (int)RSTRING_LEN(str
), RSTRING_PTR(str
));
934 mesg
= rb_str_new2(err
);
936 rb_call_super(1, &mesg
);
937 rb_iv_set(self
, "errno", error
);
943 * system_call_error.errno => fixnum
945 * Return this SystemCallError's error number.
949 syserr_errno(VALUE self
)
951 return rb_attr_get(self
, rb_intern("errno"));
956 * system_call_error === other => true or false
958 * Return +true+ if the receiver is a generic +SystemCallError+, or
959 * if the error numbers _self_ and _other_ are the same.
963 syserr_eqq(VALUE self
, VALUE exc
)
967 if (!rb_obj_is_kind_of(exc
, rb_eSystemCallError
)) return Qfalse
;
968 if (self
== rb_eSystemCallError
) return Qtrue
;
970 num
= rb_attr_get(exc
, rb_intern("errno"));
972 VALUE klass
= CLASS_OF(exc
);
974 while (TYPE(klass
) == T_ICLASS
|| FL_TEST(klass
, FL_SINGLETON
)) {
975 klass
= (VALUE
)RCLASS_SUPER(klass
);
977 num
= rb_const_get(klass
, rb_intern("Errno"));
979 e
= rb_const_get(self
, rb_intern("Errno"));
980 if (FIXNUM_P(num
) ? num
== e
: rb_equal(num
, e
))
987 * Errno.const_missing => SystemCallError
989 * Returns default SystemCallError class.
992 errno_missing(VALUE self
, VALUE id
)
998 * Descendants of class <code>Exception</code> are used to communicate
999 * between <code>raise</code> methods and <code>rescue</code>
1000 * statements in <code>begin/end</code> blocks. <code>Exception</code>
1001 * objects carry information about the exception---its type (the
1002 * exception's class name), an optional descriptive string, and
1003 * optional traceback information. Programs may subclass
1004 * <code>Exception</code> to add additional information.
1008 Init_Exception(void)
1010 rb_eException
= rb_define_class("Exception", rb_cObject
);
1011 rb_define_singleton_method(rb_eException
, "exception", rb_class_new_instance
, -1);
1012 rb_define_method(rb_eException
, "exception", exc_exception
, -1);
1013 rb_define_method(rb_eException
, "initialize", exc_initialize
, -1);
1014 rb_define_method(rb_eException
, "==", exc_equal
, 1);
1015 rb_define_method(rb_eException
, "to_s", exc_to_s
, 0);
1016 rb_define_method(rb_eException
, "message", exc_message
, 0);
1017 rb_define_method(rb_eException
, "inspect", exc_inspect
, 0);
1018 rb_define_method(rb_eException
, "backtrace", exc_backtrace
, 0);
1019 rb_define_method(rb_eException
, "set_backtrace", exc_set_backtrace
, 1);
1021 rb_eSystemExit
= rb_define_class("SystemExit", rb_eException
);
1022 rb_define_method(rb_eSystemExit
, "initialize", exit_initialize
, -1);
1023 rb_define_method(rb_eSystemExit
, "status", exit_status
, 0);
1024 rb_define_method(rb_eSystemExit
, "success?", exit_success_p
, 0);
1026 rb_eFatal
= rb_define_class("fatal", rb_eException
);
1027 rb_eSignal
= rb_define_class("SignalException", rb_eException
);
1028 rb_eInterrupt
= rb_define_class("Interrupt", rb_eSignal
);
1030 rb_eStandardError
= rb_define_class("StandardError", rb_eException
);
1031 rb_eTypeError
= rb_define_class("TypeError", rb_eStandardError
);
1032 rb_eArgError
= rb_define_class("ArgumentError", rb_eStandardError
);
1033 rb_eIndexError
= rb_define_class("IndexError", rb_eStandardError
);
1034 rb_eKeyError
= rb_define_class("KeyError", rb_eIndexError
);
1035 rb_eRangeError
= rb_define_class("RangeError", rb_eStandardError
);
1037 rb_eScriptError
= rb_define_class("ScriptError", rb_eException
);
1038 rb_eSyntaxError
= rb_define_class("SyntaxError", rb_eScriptError
);
1039 rb_eLoadError
= rb_define_class("LoadError", rb_eScriptError
);
1040 rb_eNotImpError
= rb_define_class("NotImplementedError", rb_eScriptError
);
1042 rb_eNameError
= rb_define_class("NameError", rb_eStandardError
);
1043 rb_define_method(rb_eNameError
, "initialize", name_err_initialize
, -1);
1044 rb_define_method(rb_eNameError
, "name", name_err_name
, 0);
1045 rb_define_method(rb_eNameError
, "to_s", name_err_to_s
, 0);
1046 rb_cNameErrorMesg
= rb_define_class_under(rb_eNameError
, "message", rb_cData
);
1047 rb_define_singleton_method(rb_cNameErrorMesg
, "!", name_err_mesg_new
, 3);
1048 rb_define_method(rb_cNameErrorMesg
, "==", name_err_mesg_equal
, 1);
1049 rb_define_method(rb_cNameErrorMesg
, "to_str", name_err_mesg_to_str
, 0);
1050 rb_define_method(rb_cNameErrorMesg
, "_dump", name_err_mesg_to_str
, 1);
1051 rb_define_singleton_method(rb_cNameErrorMesg
, "_load", name_err_mesg_load
, 1);
1052 rb_eNoMethodError
= rb_define_class("NoMethodError", rb_eNameError
);
1053 rb_define_method(rb_eNoMethodError
, "initialize", nometh_err_initialize
, -1);
1054 rb_define_method(rb_eNoMethodError
, "args", nometh_err_args
, 0);
1056 rb_eRuntimeError
= rb_define_class("RuntimeError", rb_eStandardError
);
1057 rb_eSecurityError
= rb_define_class("SecurityError", rb_eException
);
1058 rb_eNoMemError
= rb_define_class("NoMemoryError", rb_eException
);
1060 syserr_tbl
= st_init_numtable();
1061 rb_eSystemCallError
= rb_define_class("SystemCallError", rb_eStandardError
);
1062 rb_define_method(rb_eSystemCallError
, "initialize", syserr_initialize
, -1);
1063 rb_define_method(rb_eSystemCallError
, "errno", syserr_errno
, 0);
1064 rb_define_singleton_method(rb_eSystemCallError
, "===", syserr_eqq
, 1);
1066 rb_mErrno
= rb_define_module("Errno");
1067 rb_define_singleton_method(rb_mErrno
, "const_missing", errno_missing
, 1);
1069 rb_define_global_function("warn", rb_warn_m
, 1);
1073 rb_raise(VALUE exc
, const char *fmt
, ...)
1079 vsnprintf(buf
, BUFSIZ
, fmt
, args
);
1081 rb_exc_raise(rb_exc_new2(exc
, buf
));
1085 rb_loaderror(const char *fmt
, ...)
1090 va_start(args
, fmt
);
1091 vsnprintf(buf
, BUFSIZ
, fmt
, args
);
1093 rb_exc_raise(rb_exc_new2(rb_eLoadError
, buf
));
1097 rb_notimplement(void)
1099 rb_raise(rb_eNotImpError
,
1100 "%s() function is unimplemented on this machine",
1101 rb_id2name(rb_frame_this_func()));
1105 rb_fatal(const char *fmt
, ...)
1110 va_start(args
, fmt
);
1111 vsnprintf(buf
, BUFSIZ
, fmt
, args
);
1114 rb_exc_fatal(rb_exc_new2(rb_eFatal
, buf
));
1118 rb_sys_fail(const char *mesg
)
1125 rb_bug("rb_sys_fail(%s) - errno == 0", mesg
? mesg
: "");
1128 arg
= mesg
? rb_str_new2(mesg
) : Qnil
;
1129 rb_exc_raise(rb_class_new_instance(1, &arg
, get_syserr(n
)));
1133 rb_sys_warning(const char *fmt
, ...)
1141 if (!RTEST(ruby_verbose
)) return;
1143 snprintf(buf
, BUFSIZ
, "warning: %s", fmt
);
1144 snprintf(buf
+strlen(buf
), BUFSIZ
-strlen(buf
), ": %s", strerror(errno_save
));
1146 va_start(args
, fmt
);
1147 warn_print(buf
, args
);
1153 rb_load_fail(const char *path
)
1155 rb_loaderror("%s -- %s", strerror(errno
), path
);
1159 rb_error_frozen(const char *what
)
1161 rb_raise(rb_eRuntimeError
, "can't modify frozen %s", what
);
1165 rb_check_frozen(VALUE obj
)
1167 if (OBJ_FROZEN(obj
)) rb_error_frozen(rb_obj_classname(obj
));
1174 set_syserr(EPERM
, "EPERM");
1177 set_syserr(ENOENT
, "ENOENT");
1180 set_syserr(ESRCH
, "ESRCH");
1183 set_syserr(EINTR
, "EINTR");
1186 set_syserr(EIO
, "EIO");
1189 set_syserr(ENXIO
, "ENXIO");
1192 set_syserr(E2BIG
, "E2BIG");
1195 set_syserr(ENOEXEC
, "ENOEXEC");
1198 set_syserr(EBADF
, "EBADF");
1201 set_syserr(ECHILD
, "ECHILD");
1204 set_syserr(EAGAIN
, "EAGAIN");
1207 set_syserr(ENOMEM
, "ENOMEM");
1210 set_syserr(EACCES
, "EACCES");
1213 set_syserr(EFAULT
, "EFAULT");
1216 set_syserr(ENOTBLK
, "ENOTBLK");
1219 set_syserr(EBUSY
, "EBUSY");
1222 set_syserr(EEXIST
, "EEXIST");
1225 set_syserr(EXDEV
, "EXDEV");
1228 set_syserr(ENODEV
, "ENODEV");
1231 set_syserr(ENOTDIR
, "ENOTDIR");
1234 set_syserr(EISDIR
, "EISDIR");
1237 set_syserr(EINVAL
, "EINVAL");
1240 set_syserr(ENFILE
, "ENFILE");
1243 set_syserr(EMFILE
, "EMFILE");
1246 set_syserr(ENOTTY
, "ENOTTY");
1249 set_syserr(ETXTBSY
, "ETXTBSY");
1252 set_syserr(EFBIG
, "EFBIG");
1255 set_syserr(ENOSPC
, "ENOSPC");
1258 set_syserr(ESPIPE
, "ESPIPE");
1261 set_syserr(EROFS
, "EROFS");
1264 set_syserr(EMLINK
, "EMLINK");
1267 set_syserr(EPIPE
, "EPIPE");
1270 set_syserr(EDOM
, "EDOM");
1273 set_syserr(ERANGE
, "ERANGE");
1276 set_syserr(EDEADLK
, "EDEADLK");
1279 set_syserr(ENAMETOOLONG
, "ENAMETOOLONG");
1282 set_syserr(ENOLCK
, "ENOLCK");
1285 set_syserr(ENOSYS
, "ENOSYS");
1288 set_syserr(ENOTEMPTY
, "ENOTEMPTY");
1291 set_syserr(ELOOP
, "ELOOP");
1294 set_syserr(EWOULDBLOCK
, "EWOULDBLOCK");
1297 set_syserr(ENOMSG
, "ENOMSG");
1300 set_syserr(EIDRM
, "EIDRM");
1303 set_syserr(ECHRNG
, "ECHRNG");
1306 set_syserr(EL2NSYNC
, "EL2NSYNC");
1309 set_syserr(EL3HLT
, "EL3HLT");
1312 set_syserr(EL3RST
, "EL3RST");
1315 set_syserr(ELNRNG
, "ELNRNG");
1318 set_syserr(EUNATCH
, "EUNATCH");
1321 set_syserr(ENOCSI
, "ENOCSI");
1324 set_syserr(EL2HLT
, "EL2HLT");
1327 set_syserr(EBADE
, "EBADE");
1330 set_syserr(EBADR
, "EBADR");
1333 set_syserr(EXFULL
, "EXFULL");
1336 set_syserr(ENOANO
, "ENOANO");
1339 set_syserr(EBADRQC
, "EBADRQC");
1342 set_syserr(EBADSLT
, "EBADSLT");
1345 set_syserr(EDEADLOCK
, "EDEADLOCK");
1348 set_syserr(EBFONT
, "EBFONT");
1351 set_syserr(ENOSTR
, "ENOSTR");
1354 set_syserr(ENODATA
, "ENODATA");
1357 set_syserr(ETIME
, "ETIME");
1360 set_syserr(ENOSR
, "ENOSR");
1363 set_syserr(ENONET
, "ENONET");
1366 set_syserr(ENOPKG
, "ENOPKG");
1369 set_syserr(EREMOTE
, "EREMOTE");
1372 set_syserr(ENOLINK
, "ENOLINK");
1375 set_syserr(EADV
, "EADV");
1378 set_syserr(ESRMNT
, "ESRMNT");
1381 set_syserr(ECOMM
, "ECOMM");
1384 set_syserr(EPROTO
, "EPROTO");
1387 set_syserr(EMULTIHOP
, "EMULTIHOP");
1390 set_syserr(EDOTDOT
, "EDOTDOT");
1393 set_syserr(EBADMSG
, "EBADMSG");
1396 set_syserr(EOVERFLOW
, "EOVERFLOW");
1399 set_syserr(ENOTUNIQ
, "ENOTUNIQ");
1402 set_syserr(EBADFD
, "EBADFD");
1405 set_syserr(EREMCHG
, "EREMCHG");
1408 set_syserr(ELIBACC
, "ELIBACC");
1411 set_syserr(ELIBBAD
, "ELIBBAD");
1414 set_syserr(ELIBSCN
, "ELIBSCN");
1417 set_syserr(ELIBMAX
, "ELIBMAX");
1420 set_syserr(ELIBEXEC
, "ELIBEXEC");
1423 set_syserr(EILSEQ
, "EILSEQ");
1426 set_syserr(ERESTART
, "ERESTART");
1429 set_syserr(ESTRPIPE
, "ESTRPIPE");
1432 set_syserr(EUSERS
, "EUSERS");
1435 set_syserr(ENOTSOCK
, "ENOTSOCK");
1438 set_syserr(EDESTADDRREQ
, "EDESTADDRREQ");
1441 set_syserr(EMSGSIZE
, "EMSGSIZE");
1444 set_syserr(EPROTOTYPE
, "EPROTOTYPE");
1447 set_syserr(ENOPROTOOPT
, "ENOPROTOOPT");
1449 #ifdef EPROTONOSUPPORT
1450 set_syserr(EPROTONOSUPPORT
, "EPROTONOSUPPORT");
1452 #ifdef ESOCKTNOSUPPORT
1453 set_syserr(ESOCKTNOSUPPORT
, "ESOCKTNOSUPPORT");
1456 set_syserr(EOPNOTSUPP
, "EOPNOTSUPP");
1459 set_syserr(EPFNOSUPPORT
, "EPFNOSUPPORT");
1462 set_syserr(EAFNOSUPPORT
, "EAFNOSUPPORT");
1465 set_syserr(EADDRINUSE
, "EADDRINUSE");
1467 #ifdef EADDRNOTAVAIL
1468 set_syserr(EADDRNOTAVAIL
, "EADDRNOTAVAIL");
1471 set_syserr(ENETDOWN
, "ENETDOWN");
1474 set_syserr(ENETUNREACH
, "ENETUNREACH");
1477 set_syserr(ENETRESET
, "ENETRESET");
1480 set_syserr(ECONNABORTED
, "ECONNABORTED");
1483 set_syserr(ECONNRESET
, "ECONNRESET");
1486 set_syserr(ENOBUFS
, "ENOBUFS");
1489 set_syserr(EISCONN
, "EISCONN");
1492 set_syserr(ENOTCONN
, "ENOTCONN");
1495 set_syserr(ESHUTDOWN
, "ESHUTDOWN");
1498 set_syserr(ETOOMANYREFS
, "ETOOMANYREFS");
1501 set_syserr(ETIMEDOUT
, "ETIMEDOUT");
1504 set_syserr(ECONNREFUSED
, "ECONNREFUSED");
1507 set_syserr(EHOSTDOWN
, "EHOSTDOWN");
1510 set_syserr(EHOSTUNREACH
, "EHOSTUNREACH");
1513 set_syserr(EALREADY
, "EALREADY");
1516 set_syserr(EINPROGRESS
, "EINPROGRESS");
1519 set_syserr(ESTALE
, "ESTALE");
1522 set_syserr(EUCLEAN
, "EUCLEAN");
1525 set_syserr(ENOTNAM
, "ENOTNAM");
1528 set_syserr(ENAVAIL
, "ENAVAIL");
1531 set_syserr(EISNAM
, "EISNAM");
1534 set_syserr(EREMOTEIO
, "EREMOTEIO");
1537 set_syserr(EDQUOT
, "EDQUOT");
1539 rb_eNOERROR
= set_syserr(0, "NOERROR");
1543 err_append(const char *s
)
1545 rb_thread_t
*th
= GET_THREAD();
1546 VALUE err
= th
->errinfo
;
1548 if (th
->parse_in_eval
) {
1550 err
= rb_exc_new2(rb_eSyntaxError
, s
);
1554 VALUE str
= rb_obj_as_string(err
);
1556 rb_str_cat2(str
, "\n");
1557 rb_str_cat2(str
, s
);
1558 th
->errinfo
= rb_exc_new3(rb_eSyntaxError
, str
);
1563 err
= rb_exc_new2(rb_eSyntaxError
, "compile error");
1567 rb_write_error("\n");