7

Im trying to call python functions from C code, and i followed a sample from here

I also have the correct include file directries, library directries, and linked the python32.lib (im using python 32) however the error was that python/C APIs such as PyString_FromString, PyInt_FromLong, PyInt_AsLong are undefined (error in the debugger)

this is strange because im also using other APIs, but they're all fine...

whats the problem here??

int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pDict, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    pName = PyString_FromString(argv[1]);
    /* Error checking of pName left out */

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pDict = PyModule_GetDict(pModule);
        /* pDict is a borrowed reference */

Py_Initialize(), PyImport_Import(), PyModule_GetDict() all work fine, but not PyString_FromString...

1
  • 2
    I really think that the function PyString_FromString was renamed to PyBytes_FromString into the Python 3.x. Commented Nov 22, 2011 at 16:08

1 Answer 1

18

The example code you used is for ancient Python version, 2.3.2. Python 3.x line introduced a number of incompatibilites not only in the language but in the C API as well.

The functions you mention simply no longer exist in Python 3.2.

PyString_ functions were renamed to PyBytes_.

PyInt_ functions are gone, PyLong_ should be used instead.

Here's the same example that you've used but for Python 3:

5.3. Pure Embedding

Note that it's using PyUnicode_ instead of PyString_/PyBytes_. In many places where Python 2.x used byte strings, Python 3.x uses unicode strings.

By the way, I usually use this page to look up all possible calls:

Index – P

Sign up to request clarification or add additional context in comments.

2 Comments

so i followed ur suggestion and it worked. now i want to test it using the documentation example you've provided. do i go into the .exe directry and cmd 'call multiply multiply 3 2' ??
I think so, yes, assuming the exe file is called call. If you get stuck, ask a separate question.

Your Answer

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.