Acees violation from extension if compiled with MinGW, but ok with Borland

Andrew Gregory andrew.gregory at npl.co.uk
Mon Dec 16 10:36:29 EST 2002


The function mysquare below generates a python exception if the
argument is zero. This works perfectly when compiled with Borland, but
generates an "access violation" (run time error 0xc0000005) if
compiled with MinGW.
Using MSVC Python 2.2.2 binaries.

_pysimple.h
static char Myerrorstring[80];

// Error class
class Myerror
{
   // Constructor
   public:
   Myerror(char *s) { sprintf(Myerrorstring,"%s",s); };
};

// Function
int mysquare(int i)
{
  if (i==0)
  { throw Myerror("Input zero"); };

  return i*i;
};


Used SWIG -c++ -python pysimple.i to generate pysimple_wrap.cxx

where pysimple.i is

%module pysimple
%{
   #include "pysimple.h"
%}

%exception {
  try {
    $action
  }
  catch (Myerror) {
    PyErr_SetString(PyExc_SystemError,Myerrorstring);
    return NULL;
  }

}

%include pysimple.h


Compiled using distutils
python setup.py build --compiler=mingw32

where for MinGW setup.py is

from distutils.core import setup, Extension
setup (name = "_pysimple",
       version = "1.0",
       maintainer = "Andrew Gregory",
       maintainer_email = "andrew.gregory at npl.co.uk",
       description = "Sample Python C++ DLL",
       ext_modules = [Extension('_pysimple',
                      library_dirs=['C:\\mingw\\lib'],
                      libraries=['supc++'], # if using iostreams etc.
use stdc++
                      sources=['pysimple_wrap.cxx'])])



More information about the Python-list mailing list