Search code examples
pythoncpython-c-api

reading a global variable of python in c


I am trying to learn how to use the Python/C API correctly - all I actually need to do is to read a global variable (in my case dictionary - but I'm starting with a simple integer variable). Using the discussion: How to access a Python global variable from C? and the source of the answer there: http://bytes.com/topic/python/answers/705918-c-api-embedded-python-how-get-set-named-variables I wrote this little thing:

Python code (tryStuff.py):

var1 = 1

var2 = ['bla', 'blalba']

var3 = {"3" : "Three", "2" : "Two", "1" : "One", "0" : "Ignition!"}

print "end of file - tryStuff!!"

C code (embedPythonTry.c):

#include <python2.7/Python.h>

int main(int argc, char **argv){
  Py_Initialize();
  PyRun_SimpleString("import sys");
  PyRun_SimpleString("sys.path.append('<the absolute path of the folder in which the python file is located>')");
  PyImport_ImportModule("tryStuff");
  printf("After the import, before the addition\n");
  PyObject *mainModule = PyImport_AddModule("__main__");
  PyObject *var1Py = PyObject_GetAttrString(mainModule, "var1");
  int var1Int = PyInt_AsLong(var1Py);
  printf("var1=%d ; var1==NULL: %d\n", var1Int, var1Py==NULL);
  Py_XDECREF(var1Py);
  Py_Finalize();
  return 0;
}

The output of running this c program is:

end of file - tryStuff!!
After the import, before the addition
var1=-1 ; var1==NULL: 1

Which means the Python interpreter finds and runs the correct Python script, but somehow it can't manage to read the variable (var1).

Can anyone spot the problem - I am kinda' lost already. It looks like the most simple situation that can be to apply the Python/C API, but it doesn't work. What am I missing?


Solution

  • You should call PyObject_GetAttrString on the result of PyImport_ImportModule. I have no idea why you think that the __main__ module should define that variable:

    PyObject *mod = PyImport_ImportModule("tryStuff");
    PyObject *var1Py = PyObject_GetAttrString(mod, "var1");
    

    You should also add the check on the results, since PyImport_ImportModule can return NULL when the import fails.