By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,105 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,105 IT Pros & Developers. It's quick & easy.

About Python's C API

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
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

"Jeff Epler" <je****@unpythonic.net>
???????:ma*********************************@python .org...
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

Jul 18 '05 #3

P: n/a
Link wrote:
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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.