Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Python 3.3.2 Embedding

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
1 reply to this topic

#1 Davaris1   Members   

151
Like
0Likes
Like

Posted 24 May 2013 - 08:03 AM

I am following this turorial and am not getting anywhere, with the code that was provided. I am running Python 3.3.2.

 

It returns null on

 

PyObject* pluginModule = PyImport_Import(name);

 

 

static const char* PLUGIN_NAME = "shout_filter";


String<> CallPlugIn(const String<>& ln)
{
    PyObject* name = PyBytes_FromString(PLUGIN_NAME);
    PyObject* pluginModule = PyImport_Import(name);
    Py_DECREF(name);
    if (!pluginModule)
    {
        PyErr_Print();
        return "Error importing module";
    }
    PyObject* filterFunc = PyObject_GetAttrString(pluginModule, "filterFunc");
    Py_DECREF(pluginModule);
    if (!filterFunc)
    {
        PyErr_Print();
        return "Error retrieving 'filterFunc'";
    }
    PyObject* args = Py_BuildValue("(s)", ln);
    if (!args)
    {
        PyErr_Print();
        Py_DECREF(filterFunc);
        return "Error building args tuple";
    }
    PyObject* resultObj = PyObject_CallObject(filterFunc, args);
    Py_DECREF(filterFunc);
    Py_DECREF(args);
    if (!resultObj)
    {
        PyErr_Print();
        return "Error invoking 'filterFunc'";
    }
    const char* resultStr = PyBytes_AsString(resultObj);
    if (!resultStr)
    {
        PyErr_Print();
        Py_DECREF(resultObj);
        return "Error converting result to C string";
    }
    String<> result = resultStr;
    Py_DECREF(resultObj);
    return result;
}

int RunThatPythonScript()
{
    Py_Initialize();
    PyObject* sysPath = PySys_GetObject((char*)"path");
    PyObject* curDir = PyBytes_FromString(".");
    PyList_Append(sysPath, curDir);
    Py_DECREF(curDir);
    String<> input = "Do Something With This Text";
    CallPlugIn(input);
    Py_Finalize();
    return 0;
}
 

 

I was getting the same issue with the official version demo:

 

http://docs.python.org/3/extending/embedding.html#embedding-python-in-c

 

Any suggestions?



#2 Davaris1   Members   

151
Like
0Likes
Like

Posted 24 May 2013 - 09:17 AM

I made a modification to the start of the CallPlugIn function

 

 

    //PyObject* pluginModule = PyImport_Import(name);
    PyObject* pluginModule = PyImport_ImportModule(PLUGIN_NAME);
 

 

It got me further into the function, but the result string is null.

    const char* resultStr = PyBytes_AsString(resultObj);


 

 

 

Edit:

I tried this code and it got the right result.

    char *cstrret;
    PyArg_Parse(resultObj, "s", &cstrret);
 

 

I used this tutorial:

 

https://www6.software.ibm.com/developerworks/education/l-pythonscript/l-pythonscript-ltr.pdf

 

Its the only one I've found that works.



 


Edited by Davaris1, 24 May 2013 - 09:24 AM.





Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.