diff options
| author | qrort <[email protected]> | 2022-11-30 23:47:12 +0300 |
|---|---|---|
| committer | qrort <[email protected]> | 2022-11-30 23:47:12 +0300 |
| commit | 22f8ae0e3f5d68b92aecccdf96c1d841a0334311 (patch) | |
| tree | bffa27765faf54126ad44bcafa89fadecb7a73d7 /contrib/libs/pycxx/Python3/cxx_exceptions.cxx | |
| parent | 332b99e2173f0425444abb759eebcb2fafaa9209 (diff) | |
validate canons without yatest_common
Diffstat (limited to 'contrib/libs/pycxx/Python3/cxx_exceptions.cxx')
| -rw-r--r-- | contrib/libs/pycxx/Python3/cxx_exceptions.cxx | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/contrib/libs/pycxx/Python3/cxx_exceptions.cxx b/contrib/libs/pycxx/Python3/cxx_exceptions.cxx new file mode 100644 index 00000000000..9dff2753ddd --- /dev/null +++ b/contrib/libs/pycxx/Python3/cxx_exceptions.cxx @@ -0,0 +1,72 @@ +// +// cxx_exceptions.cxx +// +#include "../Exception.hxx" +#include "../Extensions.hxx" + +#include <map> + +namespace Py +{ +typedef void (*throw_exception_func_t)( PyObject* exc ); + +std::map<void *, throw_exception_func_t> py_exc_type_to_exc_func; + +void addPythonException( ExtensionExceptionType &py_exc_type, throw_exception_func_t func ) +{ + py_exc_type_to_exc_func.insert( std::make_pair( py_exc_type.ptr(), func ) ); +} + +void addPythonException( PyObject *py_exc_type, throw_exception_func_t func ) +{ + py_exc_type_to_exc_func.insert( std::make_pair( py_exc_type, func ) ); +} + +void ifPyErrorThrowCxxException() +{ + if( PyErr_Occurred() ) + { + PyObject *ptype, *pvalue, *ptrace; + PyErr_Fetch( &ptype, &pvalue, &ptrace ); + PyErr_Restore( ptype, pvalue, ptrace ); + + Object q( ptype ); + + std::map<void *, throw_exception_func_t>::iterator func = py_exc_type_to_exc_func.find( ptype ); + if( func != py_exc_type_to_exc_func.end() ) + { +#ifdef PYCXX_DEBUG + std::cout << "ifPyErrorThrowCxxException found throwFunc: " << q << std::endl; +#endif + (func->second)(pvalue); + } + else + { +#ifdef PYCXX_DEBUG + std::cout << "ifPyErrorThrowCxxException no throwFunc: " << q << std::endl; +#endif + throw Exception(); + } + } +} + +void initExceptions() +{ + static bool init_done = false; + if( init_done ) + { + return; + } + +#define PYCXX_STANDARD_EXCEPTION( eclass, bclass ) \ + addPythonException( eclass::exceptionType(), eclass::throwFuncObj ); + +#include "cxx_standard_exceptions.hxx" + +#undef PYCXX_STANDARD_EXCEPTION + + init_done = true; +} + + +} // end of namespace Py |
