Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
PyException.cxx
Go to the documentation of this file.
1// Standard
2#include <string.h>
3
4// Bindings
5#include "CPyCppyy.h"
6#define CPYCPPYY_INTERNAL 1
8#undef CPYCPPYY_INTERNAL
9
10
11//______________________________________________________________________________
12// C++ exception for throwing python exceptions
13// ============================================
14// Purpose: A C++ exception class for throwing python exceptions
15// through C++ code.
16// Created: Apr, 2004, Scott Snyder, from the version in D0's python_util.
17//
18// Note: Don't be tempted to declare the virtual functions defined here
19// as inline.
20// If you do, you may not be able to properly throw these
21// exceptions across shared libraries.
22
23
24//- constructors/destructor --------------------------------------------------
26{
27#ifdef WITH_THREAD
28 PyGILState_STATE state = PyGILState_Ensure();
29#endif
30
31#if PY_VERSION_HEX >= 0x030c0000
33 PyObject *pytype = pyvalue ? (PyObject *)Py_TYPE(pyvalue) : nullptr;
34 PyObject* traceback = pyvalue ? PyException_GetTraceback(pyvalue) : nullptr;
35#else
36 PyObject* pytype = nullptr, *pyvalue = nullptr, *pytrace = nullptr;
38 PyObject* traceback = pytrace; // to keep the original unchanged
39 Py_XINCREF(traceback);
40#endif
41
42 if (pytype && pyvalue) {
43 const char* tname = PyExceptionClass_Name(pytype);
44 if (tname) {
45 char* dot = strrchr((char*)tname, '.');
46 if (dot) tname = dot+1;
47 fMsg += tname;
48 fMsg += ": ";
49 }
50
52 if (msg) {
55 }
56 }
57
58 std::string locName;
59 std::string locFile;
60 int locLine = 0;
61
62 while (traceback && traceback != Py_None) {
63 PyObject* frame = PyObject_GetAttrString(traceback, "tb_frame");
64 PyObject* code = PyObject_GetAttrString(frame, "f_code");
65 Py_DECREF(frame);
66
67 PyObject* filename = PyObject_GetAttrString(code, "co_filename");
68 Py_DECREF(code);
69
74
75 PyObject* name = PyObject_GetAttrString(code, "co_name");
80
81 PyObject* lineno = PyObject_GetAttrString(traceback, "tb_lineno");
84
85 if (locFile == "<string>") { // these are not that useful, skipping
86 PyObject* nextTraceback = PyObject_GetAttrString(traceback, "tb_next");
87 Py_DECREF(traceback);
88 traceback = nextTraceback;
89 continue;
90 }
91
92 break;
93 }
94
95 Py_XDECREF(traceback);
96
97#if PY_VERSION_HEX >= 0x030c0000
99#else
101#endif
102
103 if (fMsg.empty())
104 fMsg = "python exception";
105
106 if (!locFile.empty()) {
107
108 // only keeping the filename, not the full path
109 locFile = locFile.substr(locFile.find_last_of("/\") + 1);
110
111 fMsg += " (at " + locFile + ":" + std::to_string(locLine);
112
113 if (locName != "<module>")
114 fMsg += " in " + locName;
115
116 fMsg += ")";
117 }
118
119#ifdef WITH_THREAD
120 PyGILState_Release(state);
121#endif
122}
123
128
129
130//- public members -----------------------------------------------------------
132{
133// Return reason for throwing this exception: a python exception was raised.
134 return fMsg.c_str();
135}
136
138{
139// clear Python error, to allow full error handling C++ side
140 PyErr_Clear();
141}
#define Py_TYPE(ob)
Definition CPyCppyy.h:196
#define CPyCppyy_PyText_AsString
Definition CPyCppyy.h:76
_object PyObject
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
char name[80]
Definition TGX11.cxx:110
virtual const char * what() const noexcept
void clear() const noexcept
virtual ~PyException() noexcept