questions on python script compiling for embedding

phoolimin at gmail.com phoolimin at gmail.com
Wed May 17 11:16:33 CEST 2006


Dear all,

I am trying to embed python into another scripting language, to do this
I need to solve a number of problems on importing or compiling python
script. First let me state what exactly I want to do, that is, I want
the following syntax to be supported in the host language which I am
developing:

######################################################
# scripts in the host language

routine Test()
do
   println("printed by Test()");
end

global astring = "ASTRING";

# begin python scripts:
@begin(python)

def PyTest( s ):
    print s
    return 1000;

# access global variables from the host scripts
pystring = astring;

# call function defined in the host script:
Test();

@end(python)

# call a python function:
n = PyTest( "invoke python function" );

######################################################

So there are the following problems come out naturally:

#1. can I compile a block of python script to get the list of
variables, classes and functions etc. (or at least the names) without
executing?

Because I need the host script to see the python definitions.

The Py_CompileStringFlags() seems to do the work. But it returns a code
object, which I don't know exactly and if it is possible to get the
list of variables, classes and functions from this kind of object.

##2. Can I pass a list global variables for such compiling?

Because I need python scripts also see the variables functions etc.
defined in the host scripts.

*********
For these two problem, a C API like this would be pefect:
PyObject* Py_CompileStringFlagsXXX( const char *str, const char *file,
int start, PyObject *module, PyCompilerFlags *flags );

Different from Py_CompileStringFlags(), this function should take
another parameter (Python module object), which serve as a
**namespace**, to solve undefined names in the souce "str", and store
global variables, functions and classes etc defined in this source
string.

So the third question is:

###3. Is there some way, or how to do such thing as
Py_CompileStringFlagsXXX() would do???

Of course, I do not expect such API should be added soon or even added.

Thanks a lot,

Limin




More information about the Python-list mailing list