1 // This is the support for QMessageLogger.
2 //
3 // Copyright (c) 2021 Riverbank Computing Limited <info@riverbankcomputing.com>
4 //
5 // This file is part of PyQt5.
6 //
7 // This file may be used under the terms of the GNU General Public License
8 // version 3.0 as published by the Free Software Foundation and appearing in
9 // the file LICENSE included in the packaging of this file. Please review the
10 // following information to ensure the GNU General Public License version 3.0
11 // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 //
13 // If you do not wish to use this file under the terms of the GPL version 3.0
14 // then you may purchase a commercial license. For more information contact
15 // info@riverbankcomputing.com.
16 //
17 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19
20
21 #include <Python.h>
22
23 #include "qpycore_api.h"
24
25 #include "sipAPIQtCore.h"
26
27
28 // Return the current Python context. This should be called with the GIL.
qpycore_current_context(const char ** file,const char ** function)29 int qpycore_current_context(const char **file, const char **function)
30 {
31 static PyObject *currentframe = 0;
32 static PyObject *getframeinfo = 0;
33 static PyObject *saved_file = 0;
34 static PyObject *saved_function = 0;
35
36 PyObject *frame, *info, *file_obj, *linenr_obj, *function_obj;
37 int linenr;
38
39 frame = info = NULL;
40
41 // Make sure we have what we need from the inspect module.
42 if (!currentframe || !getframeinfo)
43 {
44 PyObject *inspect = PyImport_ImportModule("inspect");
45
46 if (inspect)
47 {
48 if (!currentframe)
49 currentframe = PyObject_GetAttrString(inspect, "currentframe");
50
51 if (!getframeinfo)
52 getframeinfo = PyObject_GetAttrString(inspect, "getframeinfo");
53
54 Py_DECREF(inspect);
55 }
56
57 if (!currentframe || !getframeinfo)
58 goto py_error;
59 }
60
61 // Get the current frame.
62 if ((frame = PyObject_CallFunctionObjArgs(currentframe, NULL)) == NULL)
63 goto py_error;
64
65 // Get the frame details.
66 if ((info = PyObject_CallFunctionObjArgs(getframeinfo, frame, NULL)) == NULL)
67 goto py_error;;
68
69 if ((file_obj = PyTuple_GetItem(info, 0)) == NULL)
70 goto py_error;
71
72 if ((linenr_obj = PyTuple_GetItem(info, 1)) == NULL)
73 goto py_error;
74
75 if ((function_obj = PyTuple_GetItem(info, 2)) == NULL)
76 goto py_error;
77
78 Py_XDECREF(saved_file);
79 #if PY_MAJOR_VERSION >= 3
80 saved_file = PyUnicode_AsEncodedString(file_obj, "latin_1", "ignore");
81 #else
82 saved_file = file_obj;
83 Py_INCREF(saved_file);
84 #endif
85 *file = SIPBytes_AsString(saved_file);
86
87 // Ignore any overflow exception.
88 linenr = sipLong_AsInt(linenr_obj);
89
90 Py_XDECREF(saved_function);
91 #if PY_MAJOR_VERSION >= 3
92 saved_function = PyUnicode_AsEncodedString(function_obj, "latin_1", "ignore");
93 #else
94 saved_function = function_obj;
95 Py_INCREF(saved_function);
96 #endif
97 *function = SIPBytes_AsString(saved_function);
98
99 Py_DECREF(info);
100 Py_DECREF(frame);
101
102 return linenr;
103
104 py_error:
105 Py_XDECREF(info);
106 Py_XDECREF(frame);
107
108 pyqt5_err_print();
109
110 *file = *function = "";
111 return 0;
112 }
113