Mercurial > pymonkey
comparison utils.c @ 46:a0f677cfc679
Added basic functionality for passing useful exceptions between Python and JS code.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Mon, 06 Jul 2009 01:37:16 -0700 |
parents | 03aec8572461 |
children | 3f4982759e55 |
comparison
equal
deleted
inserted
replaced
45:03aec8572461 | 46:a0f677cfc679 |
---|---|
130 // TODO: Support more types. | 130 // TODO: Support more types. |
131 PyErr_SetString(PyExc_NotImplementedError, | 131 PyErr_SetString(PyExc_NotImplementedError, |
132 "Data type conversion not implemented."); | 132 "Data type conversion not implemented."); |
133 return NULL; | 133 return NULL; |
134 } | 134 } |
135 | |
136 void | |
137 PYM_pythonExceptionToJs(PYM_JSContextObject *context) | |
138 { | |
139 PyObject *type; | |
140 PyObject *value; | |
141 PyObject *traceback; | |
142 | |
143 PyErr_Fetch(&type, &value, &traceback); | |
144 PyObject *str = PyObject_Unicode(value); | |
145 if (str == NULL) | |
146 JS_ReportError(context->cx, "Python exception occurred"); | |
147 else { | |
148 jsval val; | |
149 if (PYM_pyObjectToJsval(context, str, &val) == 0) | |
150 // TODO: Include filename/line information. | |
151 JS_SetPendingException(context->cx, val); | |
152 else | |
153 JS_ReportError(context->cx, "Python exception occurred"); | |
154 } | |
155 | |
156 Py_DECREF(type); | |
157 Py_DECREF(value); | |
158 Py_DECREF(traceback); | |
159 } | |
160 | |
161 void | |
162 PYM_jsExceptionToPython(PYM_JSContextObject *context) | |
163 { | |
164 if (!JS_IsExceptionPending(context->cx) && | |
165 PyErr_Occurred()) | |
166 return; | |
167 | |
168 jsval val; | |
169 if (JS_GetPendingException(context->cx, &val)) { | |
170 JSString *str = JS_ValueToString(context->cx, val); | |
171 if (str != NULL) { | |
172 const char *chars = JS_GetStringBytes(str); | |
173 PyErr_SetString(PYM_error, chars); | |
174 } else | |
175 PyErr_SetString(PYM_error, "JS exception occurred"); | |
176 } else | |
177 PyErr_SetString(PYM_error, "JS_GetPendingException() failed"); | |
178 } |