About Python's C API

Link link0937 at hotmail.com
Thu Sep 18 03:08:38 CEST 2003

Dear sir,

I am used to developing C program on MSVC7.0  platform.
This is my first time to use Python for calling by C program.
Now, it is hard to deal with the problem about extracting the variable
which be definied in Python.  That how can I do?

I have gotten the JPython 's sample that the following code can be looked.
Have anyone help me to obtain same results that be called by Python's C API?
What do I mean?

See attached the following code.

// JPython code
import org.python.util.PythonInterpreter;
import org.python.core.*;

public class SimpleEmbedded {
    public static void main(String []args)
throws PyException
PythonInterpreter interp =
    new PythonInterpreter();

System.out.println("Hello, brave new world");
interp.exec("import sys");
interp.exec("print sys");

interp.set("a", new PyInteger(42));
interp.exec("print a");
interp.exec("x = 2+2");
PyObject x = interp.get("x");

System.out.println("x: "+x);
System.out.println("Goodbye, cruel world");

I would like to re-write those program to Python.

// C Language code ==============================
void main() {
    printf("Hello, brave new world");

    PyRun_SimpleString("import sys");
    PyRun_SimpleString("print sys");
    // interp.set("a", new PyInteger(42));

    PyRun_SimpleString("print a");
    PyRun_SimpleString("x = 2+2");
    // PyObject x = interp.get("x");
    // System.out.println("x: "+x);

    printf("Goodbye, cruel world");

See the above C Language code,

After running the statement PyRun_SimpleString("x = 2+2"),
the variable x can  be definied and be assigned value 4 in Python.

Meanwhile, next one, I would like to get the x porinter.

Could you help me how can I do?
Or show me the other way to reach my purpose.

Sincerely yours,

More information about the Python-list mailing list