summaryrefslogtreecommitdiffstats
path: root/contrib/libs/pycxx/Python3/cxx_exceptions.cxx
blob: 9dff2753ddd853063ac46ccfb067bba702484622 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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