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

ruby-changes:44194

From: nobu <ko1@a...>
Date: Tue, 27 Sep 2016 17:35:36 +0900 (JST)
Subject: [ruby-changes:44194] nobu:r56268 (trunk): iseq.c: syntax error in compile_file

nobu	2016-09-27 17:35:31 +0900 (Tue, 27 Sep 2016)

  New Revision: 56268

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56268

  Log:
    iseq.c: syntax error in compile_file
    
    * iseq.c (iseqw_s_compile_file): deal with syntax error as well as
      compile, and should not abort when rescued.

  Modified files:
    trunk/ChangeLog
    trunk/iseq.c
    trunk/test/ruby/test_iseq.rb
Index: test/ruby/test_iseq.rb
===================================================================
--- test/ruby/test_iseq.rb	(revision 56267)
+++ test/ruby/test_iseq.rb	(revision 56268)
@@ -1,4 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/test/ruby/test_iseq.rb#L1
 require 'test/unit'
+require 'tempfile'
 
 class TestISeq < Test::Unit::TestCase
   ISeq = RubyVM::InstructionSequence
@@ -243,6 +244,23 @@ class TestISeq < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_iseq.rb#L244
     assert_send([e2, :start_with?, __FILE__])
   end
 
+  def test_compile_file_error
+    Tempfile.create(%w"test_iseq .rb") do |f|
+      f.puts "end"
+      f.close
+      path = f.path
+      assert_in_out_err(%W[- #{path}], "#{<<-"begin;"}\n#{<<-"end;"}", /keyword_end/, [], success: true)
+      begin;
+        path = ARGV[0]
+        begin
+          RubyVM::InstructionSequence.compile_file(path)
+        rescue SyntaxError => e
+          puts e.message
+        end
+      end;
+    end
+  end
+
   def test_translate_by_object
     assert_separately([], <<-"end;")
       class Object
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 56267)
+++ ChangeLog	(revision 56268)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Sep 27 17:35:28 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* iseq.c (iseqw_s_compile_file): deal with syntax error as well as
+	  compile, and should not abort when rescued.
+
 Tue Sep 27 12:07:17 2016  NARUSE, Yui  <naruse@r...>
 
 	* lib/cgi/cookie.rb (parse): don't allow , as a separator. [Bug #12791]
Index: iseq.c
===================================================================
--- iseq.c	(revision 56267)
+++ iseq.c	(revision 56268)
@@ -823,8 +823,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/iseq.c#L823
 iseqw_s_compile_file(int argc, VALUE *argv, VALUE self)
 {
     VALUE file, line = INT2FIX(1), opt = Qnil;
-    VALUE parser;
-    VALUE f;
+    VALUE parser, f, exc = Qnil;
     NODE *node;
     rb_compile_option_t option;
     int i;
@@ -841,9 +840,12 @@ iseqw_s_compile_file(int argc, VALUE *ar https://github.com/ruby/ruby/blob/trunk/iseq.c#L840
     f = rb_file_open_str(file, "r");
 
     parser = rb_parser_new();
+    rb_parser_set_context(parser, NULL, FALSE);
     node = rb_parser_compile_file_path(parser, file, f, NUM2INT(line));
+    if (!node) exc = GET_THREAD()->errinfo;
 
     rb_io_close(f);
+    if (!node) rb_exc_raise(exc);
 
     make_compile_option(&option, opt);
 

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

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