newbie question: retrieving values of variables through C API

J

joshusdog

I've got an application that embeds the Python interpreter. I have the
following command in my code:

PyRun_SimpleString("a = \"hello\"");

My question is, what is the C API function call for retrieving the
value of the variable "a"?
 
G

Gabriel Genellina

I've got an application that embeds the Python interpreter. I have the
following command in my code:

PyRun_SimpleString("a = \"hello\"");

My question is, what is the C API function call for retrieving the
value of the variable "a"?

The code is run in the context of the __main__ module. So you could do
something like this:

PyObject *m, *d, *v, *value;
m = PyImport_AddModule("__main__");
if (m == NULL)
return -1;
d = PyModule_GetDict(m);
value = PyDict_GetItemString(d, "a")

(The code above is mostly copied from PyRun_SimpleStringFlags)
You may find other variants of PyRun_XXX more convenient to use.
 

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,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top