From f04dab114e5f0663ae6b940bacab9cd0e2620301 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 27 Sep 2016 08:35:31 +0000 Subject: [PATCH] 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56268 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ iseq.c | 5 +++-- test/ruby/test_iseq.rb | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index afde8df..d242bd6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Sep 27 17:35:28 2016 Nobuyoshi Nakada + + * iseq.c (iseqw_s_compile_file): deal with syntax error as well as + compile, and should not abort when rescued. + Tue Nov 15 03:14:02 2016 NARUSE, Yui * ext/-test/file/fs.c (get_atime_p): Updating of file access times diff --git a/iseq.c b/iseq.c index e679108..797473e 100644 --- a/iseq.c +++ b/iseq.c @@ -812,8 +812,7 @@ static VALUE 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; const char *fname; rb_compile_option_t option; @@ -827,8 +826,10 @@ iseqw_s_compile_file(int argc, VALUE *argv, VALUE self) parser = rb_parser_new(); node = rb_parser_compile_file(parser, fname, f, NUM2INT(line)); + if (!node) exc = GET_THREAD()->errinfo; rb_io_close(f); + if (!node) rb_exc_raise(exc); make_compile_option(&option, opt); diff --git a/test/ruby/test_iseq.rb b/test/ruby/test_iseq.rb index 4561eeb..7a8f170 100644 --- a/test/ruby/test_iseq.rb +++ b/test/ruby/test_iseq.rb @@ -1,4 +1,5 @@ require 'test/unit' +require 'tempfile' class TestISeq < Test::Unit::TestCase ISeq = RubyVM::InstructionSequence @@ -212,4 +213,21 @@ class Segfault at_exit { assert_equal([:n, :x], Segfault.new.segfault.sort) } end; 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;"}", /compile error/, /keyword_end/) + begin; + path = ARGV[0] + begin + RubyVM::InstructionSequence.compile_file(path) + rescue SyntaxError => e + puts e.message + end + end; + end + end end -- 2.10.1