[C++-sig] Re: "unidentifiable C++ exception"

David Abrahams dave at boost-consulting.com
Sat Sep 18 13:09:23 CEST 2004


Nicholas Gildea <nick.gildea at gmail.com> writes:

> First, sorry if this is a little off-topic, but I wasn't sure where else to ask.
>
> I've started (trying) to use Boost.Python to embed Python in a C++
> application. After managing to successfully get the "Hello World"
> style example working, I've run into a problem. When I started to use
> PyRun_File instead of PyRun_String, I first got an access violation
> and then after some Googling saw a suggestion to use the Boost.Python
> exception handling wrappers to try and solve the problem, which
> generates the "unidentifiable C++ exception" error.

The access violation is the real problem.  Stick the following  in
your file and the EH stuff will stop interfering with it.

  # include <windows.h>
  #  pragma warning(push)
  #  pragma warning(disable:4297)
  extern "C" void straight_to_debugger(unsigned int, EXCEPTION_POINTERS*)
  {
      throw;
  }
  extern "C" void (*old_translator)(unsigned, EXCEPTION_POINTERS*)
           = _set_se_translator(straight_to_debugger);
  #  pragma warning(pop)

> My code looks like the following:
>
> #include <boost/python.hpp>
> #include <stdio.h>
> #include <iostream>
>
> #pragma comment( lib, "python23.lib" )
> #pragma comment( lib, "boost_python.lib" )
>
> using namespace boost::python;
>
> void test()
> {
> 	handle<> mainModule( borrowed( PyImport_AddModule( "__main__" ) ) );
> 	handle<> mainNamespace( borrowed( PyModule_GetDict( mainModule.get() ) ) );
>
> 	const char* const path = "test.py";
>
> 	FILE* f = fopen( path, "r" );
> 	if( f == NULL )
> 	{
> 		std::cout << "[c++] Couldn't open file!\n";
> 		return;
> 	}
>
> 	handle<>( PyRun_File( f, path, Py_file_input, mainNamespace.get(),
> mainNamespace.get() ) );
>
> 	fclose( f );
> }
>
> int main( int argc, char** argv )
> {
> 	Py_Initialize();
>
> 	if( handle_exception( test ) )
> 	{
> 		if( PyErr_Occurred() )
> 		{
> 			PyErr_Print();
> 		}
>
> 		return 1;
> 	}
>
> 	return 0;	
> }
>
> with test.py consisting of just a print statement. I'm compiling this
> using VC7.1, and using the library supplied with Python 2.3.3 and have
> built boost_python.lib from boost version 1.31 (also in VC7.1).

The problem is almost certainly that the Python23.lib you're using was
built with a different version of VC, which had a different layout for
the FILE structure.  The solution is to rebuild it from source.

HTH,

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list