|
From: Phil S. <phi...@ho...> - 2001-11-05 04:22:55
|
Thanks Finn, I will try this stuff out sometime this week.
Jython _is_ more magical than I thought!
Finn Bock wrote:
>
> >> >
> >> >I'm writing three tasks:
> >> >1) a jython task. This is like the ant <script> task
> >> >but it has several improvements:
> >> > - handles indented jython scripts.
> >> > - massages stack traces to make line numbers relative to
> >> >the ant build file, rather than relative to the start of the
> >> >script within the build file
> >
> >By the way, I gave up on figuring out how PyTracebacks work
> >(too many PyObjects)
>
> The trick is never to trust what you see when calling
> System.out.print(). Some of the classes in the exception & traceback
> subsystem override toString().
>
> >and ended up doing a regex thing to
> >patch up the line numbers. Am I missing some critical
> >piece of information?
>
> It turned out that jython is badly prepared for setting the start line
> numbers in a call to compile(). I have added for feature request about
> it, but until it is implementated you might be able to use the hack
> below.
>
> >> jytaskdef would then call project.addTaskDefinition("MyTask", MyTask).
> >>
> >> So, how big is my misunderstanding?
> >
> >Well, I didn't think that you could call jython classes
> >directly from java...
>
> A python class which extends a java class or java interface is a
> completely ordinary java class.
>
> >ant needs to be able to load the class in a classloader
>
> If the new task is added by project.addTaskDefinition(String,Class)
> there is no need to let ant do the loading of the class.
>
> You can get hold of the new Task and use like this:
>
> PyObject newClass = interp.get("MyTask");
> Object tmp = newClass.__tojava__(Class.class);
> if (tmp != Py.NoConversion && Task.class.isAssignableFrom((Class) tmp) {
> project.addTaskDefinition("MyTask", (Class)tmp);
> }
>
> >and call various setxxx methods
> >and then call execute.
> >
> >Maybe jython is even more magical than I thought?
>
> regards,
> finn
>
> public class x {
> public static void main(String[] args) {
> PythonInterpreter interp = new PythonInterpreter();
> try {
> // a syntax error
> interp.exec(
> "if 1:\n" +
> " a = 1\n" +
> " b = 0\n");
> } catch (PyException exc) {
> fixAndPrintError(exc, 20);
> }
>
> try {
> // throws a NameError
> interp.exec(
> "def foo():\n" +
> " a = c\n" +
> "foo()\n");
> } catch (PyException exc) {
> fixAndPrintError(exc, 20);
> }
> }
>
> private static void fixAndPrintError(PyException exc, int startLine) {
> System.out.println();
> System.out.println("Traceback (innermost last):");
>
> if (exc instanceof PySyntaxError) {
> PySyntaxError se = (PySyntaxError)exc;
> se.instantiate();
> int line = se.value.__getattr__("lineno").__int__().getValue();
> int col = se.value.__getattr__("offset").__int__().getValue();
> String text = se.value.__getattr__("text").toString();
> String filename = se.value.__getattr__("filename").toString();
> System.out.println(" File \""+filename+"\", line "+
> (line+startLine));
> if (text != null && text.length() != 0) {
> System.out.println("\t"+text);
> String space = "\t";
> for(int j=1; j<col; j++)
> space = space+" ";
> System.out.println(space+"^");
> }
> System.out.println("SyntaxError: " + exc.value.__getitem__(0));
> return;
> }
>
> PyTraceback tb = exc.traceback;
> StringBuffer buf = new StringBuffer();
> dumpStack(tb, buf, startLine);
> System.out.print(buf);
>
> PyObject typeName;
> if (exc.type instanceof PyClass) {
> typeName = new PyString(((PyClass)exc.type).__name__);
> } else {
> typeName = exc.type;
> }
>
> System.out.println(typeName + ": " + exc.value);
> }
>
> private static String line(PyTraceback tb, int startLine) {
> if (tb.tb_frame == null || tb.tb_frame.f_code == null)
> return " (no code object) at line "+
> (tb.tb_lineno + startLine)+"\n";
> return " File \""+tb.tb_frame.f_code.co_filename+
> "\", line "+(tb.tb_lineno + startLine)+
> ", in "+tb.tb_frame.f_code.co_name+"\n";
> }
>
> public static void dumpStack(PyTraceback tb, StringBuffer buf,
> int startLine) {
> buf.append(line(tb, startLine));
> if (tb.tb_next != Py.None && tb.tb_next != tb)
> dumpStack((PyTraceback) tb.tb_next, buf, startLine);
> else if (tb.tb_next == tb) {
> buf.append("circularity detected!"+tb+tb.tb_next);
> }
> }
>
> }
>
> _______________________________________________
> Jython-dev mailing list
> Jyt...@li...
> https://lists.sourceforge.net/lists/listinfo/jython-dev
|