Step-by-step exec

gregory.lielens at gmail.com gregory.lielens at gmail.com
Thu Nov 6 06:27:53 EST 2008


Hi,

I am using a small python file as an input file (defining constants,
parameters, input data, ...) for a python application.
The input file is simply read by an exec statement in a specific
dictionary, and then the application retrieve all the data it need
from the dictionary...
Everything is working nicely, but I'd like to have something a little
bit more robust regarding input file errors: now
any error in the python input script raise an exception and stop the
execution.
What I am trying to do is to execute it "step-by-step", so that I can
capture the exception if one line (or multi-line statement) fails,
print a warning about the failure, and continue the execution fo the
following lines/statements. Of course, an error on one line can
trigger errors in the following lines, but it does not matter in the
application I have in mind, the goal is to parse as much of the input
script as possible, warn about the errors, and check what's inside the
dictionary after the exec.
One way to do it is to read the input script line per line, and exec
each line in turn. However, this is not convenient as it does not
allow multi-line statements, or basic control flow like if - else
statements or loops.

Is there a better way for a step-by-step exec? Syntax errors in the
input script are not really a problem (as it is generated elsewhere,
it is not directly edited by users), although it would be nice to
catch. The biggest problem are runtime errors (attribute error, value
error, ...). Maybe compiling the file into a code object, and
executing this code object step-by-step in a way similar to debug? pdb
module should do something similar....

Best regards,

Greg.



More information about the Python-list mailing list