From: <ga...@ne...> - 2005-03-19 19:26:48
|
hi, i've succesfully embedded jython into my java program.. but now i'd like to do something more.. 1. is there a way to control the running-time of a jython program? for example..what if a jython program contains an endless loop? i'd like to specify for example that a jython program can only run for 10seconds, or that it can only execute let's say 1000bytecode instructions.. is there a way for that? 2. somehow implement step-by-step execution..means the jython interpreter would stop after every instruction, and i could analyze it's state..any ideas on this? thanks a lot, gabor farkas |
From: Diez B. R. <de...@we...> - 2005-03-20 11:20:39
|
> is there a way to control the running-time of a jython program? for > example..what if a jython program contains an endless loop? i'd like to > specify for example that a jython program can only run for 10seconds, or > that it can only execute let's say 1000bytecode instructions.. is there > a way for that? Not directly, see next answer. > 2. > somehow implement step-by-step execution..means the jython interpreter > would stop after every instruction, and i could analyze it's state..any > ideas on this? Use sys.settrace(). Read the docs on it how to use it. And you can also use it to accomplish your above mentioned goal. But beware: There is no absolut guarantee that you will have full control over the runtime of your program! The single execution steps are atomic - in CPython these are the bytecodes, I'm not sure if thats true for jython, too. But the important thing is that code like this time.sleep(1000000000) won't be interrupted by the tracing, as it is one atomic call to an underlying function implementation. So in the end, the only _totally_ sure way of limiting a interpreters existence is to kill it - by using a subprocess (difficult in java) or maybe run it in an extra thread and kill that (deprecated in java) Regards, Diez |
From: <ga...@ne...> - 2005-03-21 12:12:04
|
Diez B. Roggisch wrote: >>is there a way to control the running-time of a jython program? for >>example..what if a jython program contains an endless loop? i'd like to >>specify for example that a jython program can only run for 10seconds, or >>that it can only execute let's say 1000bytecode instructions.. is there >>a way for that? > > > Not directly, see next answer. > > >>2. >>somehow implement step-by-step execution..means the jython interpreter >>would stop after every instruction, and i could analyze it's state..any >>ideas on this? > > > Use sys.settrace(). Read the docs on it how to use it. And you can also use it > to accomplish your above mentioned goal. thanks.. i'm trying it, but with little success.. the following script: ====== import sys def foo(frame,event,arg): print 'tracing...' sys.settrace(foo) print 1 print 2 print 3 ====== produces: 1 2 3 imho it should 1 tracing 2 tracing 3 tracing.. or something like that.. any ideas what am i doing wrong? gabor |
From: Diez B. R. <de...@we...> - 2005-03-21 12:21:27
|
> ====== > import sys > > def foo(frame,event,arg): > print 'tracing...' > > > sys.settrace(foo) > > print 1 > print 2 > print 3 > ====== Works for me. > any ideas what am i doing wrong? Not really. What jython version? Diez |
From: <ga...@ne...> - 2005-03-21 12:35:05
Attachments:
e.py
|
Diez B. Roggisch wrote: >>====== >>import sys >> >>def foo(frame,event,arg): >> print 'tracing...' >> >> >>sys.settrace(foo) >> >>print 1 >>print 2 >>print 3 >>====== > > > Works for me. > > > >>any ideas what am i doing wrong? > > > Not really. What jython version? > btw. before we go on to analyze this problem, i suspect that i do something very stupid.. because the script does not even work on cpython... i attached it. the output it generates with cpython and with jython is : ==== 1 2 3 ==== i would like the following output ==== 1 tracing... 2 tracing... 3 tracing... ==== (or maybe more tracing lines)... gabor |
From: Kent J. <ke...@td...> - 2005-03-21 12:46:34
|
Gábor Farkas wrote: > Diez B. Roggisch wrote: > >>> ====== >>> import sys >>> >>> def foo(frame,event,arg): >>> print 'tracing...' >>> >>> >>> sys.settrace(foo) >>> >>> print 1 >>> print 2 >>> print 3 >>> ====== >> >> >> >> Works for me. FWIW, for me it works as expected if I type the commands into the Jython interactive interpreter. If I type the commands into the Python 2.4 interpreter it prints 'tracing...' *twice* for each print. If I run the attached program from Jython or Python I get just the output from the print statement. Kent |
From: <ga...@ne...> - 2005-03-21 12:48:03
|
Kent Johnson wrote: > G=C3=A1bor Farkas wrote: >=20 >> Diez B. Roggisch wrote: >> >>>> =3D=3D=3D=3D=3D=3D >>>> import sys >>>> >>>> def foo(frame,event,arg): >>>> print 'tracing...' >>>> >>>> >>>> sys.settrace(foo) >>>> >>>> print 1 >>>> print 2 >>>> print 3 >>>> =3D=3D=3D=3D=3D=3D >>> >>> >>> >>> >>> Works for me. >=20 >=20 > FWIW, for me it works as expected if I type the commands into the Jytho= n=20 > interactive interpreter. If I type the commands into the Python 2.4=20 > interpreter it prints 'tracing...' *twice* for each print. If I run the= =20 > attached program from Jython or Python I get just the output from the=20 > print statement. >=20 yes, that's my problem too gabor |
From: Abhay S. <ar...@me...> - 2005-03-21 13:24:45
|
On Mar 21, 2005, at 7:47 AM, G=E1bor Farkas wrote: > [trace function not working as expected] > Makes sense to me. Check out the documentation: <http://www.python.org/doc/lib/debugger-hooks.html> Here's a quote from that page: > The global trace function is invoked (with event set to 'call')=20 > whenever a new local scope is entered; it should return a reference to=20= > the local trace function to be used that scope, or None if the scope=20= > shouldn't be traced. > > The local trace function should return a reference to itself (or to=20 > another function for further tracing in that scope), or None to turn=20= > off tracing in that scope. Hope that helps. - Abhay |
From: Diez B. R. <de...@we...> - 2005-03-21 12:50:33
|
The interesting thing is that it works in interactive mode, but not run as a script. I'm out of ideas here and google didn't help either. Anybody any ideas? Diez |
From: <ga...@ne...> - 2005-03-21 13:02:44
|
Diez B. Roggisch wrote: > The interesting thing is that it works in interactive mode, but not run as a > script. I'm out of ideas here and google didn't help either. Anybody any > ideas? > got it ;) " Help on built-in function settrace in module sys: settrace(...) settrace(function) Set the global debug tracing function. It will be called on each function call. See the debugger chapter in the library manual. " so it is called on each FUNCTION CALL.. ;(( and it seems that "print 1" is not a function call.. this works: import sys def printIt(thing): print thing def foo(first,second,third): print 'tracing' sys.settrace(foo) print(1) print(2) print(3) at the end...i should have looked first in "help(sys.settrace)" ;)))) thanks for all of your comments, gabor |
From: Kent J. <ke...@td...> - 2005-03-21 13:04:04
|
Diez B. Roggisch wrote: > The interesting thing is that it works in interactive mode, but not run as a > script. I'm out of ideas here and google didn't help either. Anybody any > ideas? This version works better. Maybe settrace() doesn't trace in the scope where it is set? and maybe each line of the interactive interpreter is considered a new scope? Or something like that... import sys def foo(frame,event,arg): print 'tracing', event return foo sys.settrace(foo) def bar(): print 1 print 2 print 3 bar() Jython output when run as a program is tracing call tracing line tracing line 1 tracing line 2 tracing line 3 tracing return Python 2.4 output is tracing call tracing line 1 tracing line 2 tracing line 3 tracing return Kent |