[IronPython] Don't execute files containing errors?

Curt Hagenlocher curt at hagenlocher.org
Fri Feb 19 14:38:10 CET 2010


The short answer is "no, that's not possible".

This is effectively a version of the halting problem (
http://en.wikipedia.org/wiki/Halting_problem). For an arbitrary script, you
can check its syntax for correctness without executing fairly easily. Other
errors are potentially detectable as long as the user refrains from using
certain programming constructs. In your example below, you might be able to
demonstrate that "undefinedVariable" was uninitialized. But the same test
would probably also come to that conclusion in the following code:

a = "This is a test"
exec "undef" + "inedVariables"[:-1] + ' = 10'
print undefinedVariable
b = "This is another test"

As for rolling back changes, you could run the script into a temporary scope
if you knew that you were only setting variables. If it succeeds, then run
it into a "real" scope. The problem is that an arbitrary script can have
side effects. Consider the following short script:

import sys
sys.path = []

To protect against that, you effectively need a full sandbox to run code
against.

On Thu, Feb 18, 2010 at 7:16 PM, Alex Turpin <alex.turpin at gmail.com> wrote:

> Hey all,
>
> I am currently embedding IronPython in a C# app of mine. I need to
> execute external script files, but to also be able to only executes
> the ones that will be error free. Here is an example script:
>
> a = "This is a test"
> print undefinedVariable
> b = "This is another test"
>
> If I execute this script, it will halt on line 1 and thus only
> executing half of it. This can be extremely confusing since declaring
> the variable "a" will have worked, but not "b". I am using
> PythonEngine.CreateScriptSourceFromFile.
>
> Is there any way to check if a script contains errors before executing
> it, or at the very least reverting the changes it made?
>
> Thanks.
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100219/db6b4a2c/attachment.html>


More information about the Ironpython-users mailing list