Python C-Api 32bit PyDict_Check Access Violation, Win7, Python 2.66

240 views Asked by At

I am trying to use the Python C-Api for 32bit Project in VS2010.

I have installed the 64bit and 32bit Python. To distinguish the two Versions, I have renamed the 32bit dll to 'python26_32.dll'. I have created an according import .lib file 'python26_32.lib' with the VS dumpbin and lib tool (see. https://adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll/).

I have adjusted 'pyconfig.h' and uncommented the #pragma comment(lib...) Statements.

The projects compiles fine for 32bit. Running the exe gets me an Access Violation when calling PyDict_Check. Other method calls before worked fine (e.g. Py_Initialize, PyRun_SimpleString, PyDict_New...)

Here is my small example:

#include "stdafx.h"
#include "python.h" //include in stdafx.h has the same result
#include <iostream>

using std::cout; using std::endl;

int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Tryin to initialize Python." << endl;
    Py_SetPythonHome("D:\\Python26_32");
    Py_Initialize();
    char *codeString = 
        "import platform\n"
        "import sys\n"
        "print platform.architecture()[0]\n"
        "print sys.path\n"
        "sys.path.insert(0,'D:\\Python26_32')\n"
        "print sys.path\n";

    PyRun_SimpleString(codeString);

    PyObject *pDict;
    pDict = PyDict_New();
    PyDict_SetItemString(pDict, "Key1", PyString_FromString("Value1"));
    int i = PyDict_Check(pDict);

    cout << "working" << endl;
    return 0;
}

I have noticed, that 'PyDict_Check' is not in the dll's exports. It is defined in the python header files.

I have tried to adjust the 'Path' (in Windows and in via the api (see example)) but that did not help.

The 64bit version works fine (after changing the relvant VC++ directories and the Py_SetPythonHome Statement.

Most confusing to me is, that parts of the c-api workes and others not.

Just tried PyDict_CheckExact, this works.

Many thanks for your help.

1

There are 1 answers

0
Andre On BEST ANSWER

After trying other versions of Python (2.7.9) with same result, I recognized that building the "Release Version" always worked. After disabling Py_DEBUGin pyconfig.h the Debug Build worked too for my needs.

Although I can not explain the behavior, this solved the problem for me. Therefore I would mark the problem as solved.