[C++-sig] Exception support in CVS version of Boost.Python?

Niall Douglas s_sourceforge at nedprod.com
Fri Oct 10 20:41:50 CEST 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 10 Oct 2003 at 10:20, Dan Halbert wrote:

> One thing I could still use is automatic translation of C++ exceptions
> to Python exceptions. In the C++-SIG mail archives, I see discussion
> of Gottfried G's exception translation automation. I think I see code
> for it in the CVS snapshot of BPL in Boost. But I'm not quite sure how
> this mechanism to be used, as it seemed to be evolving for a while. It
> doesn't seem to be mentioned in the updated documentation.

I'm pretty sure that part hasn't changed at all.

> Could someone supply a simple example of the Pyste exception
> translation construct and a handler function?

Attached below. See the FXException bit.

> And I'll need the latest CVS version of GCC_XML, right?

I've used the release version fine. Nicodemus knows more.

> Also, is the Boost.Python CVS snapshot pretty stable (in terms of
> being usable) and mostly waiting for Boost 1.31 to become official? My
> current testing has all been with the Boost.Python code in the 1.30.2
> release version.

Sometimes CVS works, other times not as is its nature. But mostly it 
does and the improved functionality is most useful.

Cheers,
Niall

- --- cut ---
/*********************************************************************
***********
*                                                                     
          *
*                             Boost.python converters                 
          *
*                                                                     
          *
**********************************************************************
***********
*        Copyright (C) 2003 by Niall Douglas.   All Rights Reserved.  
          *
*       NOTE THAT I DO NOT PERMIT ANY OF MY CODE TO BE PROMOTED TO 
THE GPL      *
**********************************************************************
***********
* This code is free software; you can redistribute it and/or          
          *
* modify it under the terms of the GNU Library General Public         
          *
* License as published by the Free Software Foundation; either        
          *
* version 2.1 of the License, or (at your option) any later version   
          *
* EXCEPT that clause 3 of the LGPL does not apply ie; you may not     
          *
* "upgrade" this code to the GPL without my prior written permission. 
          *
* Please consult the file "License_Addendum2.txt" accompanying this 
file.       *
*                                                                     
          *
* This library is distributed in the hope that it will be useful,     
          *
* but WITHOUT ANY WARRANTY; without even the implied warranty of      
          *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                
          *
**********************************************************************
**********/

#include <complex>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/to_python_converter.hpp>
#include <boost/python/exception_translator.hpp>
#include "../include/fxdefs.h"
#include "../include/FXString.h"
#include "../include/FXException.h"
#include "../include/FXPtrHold.h"
#include "../include/FXProcess.h"

using namespace boost::python;

//********************************************************************
***********
// FXString converter

struct FXString_to_python_str
{
	static PyObject *convert(const FX::FXString &str)
	{
		return PyString_FromStringAndSize(str.text(), str.length());
		//return incref(boost::python::object(str).ptr());
	}
};

struct FXString_from_python_str
{
	FXString_from_python_str()
	{
		converter::registry::push_back(&convertible, &construct, 
type_id<FX::FXString>());
	}
	static void *convertible(PyObject *obj)
	{
		return PyString_Check(obj) ? obj : 0;
	}
	static void construct(PyObject *obj, 
converter::rvalue_from_python_stage1_data *data)
	{
		const char *value=PyString_AsString(obj);
		if(!value) throw_error_already_set();
		void *storage=((converter::rvalue_from_python_storage<FX::FXString> 
*) data)->storage.bytes;
		new(storage) FX::FXString(value, PyString_Size(obj));
		data->convertible = storage;
	}
};

void RegisterConvFXString()
{
	to_python_converter<FX::FXString, FXString_to_python_str>();
	FXString_from_python_str();
}

//********************************************************************
***********
// FXException converter

static void FXExceptionTranslator(const FX::FXException &e)
{
	PyObject *type=0;
	switch(e.code())
	{
	case FXEXCEPTION_NOMEMORY:
		type=PyExc_MemoryError;
		break;
	case FXEXCEPTION_NORESOURCE:
		type=PyExc_EnvironmentError;
		break;
	case FXEXCEPTION_IOERROR:
	case FXEXCEPTION_NOTFOUND:
		type=PyExc_IOError;
		break;
	case FXEXCEPTION_OSSPECIFIC:
		type=PyExc_OSError;
		break;
	default:
		type=PyExc_RuntimeError;
		break;
	}
	PyErr_SetString(type, e.report().text());
}

void RegisterConvFXException()
{
	
register_exception_translator<FX::FXException>(&FXExceptionTranslator)
;
}

//********************************************************************
***********
// Overall initialiser

using namespace FX;

void InitialiseTnFOXPython()
{
	RegisterConvFXException();
	RegisterConvFXString();
	static FXPtrHold<FXProcess> myprocess;
	FXProcess *alreadyinited=FXProcess::instance();
	if(!alreadyinited)
	{
		FXERRHM(myprocess=new FXProcess);
	}
}






-----BEGIN PGP SIGNATURE-----
Version: idw's PGP-Frontend 4.9.6.1 / 9-2003 + PGP 8.0.2

iQA/AwUBP4b9bsEcvDLFGKbPEQIKpwCeKSCV/AttkBIZjhRBvVky3hHJR/EAoKu1
MHwBz7FF9JkdMcUCFTwRLzJt
=vfop
-----END PGP SIGNATURE-----




More information about the Cplusplus-sig mailing list