interpreter limits

Joseph T. Bore jbore at tjtech.com
Tue Apr 13 12:52:08 EDT 2004


I'm embedding python in my app, and I'm using it as a scripting
language for it, everything works great.  

My only concern is how to handle the possibility that a user
accidentally puts an infinite loop in the code that gets called by the
app.

Something as simple as a function that has as it's body:

          while 1:
                pass

would require the application to be killed.  Hopefully this will
rarely happen, but it's a *real* and unacceptable possibility.

Looking around, it seems that this has occasionally been brought up by
others, but no solution has been arrived at.  I even looked at grail,
figuring that perhaps there were some controls put in for the python
applets it would load, but no luck there either it seems.

I dont know a heck of a lot about the implementation of the
interpreter/vm but would it be possible to implement exec or eval with
an optional argument, that argument would be a maximum number of byte
codes the interpreter would execute before throwing an exception.

this would eliminate the out of control case where a function  runs
the above code and never returns.  you could make the value large
enough to handle just about everything except for infinite loops.

something like:

          try:
                exec(codeToExecute, maxTicks = 100000)
          except InterpreterLimitReached:
                print "your code ran too long"

Anyone have any idea if this is at all implementable?

jbore




More information about the Python-list mailing list