Re: [Jython-users] Progress within a call to the PythonInterpreter

In article
<16303_1264006409_4B573509_16303_24276_1_4B573511.9040506@...
>,
Jaime Saiz <Jaime.Saiz@...> wrote:
> For example, I see nothing from where I could know that 3654 calls to
> my class will be performed (if I'd know that, that would be enough!).<br>
> As you can see, in this simple single line, there are many calls to
> traceCall and the others, so no clue about the progress.<br>
You need to limit the scope of tracing. satisfyFunction, satisfyTask,
etc. are not part of Jython; they're part of whatever Python framework
is calling your code.
If it helps, you can set up a Java trace function from inside Jython
code, for example:
>>> from org.python.core import Py
>>> import MyTraceFunction
>>> Py.getThreadState().tracefunc = MyTraceFunction()
>>> 2 + 2
MyTraceFunction.traceCall <frame object at 0x1>
MyTraceFunction.traceLine <frame object at 0x1> line 1
4
MyTraceFunction.traceReturn <frame object at 0x1> ret None
--
Nicholas Riley <njriley@...>

Thread view

Hi,
I'm working in a Java application that allows executing Jython scripts.
We want to provide a progress bar to know the status of the current
executing Jython statement.
I've had a look at TraceFunction, but the Javadoc does not help.
Debugging it does not help either; I cannot see how to get this
information for the PyFrame passed to the methods.
Is there any way to know the statement progress? I mean something that
could be converted to a percentage.
Knowing, for instance, which line of a multiline command is being
executed could help also.
Note: we're using Jython 2.1.
Thanks in advance.
Jaime
================================================================================================
This message and any attachments are intended for the use of the addressee or addressees only.
The unauthorised disclosure, use, dissemination or copying (either in whole or in part) of its content is prohibited.
If you received this message in error, please delete it from your system and notify the sender.
E-mails can be altered and their integrity cannot be guaranteed.
ESA shall not be liable for any e-mail if modified.
=================================================================================================

In article
<16305_1263998711_4B5716F7_16305_137860_1_4B5716FE.507@...>,
Jaime Saiz <Jaime.Saiz@...> wrote:
> I've had a look at TraceFunction, but the Javadoc does not help.
Read the Python documentation on sys.settrace:
<http://docs.python.org/library/sys.html#sys.settrace&gt;
That will give you line-by-line progress, or callbacks on function
calls. Handling loops and the like is up to you; it really depends on
the code as to whether this will give an acceptable indication of
progress. The progress of an arbitrary piece of code is of course
undecidable.
--
Nicholas Riley <njriley@...>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Nicholas Riley wrote:
<blockquote cite="mid:njriley-F1D0FF.10143620012010@..."
type="cite">
<pre wrap="">
Read the Python documentation on sys.settrace:
<a class="moz-txt-link-rfc2396E" href="http://docs.python.org/library/sys.html#sys.settrace">&lt;http://docs.python.org/library/sys.html#sys.settrace&gt;</a&gt;
That will give you line-by-line progress, or callbacks on function
calls. Handling loops and the like is up to you; it really depends on
the code as to whether this will give an acceptable indication of
progress. The progress of an arbitrary piece of code is of course
undecidable.
</pre>
</blockquote>
<br>
Thank you, Nicholas.<br>
<br>
To explain more: my trace code would be Java code, not a Jython
function.<br>
<br>
As far as I know, abstract class <a
href="http://web.mit.edu/jython/jythonRelease_2_2alpha1/Doc/javadoc/index.html">org.python.core.TraceFunction</a&gt;
is meant for that in Java.<br>
<br>
With a simple implementation like this:<br>
<br>
public class MyTraceFunction extends TraceFunction {<br>
<br>
&nbsp;&nbsp;&nbsp; @Override<br>
&nbsp;&nbsp;&nbsp; public TraceFunction traceCall(PyFrame frame) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("MyTraceFunction.traceCall " + frame);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; @Override<br>
&nbsp;&nbsp;&nbsp; public TraceFunction traceReturn(PyFrame frame, PyObject ret) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("MyTraceFunction.traceReturn " + frame + "
ret " + ret);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; @Override<br>
&nbsp;&nbsp;&nbsp; public TraceFunction traceLine(PyFrame frame, int line) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("MyTraceFunction.traceLine " + frame + "
line " + line);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this;<br>
&nbsp;&nbsp;&nbsp; }<br>
<br>
&nbsp;&nbsp;&nbsp; @Override<br>
&nbsp;&nbsp;&nbsp; public TraceFunction traceException(PyFrame frame, PyException exc)
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("MyTraceFunction.traceException " + frame +
" exc " + exc);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this;<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
<br>
and setting it as the trace function with<br>
<br>
Py.getSystemState().tracefunc = new MyTraceFunction();<br>
<br>
a simple Jython command like<br>
<br>
a = 1<br>
<br>
generates 3654 lines in the standard output, whose first 30 are:<br>
<br>
MyTraceFunction.traceLine &lt;frame in "satisfyFunction" at line 43&gt;
line 43<br>
MyTraceFunction.traceCall &lt;frame in "satisfyTask" at line 0&gt;<br>
MyTraceFunction.traceLine &lt;frame in "satisfyTask" at line 33&gt;
line 33<br>
MyTraceFunction.traceLine &lt;frame in "satisfyTask" at line 34&gt;
line 34<br>
MyTraceFunction.traceReturn &lt;frame in "satisfyTask" at line 34&gt;
ret 0<br>
MyTraceFunction.traceReturn &lt;frame in "satisfyFunction" at line
43&gt; ret 0<br>
MyTraceFunction.traceCall &lt;frame in "satisfyFunction" at line 0&gt;<br>
MyTraceFunction.traceLine &lt;frame in "satisfyFunction" at line 93&gt;
line 93<br>
MyTraceFunction.traceLine &lt;frame in "satisfyFunction" at line 94&gt;
line 94<br>
MyTraceFunction.traceReturn &lt;frame in "satisfyFunction" at line
94&gt; ret 0<br>
MyTraceFunction.traceCall &lt;frame in "satisfyFunction" at line 0&gt;<br>
MyTraceFunction.traceLine &lt;frame in "satisfyFunction" at line 42&gt;
line 42<br>
MyTraceFunction.traceLine &lt;frame in "satisfyFunction" at line 43&gt;
line 43<br>
MyTraceFunction.traceCall &lt;frame in "satisfyTask" at line 0&gt;<br>
MyTraceFunction.traceLine &lt;frame in "satisfyTask" at line 33&gt;
line 33<br>
MyTraceFunction.traceLine &lt;frame in "satisfyTask" at line 34&gt;
line 34<br>
MyTraceFunction.traceReturn &lt;frame in "satisfyTask" at line 34&gt;
ret 0<br>
MyTraceFunction.traceReturn &lt;frame in "satisfyFunction" at line
43&gt; ret 0<br>
MyTraceFunction.traceCall &lt;frame in "satisfyFunction" at line 0&gt;<br>
MyTraceFunction.traceLine &lt;frame in "satisfyFunction" at line 93&gt;
line 93<br>
MyTraceFunction.traceLine &lt;frame in "satisfyFunction" at line 94&gt;
line 94<br>
MyTraceFunction.traceReturn &lt;frame in "satisfyFunction" at line
94&gt; ret 0<br>
MyTraceFunction.traceCall &lt;frame in "satisfyFunction" at line 0&gt;<br>
MyTraceFunction.traceLine &lt;frame in "satisfyFunction" at line 42&gt;
line 42<br>
MyTraceFunction.traceLine &lt;frame in "satisfyFunction" at line 43&gt;
line 43<br>
MyTraceFunction.traceCall &lt;frame in "satisfyTask" at line 0&gt;<br>
MyTraceFunction.traceLine &lt;frame in "satisfyTask" at line 33&gt;
line 33<br>
MyTraceFunction.traceLine &lt;frame in "satisfyTask" at line 34&gt;
line 34<br>
MyTraceFunction.traceReturn &lt;frame in "satisfyTask" at line 34&gt;
ret 0<br>
MyTraceFunction.traceReturn &lt;frame in "satisfyFunction" at line
43&gt; ret 0<br>
...<br>
<br>
I don't see anything meaningful from there; I mean, I see nothing from
which I can deduce the progress of "a = 1".<br>
For example, I see nothing from where I could know that 3654 calls to
my class will be performed (if I'd know that, that would be enough!).<br>
As you can see, in this simple single line, there are many calls to
traceCall and the others, so no clue about the progress.<br>
<br>
Digging into the PyFrame object that is passed to these trace* methods
doesn't conclude anything more.<br>
<br>
Any help would be appreciated. Thanks again!<br>
Jaime<br>
<PRE>================================================================================================
This message and any attachments are intended for the use of the addressee or addressees only.
The unauthorised disclosure, use, dissemination or copying (either in whole or in part) of its content is prohibited.
If you received this message in error, please delete it from your system and notify the sender.
E-mails can be altered and their integrity cannot be guaranteed.
ESA shall not be liable for any e-mail if modified.
=================================================================================================
</PRE></body>
</html>

In article
<16303_1264006409_4B573509_16303_24276_1_4B573511.9040506@...
>,
Jaime Saiz <Jaime.Saiz@...> wrote:
> For example, I see nothing from where I could know that 3654 calls to
> my class will be performed (if I'd know that, that would be enough!).<br>
> As you can see, in this simple single line, there are many calls to
> traceCall and the others, so no clue about the progress.<br>
You need to limit the scope of tracing. satisfyFunction, satisfyTask,
etc. are not part of Jython; they're part of whatever Python framework
is calling your code.
If it helps, you can set up a Java trace function from inside Jython
code, for example:
>>> from org.python.core import Py
>>> import MyTraceFunction
>>> Py.getThreadState().tracefunc = MyTraceFunction()
>>> 2 + 2
MyTraceFunction.traceCall <frame object at 0x1>
MyTraceFunction.traceLine <frame object at 0x1> line 1
4
MyTraceFunction.traceReturn <frame object at 0x1> ret None
--
Nicholas Riley <njriley@...>

Nicholas Riley wrote:
> You need to limit the scope of tracing. satisfyFunction, satisfyTask,
> etc. are not part of Jython; they're part of whatever Python framework
> is calling your code.
>
You are right indeed; I can ignore those scopes.
Now a simple call executes traceLine in the top scope exactly the number
of lines there. Great!
However, I still miss the total number of lines that are to be executed:
PyFrame has f_lineno (current line number) but I miss something like
f_nlines (indicating the total number of lines to be executed).
Anyway, I can deduce them by other means.
Thank you very much!
Jaime
================================================================================================
This message and any attachments are intended for the use of the addressee or addressees only.
The unauthorised disclosure, use, dissemination or copying (either in whole or in part) of its content is prohibited.
If you received this message in error, please delete it from your system and notify the sender.
E-mails can be altered and their integrity cannot be guaranteed.
ESA shall not be liable for any e-mail if modified.
=================================================================================================