@@ -331,7 +331,7 @@ static VALUE
331
331
exc_to_s (exc )
332
332
VALUE exc ;
333
333
{
334
- VALUE mesg = rb_iv_get (exc , "mesg" );
334
+ VALUE mesg = rb_attr_get (exc , rb_intern ( "mesg" ) );
335
335
336
336
if (NIL_P (mesg )) return rb_class_path (CLASS_OF (exc ));
337
337
if (OBJ_TAINTED (exc )) OBJ_TAINT (mesg );
@@ -421,7 +421,7 @@ static VALUE
421
421
exit_status (exc )
422
422
VALUE exc ;
423
423
{
424
- return rb_iv_get (exc , "status" );
424
+ return rb_attr_get (exc , rb_intern ( "status" ) );
425
425
}
426
426
427
427
void
@@ -434,31 +434,56 @@ rb_name_error(id, fmt, va_alist)
434
434
va_dcl
435
435
#endif
436
436
{
437
- VALUE exc ;
438
-
437
+ VALUE exc , argv [2 ];
439
438
va_list args ;
440
439
char buf [BUFSIZ ];
441
440
442
441
va_init_list (args , fmt );
443
442
vsnprintf (buf , BUFSIZ , fmt , args );
444
443
va_end (args );
445
- exc = rb_exc_new2 (rb_eNameError , buf );
446
- rb_iv_set (exc , "name" , ID2SYM (id ));
444
+
445
+ argv [0 ] = rb_str_new2 (buf );
446
+ argv [1 ] = ID2SYM (id );
447
+ exc = rb_class_new_instance (2 , argv , rb_eNameError );
447
448
rb_exc_raise (exc );
448
449
}
449
450
451
+ static VALUE
452
+ name_err_initialize (argc , argv , self )
453
+ int argc ;
454
+ VALUE * argv ;
455
+ VALUE self ;
456
+ {
457
+ VALUE name = (argc > 1 ) ? argv [-- argc ] : Qnil ;
458
+ exc_initialize (argc , argv , self );
459
+ rb_iv_set (self , "name" , name );
460
+ return self ;
461
+ }
462
+
450
463
static VALUE
451
464
name_err_name (self )
452
465
VALUE self ;
453
466
{
454
- return rb_iv_get (self , "name" );
467
+ return rb_attr_get (self , rb_intern ("name" ));
468
+ }
469
+
470
+ static VALUE
471
+ nometh_err_initialize (argc , argv , self )
472
+ int argc ;
473
+ VALUE * argv ;
474
+ VALUE self ;
475
+ {
476
+ VALUE args = (argc > 2 ) ? argv [-- argc ] : Qnil ;
477
+ name_err_initialize (argc , argv , self );
478
+ rb_iv_set (self , "args" , args );
479
+ return self ;
455
480
}
456
481
457
482
static VALUE
458
483
nometh_err_args (self )
459
484
VALUE self ;
460
485
{
461
- return rb_iv_get (self , "args" );
486
+ return rb_attr_get (self , rb_intern ( "args" ) );
462
487
}
463
488
464
489
void
@@ -505,11 +530,45 @@ get_syserr(n)
505
530
return error ;
506
531
}
507
532
533
+ static VALUE
534
+ syserr_initialize (argc , argv , self )
535
+ int argc ;
536
+ VALUE * argv ;
537
+ VALUE self ;
538
+ {
539
+ #if !defined(_WIN32 ) && !defined(__VMS )
540
+ char * strerror ();
541
+ #endif
542
+ char * err ;
543
+ char * buf ;
544
+ VALUE error , mesg ;
545
+
546
+ if (rb_scan_args (argc , argv , "11" , & mesg , & error ) == 1 && FIXNUM_P (mesg )) {
547
+ error = mesg ;
548
+ mesg = Qnil ;
549
+ }
550
+ err = strerror (NUM2LONG (error ));
551
+ if (!err ) err = "Unknown error" ;
552
+ if (RTEST (mesg )) {
553
+ StringValue (mesg );
554
+ buf = ALLOCA_N (char , strlen (err )+ RSTRING (mesg )-> len + 4 );
555
+ sprintf (buf , "%s - %s" , err , RSTRING (mesg )-> ptr );
556
+ mesg = rb_str_new2 (buf );
557
+ }
558
+ else {
559
+ mesg = rb_str_new2 (err );
560
+ }
561
+
562
+ exc_initialize (1 , & mesg , self );
563
+ rb_iv_set (self , "errno" , error );
564
+ return self ;
565
+ }
566
+
508
567
static VALUE
509
568
syserr_errno (self )
510
569
VALUE self ;
511
570
{
512
- return rb_iv_get (self , "errno" );
571
+ return rb_attr_get (self , rb_intern ( "errno" ) );
513
572
}
514
573
515
574
static VALUE
@@ -521,7 +580,7 @@ syserr_eqq(self, exc)
521
580
if (!rb_obj_is_kind_of (exc , rb_eSystemCallError )) return Qfalse ;
522
581
if (self == rb_eSystemCallError ) return Qtrue ;
523
582
524
- num = rb_iv_get (exc , "errno" );
583
+ num = rb_attr_get (exc , rb_intern ( "errno" ) );
525
584
if (NIL_P (num )) {
526
585
VALUE klass = CLASS_OF (exc );
527
586
@@ -565,8 +624,10 @@ Init_Exception()
565
624
rb_eIndexError = rb_define_class ("IndexError" , rb_eStandardError );
566
625
rb_eRangeError = rb_define_class ("RangeError" , rb_eStandardError );
567
626
rb_eNameError = rb_define_class ("NameError" , rb_eStandardError );
627
+ rb_define_method (rb_eNameError , "initialize" , name_err_initialize , -1 );
568
628
rb_define_method (rb_eNameError , "name" , name_err_name , 0 );
569
629
rb_eNoMethodError = rb_define_class ("NoMethodError" , rb_eNameError );
630
+ rb_define_method (rb_eNoMethodError , "initialize" , nometh_err_initialize , -1 );
570
631
rb_define_method (rb_eNoMethodError , "args" , nometh_err_args , 0 );
571
632
572
633
rb_eScriptError = rb_define_class ("ScriptError" , rb_eException );
@@ -651,35 +712,18 @@ void
651
712
rb_sys_fail (mesg )
652
713
const char * mesg ;
653
714
{
654
- #if !defined(_WIN32 ) && !defined(__VMS )
655
- char * strerror ();
656
- #endif
657
- char * err ;
658
- char * buf ;
659
715
extern int errno ;
660
716
int n = errno ;
661
- VALUE ee ;
717
+ VALUE argv [ 2 ] ;
662
718
663
- if (errno == 0 ) {
719
+ errno = 0 ;
720
+ if (n == 0 ) {
664
721
rb_bug ("rb_sys_fail() - errno == 0" );
665
722
}
666
723
667
- err = strerror (errno );
668
- if (mesg ) {
669
- volatile VALUE tmp = rb_str_inspect (rb_str_new2 (mesg ));
670
-
671
- buf = ALLOCA_N (char , strlen (err )+ RSTRING (tmp )-> len + 4 );
672
- sprintf (buf , "%s - %s" , err , RSTRING (tmp )-> ptr );
673
- }
674
- else {
675
- buf = ALLOCA_N (char , strlen (err )+ 1 );
676
- strcpy (buf , err );
677
- }
678
-
679
- errno = 0 ;
680
- ee = rb_exc_new2 (get_syserr (n ), buf );
681
- rb_iv_set (ee , "errno" , INT2NUM (n ));
682
- rb_exc_raise (ee );
724
+ argv [0 ] = mesg ? rb_str_new2 (mesg ) : Qnil ;
725
+ argv [1 ] = INT2NUM (n );
726
+ rb_exc_raise (rb_class_new_instance (2 , argv , get_syserr (n )));
683
727
}
684
728
685
729
void
@@ -734,6 +778,7 @@ init_syserr()
734
778
{
735
779
syserr_tbl = st_init_numtable ();
736
780
rb_eSystemCallError = rb_define_class ("SystemCallError" , rb_eStandardError );
781
+ rb_define_method (rb_eSystemCallError , "initialize" , syserr_initialize , -1 );
737
782
rb_define_method (rb_eSystemCallError , "errno" , syserr_errno , 0 );
738
783
rb_define_singleton_method (rb_eSystemCallError , "===" , syserr_eqq , 1 );
739
784
0 commit comments