[pygccxml-commit] SF.net SVN: pygccxml: [471] pyplusplus_dev/unittests
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-08-26 07:32:38
|
Revision: 471 Author: roman_yakovenko Date: 2006-08-26 00:32:22 -0700 (Sat, 26 Aug 2006) ViewCVS: http://svn.sourceforge.net/pygccxml/?rev=471&view=rev Log Message: ----------- introducing built-in functionality for exception translation Modified Paths: -------------- pyplusplus_dev/pyplusplus/code_creators/__init__.py pyplusplus_dev/pyplusplus/decl_wrappers/class_wrapper.py pyplusplus_dev/pyplusplus/module_creator/creator.py pyplusplus_dev/unittests/cppexceptions_tester.py pyplusplus_dev/unittests/data/cppexceptions_to_be_exported.hpp pyplusplus_dev/unittests/test_all.py Added Paths: ----------- pyplusplus_dev/pyplusplus/code_creators/exception_translator.py Modified: pyplusplus_dev/pyplusplus/code_creators/__init__.py =================================================================== --- pyplusplus_dev/pyplusplus/code_creators/__init__.py 2006-08-26 07:16:06 UTC (rev 470) +++ pyplusplus_dev/pyplusplus/code_creators/__init__.py 2006-08-26 07:32:22 UTC (rev 471) @@ -107,4 +107,8 @@ from indexing_suites import indexing_suite1_t from indexing_suites import indexing_suite2_t -from indexing_suites import value_traits_t \ No newline at end of file +from indexing_suites import value_traits_t + +from exception_translator import exception_translator_t +from exception_translator import exception_translator_register_t + Added: pyplusplus_dev/pyplusplus/code_creators/exception_translator.py =================================================================== --- pyplusplus_dev/pyplusplus/code_creators/exception_translator.py (rev 0) +++ pyplusplus_dev/pyplusplus/code_creators/exception_translator.py 2006-08-26 07:32:22 UTC (rev 471) @@ -0,0 +1,43 @@ +# 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 types +import algorithm +import code_creator +import declaration_based +from pygccxml import declarations + +class exception_translator_t( declaration_based.declaration_based_t ): + def __init__(self, exception_class ): + declaration_based.declaration_based_t.__init__( self, declaration=exception_class ) + + @property + def translator_name( self ): + return 'translate_%(alias)s' % { 'alias' : self.declaration.alias } + + def _create_impl(self): + return os.linesep.join([ + "void translate_%(alias)s( const %(cls_name)s& %(arg_name)s ){" \ + , self.indent( self.declaration.exception_translation_code ) + , "}"]) \ + % { 'alias' : self.declaration.alias + , 'cls_name' : self.decl_identifier + , 'arg_name' : self.declaration.exception_argument_name } + + +class exception_translator_register_t( declaration_based.declaration_based_t ): + def __init__(self, exception_class, exception_translator): + declaration_based.declaration_based_t.__init__( self, declaration=exception_class ) + self.works_on_instance = False + self.translator = exception_translator + + def _create_impl( self ): + return '%(register_exception_translator)s< %(cls)s >( &%(translator)s );' \ + % { 'register_exception_translator' : algorithm.create_identifier( self, 'boost::python::register_exception_translator' ) + , 'cls' : self.decl_identifier + , 'translator' : self.translator.translator_name } + + \ No newline at end of file Modified: pyplusplus_dev/pyplusplus/decl_wrappers/class_wrapper.py =================================================================== --- pyplusplus_dev/pyplusplus/decl_wrappers/class_wrapper.py 2006-08-26 07:16:06 UTC (rev 470) +++ pyplusplus_dev/pyplusplus/decl_wrappers/class_wrapper.py 2006-08-26 07:32:22 UTC (rev 471) @@ -136,7 +136,8 @@ self._wrapper_code = [] self._null_constructor_body = '' self._copy_constructor_body = '' - + self._exception_translation_code = None + def _get_redefine_operators( self ): return self._redefine_operators def _set_redefine_operators( self, new_value ): @@ -207,6 +208,43 @@ copy_constructor_body = property( _get_copy_constructor_body, _set_copy_constructor_body , doc="copy constructor code, that will be added as is to the copy constructor of class-wrapper") + @property + def exception_argument_name( self ): + """exception argument name for translate exception function + + If you don't understand what this argument is, please take a look on + Boost.Python documentation: http://www.boost.org/libs/python/doc/v2/exception_translator.html + """ + return 'exc' + + def _get_exception_translation_code( self ): + return self._exception_translation_code + def _set_exception_translation_code( self, code ): + self._exception_translation_code = code + exception_translation_code = property( _get_exception_translation_code, _set_exception_translation_code + , doc="C++ exception to Python exception translation code" \ + +"\nExample: PyErr_SetString(PyExc_RuntimeError, exc.what()); " \ + +"\nPy++ will generate the rest of the code." \ + +"\nPay attention: the exception variable name is exc." ) + + def translate_exception_to_string( self, python_exception_type, to_string ): + """registers exception translation to string + + @param python_exception_type: Python exception type, for example PyExc_RuntimeError + @type python_exception_type: str + + @param to_string: C++ expression that extracts information from exception. + The type of expression should be char*. + @type to_string: str + """ + #NICE TO HAVE: + #1. exception\assert\warning should be raised if python_exception_type + # does not contain valid Python exception + #2. Py++ can validate, that member function returns char* + code = "PyErr_SetString( %(exception_type)s, %(to_string)s ); " \ + % { 'exception_type' : python_exception_type, 'to_string' : to_string } + self.exception_translation_code = code + def add_declaration_code( self, code ): """adds the code to the declaration section""" self.declaration_code.append( user_text.user_text_t( code ) ) Modified: pyplusplus_dev/pyplusplus/module_creator/creator.py =================================================================== --- pyplusplus_dev/pyplusplus/module_creator/creator.py 2006-08-26 07:16:06 UTC (rev 470) +++ pyplusplus_dev/pyplusplus/module_creator/creator.py 2006-08-26 07:32:22 UTC (rev 471) @@ -743,6 +743,14 @@ class_inst.remove_creator( static_method ) class_inst.adopt_creator( static_method ) + if temp_curr_decl.exception_translation_code: + translator = code_creators.exception_translator_t( temp_curr_decl ) + self.__extmodule.adopt_declaration_creator( translator ) + class_inst.user_declarations.append( translator ) + translator_register \ + = code_creators.exception_translator_register_t( temp_curr_decl, translator ) + class_inst.adopt_creator( translator_register ) + self.curr_decl = temp_curr_decl self.curr_code_creator = temp_curr_parent Modified: pyplusplus_dev/unittests/cppexceptions_tester.py =================================================================== --- pyplusplus_dev/unittests/cppexceptions_tester.py 2006-08-26 07:16:06 UTC (rev 470) +++ pyplusplus_dev/unittests/cppexceptions_tester.py 2006-08-26 07:32:22 UTC (rev 471) @@ -38,26 +38,30 @@ , *args ) def customize( self, mb ): - mb.class_( 'custom_exception_t' ).add_code( REGISTER_CODE, False) - mb.build_code_creator( tester_t.EXTENSION_NAME ) + #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 ) + #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 ) + custom_exception = mb.class_( 'custom_exception_t' ) + custom_exception.translate_exception_to_string( 'PyExc_RuntimeError', 'exc.what().c_str()' ) 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" ) + self.failUnless( "profe of concept" in str( error ) ) + #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)) Modified: pyplusplus_dev/unittests/data/cppexceptions_to_be_exported.hpp =================================================================== --- pyplusplus_dev/unittests/data/cppexceptions_to_be_exported.hpp 2006-08-26 07:16:06 UTC (rev 470) +++ pyplusplus_dev/unittests/data/cppexceptions_to_be_exported.hpp 2006-08-26 07:32:22 UTC (rev 471) @@ -1,15 +1,15 @@ -// 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__ +// 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{ +namespace cppexceptions{ + struct custom_exception_t{ custom_exception_t( const std::string& error ) : m_error( error ) @@ -18,7 +18,7 @@ const std::string& what() const{ return m_error; } - + private: const std::string m_error; }; @@ -27,6 +27,6 @@ throw custom_exception_t( "profe of concept" ); } -} - +} + #endif//__cppexceptions_to_be_exported_hpp__ Modified: pyplusplus_dev/unittests/test_all.py =================================================================== --- pyplusplus_dev/unittests/test_all.py 2006-08-26 07:16:06 UTC (rev 470) +++ pyplusplus_dev/unittests/test_all.py 2006-08-26 07:32:22 UTC (rev 471) @@ -58,6 +58,7 @@ import vector3_tester import default_args_tester import unnamed_classes_tester +import cppexceptions_tester def create_suite(times): testers = [ @@ -112,6 +113,7 @@ , abstract_classes_tester , indexing_suites2_tester , unnamed_classes_tester + , cppexceptions_tester ] main_suite = unittest.TestSuite() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |