About Python's C API

L

Link

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() {
Py_Initialize();
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);

Py_Finalize();
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,
Link
 
J

Jeff Epler

Let me start by saying I'm not a pro with the C API of Python.

With PyRun_SimpleString(), everything takes place in the special
__main__ module:
PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
{
PyObject *m, *d, *v;
m = PyImport_AddModule("__main__");
if (m == NULL)
return -1;
d = PyModule_GetDict(m);
v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
if (v == NULL) {
PyErr_Print();
return -1;
}
Py_DECREF(v);
if (Py_FlushLine())
PyErr_Clear();
return 0;
}

So, to get the attribute x from __main__, convert it to a string, and
printf() it, you'd use this sequence [all untested]:
PyObject *m, *d, *x, *s;

/* m is a "new" reference */
m = PyImport_AddModule("__main__");
if (m == NULL)
return -1;
/* d is a "borrowed" reference */
d = PyModule_GetDict(m);
Py_DECREF(m); m = NULL; /* Done with m -- decref and set to NULL */
x = PyDict_GetItemString(d, "x")
/* x is a "borrowed" reference */
if (x == NULL) {
PyErr_Print();
return;
}
/* s is a "new" reference */
s = PyObject_Str(x);
if (s == NULL) {
PyErr_Print();
return;
}
printf("x: %s\n", PyString_AsString(s));
Py_DECREF(s); s = NULL; /* Done with s -- decref and set to NULL */

If you're programming in C++, you might benefit from checking out boost.python at
http://boost.org/libs/python/doc/index.html
I'm not actually a boost.python user, but I've heard good things about it.

Jeff
 
L

Link

Jeff,

First, I need to say you are so smart.
I really appreciated your help that of course good idea to solve about my
extraction's prblem.

Following your advise, i obtained the value of variable from Python in
developing C/API.

By the way, if I had added the statement "Py_DECREF(m); m = NULL;",
It would be crash during running program.
So that need to remove this statement.

Shall I ask you any problem if no this statment at here?
Any trouble will be happened?

With best regards.
Link
 
T

Ted Holden

Link said:
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.


Much easier to use swig than to code that sort of thing directly.

www.swig.org
 

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
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top