Python extension in C++ with MinGW

Andrew Gregory
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
   Myerror(char *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

The SWIG interface file (pysimple.i) is

%module pysimple
   #include "pysimple.h"

%exception {
  try {
  catch (Myerror) {
    return NULL;


%include pysimple.h

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

from distutils.core import setup, Extension
setup (name = "_pysimple",
       version = "1.0",
       maintainer = "Andrew Gregory",
       maintainer_email = "",
       description = "Sample Python C++ DLL",
       ext_modules = [Extension('_pysimple',
                      libraries=['supc++'],                    # if
using iostreams etc. use stdc++

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