embedded function body

M

Marco Colombo

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,840
Latest member
Tiffany471

Latest Threads

Top