embedded function body

Marco Colombo marco.colombo at gmail.com
Fri May 20 12:57:38 EDT 2005


Hi,
I need to embed an user-supplied python function body in a C program.
That is, the user has no control over the function definition:

def afunction():
    <here goes user code>

Now, the problem is that I can't just append the supplied string,
because I need to properly indent it which isn't trivial - just adding
a tab in front of every line won't do. I'm considering two approaches:
1) write an indent function which is more aware of python syntax;
2) use the python lexxer and work with parse trees.

I've done some experiments with 2). The idea was to parse the following
program:

def afunction():
    pass

and the user supplied string (as a complete program) separately, then
replace the <suite> node in the function definition with one including
the statements from the user program.

I've got something that seems to work (but getting
allocation/deallocation right requires some work), but it seems to me
it's a bit awkward. Is the PyNode_* interface public and stable? Is it
ok to use it?

Any advice? Am I missing something? Should I try approach 1)?

TIA,
.TM.




More information about the Python-list mailing list