| 1 |
%{ |
| 2 |
/** |
| 3 |
* HAVE_SNPRINTF is defined in several places. This prevents the warning message |
| 4 |
* from appearing when compiling the swig interface. |
| 5 |
*/ |
| 6 |
#ifdef HAVE_SNPRINTF |
| 7 |
#undef HAVE_SNPRINTF |
| 8 |
#endif |
| 9 |
|
| 10 |
/** |
| 11 |
* This is a workaround for a minor swig bug when building on gcc 4.6.1 and above. |
| 12 |
* Prior to gcc 4.6.1 the STL headers like vector, string, etc. used to |
| 13 |
* automatically pull in the cstddef header but starting with gcc 4.6.1 they no |
| 14 |
* longer do. This leads to swig generated a file that does not compile so we |
| 15 |
* explicitly include cstddef so the swig generated file will compile. |
| 16 |
*/ |
| 17 |
#include <cstddef> |
| 18 |
|
| 19 |
%} |
| 20 |
%include "std_string.i" |
| 21 |
%include "std_vector.i" |
| 22 |
%include "std_pair.i" |
| 23 |
%include "std_list.i" |
| 24 |
%include "std_map.i" |
| 25 |
%include "std_set.i" |
| 26 |
%include "typemaps.i" |
| 27 |
%include "exception.i" |
| 28 |
|
| 29 |
%module(directors="1") fife |
| 30 |
$imports |
| 31 |
|
| 32 |
/** |
| 33 |
* Some materials to understand exception handling: |
| 34 |
* |
| 35 |
* Basics about python exceptions: |
| 36 |
* http://docs.python.org/tut/node10.html |
| 37 |
* Python exception handling in C APIs |
| 38 |
* http://docs.python.org/api/exceptions.html |
| 39 |
* http://docs.python.org/api/exceptionHandling.html |
| 40 |
* SWIG exception handling |
| 41 |
* http://www.swig.org/Doc1.3/Customization.html#exception |
| 42 |
* http://www.swig.org/Doc1.3/SWIGPlus.html#SWIGPlus_exception_specifications |
| 43 |
* http://www.swig.org/Doc1.3/Python.html#Python_nn36 |
| 44 |
*/ |
| 45 |
|
| 46 |
%feature("autodoc", "1"); // 0 == no param types, 1 == show param types |
| 47 |
|
| 48 |
%{ |
| 49 |
#include "util/base/fife_stdint.h" |
| 50 |
%} |
| 51 |
|
| 52 |
/** |
| 53 |
* Integer definitions (See swigs stdint.i implementation) |
| 54 |
* |
| 55 |
*/ |
| 56 |
|
| 57 |
/* Signed. */ |
| 58 |
typedef signed char int8_t; |
| 59 |
typedef short int int16_t; |
| 60 |
typedef int int32_t; |
| 61 |
#if defined(SWIGWORDSIZE64) |
| 62 |
typedef long int int64_t; |
| 63 |
#else |
| 64 |
typedef long long int int64_t; |
| 65 |
#endif |
| 66 |
|
| 67 |
/* Unsigned. */ |
| 68 |
typedef unsigned char uint8_t; |
| 69 |
typedef unsigned short int uint16_t; |
| 70 |
typedef unsigned int uint32_t; |
| 71 |
#if defined(SWIGWORDSIZE64) |
| 72 |
typedef unsigned long int uint64_t; |
| 73 |
#else |
| 74 |
typedef unsigned long long int uint64_t; |
| 75 |
#endif |
| 76 |
|
| 77 |
namespace std { |
| 78 |
%template(StringVector) vector<std::string>; |
| 79 |
%template(UintVector) vector<uint32_t>; |
| 80 |
%template(IntVector) vector<int32_t>; |
| 81 |
%template(FloatVector) vector<float>; |
| 82 |
%template(DoubleVector) vector<double>; |
| 83 |
%template(BoolVector) vector<bool>; |
| 84 |
|
| 85 |
%template(Uint16Uint16Pair) pair<uint16_t, uint16_t>; |
| 86 |
%template(Uint16Uint16PairVector) vector<std::pair<uint16_t, uint16_t> >; |
| 87 |
}; |
| 88 |
|
| 89 |
%{ |
| 90 |
#include "util/base/exception.h" |
| 91 |
static void handleDirectorException() { |
| 92 |
PyObject* exception = NULL; |
| 93 |
PyObject* value = NULL; |
| 94 |
PyObject* traceback = NULL; |
| 95 |
PyErr_Fetch(&exception, &value, &traceback); |
| 96 |
PyErr_NormalizeException(&exception, &value, &traceback); |
| 97 |
if (exception) { |
| 98 |
PySys_SetObject((char*)"last_type", exception); |
| 99 |
PySys_SetObject((char*)"last_value", value); |
| 100 |
PySys_SetObject((char*)"last_traceback", traceback); |
| 101 |
|
| 102 |
PyObject* d = PyModule_GetDict (PyImport_AddModule ("__main__")); |
| 103 |
PyDict_SetItemString(d, "exc_type", exception); |
| 104 |
PyDict_SetItemString(d, "exc_value", value); |
| 105 |
PyDict_SetItemString(d, "exc_traceback", traceback ? traceback : Py_None); |
| 106 |
|
| 107 |
char buf[1024]; |
| 108 |
sprintf (buf, "\n\ |
| 109 |
import traceback\n\ |
| 110 |
s = 'Traceback (most recent call last):\\n'\n\ |
| 111 |
for filename, line, function, text in traceback.extract_tb(exc_traceback):\n\ |
| 112 |
s = s + ' File \"%%s\", line %%d, in %%s\\n %%s' %% (filename, line, function, text)\n\ |
| 113 |
if s[-1] != '\\n': s = s + '\\n'\n\ |
| 114 |
for l in traceback.format_exception_only(exc_type, exc_value):\n\ |
| 115 |
s = s + l\n\ |
| 116 |
if s[-1] != '\\n': s = s + '\\n'\n\ |
| 117 |
print(s)\n\ |
| 118 |
"); |
| 119 |
PyObject* e = PyRun_String(buf, Py_file_input, d, d); |
| 120 |
if (!e) { |
| 121 |
PyErr_Print(); |
| 122 |
} |
| 123 |
Py_XDECREF(e); |
| 124 |
Py_XDECREF(d); |
| 125 |
Py_XDECREF(exception); |
| 126 |
Py_XDECREF(value); |
| 127 |
Py_XDECREF(traceback); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#define _FIFE_CONVERTED_EXC_HANDLER(_fife_exc_type, _converted_type) \ |
| 132 |
catch (FIFE::_fife_exc_type& _e) { \ |
| 133 |
PyErr_Clear(); \ |
| 134 |
SWIG_exception(_converted_type, _e.what()); \ |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
#define _FIFE_EXC_HANDLER(_fife_exc_type) \ |
| 139 |
catch (FIFE::_fife_exc_type& _e) { \ |
| 140 |
SWIG_Python_Raise( \ |
| 141 |
SWIG_NewPointerObj( \ |
| 142 |
new FIFE::_fife_exc_type(_e), \ |
| 143 |
SWIGTYPE_p_FIFE__##_fife_exc_type, SWIG_POINTER_OWN), \ |
| 144 |
#_fife_exc_type, SWIGTYPE_p_FIFE__##_fife_exc_type); \ |
| 145 |
SWIG_fail; \ |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
#define _FIFE_DIRECTOR_EXC_HANDLER() \ |
| 150 |
catch (Swig::DirectorException &) { \ |
| 151 |
PyErr_Clear(); \ |
| 152 |
SWIG_exception(SWIG_RuntimeError, "Caught a director exception"); \ |
| 153 |
} |
| 154 |
%} |
| 155 |
|
| 156 |
%exceptionclass FIFE::Exception; |
| 157 |
%exceptionclass FIFE::SDLException; |
| 158 |
%exceptionclass FIFE::NotFound; |
| 159 |
%exceptionclass FIFE::NotSet; |
| 160 |
%exceptionclass FIFE::IndexOverflow; |
| 161 |
%exceptionclass FIFE::InvalidFormat; |
| 162 |
%exceptionclass FIFE::CannotOpenFile; |
| 163 |
%exceptionclass FIFE::InvalidConversion; |
| 164 |
%exceptionclass FIFE::NotSupported; |
| 165 |
%exceptionclass FIFE::NameClash; |
| 166 |
%exceptionclass FIFE::Duplicate; |
| 167 |
%exceptionclass FIFE::ScriptException; |
| 168 |
%exceptionclass FIFE::EventException; |
| 169 |
%exceptionclass FIFE::GuiException; |
| 170 |
%exceptionclass FIFE::InconsistencyDetected; |
| 171 |
%exceptionclass FIFE::OutOfMemory; |
| 172 |
|
| 173 |
%extend FIFE::Exception { |
| 174 |
const char *__str__() { |
| 175 |
return self->what(); |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
%feature("director:except") { |
| 180 |
if ($$error != NULL) { |
| 181 |
handleDirectorException(); |
| 182 |
throw Swig::DirectorMethodException(); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
%exception { |
| 187 |
try { |
| 188 |
$$action |
| 189 |
} |
| 190 |
_FIFE_DIRECTOR_EXC_HANDLER() |
| 191 |
_FIFE_EXC_HANDLER(SDLException) |
| 192 |
_FIFE_EXC_HANDLER(NotFound) |
| 193 |
_FIFE_EXC_HANDLER(NotSet) |
| 194 |
_FIFE_EXC_HANDLER(IndexOverflow) |
| 195 |
_FIFE_EXC_HANDLER(InvalidFormat) |
| 196 |
_FIFE_EXC_HANDLER(CannotOpenFile) |
| 197 |
_FIFE_EXC_HANDLER(InvalidConversion) |
| 198 |
_FIFE_EXC_HANDLER(NotSupported) |
| 199 |
_FIFE_EXC_HANDLER(NameClash) |
| 200 |
_FIFE_EXC_HANDLER(Duplicate) |
| 201 |
_FIFE_EXC_HANDLER(ScriptException) |
| 202 |
_FIFE_EXC_HANDLER(EventException) |
| 203 |
_FIFE_EXC_HANDLER(GuiException) |
| 204 |
_FIFE_EXC_HANDLER(InconsistencyDetected) |
| 205 |
_FIFE_EXC_HANDLER(OutOfMemory) |
| 206 |
_FIFE_EXC_HANDLER(Exception) |
| 207 |
} |
| 208 |
|
| 209 |
$inclusions |
| 210 |
|
| 211 |
%include engine/swigwrappers/python/extensions.i.templ |