summaryrefslogtreecommitdiff
path: root/venv/lib/python3.11/site-packages/greenlet/greenlet_exceptions.hpp
blob: 3807018bdc6dedbd808afc9d197b29ba5733a7fa (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef GREENLET_EXCEPTIONS_HPP
#define GREENLET_EXCEPTIONS_HPP

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdexcept>
#include <string>

#ifdef __clang__
#    pragma clang diagnostic push
#    pragma clang diagnostic ignored "-Wunused-function"
#endif

namespace greenlet {

    class PyErrOccurred : public std::runtime_error
    {
    public:

        // CAUTION: In debug builds, may run arbitrary Python code.
        static const PyErrOccurred
        from_current()
        {
            assert(PyErr_Occurred());
#ifndef NDEBUG
            // This is not exception safe, and
            // not necessarily safe in general (what if it switches?)
            // But we only do this in debug mode, where we are in
            // tight control of what exceptions are getting raised and
            // can prevent those issues.

            // You can't call PyObject_Str with a pending exception.
            PyObject* typ;
            PyObject* val;
            PyObject* tb;

            PyErr_Fetch(&typ, &val, &tb);
            PyObject* typs = PyObject_Str(typ);
            PyObject* vals = PyObject_Str(val ? val : typ);
            const char* typ_msg = PyUnicode_AsUTF8(typs);
            const char* val_msg = PyUnicode_AsUTF8(vals);
            PyErr_Restore(typ, val, tb);

            std::string msg(typ_msg);
            msg += ": ";
            msg += val_msg;
            PyErrOccurred ex(msg);
            Py_XDECREF(typs);
            Py_XDECREF(vals);

            return ex;
#else
            return PyErrOccurred();
#endif
        }

        PyErrOccurred() : std::runtime_error("")
        {
            assert(PyErr_Occurred());
        }

        PyErrOccurred(const std::string& msg) : std::runtime_error(msg)
        {
            assert(PyErr_Occurred());
        }

        PyErrOccurred(PyObject* exc_kind, const char* const msg)
            : std::runtime_error(msg)
        {
            PyErr_SetString(exc_kind, msg);
        }

        PyErrOccurred(PyObject* exc_kind, const std::string msg)
            : std::runtime_error(msg)
        {
            // This copies the c_str, so we don't have any lifetime
            // issues to worry about.
            PyErr_SetString(exc_kind, msg.c_str());
        }
    };

    class TypeError : public PyErrOccurred
    {
    public:
        TypeError(const char* const what)
            : PyErrOccurred(PyExc_TypeError, what)
        {
        }
        TypeError(const std::string what)
            : PyErrOccurred(PyExc_TypeError, what)
        {
        }
    };

    class ValueError : public PyErrOccurred
    {
    public:
        ValueError(const char* const what)
            : PyErrOccurred(PyExc_ValueError, what)
        {
        }
    };

    class AttributeError : public PyErrOccurred
    {
    public:
        AttributeError(const char* const what)
            : PyErrOccurred(PyExc_AttributeError, what)
        {
        }
    };

    /**
     * Calls `Py_FatalError` when constructed, so you can't actually
     * throw this. It just makes static analysis easier.
     */
    class PyFatalError : public std::runtime_error
    {
    public:
        PyFatalError(const char* const msg)
            : std::runtime_error(msg)
        {
            Py_FatalError(msg);
        }
    };

    static inline PyObject*
    Require(PyObject* p, const std::string& msg="")
    {
        if (!p) {
            throw PyErrOccurred(msg);
        }
        return p;
    };

    static inline void
    Require(const int retval)
    {
        if (retval < 0) {
            throw PyErrOccurred();
        }
    };


};
#ifdef __clang__
#    pragma clang diagnostic pop
#endif

#endif