| 1 |
/* |
|---|
| 2 |
Copyright 2006, 2007 Kirk McDonald |
|---|
| 3 |
|
|---|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
|---|
| 5 |
this software and associated documentation files (the "Software"), to deal in |
|---|
| 6 |
the Software without restriction, including without limitation the rights to |
|---|
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|---|
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
|---|
| 9 |
so, subject to the following conditions: |
|---|
| 10 |
|
|---|
| 11 |
The above copyright notice and this permission notice shall be included in all |
|---|
| 12 |
copies or substantial portions of the Software. |
|---|
| 13 |
|
|---|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|---|
| 20 |
SOFTWARE. |
|---|
| 21 |
*/ |
|---|
| 22 |
module pyd.exception; |
|---|
| 23 |
|
|---|
| 24 |
import python; |
|---|
| 25 |
import pyd.lib_abstract : |
|---|
| 26 |
toString, |
|---|
| 27 |
prettytypeof, |
|---|
| 28 |
objToStr |
|---|
| 29 |
; |
|---|
| 30 |
//import meta.Nameof; |
|---|
| 31 |
//import std.string; |
|---|
| 32 |
|
|---|
| 33 |
/** |
|---|
| 34 |
* This function first checks if a Python exception is set, and then (if one |
|---|
| 35 |
* is) pulls it out, stuffs it in a PythonException, and throws that exception. |
|---|
| 36 |
* |
|---|
| 37 |
* If this exception is never caught, it will be handled by exception_catcher |
|---|
| 38 |
* (below) and passed right back into Python as though nothing happened. |
|---|
| 39 |
*/ |
|---|
| 40 |
void handle_exception() { |
|---|
| 41 |
PyObject* type, value, traceback; |
|---|
| 42 |
if (PyErr_Occurred() !is null) { |
|---|
| 43 |
PyErr_Fetch(&type, &value, &traceback); |
|---|
| 44 |
throw new PythonException(type, value, traceback); |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
// Used internally. |
|---|
| 49 |
T error_code(T) () { |
|---|
| 50 |
static if (is(T == PyObject*)) { |
|---|
| 51 |
return null; |
|---|
| 52 |
} else static if (is(T == int)) { |
|---|
| 53 |
return -1; |
|---|
| 54 |
} else static if (is(T == void)) { |
|---|
| 55 |
return; |
|---|
| 56 |
} else static assert(false, "exception_catcher cannot handle return type " ~ prettytypeof!(T)); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
/** |
|---|
| 60 |
* It is intended that any functions that interface directly with Python which |
|---|
| 61 |
* have the possibility of a D exception being raised wrap their contents in a |
|---|
| 62 |
* call to this function, e.g.: |
|---|
| 63 |
* |
|---|
| 64 |
*$(D_CODE extern (C) |
|---|
| 65 |
*PyObject* some_func(PyObject* self) { |
|---|
| 66 |
* return _exception_catcher({ |
|---|
| 67 |
* // ... |
|---|
| 68 |
* }); |
|---|
| 69 |
*}) |
|---|
| 70 |
*/ |
|---|
| 71 |
T exception_catcher(T) (T delegate() dg) { |
|---|
| 72 |
try { |
|---|
| 73 |
return dg(); |
|---|
| 74 |
} |
|---|
| 75 |
// A Python exception was raised and duly re-thrown as a D exception. |
|---|
| 76 |
// It should now be re-raised as a Python exception. |
|---|
| 77 |
catch (PythonException e) { |
|---|
| 78 |
PyErr_Restore(e.type(), e.value(), e.traceback()); |
|---|
| 79 |
return error_code!(T)(); |
|---|
| 80 |
} |
|---|
| 81 |
// A D exception was raised and should be translated into a meaningful |
|---|
| 82 |
// Python exception. |
|---|
| 83 |
catch (Exception e) { |
|---|
| 84 |
PyErr_SetString(PyExc_RuntimeError, ("D Exception: " ~ e.classinfo.name ~ ": " ~ e.msg ~ \0).ptr); |
|---|
| 85 |
return error_code!(T)(); |
|---|
| 86 |
} |
|---|
| 87 |
// Some other D object was thrown. Deal with it. |
|---|
| 88 |
catch (Object o) { |
|---|
| 89 |
PyErr_SetString(PyExc_RuntimeError, ("thrown D Object: " ~ o.classinfo.name ~ ": " ~ objToStr(o) ~ \0).ptr); |
|---|
| 90 |
return error_code!(T)(); |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
alias exception_catcher!(PyObject*) exception_catcher_PyObjectPtr; |
|---|
| 95 |
alias exception_catcher!(int) exception_catcher_int; |
|---|
| 96 |
alias exception_catcher!(void) exception_catcher_void; |
|---|
| 97 |
|
|---|
| 98 |
/** |
|---|
| 99 |
* This simple exception class holds a Python exception. |
|---|
| 100 |
*/ |
|---|
| 101 |
class PythonException : Exception { |
|---|
| 102 |
protected: |
|---|
| 103 |
PyObject* m_type, m_value, m_trace; |
|---|
| 104 |
public: |
|---|
| 105 |
this(PyObject* type, PyObject* value, PyObject* traceback) { |
|---|
| 106 |
super(.toString(PyString_AsString(value))); |
|---|
| 107 |
m_type = type; |
|---|
| 108 |
m_value = value; |
|---|
| 109 |
m_trace = traceback; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
~this() { |
|---|
| 113 |
if (m_type) Py_DECREF(m_type); |
|---|
| 114 |
if (m_value) Py_DECREF(m_value); |
|---|
| 115 |
if (m_trace) Py_DECREF(m_trace); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
PyObject* type() { |
|---|
| 119 |
if (m_type) Py_INCREF(m_type); |
|---|
| 120 |
return m_type; |
|---|
| 121 |
} |
|---|
| 122 |
PyObject* value() { |
|---|
| 123 |
if (m_value) Py_INCREF(m_value); |
|---|
| 124 |
return m_value; |
|---|
| 125 |
} |
|---|
| 126 |
PyObject* traceback() { |
|---|
| 127 |
if (m_trace) Py_INCREF(m_trace); |
|---|
| 128 |
return m_trace; |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|