Step-by-step exec

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Nov 7 05:20:13 EST 2008


On Thu, 06 Nov 2008 03:27:53 -0800, gregory.lielens wrote:

> 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...

Surely a better, more Pythonic, and *faster* way to accomplish the same 
thing is with import?


> 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.

Which is the right thing to do.


> 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, 

I'm curious what sort of application you have where it doesn't matter 
that programmatic statements are invalid.


> 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.

So basically you want to create a Python interpreter you can stop and 
start which runs inside Python. Is that right?



-- 
Steven



More information about the Python-list mailing list