reading a global variable of python in c

£可爱£侵袭症+ 提交于 2019-12-30 11:20:52

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/24732753/reading-a-global-variable-of-python-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!