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

ruby-changes:18602

From: tenderlove <ko1@a...>
Date: Sat, 22 Jan 2011 10:33:08 +0900 (JST)
Subject: [ruby-changes:18602] Ruby:r30626 (trunk): * ext/psych/parser.c (parse): add the file name to the exception when

tenderlove	2011-01-22 10:26:40 +0900 (Sat, 22 Jan 2011)

  New Revision: 30626

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

  Log:
    * ext/psych/parser.c (parse): add the file name to the exception when
      parse errors occur.
    * test/psych/test_parser.rb: test for parse error file name

  Modified files:
    trunk/ChangeLog
    trunk/ext/psych/parser.c
    trunk/test/psych/test_parser.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 30625)
+++ ChangeLog	(revision 30626)
@@ -1,3 +1,10 @@
+Sat Jan 22 10:25:19 2011  Aaron Patterson <aaron@t...>
+
+	* ext/psych/parser.c (parse): add the file name to the exception when
+	  parse errors occur.
+
+	* test/psych/test_parser.rb: test for parse error file name
+
 Sat Jan 22 10:12:30 2011  Aaron Patterson <aaron@t...>
 
 	* ext/psych/parser.c (parse): fix assertion error when reusing a
Index: ext/psych/parser.c
===================================================================
--- ext/psych/parser.c	(revision 30625)
+++ ext/psych/parser.c	(revision 30626)
@@ -4,6 +4,7 @@
 VALUE ePsychSyntaxError;
 
 static ID id_read;
+static ID id_path;
 static ID id_empty;
 static ID id_start_stream;
 static ID id_end_stream;
@@ -93,13 +94,20 @@
 
     while(!done) {
 	if(!yaml_parser_parse(parser, &event)) {
+	    VALUE path;
 	    size_t line   = parser->mark.line;
 	    size_t column = parser->mark.column;
 
+	    if(rb_respond_to(yaml, id_path))
+		path = rb_funcall(yaml, id_path, 0);
+	    else
+		path = rb_str_new2("<unknown>");
+
 	    yaml_parser_delete(parser);
 	    yaml_parser_initialize(parser);
 
-	    rb_raise(ePsychSyntaxError, "couldn't parse YAML at line %d column %d",
+	    rb_raise(ePsychSyntaxError, "(%s): couldn't parse YAML at line %d column %d",
+		    StringValuePtr(path),
 		    (int)line, (int)column);
 	}
 
@@ -360,6 +368,7 @@
     rb_define_method(cPsychParser, "external_encoding=", set_external_encoding, 1);
 
     id_read           = rb_intern("read");
+    id_path           = rb_intern("path");
     id_empty          = rb_intern("empty");
     id_start_stream   = rb_intern("start_stream");
     id_end_stream     = rb_intern("end_stream");
Index: test/psych/test_parser.rb
===================================================================
--- test/psych/test_parser.rb	(revision 30625)
+++ test/psych/test_parser.rb	(revision 30626)
@@ -138,6 +138,23 @@
       end
     end
 
+    def test_syntax_error_has_path_for_string
+      e = assert_raises(Psych::SyntaxError) do
+        @parser.parse("---\n\"foo\"\n\"bar\"\n")
+      end
+      assert_match '(<unknown>):', e.message
+    end
+
+    def test_syntax_error_has_path_for_io
+      io = StringIO.new "---\n\"foo\"\n\"bar\"\n"
+      def io.path; "hello!"; end
+
+      e = assert_raises(Psych::SyntaxError) do
+        @parser.parse(io)
+      end
+      assert_match "(#{io.path}):", e.message
+    end
+
     def test_mapping_end
       @parser.parse("---\n!!map { key: value }")
       assert_called :end_mapping

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

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