First pass at exception translator template
So, this is probably gross or non thread-safe in some manner that I'm not clued into yet. I'd appreciate any elegant refactorings, but this seems to work and will allow me to just specify a bunch of "REGISTER_EXCEPTION" lines in my module. --------8<------------------------------- template <typename ExceptionType> struct ExceptionTranslator { typedef ExceptionTranslator<ExceptionType> type; static PyObject *pyException; static void RegisterExceptionTranslator(scope & scope, const char* moduleName, const char* name) { // Add the exception to the module scope std::strstream exName; exName << moduleName << "." << name << '\0'; pyException = PyErr_NewException(exName.str(), NULL, NULL); handle<> instanceException(pyException); scope.attr(name) = object(instanceException); // Register a translator for the type register_exception_translator< ExceptionType > ( &ExceptionTranslator<ExceptionType>::translateException ); } static void translateException(const ExceptionType& ex) { PyErr_SetString(pyException, ex.getMessage().ptr()); } }; template<typename ExceptionType> PyObject* ExceptionTranslator<ExceptionType>::pyException; // Convenience macro #define REGISTER_EXCEPTION(scopeRef, moduleName, className) \ ExceptionTranslator<className>::RegisterExceptionTranslator(scopeRef, moduleName, #className) // Module ====================================================================== BOOST_PYTHON_MODULE(my_module) { scope moduleScope; REGISTER_EXCEPTION(moduleScope, "my_module", InstanceException); ..... } -- Gavin Doughtie DreamWorks SKG
Gavin Doughtie <gdoughtie@anim.dreamworks.com> writes:
So, this is probably gross or non thread-safe in some manner that I'm not clued into yet. I'd appreciate any elegant refactorings, but this seems to work and will allow me to just specify a bunch of "REGISTER_EXCEPTION" lines in my module.
Gavin, Thanks for your post; I'll try to look at it in the next couple of days. -- Dave Abrahams Boost Consulting www.boost-consulting.com
David Abrahams <dave@boost-consulting.com> writes:
Gavin Doughtie <gdoughtie@anim.dreamworks.com> writes:
So, this is probably gross or non thread-safe in some manner that I'm not clued into yet. I'd appreciate any elegant refactorings, but this seems to work and will allow me to just specify a bunch of "REGISTER_EXCEPTION" lines in my module.
Gavin,
Thanks for your post; I'll try to look at it in the next couple of days.
Gavin, did this ever go anywhere, or did I drop it on the floor? -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams -
Gavin Doughtie