From: <pj...@us...> - 2008-11-10 00:36:47
|
Revision: 5560 http://jython.svn.sourceforge.net/jython/?rev=5560&view=rev Author: pjenvey Date: 2008-11-10 00:36:41 +0000 (Mon, 10 Nov 2008) Log Message: ----------- pretty up SyntaxError surrounding output: o don't initialize an exception traceback if there's no frame o port over CPython's pythonrun::print_error_text o always include a trailing nl in SyntaxError text like CPython Modified Paths: -------------- trunk/jython/src/org/python/core/ParserFacade.java trunk/jython/src/org/python/core/Py.java trunk/jython/src/org/python/core/PyException.java Modified: trunk/jython/src/org/python/core/ParserFacade.java =================================================================== --- trunk/jython/src/org/python/core/ParserFacade.java 2008-11-09 20:15:15 UTC (rev 5559) +++ trunk/jython/src/org/python/core/ParserFacade.java 2008-11-10 00:36:41 UTC (rev 5560) @@ -40,16 +40,16 @@ private ParserFacade() {} - static String getLine(BufferedReader reader, int line) { + private static String getLine(BufferedReader reader, int line) { if (reader == null) { return ""; } String text = null; try { - for(int i=0; i < line; i++) { + for (int i = 0; i < line; i++) { text = reader.readLine(); } - return text; + return text == null ? text : text + "\n"; } catch (IOException ioe) { } return text; Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2008-11-09 20:15:15 UTC (rev 5559) +++ trunk/jython/src/org/python/core/Py.java 2008-11-10 00:36:41 UTC (rev 5560) @@ -1007,18 +1007,23 @@ stderr.print(((PyTraceback) tb).dumpStack()); } if (__builtin__.isinstance(value, Py.SyntaxError)) { - stderr.println(" File \"" + value.__findattr__("filename") + - "\", line " + value.__findattr__("lineno")); + PyObject filename = value.__findattr__("filename"); PyObject text = value.__findattr__("text"); - if (text != Py.None && text.__len__() != 0) { - stderr.println("\t" + text); - String space = "\t"; - int col = ((PyInteger) value.__findattr__("offset").__int__()).getValue(); - for (int j = 1; j < col; j++) { - space = space + " "; - } - stderr.println(space + "^"); + PyObject lineno = value.__findattr__("lineno"); + stderr.print(" File \""); + stderr.print(filename == Py.None || filename == null ? + "<string>" : filename.toString()); + stderr.print("\", line "); + stderr.print(lineno == null ? Py.newString("0") : lineno); + stderr.print("\n"); + if (text != Py.None && text != null && text.__len__() != 0) { + printSyntaxErrorText(stderr, value.__findattr__("offset").asInt(), + text.toString()); } + value = value.__findattr__("msg"); + if (value == null) { + value = Py.None; + } } if (value instanceof PyJavaInstance) { @@ -1031,6 +1036,56 @@ stderr.println(formatException(type, value, tb)); } + /** + * Print the two lines showing where a SyntaxError was caused. + * + * @param out StdoutWrapper to print to + * @param offset the offset into text + * @param text a source code String line + */ + private static void printSyntaxErrorText(StdoutWrapper out, int offset, String text) { + if (offset >= 0) { + if (offset > 0 && offset == text.length()) { + offset--; + } + + // Eat lines if the offset is on a subsequent line + while (true) { + int nl = text.indexOf("\n"); + if (nl == -1 || nl >= offset) { + break; + } + offset -= nl + 1; + text = text.substring(nl + 1, text.length()); + } + + // lstrip + int i = 0; + for (; i < text.length(); i++) { + char c = text.charAt(i); + if (c != ' ' && c != '\t') { + break; + } + offset--; + } + text = text.substring(i, text.length()); + } + + out.print(" "); + out.print(text); + if (text.length() == 0 || !text.endsWith("\n")) { + out.print("\n"); + } + if (offset == -1) { + return; + } + out.print(" "); + for (offset--; offset > 0; offset--) { + out.print(" "); + } + out.print("^\n"); + } + static String formatException(PyObject type, PyObject value, PyObject tb) { StringBuffer buf = new StringBuffer(); Modified: trunk/jython/src/org/python/core/PyException.java =================================================================== --- trunk/jython/src/org/python/core/PyException.java 2008-11-09 20:15:15 UTC (rev 5559) +++ trunk/jython/src/org/python/core/PyException.java 2008-11-10 00:36:41 UTC (rev 5560) @@ -44,14 +44,17 @@ this.type = type; this.value = value; - if (traceback == null) { + if (traceback != null) { + this.traceback = traceback; + } else { PyFrame frame = Py.getFrame(); - traceback = new PyTraceback(frame); - if (frame != null && frame.tracefunc != null) { - frame.tracefunc = frame.tracefunc.traceException(frame, this); + if (frame != null) { + this.traceback = new PyTraceback(frame); + if (frame.tracefunc != null) { + frame.tracefunc = frame.tracefunc.traceException(frame, this); + } } } - this.traceback = traceback; } public PyException(PyObject type, String value) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |