Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Various fixes for psych #267

Merged
merged 5 commits into from

3 participants

@tenderlove

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

tenderlove added some commits
@tenderlove tenderlove pass the file name to the exception factory method 3cf6da2
@tenderlove tenderlove make sure Psych::Syntax error is required, and raise that exception i…
…n the case of parse errors
e706229
@tenderlove tenderlove 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
@tenderlove tenderlove set the exception message on allocated exceptions.
fixes test/psych/visitors/test_to_ruby.rb -n test_exception
fafdc7f
@tenderlove tenderlove 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
@travisbot

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

@headius headius merged commit 1cbf69f into jruby:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 21, 2012
  1. @tenderlove
  2. @tenderlove
  3. @tenderlove

    Emitter#initialize takes optional formatting options

    tenderlove authored
    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).
  4. @tenderlove

    set the exception message on allocated exceptions.

    tenderlove authored
    fixes test/psych/visitors/test_to_ruby.rb -n test_exception
  5. @tenderlove

    use the path from the IO if no path is provided.

    tenderlove authored
    fixes test/psych/test_parser.rb -n test_syntax_error_has_path_for_io
This page is out of date. Refresh to see the latest.
View
17 src/org/jruby/ext/psych/PsychEmitter.java
@@ -85,6 +85,23 @@ public IRubyObject initialize(ThreadContext context, IRubyObject io) {
return context.nil;
}
+ @JRubyMethod(visibility = PRIVATE)
+ public IRubyObject initialize(ThreadContext context, IRubyObject io, IRubyObject rbOptions) {
+ IRubyObject width = rbOptions.callMethod(context, "line_width");
+ IRubyObject canonical = rbOptions.callMethod(context, "canonical");
+ IRubyObject level = rbOptions.callMethod(context, "indentation");
+
+ options = new DumperOptions();
+
+ options.setCanonical(canonical.isTrue());
+ options.setIndent((int)level.convertToInteger().getLongValue());
+ options.setWidth((int)width.convertToInteger().getLongValue());
+
+ emitter = new Emitter(new OutputStreamWriter(new IOOutputStream(io)), options);
+
+ return context.nil;
+ }
+
@JRubyMethod
public IRubyObject start_stream(ThreadContext context, IRubyObject encoding) {
if (!(encoding instanceof RubyFixnum)) {
View
77 src/org/jruby/ext/psych/PsychParser.java
@@ -87,6 +87,8 @@ public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
}
}, psych);
+ RubyKernel.require(runtime.getNil(),
+ runtime.newString("psych/syntax_error"), Block.NULL_BLOCK);
psychParser.defineConstant("ANY", runtime.newFixnum(YAML_ANY_ENCODING));
psychParser.defineConstant("UTF8", runtime.newFixnum(YAML_UTF8_ENCODING));
psychParser.defineConstant("UTF16LE", runtime.newFixnum(YAML_UTF16LE_ENCODING));
@@ -103,7 +105,9 @@ public PsychParser(Ruby runtime, RubyClass klass) {
@JRubyMethod
public IRubyObject parse(ThreadContext context, IRubyObject yaml) {
- return parse(context, yaml, RubyString.newString(context.runtime, "<unknown>"));
+ Ruby runtime = context.runtime;
+
+ return parse(context, yaml, runtime.getNil());
}
private IRubyObject stringOrNilFor(Ruby runtime, String value, boolean tainted) {
@@ -137,6 +141,11 @@ public IRubyObject parse(ThreadContext context, IRubyObject yaml, IRubyObject pa
// FIXME? only supports Unicode, since we have to produces strings...
try {
parser = new ParserImpl(readerFor(yaml));
+
+ if (path.isNil() && yaml.respondsTo("path")) {
+ path = yaml.callMethod(context, "path");
+ }
+
IRubyObject handler = getInstanceVariable("@handler");
while (true) {
@@ -173,21 +182,20 @@ public IRubyObject parse(ThreadContext context, IRubyObject yaml, IRubyObject pa
}
} catch (ParserException pe) {
parser = null;
- RubyKernel.raise(context, runtime.getKernel(),
- new IRubyObject[] {runtime.getModule("Psych").getConstant("SyntaxError"), runtime.newString(syntaxError(context, yaml, pe))},
- Block.NULL_BLOCK);
+ raiseParserException(context, yaml, pe, path);
+
} catch (ScannerException se) {
parser = null;
StringBuilder message = new StringBuilder("syntax error");
if (se.getProblemMark() != null) {
message.append(se.getProblemMark().toString());
}
- throw runtime.newArgumentError(message.toString());
+ raiseParserException(context, yaml, se, path);
+
} catch (ReaderException re) {
parser = null;
- RubyKernel.raise(context, runtime.getKernel(),
- new IRubyObject[] {runtime.getModule("Psych").getConstant("SyntaxError"), runtime.newString(re.getLocalizedMessage())},
- Block.NULL_BLOCK);
+ raiseParserException(context, yaml, re, path);
+
} catch (Throwable t) {
UnsafeFactory.getUnsafe().throwException(t);
return this;
@@ -251,14 +259,51 @@ private void handleSequenceStart(ThreadContext context, SequenceStartEvent sse,
invoke(context, handler, "start_sequence", anchor, tag, implicit, style);
}
- private static String syntaxError(ThreadContext context, IRubyObject yaml, MarkedYAMLException mye) {
- String path;
- if (yaml.respondsTo("path")) {
- path = yaml.callMethod(context, "path").toString();
- } else {
- path = "<unknown>";
- }
- return path + ": couldn't parse YAML at line " + mye.getProblemMark().getLine() + " column " + mye.getProblemMark().getColumn();
+ private static void raiseParserException(ThreadContext context, IRubyObject yaml, ReaderException re, IRubyObject rbPath) {
+ Ruby runtime;
+ RubyClass se;
+ IRubyObject exception;
+
+ runtime = context.runtime;
+ se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError");
+
+ exception = se.newInstance(context,
+ new IRubyObject[] {
+ rbPath,
+ runtime.newFixnum(0),
+ runtime.newFixnum(0),
+ runtime.newFixnum(re.getPosition()),
+ (null == re.getName() ? runtime.getNil() : runtime.newString(re.getName())),
+ (null == re.toString() ? runtime.getNil() : runtime.newString(re.toString()))
+ },
+ Block.NULL_BLOCK);
+
+ RubyKernel.raise(context, runtime.getKernel(), new IRubyObject[] { exception }, Block.NULL_BLOCK);
+ }
+
+ private static void raiseParserException(ThreadContext context, IRubyObject yaml, MarkedYAMLException mye, IRubyObject rbPath) {
+ Ruby runtime;
+ Mark mark;
+ RubyClass se;
+ IRubyObject exception;
+
+ runtime = context.runtime;
+ se = (RubyClass)runtime.getModule("Psych").getConstant("SyntaxError");
+
+ mark = mye.getProblemMark();
+
+ exception = se.newInstance(context,
+ new IRubyObject[] {
+ rbPath,
+ runtime.newFixnum(mark.getLine() + 1),
+ runtime.newFixnum(mark.getColumn() + 1),
+ runtime.newFixnum(mark.getIndex()),
+ (null == mye.getProblem() ? runtime.getNil() : runtime.newString(mye.getProblem())),
+ (null == mye.getContext() ? runtime.getNil() : runtime.newString(mye.getContext()))
+ },
+ Block.NULL_BLOCK);
+
+ RubyKernel.raise(context, runtime.getKernel(), new IRubyObject[] { exception }, Block.NULL_BLOCK);
}
private static int translateStyle(Character style) {
View
3  src/org/jruby/ext/psych/PsychToRuby.java
@@ -31,6 +31,7 @@
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.RubyObject;
+import org.jruby.RubyException;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ThreadContext;
@@ -50,7 +51,7 @@ public static void initPsychToRuby(Ruby runtime, RubyModule psych) {
public static IRubyObject build_exception(ThreadContext context, IRubyObject self, IRubyObject klass, IRubyObject message) {
if (klass instanceof RubyClass) {
IRubyObject exception = ((RubyClass)klass).allocate();
- exception.getInternalVariables().setInternalVariable("mesg", message);
+ ((RubyException)exception).message = message;
return exception;
} else {
throw context.runtime.newTypeError(klass, context.runtime.getClassClass());
Something went wrong with that request. Please try again.