File: PythonExceptionTypemaps.i

package info (click to toggle)
robotraconteur 1.2.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,380 kB
  • sloc: cpp: 1,149,268; cs: 87,653; java: 58,127; python: 26,897; ansic: 356; sh: 152; makefile: 90; xml: 51
file content (251 lines) | stat: -rw-r--r-- 7,679 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//Allow all STL errors to fall through to the global error catcher
%typemap(throws) std::bad_exception     "throw _e;"
%typemap(throws) std::domain_error      "throw _e;"
%typemap(throws) std::exception         "throw _e;"
%typemap(throws) std::invalid_argument  "throw _e;"
%typemap(throws) std::length_error      "throw _e;"
%typemap(throws) std::logic_error       "throw _e;"
%typemap(throws) std::out_of_range      "throw _e;"
%typemap(throws) std::overflow_error    "throw _e;"
%typemap(throws) std::range_error       "throw _e;"
%typemap(throws) std::runtime_error     "throw _e;"
%typemap(throws) std::underflow_error   "throw _e;"

%{

static void RRExceptionToPythonError(RobotRaconteurException& rrexp)
{
	PyObject* modules_dict= PyImport_GetModuleDict();
	if (modules_dict==NULL)
	{
		PyErr_SetString(PyExc_Exception, "Could not load RobotRaconteurPythonError module");
		return;
	}

	PyObject* err_module=PyDict_GetItemString(modules_dict, "RobotRaconteur.RobotRaconteurPythonError");
	if (err_module==NULL)
	{
		PyErr_SetString(PyExc_Exception, "Could not load RobotRaconteurPythonError module");
		return;
	}

	swig::SwigVar_PyObject exceptionUtil=PyObject_GetAttrString(err_module, "RobotRaconteurExceptionUtil");
	if (exceptionUtil==NULL)
	{
		PyErr_SetString(PyExc_Exception, "Could not load RobotRaconteurPythonError.RobotRaconteurExceptionUtil class");
		return;
	}

	swig::SwigVar_PyObject errorCodeToException=PyObject_GetAttrString(exceptionUtil, "ErrorInfoToException");
	if (errorCodeToException==NULL)
	{
		PyErr_SetString(PyExc_Exception, "Could not load RobotRaconteurExceptionUtil.ErrorInfoToException function");
		return;
	}

	RobotRaconteur::HandlerErrorInfo error(rrexp);

	swig::SwigVar_PyObject error_obj;
  	error_obj = SWIG_NewPointerObj(SWIG_as_voidptr(&error), SWIGTYPE_p_RobotRaconteur__HandlerErrorInfo,  0 );

	swig::SwigVar_PyObject pyErr=PyObject_CallFunction(errorCodeToException, "O", (PyObject*)error_obj);
	swig::SwigVar_PyObject pyErrType=PyObject_Type(pyErr);

	PyErr_SetObject(pyErrType,pyErr);
}

%}

%define RR_Py_Exception_GIL()
%exception %{

    try {
		RR_Release_GIL gil_save;
        $action
    }

    catch (RobotRaconteurException &e) {
		//PyErr_SetString(PyExc_Exception, const_cast<char*>(e.what()));
		RRExceptionToPythonError(e);
		goto fail;
	}
    catch (std::exception &e) {
        PyErr_SetString(PyExc_Exception, const_cast<char*>(e.what()));
        goto fail;
    }
    /*catch (...)
    {
		PyErr_SetString(PyExc_Exception,"Unknown Robot Raconteur error");
		goto fail;
    }*/

%}
%enddef

%define RR_Py_Exception()
%exception %{
    try {
        $action
    }

    catch (RobotRaconteurException &e) {
		//PyErr_SetString(PyExc_Exception, const_cast<char*>(e.what()));
		RRExceptionToPythonError(e);
		goto fail;
	}
    catch (std::exception &e) {
        PyErr_SetString(PyExc_Exception, const_cast<char*>(e.what()));
        goto fail;
    }
    /*catch (...)
    {
		PyErr_SetString(PyExc_Exception,"Unknown Robot Raconteur error");
		goto fail;
    }*/

%}
%enddef

RR_Py_Exception()

%{

static void ThrowPythonError()
{
	PyObject* exc1;
	PyObject* val1;
	PyObject* tb1;
	PyErr_Fetch(&exc1,&val1,&tb1);
	PyErr_NormalizeException(&exc1,&val1,&tb1);
	swig::SwigVar_PyObject exc = exc1;
	swig::SwigVar_PyObject val = val1;
	swig::SwigVar_PyObject tb = tb1;

	if (RobotRaconteur::PythonTracebackPrintExc)
	{
		std::cerr << "RobotRaconteurPython caught exception:" << std::endl;
		if (exc && val && tb)
		{
			PyErr_Display(exc,val,tb);
		}
		PyErr_Clear();
	}
	else
	{
		PyErr_Clear();
	}

	PyObject* modules_dict= PyImport_GetModuleDict();
	if (modules_dict==NULL)
	{
		throw InternalErrorException("Could not load RobotRaconteurPythonError module");
	}

	PyObject* err_module=PyDict_GetItemString(modules_dict, "RobotRaconteur.RobotRaconteurPythonError");
	if (err_module==NULL)
	{
		throw InternalErrorException("Could not load RobotRaconteurPythonError module");
	}

	swig::SwigVar_PyObject rr_py_RobotRaconteurException=PyObject_GetAttrString(err_module, "RobotRaconteurException");
	if (rr_py_RobotRaconteurException==NULL)
	{
		throw InternalErrorException("Could not load RobotRaconteurPythonError.RobotRaconteurException type");
	}

	if (PyErr_GivenExceptionMatches(exc,rr_py_RobotRaconteurException))
	{
		swig::SwigVar_PyObject message1 = PyObject_GetAttrString(val,"message");
		swig::SwigVar_PyObject errorname1 = PyObject_GetAttrString(val,"errorname");
		swig::SwigVar_PyObject errorcode1 = PyObject_GetAttrString(val,"errorcode");

		swig::SwigVar_PyObject message=PyObject_Str(message1);
		swig::SwigVar_PyObject errorname=PyObject_Str(errorname1);
		swig::SwigVar_PyObject errorcode=PyNumber_Long(errorcode1);

		if (!(PyObject*)message || !(PyObject*)errorcode || !(PyObject*)errorname)
		{
			throw InternalErrorException("Exception occurred in Python code");
		}

		std::string message2 = PyObjectToUTF8(message);
		std::string errorname2=PyObjectToUTF8(errorname);
		long errorcode2=PyLong_AsLong(errorcode);
		if (errorcode2 > 65535 || errorcode2 < 0) errorcode2=MessageErrorType_UnknownError;

		boost::intrusive_ptr<MessageEntry> m=CreateMessageEntry();
		m->Error=(MessageErrorType)(uint16_t)errorcode2;
		m->AddElement("errorname",stringToRRArray(errorname2));
		m->AddElement("errorstring", stringToRRArray(message2));

		swig::SwigVar_PyObject errorsubname = PyObject_GetAttrString(val,"errorsubname");
		if ((PyObject*)errorsubname && (PyObject*)errorsubname != Py_None)
		{
			swig::SwigVar_PyObject errorsubname2 = PyObject_Str(errorsubname);
			if ((PyObject*)errorsubname2)
			{
				m->AddElement("errorsubname",stringToRRArray(PyObjectToUTF8(errorsubname2)));
			}
		}

		swig::SwigVar_PyObject errorparam = PyObject_GetAttrString(val,"errorparam");
		if ((PyObject*)errorparam && (PyObject*)errorparam != Py_None)
		{
			PyObject* util_module=PyDict_GetItemString(modules_dict, "RobotRaconteur.RobotRaconteurPythonUtil");
			if (util_module)
			{
				swig::SwigVar_PyObject python_pack_element=PyObject_GetAttrString(util_module, "PackMessageElement");
				if ((PyObject*)python_pack_element)
				{
					swig::SwigVar_PyObject py_errorparam_elem = PyObject_CallFunction(python_pack_element,"OsOO",(PyObject*)errorparam,"varvalue errorparam",Py_None,Py_None);
					if ((PyObject*)py_errorparam_elem)
					{
						try
						{
							boost::intrusive_ptr< RobotRaconteur::MessageElement > arg1 ;
							void *argp1 ;
							int newmem1 = 0 ;
							// Extract swig pointer
							int res1 = SWIG_ConvertPtrAndOwn(py_errorparam_elem, &argp1, SWIGTYPE_p_boost__shared_ptrT_RobotRaconteur__MessageElement_t,  0 , &newmem1);
							if (SWIG_IsOK(res1)) {
								if (argp1) arg1 = boost::intrusive_ptr<  RobotRaconteur::MessageElement >(reinterpret_cast< boost::shared_ptr< RobotRaconteur::MessageElement >* >(argp1)->get(),true);
								if (newmem1 & SWIG_CAST_NEW_MEMORY) delete reinterpret_cast< boost::shared_ptr< RobotRaconteur::MessageElement >* >(argp1);
								if (arg1)
								{
									arg1->ElementName = "errorparam";
									m->AddElement(arg1);
								}
							}

						}
						catch (std::exception)
						{
							//TODO: log error
						}
					}
				}
			}
		}


		RobotRaconteurExceptionUtil::ThrowMessageEntryException(m);
	}
	else
	{
		swig::SwigVar_PyObject str=PyObject_Str(val);
		swig::SwigVar_PyObject strname1 = PyObject_GetAttrString(exc,"__name__");
		swig::SwigVar_PyObject strname=PyObject_Str(strname1);
		std::string str2=PyObjectToUTF8(str);
		std::string strname2=PyObjectToUTF8(strname);

		throw UnknownException(strname2, str2);
	}
}

%}

%feature("director:except") {
    if ($error != NULL) {
    	ThrowPythonError();
    }
}