How to Parse a Script before execution

Fredrik Lundh fredrik at pythonware.com
Wed Dec 10 05:35:13 EST 2003


"Susanne" wrote:

> i need some help with python.
> I have written some c++ extensions for python, and i can execute a
> script, calling this functions. (PyRun_SimpleFile)
> Now my problem is, that i want to parse the script first, because i
> want to know, if there are any wrong arguments (e.g. not the right
> number of arguments, or of wrong type (int, char* etc.)).

the problem is that the Python compiler won't find such problems;
argument errors are only found at runtime.

> PyRun_SimpleFile tells me, whether there is an error, but
> it executes all calls before the wrong one.

PyRun_SimpleFile compiles the entire script before it runs it, and
bails out if it gets any compilation errors.

maybe you could wire up pychecker in some way?

    http://pychecker.sourceforge.net/

(hint: use PyRun_SimpleString to run a small Python snippet that
runs PyChecker on your script, and only executes the script if Py-
Checker is happy.  If you're using this on simple scripts, you may
have to preprocess the scripts in some way, because PyChecker
actually executes the scripts as a module.  Wrapping the actual
script in a function body might work.  Make sure you prototype
this in Python before you add the code to your C++ program...)

</F>








More information about the Python-list mailing list