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
125k25 gold badges250 silver badges466 bronze badges
asked Sep 2, 2011 at 13:14
3
  • 2
    Do you know you must indent a python's while's body? Commented Sep 2, 2011 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 twistedmatrix.com/users/glyph/rant/extendit.html for a perspective on extending vs. embedding. Commented Sep 2, 2011 at 13:41
  • yes, about the missing indent, that just got lost during the posting, it's there in the IDE. Commented Sep 2, 2011 at 15:31

2 Answers 2

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.

answered Sep 2, 2011 at 13:41
Sign up to request clarification or add additional context in comments.

1 Comment

PySys_SetPath(".") clears/overwrites the search path. You'd better append to it.
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.

answered Sep 2, 2011 at 13:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.