Skip to content
This repository

Various fixes for psych #267

Merged
merged 5 commits into from over 1 year ago

3 participants

Aaron Patterson Don't Add Me To Your Organization a.k.a The Travis Bot Charles Oliver Nutter
Aaron Patterson

This gets the Java extension closer to the C extension in the 1.9 stdlib.

added some commits July 18, 2012
Aaron Patterson pass the file name to the exception factory method 3cf6da2
Aaron Patterson make sure Psych::Syntax error is required, and raise that exception i…
…n the case of parse errors
e706229
Aaron Patterson Emitter#initialize takes optional formatting options
Manipulating the options after the emitter object is constructed will
have no impact on the emitter. If the emitter is constructed with the
options, we actually see change in the output (and fix tests).
c84f26a
Aaron Patterson set the exception message on allocated exceptions.
fixes test/psych/visitors/test_to_ruby.rb -n test_exception
fafdc7f
Aaron Patterson use the path from the IO if no path is provided.
fixes test/psych/test_parser.rb -n test_syntax_error_has_path_for_io
644bdaa
Don't Add Me To Your Organization a.k.a The Travis Bot

This pull request passes (merged 644bdaa into 82eb396).

Charles Oliver Nutter headius merged commit 1cbf69f into from August 21, 2012
Charles Oliver Nutter headius closed this August 21, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 5 unique commits by 1 author.

Aug 21, 2012
Aaron Patterson pass the file name to the exception factory method 3cf6da2
Aaron Patterson make sure Psych::Syntax error is required, and raise that exception i…
…n the case of parse errors
e706229
Aaron Patterson Emitter#initialize takes optional formatting options
Manipulating the options after the emitter object is constructed will
have no impact on the emitter. If the emitter is constructed with the
options, we actually see change in the output (and fix tests).
c84f26a
Aaron Patterson set the exception message on allocated exceptions.
fixes test/psych/visitors/test_to_ruby.rb -n test_exception
fafdc7f
Aaron Patterson use the path from the IO if no path is provided.
fixes test/psych/test_parser.rb -n test_syntax_error_has_path_for_io
644bdaa
This page is out of date. Refresh to see the latest.
17  src/org/jruby/ext/psych/PsychEmitter.java
@@ -85,6 +85,23 @@ public IRubyObject initialize(ThreadContext context, IRubyObject io) {
85 85
         return context.nil;
86 86
     }
87 87
 
  88
+    @JRubyMethod(visibility = PRIVATE)
  89
+    public IRubyObject initialize(ThreadContext context, IRubyObject io, IRubyObject rbOptions) {
  90
+        IRubyObject width     = rbOptions.callMethod(context, "line_width");
  91
+        IRubyObject canonical = rbOptions.callMethod(context, "canonical");
  92
+        IRubyObject level     = rbOptions.callMethod(context, "indentation");
  93
+
  94
+        options = new DumperOptions();
  95
+
  96
+        options.setCanonical(canonical.isTrue());
  97
+        options.setIndent((int)level.convertToInteger().getLongValue());
  98
+        options.setWidth((int)width.convertToInteger().getLongValue());
  99
+
  100
+        emitter = new Emitter(new OutputStreamWriter(new IOOutputStream(io)), options);
  101
+
  102
+        return context.nil;
  103
+    }
  104
+
88 105
     @JRubyMethod
89 106
     public IRubyObject start_stream(ThreadContext context, IRubyObject encoding) {
90 107
         if (!(encoding instanceof RubyFixnum)) {
77  src/org/jruby/ext/psych/PsychParser.java
@@ -87,6 +87,8 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
87 87
             }
88 88
         }, psych);
89 89
 
  90
+        RubyKernel.require(runtime.getNil(),
  91
+                runtime.newString("psych/syntax_error"), Block.NULL_BLOCK);
90 92
         psychParser.defineConstant("ANY", runtime.newFixnum(YAML_ANY_ENCODING));
91 93
         psychParser.defineConstant("UTF8", runtime.newFixnum(YAML_UTF8_ENCODING));
92 94
         psychParser.defineConstant("UTF16LE", runtime.newFixnum(YAML_UTF16LE_ENCODING));
@@ -103,7 +105,9 @@ public PsychParser(Ruby runtime, RubyClass klass) {
103 105
 
104 106
     @JRubyMethod
105 107
     public IRubyObject parse(ThreadContext context, IRubyObject yaml) {
106  
-        return parse(context, yaml, RubyString.newString(context.runtime, "<unknown>"));
  108
+        Ruby runtime = context.runtime;
  109
+
  110
+        return parse(context, yaml, runtime.getNil());
107 111
     }
108 112
 
109 113
     private IRubyObject stringOrNilFor(Ruby runtime, String value, boolean tainted) {
@@ -137,6 +141,11 @@ public IRubyObject parse(ThreadContext context, IRubyObject yaml, IRubyObject pa
137 141
         // FIXME? only supports Unicode, since we have to produces strings...
138 142
         try {
139 143
             parser = new ParserImpl(readerFor(yaml));
  144
+
  145
+            if (path.isNil() && yaml.respondsTo("path")) {
  146
+                path = yaml.callMethod(context, "path");
  147
+            }
  148
+
140 149
             IRubyObject handler = getInstanceVariable("@handler");
141 150
 
142 151
             while (true) {
@@ -173,21 +182,20 @@ public IRubyObject parse(ThreadContext context, IRubyObject yaml, IRubyObject pa
173 182
             }
174 183
         } catch (ParserException pe) {
175 184
             parser = null;
176  
-            RubyKernel.raise(context, runtime.getKernel(),
177  
-                    new IRubyObject[] {runtime.getModule("Psych").getConstant("SyntaxError"), runtime.newString(syntaxError(context, yaml, pe))},
178  
-                    Block.NULL_BLOCK);
  185
+            raiseParserException(context, yaml, pe, path);
  186
+
179 187
         } catch (ScannerException se) {
180 188
             parser = null;
181 189
             StringBuilder message = new StringBuilder("syntax error");
182 190
             if (se.getProblemMark() != null) {
183 191
                 message.append(se.getProblemMark().toString());
184 192
             }
185  
-            throw runtime.newArgumentError(message.toString());
  193
+            raiseParserException(context, yaml, se, path);
  194
+
186 195
         } catch (ReaderException re) {
187 196
             parser = null;
188  
-            RubyKernel.raise(context, runtime.getKernel(),
189  
-                    new IRubyObject[] {runtime.getModule("Psych").getConstant("SyntaxError"), runtime.newString(re.getLocalizedMessage())},
190  
-                    Block.NULL_BLOCK);
  197
+            raiseParserException(context, yaml, re, path);
  198
+
191 199
         } catch (Throwable t) {
192 200
             UnsafeFactory.getUnsafe().throwException(t);
193 201
             return this;
@@ -251,14 +259,51 @@ private void handleSequenceStart(ThreadContext context, SequenceStartEvent sse,
251 259
         invoke(context, handler, "start_sequence", anchor, tag, implicit, style);
252 260
     }
253 261
 
254  
-    private static String syntaxError(ThreadContext context, IRubyObject yaml, MarkedYAMLException mye) {
255  
-        String path;
256  
-        if (yaml.respondsTo("path")) {
257  
-            path = yaml.callMethod(context, "path").toString();
258  
-        } else {
259  
-            path = "<unknown>";
260  
-        }
261  
-        return path + ": couldn't parse YAML at line " + mye.getProblemMark().getLine() + " column " + mye.getProblemMark().getColumn();
  262
+    private static void raiseParserException(ThreadContext context, IRubyObject yaml, ReaderException re, IRubyObject rbPath) {
  263
+        Ruby runtime;
  264
+        RubyClass se;
  265
+        IRubyObject exception;
  266
+
  267
+        runtime = context.runtime;
  268
+        se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError");
  269
+
  270
+        exception = se.newInstance(context,
  271
+                new IRubyObject[] {
  272
+                    rbPath,
  273
+                    runtime.newFixnum(0),
  274
+                    runtime.newFixnum(0),
  275
+                    runtime.newFixnum(re.getPosition()),
  276
+                    (null == re.getName() ? runtime.getNil() : runtime.newString(re.getName())),
  277
+                    (null == re.toString() ? runtime.getNil() : runtime.newString(re.toString()))
  278
+                },
  279
+                Block.NULL_BLOCK);
  280
+
  281
+        RubyKernel.raise(context, runtime.getKernel(), new IRubyObject[] { exception }, Block.NULL_BLOCK);
  282
+    }
  283
+
  284
+    private static void raiseParserException(ThreadContext context, IRubyObject yaml, MarkedYAMLException mye, IRubyObject rbPath) {
  285
+        Ruby runtime;
  286
+        Mark mark;
  287
+        RubyClass se;
  288
+        IRubyObject exception;
  289
+
  290
+        runtime = context.runtime;
  291
+        se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError");
  292
+
  293
+        mark = mye.getProblemMark();
  294
+
  295
+        exception = se.newInstance(context,
  296
+                new IRubyObject[] {
  297
+                    rbPath,
  298
+                    runtime.newFixnum(mark.getLine() + 1),
  299
+                    runtime.newFixnum(mark.getColumn() + 1),
  300
+                    runtime.newFixnum(mark.getIndex()),
  301
+                    (null == mye.getProblem() ? runtime.getNil() : runtime.newString(mye.getProblem())),
  302
+                    (null == mye.getContext() ? runtime.getNil() : runtime.newString(mye.getContext()))
  303
+                },
  304
+                Block.NULL_BLOCK);
  305
+
  306
+        RubyKernel.raise(context, runtime.getKernel(), new IRubyObject[] { exception }, Block.NULL_BLOCK);
262 307
     }
263 308
 
264 309
     private static int translateStyle(Character style) {
3  src/org/jruby/ext/psych/PsychToRuby.java
@@ -31,6 +31,7 @@
31 31
 import org.jruby.RubyClass;
32 32
 import org.jruby.RubyModule;
33 33
 import org.jruby.RubyObject;
  34
+import org.jruby.RubyException;
34 35
 import org.jruby.anno.JRubyMethod;
35 36
 import org.jruby.exceptions.RaiseException;
36 37
 import org.jruby.runtime.ThreadContext;
@@ -50,7 +51,7 @@ public static void initPsychToRuby(Ruby runtime, RubyModule psych) {
50 51
     public static IRubyObject build_exception(ThreadContext context, IRubyObject self, IRubyObject klass, IRubyObject message) {
51 52
         if (klass instanceof RubyClass) {
52 53
             IRubyObject exception = ((RubyClass)klass).allocate();
53  
-            exception.getInternalVariables().setInternalVariable("mesg", message);
  54
+            ((RubyException)exception).message = message;
54 55
             return exception;
55 56
         } else {
56 57
             throw context.runtime.newTypeError(klass, context.runtime.getClassClass());
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.