Original Post
After some reading I've Embedded Python in cpp (not a real game/application yet) I've been able to solve most of my problems myself but there're 3 problems that I don't get solved. - 1 - The c function PyRun_SimpleFile (sometimes) craches on windows, that's what I found out myself (with a little help of google [wink]). Is there a way around this problem or do I have to ready the file myself and use PyRun_SimpleString? - 2 - To access a Python function from the __main__ scope I wrote this: But because the __main__ scope already exists I think it unnessesary to (re)import it. Are there other ways to get a PyObject of the __main__ scope? - 3 - I have a python function declared: and I call it like: but Value (returned by CallObject) appears to be a unicode string (I thought "Hello world" was just a 8-bit string in python) and I can't get it converted it to a ancii string. How do you let Python return a 8-bit string or convert a Unicode string to a ASCII string (in c++)? That were all my problems I hope somebody can help me. Thanks in advance. [Edited by - flammable on June 9, 2009 11:43:40 AM]
PyObject* ModuleName = Py_BuildValue("s", "__main__");
if (ModuleName == NULL)
{
//error
Py_DECREF(ModuleName);
}
PyObject* Module = PyImport_Import(ModuleName);
//load and call the function
def greet():
return "Hello world"
PyObject* Function = PyObject_GetAttrString(Module, "greet");
if (Function == NULL || !PyCallable_Check(Function))
{
Py_DECREF(Module);
//error
return EXIT_FAILURE;
}
PyObject* Value;
Value = PyObject_CallObject(Function, NULL);