7

I am having problems using the Embedded Python for C as per the Documentation - Whenever I try using imported modules I get an :

Unhandled exception at 0x1e089e85 in PythonIncl.exe: 0xC0000005: Access violation reading location 0x00000004.

The error occurs in the PyObject_GetAttrString() method and the documentation isn't much help. I have also tried using tutorials as in an Example from IBM, but always get the same access violation.

The following is the example code from one of the tutorials which I can't seem to get to work, what is wrong here?

C-Code (in one main file):

#include <Python.h>
int main()
{
    PyObject *strret, *mymod, *strfunc, *strargs;
    char *cstrret;
    Py_Initialize();
    mymod = PyImport_ImportModule("reverse");
    strfunc = PyObject_GetAttrString(mymod, "rstring");
    strargs = Py_BuildValue("(s)", "Hello World");
    strret = PyEval_CallObject(strfunc, strargs);
    PyArg_Parse(strret, "s", &cstrret);
    printf("Reversed string: %s\n", cstrret);
    Py_Finalize();
    return 0;
}

Python code (in a file called reverse.py, same folder):

def rstring(s):
    i = len(s)-1
    t = ''
    while(i > -1):
        t += s[i]
        i -= 1
    return t

I am running a XP machine using MSVS2008, Python 2.7

A bit of context: I am trying to embed a small python script, which uses OpenOPC, in a fairly large C-program and would like to transfer data between the two. However I already fail at a proof-of-concept test with basic examples.

Amro
  • 123,847
  • 25
  • 243
  • 454
Shorkaa
  • 123
  • 3
  • 5
  • 2
    Do you know you must indent a python's while's body? – Griwes Sep 02 '11 at 13:21
  • 1
    It's almost always nicer and more powerful to write an extension for Python rather than embedding Python. Doing so looks pretty similar, but there is better tooling for extending and it's easier to access other Python tools when needed. Check out http://twistedmatrix.com/users/glyph/rant/extendit.html for a perspective on extending vs. embedding. – Mike Graham Sep 02 '11 at 13:41
  • yes, about the missing indent, that just got lost during the posting, it's there in the IDE. – Shorkaa Sep 02 '11 at 15:31

2 Answers2

16

Check the result of the PyImport_ImportModule call: It fails and returns NULL. That is because by default, the current directory is not in the search path. Add

PySys_SetPath("."); // before ..
mymod = PyImport_ImportModule("reverse");

to add the current directory to the module search path and make your example work.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • 1
    `PySys_SetPath(".")` clears/overwrites the search path. You'd better [append](http://stackoverflow.com/a/8859538/6394138) to it. – Leon Jun 09 '16 at 04:11
2

You're proceeding without checking for errors, so it's no shock your code fails this way. From your description, it sounds like mymod is NULL, which would be consistent with a failed import. One possible cause of the failed import is that the reverse.py you posted has a syntax error.

Mike Graham
  • 73,987
  • 14
  • 101
  • 130