Revision: 959
http://svn.sourceforge.net/pygccxml/?rev=959&view=rev
Author: roman_yakovenko
Date: 2007-03-27 14:25:44 -0700 (Tue, 27 Mar 2007)
Log Message:
-----------
adding custom exception example
Added Paths:
-----------
pyplusplus_dev/docs/troubleshooting_guide/exceptions/
pyplusplus_dev/docs/troubleshooting_guide/exceptions/definition.rest
pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.cpp
pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.cpp.rest
pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.rest
pyplusplus_dev/docs/troubleshooting_guide/exceptions/sconstruct
pyplusplus_dev/docs/troubleshooting_guide/exceptions/sconstruct.rest
pyplusplus_dev/docs/troubleshooting_guide/exceptions/test.py
pyplusplus_dev/docs/troubleshooting_guide/exceptions/test.py.rest
pyplusplus_dev/docs/troubleshooting_guide/exceptions/www_configuration.py
Added: pyplusplus_dev/docs/troubleshooting_guide/exceptions/definition.rest
===================================================================
--- pyplusplus_dev/docs/troubleshooting_guide/exceptions/definition.rest (rev 0)
+++ pyplusplus_dev/docs/troubleshooting_guide/exceptions/definition.rest 2007-03-27 21:25:44 UTC (rev 959)
@@ -0,0 +1,4 @@
+Boost.Python has a limitation: it does not allow to create classes, which derives
+from the classes defined in Python. It is impossible to solve the general use case,
+without changing the library, but it is pretty simple to create solution for
+"exception" classes.
Added: pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.cpp
===================================================================
--- pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.cpp (rev 0)
+++ pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.cpp 2007-03-27 21:25:44 UTC (rev 959)
@@ -0,0 +1,65 @@
+#include "boost/python.hpp"
+#include <stdexcept>
+
+/**
+ * Content:
+ * * example, which explain how to create custom exception class, which derives
+ * from Python built-in exceptions
+ *
+ **/
+
+class zero_division_error : public std::exception{
+public:
+
+ zero_division_error()
+ : std::exception(), m_msg()
+ {}
+
+ zero_division_error( const std::string& msg )
+ : std::exception(), m_msg( msg )
+ {}
+
+ zero_division_error( const zero_division_error& other )
+ : std::exception(other), m_msg( other.m_msg )
+ {}
+
+ const std::string& message() const
+ { return m_msg; }
+
+ virtual ~zero_division_error() throw(){}
+
+private:
+ const std::string m_msg;
+};
+
+double devide( double x, int y ){
+ if( !y ){
+ throw zero_division_error( "unable to devide by 0( zero )" );
+ }
+ return x/y;
+}
+
+namespace bpl = boost::python;
+
+void translate( const zero_division_error& err ){
+ bpl::object this_module( bpl::handle<>( bpl::borrowed(PyImport_AddModule("my_exceptions"))));
+ bpl::object zde_class = this_module.attr("zero_division_error");
+ bpl::object pyerr = zde_class( err );
+ PyErr_SetObject( zde_class.ptr(), bpl::incref( pyerr.ptr() ) );
+}
+
+
+BOOST_PYTHON_MODULE( my_exceptions ){
+
+ typedef bpl::return_value_policy< bpl::copy_const_reference > return_copy_const_ref;
+ bpl::class_< zero_division_error >( "_zero_division_error_" )
+ .def( bpl::init<const std::string&>() )
+ .def( bpl::init<const zero_division_error&>() )
+ .def( "message", &zero_division_error::message, return_copy_const_ref() )
+ .def( "__str__", &zero_division_error::message, return_copy_const_ref() );
+
+ bpl::register_exception_translator<zero_division_error>(&translate);
+
+ bpl::def( "devide", &::devide );
+}
+
Added: pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.cpp.rest
===================================================================
--- pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.cpp.rest (rev 0)
+++ pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.cpp.rest 2007-03-27 21:25:44 UTC (rev 959)
@@ -0,0 +1,3 @@
+.. code-block::
+ :language: C++
+ :source-file: ./exceptions.cpp
Added: pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.rest
===================================================================
--- pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.rest (rev 0)
+++ pyplusplus_dev/docs/troubleshooting_guide/exceptions/exceptions.rest 2007-03-27 21:25:44 UTC (rev 959)
@@ -0,0 +1,72 @@
+=================
+Custom exceptions
+=================
+
+.. contents:: Table of contents
+
+------------
+Introduction
+------------
+
+.. include:: ./definition.rest
+
+-------
+Content
+-------
+
+This example actually consist from 2 small, well documented examples.
+
+The first one shows how to handle conversion between tuples: `boost::tuples::tuple`_
+and Python tuple.
+
+.. _`boost::tuples::tuple` : http://boost.org/libs/tuple/doc/tuple_users_guide.html
+
+The second one shows how to add an automatic conversion from Python tuple to
+some registered class. The class registration allows you to use its functionality
+and enjoy from automatic conversion.
+
+Files
+-----
+
+* `tuples.hpp`_ file contains Boost.Tuple to\\from Python tuple conversion
+ implementation
+
+* `tuples_tester.cpp`_ file contains few functions, which test the tuples
+ conversion functionality
+
+* `custom_rvalue.cpp`_ file contains example of registration custom r-value
+ converter
+
+* `sconstruct`_ file contains build instructions for scons build tool.
+
+* `test.py`_ file contains complete unit tests for both example
+
+All files contain comments, which describe what and why was done.
+
+.. _`tuples.hpp` : ./tuples.hpp.html
+.. _`tuples_tester.cpp` : ./tuples_tester.cpp.html
+.. _`custom_rvalue.cpp` : ./custom_rvalue.cpp.html
+.. _`sconstruct` : ./sconstruct.html
+.. _`test.py` : ./test.py.html
+
+--------
+Download
+--------
+
+`automatic_conversion.zip`_
+
+.. _`automatic_conversion.zip` : ./automatic_conversion.zip
+
+
+.. _`Py++` : ./../pyplusplus.html
+.. _`pygccxml` : http://www.language-binding.net/pygccxml/pygccxml.html
+.. _`SourceForge`: http://sourceforge.net/index.php
+
+..
+ Local Variables:
+ mode: indented-text
+ indent-tabs-mode: nil
+ sentence-end-double-space: t
+ fill-column: 70
+ End:
+
Added: pyplusplus_dev/docs/troubleshooting_guide/exceptions/sconstruct
===================================================================
--- pyplusplus_dev/docs/troubleshooting_guide/exceptions/sconstruct (rev 0)
+++ pyplusplus_dev/docs/troubleshooting_guide/exceptions/sconstruct 2007-03-27 21:25:44 UTC (rev 959)
@@ -0,0 +1,10 @@
+#scons build script
+SharedLibrary( target=r'my_exceptions'
+ , source=[ r'exceptions.cpp' ]
+ , LIBS=[ r"boost_python" ]
+ , LIBPATH=[ r"/home/roman/boost_cvs/libs/python/build/bin-stage" ]
+ , CPPPATH=[ r"/home/roman/boost_cvs"
+ , r"/usr/include/python2.4" ]
+ , SHLIBPREFIX=''
+ , SHLIBSUFFIX='.so'
+)
Added: pyplusplus_dev/docs/troubleshooting_guide/exceptions/sconstruct.rest
===================================================================
--- pyplusplus_dev/docs/troubleshooting_guide/exceptions/sconstruct.rest (rev 0)
+++ pyplusplus_dev/docs/troubleshooting_guide/exceptions/sconstruct.rest 2007-03-27 21:25:44 UTC (rev 959)
@@ -0,0 +1,3 @@
+.. code-block::
+ :language: Python
+ :source-file: ./sconstruct
Added: pyplusplus_dev/docs/troubleshooting_guide/exceptions/test.py
===================================================================
--- pyplusplus_dev/docs/troubleshooting_guide/exceptions/test.py (rev 0)
+++ pyplusplus_dev/docs/troubleshooting_guide/exceptions/test.py 2007-03-27 21:25:44 UTC (rev 959)
@@ -0,0 +1,34 @@
+import unittest
+import my_exceptions
+
+print dir( my_exceptions._zero_division_error_.__class__ )
+
+class zero_division_error(my_exceptions._zero_division_error_, ZeroDivisionError ):
+
+ def __init__( self, msg ):
+ my_exceptions._zero_division_error_.__init__( self, msg )
+
+my_exceptions.zero_division_error = zero_division_error
+
+class tester_t( unittest.TestCase ):
+ def __init__( self, *args ):
+ unittest.TestCase.__init__( self, *args )
+
+ def test( self ):
+ print my_exceptions.devide( 1, 1 )
+ self.failUnless( 1 == my_exceptions.devide( 1, 1 ) )
+ try:
+ my_exceptions.devide( 1, 0 )
+ except zero_division_error, err:
+ print err.__class__.__name__, str(err)
+
+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/docs/troubleshooting_guide/exceptions/test.py.rest
===================================================================
--- pyplusplus_dev/docs/troubleshooting_guide/exceptions/test.py.rest (rev 0)
+++ pyplusplus_dev/docs/troubleshooting_guide/exceptions/test.py.rest 2007-03-27 21:25:44 UTC (rev 959)
@@ -0,0 +1,3 @@
+.. code-block::
+ :language: Python
+ :source-file: ./test.py
Added: pyplusplus_dev/docs/troubleshooting_guide/exceptions/www_configuration.py
===================================================================
--- pyplusplus_dev/docs/troubleshooting_guide/exceptions/www_configuration.py (rev 0)
+++ pyplusplus_dev/docs/troubleshooting_guide/exceptions/www_configuration.py 2007-03-27 21:25:44 UTC (rev 959)
@@ -0,0 +1,3 @@
+name = 'exceptions'
+files_to_skip = ['definition.rest']
+names = {}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|