-
Notifications
You must be signed in to change notification settings - Fork 10
/
pybind11_qt_sip.cpp
64 lines (56 loc) · 2.57 KB
/
pybind11_qt_sip.cpp
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
#include "pybind11_qt/details/pybind11_qt_sip.h"
#include <stdexcept>
#include <QString>
namespace py = pybind11;
namespace pybind11::detail::qt {
const sipAPIDef* sipAPI()
{
std::string exception;
static const sipAPIDef* sipApi = nullptr;
if (sipApi == nullptr) {
PyImport_ImportModule("PyQt6.sip");
{
auto errorObj = PyErr_Occurred();
if (errorObj != NULL) {
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyErr_NormalizeException(&type, &value, &traceback);
if (traceback != NULL) {
py::handle h_type(type);
py::handle h_val(value);
py::handle h_tb(traceback);
py::object tb(py::module_::import("traceback"));
py::object fmt_exp(tb.attr("format_exception"));
py::object exp_list(fmt_exp(h_type, h_val, h_tb));
py::object exp_str(py::str("\n").attr("join")(exp_list));
exception = exp_str.cast<std::string>();
}
PyErr_Restore(type, value, traceback);
throw std::runtime_error{"Failed to load SIP API: " + exception};
}
}
sipApi = (const sipAPIDef*)PyCapsule_Import("PyQt6.sip._C_API", 0);
if (sipApi == NULL) {
auto errorObj = PyErr_Occurred();
if (errorObj != NULL) {
PyObject *type, *value, *traceback;
PyErr_Fetch(&type, &value, &traceback);
PyErr_NormalizeException(&type, &value, &traceback);
if (traceback != NULL) {
py::handle h_type(type);
py::handle h_val(value);
py::handle h_tb(traceback);
py::object tb(py::module_::import("traceback"));
py::object fmt_exp(tb.attr("format_exception"));
py::object exp_list(fmt_exp(h_type, h_val, h_tb));
py::object exp_str(py::str("\n").attr("join")(exp_list));
exception = exp_str.cast<std::string>();
}
PyErr_Restore(type, value, traceback);
}
throw std::runtime_error{"Failed to load SIP API: " + exception};
}
}
return sipApi;
}
} // namespace pybind11::detail::qt