Python extension in C++ with MinGW

Andrew Gregory Andrew.Gregory@npl.co.uk
Sun Dec 22 02:24:00 GMT 2002


I'm trying to write a Python extension on Windows using MinGW (2.0.0)
and SWIG (1.3.17), but find problems with exception handling. Whatever
the merits of the code (below) I think it should work (and it DOES work
if compiled using Borland C++). If compiled using MinGW it I get an
"access violation" error (0xC0000005) when I call the function
"ProducePython Error" from Python (MSVC binaries 2.2.2). Does anyone
have any ideas, or know who to ask?

Below I've listed everything you need to reproduce the problem. The
extension is called _pysimple.pyd


Andrew Gregory.



Function in pysimple.h:

static char errorstring[80];

// Error class
class Myerror
{

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

};


int ProducePythonError()
{
	strcpy(errorstring,"No error");
	Myerror ER("A Python Error");
	throw ER;
	return 0;
};


Use    " SWIG -c++ -python pysimple.i " to create wrapper file
wrap_pysimple.cxx


The SWIG interface file (pysimple.i) is

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


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

}

%include pysimple.h


The extension can be created using "python setup.py build
--compiler=mingw32", where setup.py is

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


-------------------------------------------------------------------
This e-mail and any attachments may contain confidential and/or
privileged material; it is for the intended addressee(s) only.
If you are not a named addressee, you must not use, retain or
disclose such information.

NPL Management Ltd cannot guarantee that the e-mail or any
attachments are free from viruses.

NPL Management Ltd. Registered in England and Wales. No: 2937881
Registered Office: Teddington, Middlesex, United Kingdom TW11 0LW.
-------------------------------------------------------------------



More information about the Gcc-help mailing list