From 5a1c2945e4ac288947f2816439be779972ad6605 Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Tue, 17 Dec 2013 09:08:38 +0000 Subject: [PATCH] [0.3.x] fixed : every call to pykd leads to big stack alloctaion git-svn-id: https://pykd.svn.codeplex.com/svn@86870 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/dbgexcept.h | 60 +++++++++++++++++++++++++++++++++--------------- pykd/pymod.cpp | 10 ++------ 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/pykd/dbgexcept.h b/pykd/dbgexcept.h index 81daa7a..e6b4e7d 100644 --- a/pykd/dbgexcept.h +++ b/pykd/dbgexcept.h @@ -9,6 +9,7 @@ namespace pykd { + template< class TExcept > struct exceptPyType{ static python::handle<> pyExceptType; @@ -43,34 +44,55 @@ public: python::scope().attr( className.c_str() ) = ob; exceptPyType::pyExceptType = python::handle<>( ob.ptr() ); - - python::register_exception_translator( &exceptionTranslate ); } - - static - void - exceptionTranslate(const TExcept &e ) { - - python::object exceptObj = python::object( exceptPyType::pyExceptType )( e.what() ); - - PyErr_SetObject( exceptPyType::pyExceptType.get(), exceptObj.ptr()); - } - }; -////////////////////////////////////////////////////////////////////////////// -struct ExceptionTranslator { - static - void - indexTranslate(const kdlib::IndexException &e ) { +inline void exceptionTranslate(const kdlib::DbgException &e ) +{ + if ( typeid(e).hash_code() == typeid(kdlib::MemoryException).hash_code() ) + { + python::object exceptObj = python::object( exceptPyType::pyExceptType )( e.what() ); + PyErr_SetObject( exceptPyType::pyExceptType.get(), exceptObj.ptr()); + return; + } + + if ( typeid(e).hash_code() == typeid(kdlib::SymbolException).hash_code() ) + { + python::object exceptObj = python::object( exceptPyType::pyExceptType )( e.what() ); + PyErr_SetObject( exceptPyType::pyExceptType.get(), exceptObj.ptr()); + return; + } + + if ( typeid(e).hash_code() == typeid(kdlib::TypeException).hash_code() ) + { + python::object exceptObj = python::object( exceptPyType::pyExceptType )( e.what() ); + PyErr_SetObject( exceptPyType::pyExceptType.get(), exceptObj.ptr()); + return; + } + + if ( typeid(e).hash_code() == typeid(kdlib::IndexException).hash_code() ) + { PyErr_SetString( PyExc_IndexError, "Index out of range"); + return; } -}; + python::object exceptObj = python::object( exceptPyType::pyExceptType )( e.what() ); + PyErr_SetObject( exceptPyType::pyExceptType.get(), exceptObj.ptr()); +} -/////////////////////////////////////////////////////////////////////////////////// +inline void registerExceptions() +{ + pykd::exception( "DbgException", "Pykd base exception class" ); + pykd::exception( "MemoryException", "Target memory access exception class" ); + pykd::exception( "SymbolException", "Symbol exception" ); + pykd::exception( "TypeException", "type exception" ); + + python::register_exception_translator( &exceptionTranslate ); +} + +///////////////////////////////////////////////////////////////////////////////////// void printException(); diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp index bc077a6..7188d85 100644 --- a/pykd/pymod.cpp +++ b/pykd/pymod.cpp @@ -772,14 +772,8 @@ BOOST_PYTHON_MODULE( pykd ) // "There is no return value"); ; - python::register_exception_translator( &ExceptionTranslator::indexTranslate ); - - // kdlib exception - pykd::exception( "DbgException", "Pykd base exception class" ); - pykd::exception( "MemoryException", "Target memory access exception class" ); - pykd::exception( "SymbolException", "Symbol exception" ); - pykd::exception( "TypeException", "type exception" ); - + // C++ exception translation to python + pykd::registerExceptions(); } //////////////////////////////////////////////////////////////////////////////////