[pygccxml-commit] SF.net SVN: pygccxml: [209] pyplusplus_dev/unittests
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-06-07 20:11:39
|
Revision: 209 Author: roman_yakovenko Date: 2006-06-05 02:35:05 -0700 (Mon, 05 Jun 2006) ViewCVS: http://svn.sourceforge.net/pygccxml/?rev=209&view=rev Log Message: ----------- exception translator - just an idea Added Paths: ----------- pyplusplus_dev/unittests/cppexceptions_tester.py pyplusplus_dev/unittests/data/cppexceptions_to_be_exported.hpp Added: pyplusplus_dev/unittests/cppexceptions_tester.py =================================================================== --- pyplusplus_dev/unittests/cppexceptions_tester.py (rev 0) +++ pyplusplus_dev/unittests/cppexceptions_tester.py 2006-06-05 09:35:05 UTC (rev 209) @@ -0,0 +1,70 @@ +# Copyright 2004 Roman Yakovenko. +# Distributed under the Boost Software License, Version 1.0. (See +# accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import os +import sys +import unittest +import fundamental_tester_base +from pyplusplus import code_creators + +TRANSLATE_CODE = \ +""" +void translate(boost::python::object py_class, cppexceptions::custom_exception_t const& e){ + std::cout << "I am here"; + boost::python::object py_custom_exception = py_class(e); + boost::python::object type = py_custom_exception.attr( "__class__" ); + PyErr_SetObject( type.ptr(), py_custom_exception.ptr() ); +} + +""" + +REGISTER_CODE = \ +""" + boost::python::object py_class = custom_exception_t_exposer; + boost::python::register_exception_translator<cppexceptions::custom_exception_t>( + boost::bind( &translate, py_class, _2 ) ); + +""" + +class tester_t(fundamental_tester_base.fundamental_tester_base_t): + EXTENSION_NAME = 'cppexceptions' + + def __init__( self, *args ): + fundamental_tester_base.fundamental_tester_base_t.__init__( + self + , tester_t.EXTENSION_NAME + , *args ) + + def customize( self, mb ): + mb.class_( 'custom_exception_t' ).add_code( REGISTER_CODE, False) + mb.build_code_creator( tester_t.EXTENSION_NAME ) + + mb.code_creator.add_include( "boost/bind.hpp" ) + mb.code_creator.add_include( "iostream" ) + translate = code_creators.custom_text_t( TRANSLATE_CODE ) + mb.code_creator.adopt_creator( translate, -1 ) + + def run_tests( self, module): + custom_exception_t = module.custom_exception_t + bases = list( custom_exception_t.__bases__ ) + [RuntimeError] + custom_exception_t.__bases__ = tuple( bases ) + custom_exception_t.__str__ = custom_exception_t.what + try: + module.throw_custom_exception() + except RuntimeError, error: + self.failUnless( str(error) == "profe of concept" ) + + + +def create_suite(): + suite = unittest.TestSuite() + suite.addTest( unittest.makeSuite(tester_t)) + return suite + +def run_suite(): + unittest.TextTestRunner(verbosity=2).run( create_suite() ) + +if __name__ == "__main__": + run_suite() Added: pyplusplus_dev/unittests/data/cppexceptions_to_be_exported.hpp =================================================================== --- pyplusplus_dev/unittests/data/cppexceptions_to_be_exported.hpp (rev 0) +++ pyplusplus_dev/unittests/data/cppexceptions_to_be_exported.hpp 2006-06-05 09:35:05 UTC (rev 209) @@ -0,0 +1,32 @@ +// Copyright 2004 Roman Yakovenko. +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef __cppexceptions_to_be_exported_hpp__ +#define __cppexceptions_to_be_exported_hpp__ + +#include <string> + +namespace cppexceptions{ + +struct custom_exception_t{ + custom_exception_t( const std::string& error ) + : m_error( error ) + {} + + const std::string& what() const{ + return m_error; + } + +private: + const std::string m_error; +}; + +inline void throw_custom_exception(){ + throw custom_exception_t( "profe of concept" ); +} + +} + +#endif//__cppexceptions_to_be_exported_hpp__ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |