comparison engine/swigwrappers/python/fife.i.templ @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 98541d3b9220
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 %module(directors="1") fife
2 %include "std_string.i"
3 %include "std_vector.i"
4 %include "typemaps.i"
5 %include "exception.i"
6
7 /**
8 * Some materials to understand exception handling:
9 *
10 * Basics about python exceptions:
11 * http://docs.python.org/tut/node10.html
12 * Python exception handling in C APIs
13 * http://docs.python.org/api/exceptions.html
14 * http://docs.python.org/api/exceptionHandling.html
15 * SWIG exception handling
16 * http://www.swig.org/Doc1.3/Customization.html#exception
17 * http://www.swig.org/Doc1.3/SWIGPlus.html#SWIGPlus_exception_specifications
18 * http://www.swig.org/Doc1.3/Python.html#Python_nn36
19 */
20
21 %feature("autodoc", "1"); // 0 == no param types, 1 == show param types
22
23 namespace std {
24 %template(StringVector) vector<std::string>;
25 %template(UintVector) vector<unsigned int>;
26 %template(IntVector) vector<int>;
27 %template(FloatVector) vector<float>;
28 %template(DoubleVector) vector<double>;
29 %template(BoolVector) vector<bool>;
30 };
31
32 %{
33 #include "util/base/exception.h"
34 static void handleDirectorException() {
35 PyObject* exception = NULL;
36 PyObject* value = NULL;
37 PyObject* traceback = NULL;
38 PyErr_Fetch(&exception, &value, &traceback);
39 PyErr_NormalizeException(&exception, &value, &traceback);
40 if (exception) {
41 PySys_SetObject("last_type", exception);
42 PySys_SetObject("last_value", value);
43 PySys_SetObject("last_traceback", traceback);
44
45 PyObject* d = PyModule_GetDict (PyImport_AddModule ("__main__"));
46 PyDict_SetItemString(d, "exc_type", exception);
47 PyDict_SetItemString(d, "exc_value", value);
48 PyDict_SetItemString(d, "exc_traceback", traceback ? traceback : Py_None);
49
50 char buf[1024];
51 sprintf (buf, "\n\
52 import traceback\n\
53 s = \"\"\n\
54 for filename, line, function, text in traceback.extract_tb(exc_traceback):\n\
55 s = s + ' File \"%%s\", line %%d, in %%s\\n %%s' %% (filename, line, function, text)\n\
56 if s[-1] != '\\n': s = s + '\\n'\n\
57 for l in traceback.format_exception_only(exc_type, exc_value):\n\
58 s = s + ' ' + l\n\
59 if s[-1] != '\\n': s = s + '\\n'\n\
60 print s\n\
61 ");
62 PyObject* e = PyRun_String(buf, Py_file_input, d, d);
63 if (!e) {
64 PyErr_Print();
65 }
66 Py_XDECREF(e);
67 Py_XDECREF(d);
68 Py_XDECREF(exception);
69 Py_XDECREF(value);
70 Py_XDECREF(traceback);
71 }
72 }
73
74 #define _FIFE_EXC_HANDLER(_fife_exc_type, _converted_type) \
75 catch (FIFE::_fife_exc_type& _e) { \
76 PyErr_Clear(); \
77 SWIG_exception(_converted_type, _e.getMessage().c_str()); \
78 }
79
80 #define _FIFE_DIRECTOR_EXC_HANDLER() \
81 catch (Swig::DirectorException &_e) { \
82 PyErr_Clear(); \
83 SWIG_exception(SWIG_RuntimeError, "Catched director exception"); \
84 }
85 %}
86
87 %feature("director:except") {
88 if ($$error != NULL) {
89 handleDirectorException();
90 throw Swig::DirectorMethodException();
91 }
92 }
93
94 %exception {
95 try {
96 $$action
97 }
98 _FIFE_DIRECTOR_EXC_HANDLER()
99 _FIFE_EXC_HANDLER(Exception, SWIG_RuntimeError)
100 }
101
102 $inclusions
103
104 %include engine/swigwrappers/python/extensions.i.templ