[前][次][番号順一覧][スレッド一覧]

ruby-changes:22889

From: tenderlove <ko1@a...>
Date: Wed, 7 Mar 2012 08:38:47 +0900 (JST)
Subject: [ruby-changes:22889] tenderlove:r34938 (trunk): * error.c (rb_loaderror_with_path): Adding the missing file as an

tenderlove	2012-03-07 08:38:33 +0900 (Wed, 07 Mar 2012)

  New Revision: 34938

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=34938

  Log:
    * error.c (rb_loaderror_with_path): Adding the missing file as an
      instance variable to the LoadError exception.
    
    * load.c: call rb_loaderror_with_path so that the missing path is
      added to the exception.
    
    * ruby.c: call rb_loaderror rather than raising our own LoadError
      exception.
    
    * include/ruby/intern.h: add declaration for rb_loaderror_with_path.
    
    * test/ruby/test_require.rb: add supporting test for LoadError#path
      method.

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/error.c
    trunk/include/ruby/intern.h
    trunk/load.c
    trunk/ruby.c
    trunk/test/ruby/test_require.rb

Index: include/ruby/intern.h
===================================================================
--- include/ruby/intern.h	(revision 34937)
+++ include/ruby/intern.h	(revision 34938)
@@ -209,6 +209,7 @@
 VALUE rb_exc_new2(VALUE, const char*);
 VALUE rb_exc_new3(VALUE, VALUE);
 PRINTF_ARGS(NORETURN(void rb_loaderror(const char*, ...)), 1, 2);
+PRINTF_ARGS(NORETURN(void rb_loaderror_with_path(VALUE path, const char*, ...)), 2, 3);
 PRINTF_ARGS(NORETURN(void rb_name_error(ID, const char*, ...)), 2, 3);
 PRINTF_ARGS(NORETURN(void rb_name_error_str(VALUE, const char*, ...)), 2, 3);
 NORETURN(void rb_invalid_str(const char*, const char*));
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 34937)
+++ ChangeLog	(revision 34938)
@@ -1,3 +1,19 @@
+Wed Mar  7 08:32:43 2012  Aaron Patterson <aaron@t...>
+
+	* error.c (rb_loaderror_with_path): Adding the missing file as an
+	  instance variable to the LoadError exception.
+	
+	* load.c: call rb_loaderror_with_path so that the missing path is
+	  added to the exception.
+	
+	* ruby.c: call rb_loaderror rather than raising our own LoadError
+	  exception.
+
+	* include/ruby/intern.h: add declaration for rb_loaderror_with_path.
+
+	* test/ruby/test_require.rb: add supporting test for LoadError#path
+	  method.
+
 Wed Mar  7 08:28:00 2012  Aaron Patterson <aaron@t...>
 
 	* lib/xmlrpc/parser.rb: support i8 types. Thanks Stas Kelvich!
Index: load.c
===================================================================
--- load.c	(revision 34937)
+++ load.c	(revision 34938)
@@ -495,7 +495,7 @@
 {
     VALUE base = rb_current_realfilepath();
     if (NIL_P(base)) {
-	rb_raise(rb_eLoadError, "cannot infer basepath");
+	rb_loaderror("cannot infer basepath");
     }
     base = rb_file_dirname(base);
     return rb_require_safe(rb_file_absolute_path(fname, base), rb_safe_level());
@@ -588,12 +588,13 @@
     return type ? 's' : 'r';
 }
 
+void rb_loaderror_with_path(VALUE path, const char *fmt, ...);
+
 static void
 load_failed(VALUE fname)
 {
-    VALUE mesg = rb_str_buf_new_cstr("cannot load such file -- ");
-    rb_str_append(mesg, fname);	/* should be ASCII compatible */
-    rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg));
+    rb_loaderror_with_path(fname, "cannot load such file -- %s", RSTRING_PTR(fname));
+    RB_GC_GUARD(fname);
 }
 
 static VALUE
Index: error.c
===================================================================
--- error.c	(revision 34937)
+++ error.c	(revision 34938)
@@ -1691,7 +1691,10 @@
 
     rb_eScriptError = rb_define_class("ScriptError", rb_eException);
     rb_eSyntaxError = rb_define_class("SyntaxError", rb_eScriptError);
+
     rb_eLoadError   = rb_define_class("LoadError", rb_eScriptError);
+    rb_attr(rb_eLoadError, rb_intern("path"), 1, 0, Qfalse);
+
     rb_eNotImpError = rb_define_class("NotImplementedError", rb_eScriptError);
 
     rb_eNameError     = rb_define_class("NameError", rb_eStandardError);
@@ -1742,14 +1745,32 @@
 {
     va_list args;
     VALUE mesg;
+    VALUE err;
 
     va_start(args, fmt);
     mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
     va_end(args);
-    rb_exc_raise(rb_exc_new3(rb_eLoadError, mesg));
+    err = rb_exc_new3(rb_eLoadError, mesg);
+    rb_ivar_set(err, rb_intern("@path"), Qnil);
+    rb_exc_raise(err);
 }
 
 void
+rb_loaderror_with_path(VALUE path, const char *fmt, ...)
+{
+    va_list args;
+    VALUE mesg;
+    VALUE err;
+
+    va_start(args, fmt);
+    mesg = rb_enc_vsprintf(rb_locale_encoding(), fmt, args);
+    va_end(args);
+    err = rb_exc_new3(rb_eLoadError, mesg);
+    rb_ivar_set(err, rb_intern("@path"), path);
+    rb_exc_raise(err);
+}
+
+void
 rb_notimplement(void)
 {
     rb_raise(rb_eNotImpError,
@@ -1889,7 +1910,7 @@
 void
 rb_load_fail(const char *path)
 {
-    rb_loaderror("%s -- %s", strerror(errno), path);
+    rb_loaderror_with_path(rb_str_new2(path), "%s -- %s", strerror(errno), path);
 }
 
 void
Index: NEWS
===================================================================
--- NEWS	(revision 34937)
+++ NEWS	(revision 34938)
@@ -28,6 +28,11 @@
       * respond_to? against a protected method now returns false unless
         the second argument is true.
 
+  * LoadError
+    * added method:
+      * added LoadError#path method to return the file name that could not be
+        loaded.
+
   * Signal
     * incompatible changes:
       * Signal.trap raises ArgumentError when :SEGV, :BUS, :ILL, :FPE, :VTALRM
Index: ruby.c
===================================================================
--- ruby.c	(revision 34937)
+++ ruby.c	(revision 34938)
@@ -1560,7 +1560,7 @@
 		    }
 		}
 	    }
-	    rb_raise(rb_eLoadError, "no Ruby script found in input");
+	    rb_loaderror("no Ruby script found in input");
 	}
 
 	c = rb_io_getbyte(f);
Index: test/ruby/test_require.rb
===================================================================
--- test/ruby/test_require.rb	(revision 34937)
+++ test/ruby/test_require.rb	(revision 34938)
@@ -5,6 +5,14 @@
 require 'tmpdir'
 
 class TestRequire < Test::Unit::TestCase
+  def test_load_error_path
+    filename = "should_not_exist"
+    error = assert_raises(LoadError) do
+      require filename
+    end
+    assert_equal filename, error.path
+  end
+
   def test_require_invalid_shared_object
     t = Tempfile.new(["test_ruby_test_require", ".so"])
     t.puts "dummy"

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]