blob: 3b86c48bfbc5d66e473b144562a8c4b54d282372 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum373c8691997-04-29 18:22:47 +00002/* Error handling */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Guido van Rossum373c8691997-04-29 18:22:47 +00004#include "Python.h"
Guido van Rossumf22120a1990-12-20 23:05:40 +00005
Guido van Rossum53e8d441995-03-09 12:11:31 +00006#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +00007#ifndef MS_WINDOWS
Tim Petersdbd9ba62000-07-09 03:09:57 +00008extern char *strerror(int);
Guido van Rossum53e8d441995-03-09 12:11:31 +00009#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000010#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000011
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000012#ifdef MS_WINDOWS
Martin v. Löwis5d12abe2007-09-03 07:40:24 +000013#include <windows.h>
14#include <winbase.h>
Guido van Rossum743007d1999-04-21 15:27:31 +000015#endif
16
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +000017#include <ctype.h>
18
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000019#ifdef __cplusplus
20extern "C" {
21#endif
22
23
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000024void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000025PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000026{
Guido van Rossum885553e1998-12-21 18:33:30 +000027 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +000028 PyObject *oldtype, *oldvalue, *oldtraceback;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029
Guido van Rossuma027efa1997-05-05 20:56:21 +000030 if (traceback != NULL && !PyTraceBack_Check(traceback)) {
31 /* XXX Should never happen -- fatal error instead? */
Guido van Rossum1a5e21e2006-02-28 21:57:43 +000032 /* Well, it could be None. */
Guido van Rossuma027efa1997-05-05 20:56:21 +000033 Py_DECREF(traceback);
34 traceback = NULL;
35 }
36
37 /* Save these in locals to safeguard against recursive
38 invocation through Py_XDECREF */
39 oldtype = tstate->curexc_type;
40 oldvalue = tstate->curexc_value;
41 oldtraceback = tstate->curexc_traceback;
42
43 tstate->curexc_type = type;
44 tstate->curexc_value = value;
45 tstate->curexc_traceback = traceback;
46
47 Py_XDECREF(oldtype);
48 Py_XDECREF(oldvalue);
49 Py_XDECREF(oldtraceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000050}
51
52void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000053PyErr_SetObject(PyObject *exception, PyObject *value)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000055 PyThreadState *tstate = PyThreadState_GET();
56 PyObject *tb = NULL;
57
Thomas Wouters303de6a2006-04-20 22:42:37 +000058 if (exception != NULL &&
59 !PyExceptionClass_Check(exception)) {
Thomas Wouters303de6a2006-04-20 22:42:37 +000060 PyErr_Format(PyExc_SystemError,
Walter Dörwald573c08c2007-05-25 15:46:59 +000061 "exception %R not a BaseException subclass",
62 exception);
Thomas Wouters303de6a2006-04-20 22:42:37 +000063 return;
64 }
Guido van Rossum373c8691997-04-29 18:22:47 +000065 Py_XINCREF(value);
Guido van Rossumb4fb6e42008-06-14 20:20:24 +000066 if (tstate->exc_value != NULL && tstate->exc_value != Py_None) {
67 /* Implicit exception chaining */
68 if (value == NULL || !PyExceptionInstance_Check(value)) {
69 /* We must normalize the value right now */
70 PyObject *args, *fixed_value;
71 if (value == NULL || value == Py_None)
72 args = PyTuple_New(0);
73 else if (PyTuple_Check(value)) {
74 Py_INCREF(value);
75 args = value;
76 }
77 else
78 args = PyTuple_Pack(1, value);
79 fixed_value = args ?
80 PyEval_CallObject(exception, args) : NULL;
81 Py_XDECREF(args);
82 Py_XDECREF(value);
83 if (fixed_value == NULL)
84 return;
85 value = fixed_value;
86 }
Benjamin Petersone6528212008-07-15 15:32:09 +000087 /* Avoid reference cycles through the context chain.
88 This is O(chain length) but context chains are
89 usually very short. Sensitive readers may try
90 to inline the call to PyException_GetContext. */
91 if (tstate->exc_value != value) {
92 PyObject *o = tstate->exc_value, *context;
93 while ((context = PyException_GetContext(o))) {
94 Py_DECREF(context);
95 if (context == value) {
96 PyException_SetContext(o, NULL);
97 break;
98 }
99 o = context;
100 }
101 Py_INCREF(tstate->exc_value);
102 PyException_SetContext(value, tstate->exc_value);
103 }
Guido van Rossumb4fb6e42008-06-14 20:20:24 +0000104 }
105 if (value != NULL && PyExceptionInstance_Check(value))
106 tb = PyException_GetTraceback(value);
107 Py_XINCREF(exception);
108 PyErr_Restore(exception, value, tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109}
110
111void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000112PyErr_SetNone(PyObject *exception)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
Guido van Rossum373c8691997-04-29 18:22:47 +0000114 PyErr_SetObject(exception, (PyObject *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115}
116
117void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000118PyErr_SetString(PyObject *exception, const char *string)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119{
Walter Dörwald573c08c2007-05-25 15:46:59 +0000120 PyObject *value = PyUnicode_FromString(string);
Guido van Rossum373c8691997-04-29 18:22:47 +0000121 PyErr_SetObject(exception, value);
122 Py_XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123}
124
Guido van Rossum3a241811994-08-29 12:14:12 +0000125
Guido van Rossum373c8691997-04-29 18:22:47 +0000126PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000127PyErr_Occurred(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Tim Peters024da352001-05-30 06:09:50 +0000129 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000130
131 return tstate->curexc_type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132}
133
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000134
135int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000136PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000137{
Barry Warsawfa5c3152000-05-02 19:27:51 +0000138 if (err == NULL || exc == NULL) {
139 /* maybe caused by "import exceptions" that failed early on */
140 return 0;
141 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000142 if (PyTuple_Check(exc)) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000143 Py_ssize_t i, n;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000144 n = PyTuple_Size(exc);
145 for (i = 0; i < n; i++) {
146 /* Test recursively */
147 if (PyErr_GivenExceptionMatches(
148 err, PyTuple_GET_ITEM(exc, i)))
149 {
150 return 1;
151 }
152 }
153 return 0;
154 }
155 /* err might be an instance, so check its class. */
Brett Cannonbf364092006-03-01 04:25:17 +0000156 if (PyExceptionInstance_Check(err))
157 err = PyExceptionInstance_Class(err);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000158
Brett Cannonbf364092006-03-01 04:25:17 +0000159 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
160 /* problems here!? not sure PyObject_IsSubclass expects to
161 be called with an exception pending... */
162 return PyObject_IsSubclass(err, exc);
163 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000164
165 return err == exc;
166}
Guido van Rossum743007d1999-04-21 15:27:31 +0000167
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000168
169int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000170PyErr_ExceptionMatches(PyObject *exc)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000171{
172 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
173}
174
175
176/* Used in many places to normalize a raised exception, including in
177 eval_code2(), do_raise(), and PyErr_Print()
Benjamin Petersone6528212008-07-15 15:32:09 +0000178
179 XXX: should PyErr_NormalizeException() also call
180 PyException_SetTraceback() with the resulting value and tb?
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000181*/
182void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000183PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000184{
185 PyObject *type = *exc;
186 PyObject *value = *val;
187 PyObject *inclass = NULL;
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000188 PyObject *initial_tb = NULL;
Thomas Wouters89d996e2007-09-08 17:39:28 +0000189 PyThreadState *tstate = NULL;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000190
Guido van Rossumed473a42000-08-07 19:18:27 +0000191 if (type == NULL) {
Guido van Rossum6b3fffa2003-04-10 20:29:48 +0000192 /* There was no exception, so nothing to do. */
193 return;
Guido van Rossumed473a42000-08-07 19:18:27 +0000194 }
195
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000196 /* If PyErr_SetNone() was used, the value will have been actually
197 set to NULL.
198 */
199 if (!value) {
200 value = Py_None;
201 Py_INCREF(value);
202 }
203
Brett Cannonbf364092006-03-01 04:25:17 +0000204 if (PyExceptionInstance_Check(value))
205 inclass = PyExceptionInstance_Class(value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000206
207 /* Normalize the exception so that if the type is a class, the
208 value will be an instance.
209 */
Brett Cannonbf364092006-03-01 04:25:17 +0000210 if (PyExceptionClass_Check(type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000211 /* if the value was not an instance, or is not an instance
212 whose class is (or is derived from) type, then use the
213 value as an argument to instantiation of the type
214 class.
215 */
Brett Cannonbf364092006-03-01 04:25:17 +0000216 if (!inclass || !PyObject_IsSubclass(inclass, type)) {
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000217 PyObject *args, *res;
218
219 if (value == Py_None)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000220 args = PyTuple_New(0);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000221 else if (PyTuple_Check(value)) {
222 Py_INCREF(value);
223 args = value;
224 }
225 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000226 args = PyTuple_Pack(1, value);
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000227
228 if (args == NULL)
229 goto finally;
230 res = PyEval_CallObject(type, args);
231 Py_DECREF(args);
232 if (res == NULL)
233 goto finally;
234 Py_DECREF(value);
235 value = res;
236 }
Barry Warsaw3a749931997-09-30 15:00:18 +0000237 /* if the class of the instance doesn't exactly match the
238 class of the type, believe the instance
239 */
240 else if (inclass != type) {
241 Py_DECREF(type);
242 type = inclass;
243 Py_INCREF(type);
244 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000245 }
246 *exc = type;
247 *val = value;
248 return;
249finally:
Guido van Rossum19b55f21997-12-09 14:11:39 +0000250 Py_DECREF(type);
251 Py_DECREF(value);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000252 /* If the new exception doesn't set a traceback and the old
253 exception had a traceback, use the old traceback for the
254 new exception. It's better than nothing.
255 */
256 initial_tb = *tb;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000257 PyErr_Fetch(exc, val, tb);
Jeremy Hyltone2e2c9f2001-09-26 19:58:38 +0000258 if (initial_tb != NULL) {
259 if (*tb == NULL)
260 *tb = initial_tb;
261 else
262 Py_DECREF(initial_tb);
263 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000264 /* normalize recursively */
Thomas Wouters89d996e2007-09-08 17:39:28 +0000265 tstate = PyThreadState_GET();
266 if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
267 --tstate->recursion_depth;
268 PyErr_SetObject(PyExc_RuntimeError, PyExc_RecursionErrorInst);
269 return;
270 }
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000271 PyErr_NormalizeException(exc, val, tb);
Thomas Wouters89d996e2007-09-08 17:39:28 +0000272 --tstate->recursion_depth;
Barry Warsawc0dc92a1997-08-22 21:22:58 +0000273}
274
275
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000277PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000278{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000279 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossuma027efa1997-05-05 20:56:21 +0000280
281 *p_type = tstate->curexc_type;
282 *p_value = tstate->curexc_value;
283 *p_traceback = tstate->curexc_traceback;
284
285 tstate->curexc_type = NULL;
286 tstate->curexc_value = NULL;
287 tstate->curexc_traceback = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288}
289
290void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000291PyErr_Clear(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000292{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000293 PyErr_Restore(NULL, NULL, NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000295
296/* Convenience functions to set a type error exception and return 0 */
297
298int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000299PyErr_BadArgument(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000300{
Guido van Rossuma027efa1997-05-05 20:56:21 +0000301 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000302 "bad argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000303 return 0;
304}
305
Guido van Rossum373c8691997-04-29 18:22:47 +0000306PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307PyErr_NoMemory(void)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000308{
Vladimir Marangozov0888ff12000-08-18 18:01:06 +0000309 if (PyErr_ExceptionMatches(PyExc_MemoryError))
310 /* already current */
311 return NULL;
312
Barry Warsaw2d8adff1997-08-29 21:54:35 +0000313 /* raise the pre-allocated instance if it still exists */
314 if (PyExc_MemoryErrorInst)
315 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
316 else
317 /* this will probably fail since there's no memory and hee,
318 hee, we have to instantiate this class
319 */
320 PyErr_SetNone(PyExc_MemoryError);
321
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000322 return NULL;
323}
324
Guido van Rossum373c8691997-04-29 18:22:47 +0000325PyObject *
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000326PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000327{
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000328 PyObject *message;
Guido van Rossum373c8691997-04-29 18:22:47 +0000329 PyObject *v;
Guido van Rossum3a241811994-08-29 12:14:12 +0000330 int i = errno;
Martin v. Löwis3484a182002-03-09 12:07:51 +0000331#ifdef PLAN9
332 char errbuf[ERRMAX];
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000333#else
334#ifndef MS_WINDOWS
335 char *s;
336#else
337 WCHAR *s_buf = NULL;
338#endif /* Unix/Windows */
339#endif /* PLAN 9*/
340
Guido van Rossume9fbc091995-02-18 14:52:19 +0000341#ifdef EINTR
Guido van Rossum373c8691997-04-29 18:22:47 +0000342 if (i == EINTR && PyErr_CheckSignals())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000343 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000344#endif
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000345
Martin v. Löwis3484a182002-03-09 12:07:51 +0000346#ifdef PLAN9
347 rerrstr(errbuf, sizeof errbuf);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000348 message = PyUnicode_DecodeUTF8(errbuf, strlen(errbuf), "ignore");
Martin v. Löwis3484a182002-03-09 12:07:51 +0000349#else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000350#ifndef MS_WINDOWS
Guido van Rossume0e59821998-10-14 20:38:13 +0000351 if (i == 0)
352 s = "Error"; /* Sometimes errno didn't get set */
Barry Warsaw97d95151998-07-23 16:05:56 +0000353 else
Guido van Rossume0e59821998-10-14 20:38:13 +0000354 s = strerror(i);
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000355 message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore");
Guido van Rossum743007d1999-04-21 15:27:31 +0000356#else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000357 if (i == 0)
358 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
359 else
Guido van Rossum743007d1999-04-21 15:27:31 +0000360 {
Guido van Rossum795e1892000-02-17 15:19:15 +0000361 /* Note that the Win32 errors do not lineup with the
362 errno error. So if the error is in the MSVC error
Brett Cannonbf364092006-03-01 04:25:17 +0000363 table, we use it, otherwise we assume it really _is_
Guido van Rossum795e1892000-02-17 15:19:15 +0000364 a Win32 error code
365 */
Guido van Rossum584b16a2000-02-21 16:50:31 +0000366 if (i > 0 && i < _sys_nerr) {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000367 message = PyUnicode_FromString(_sys_errlist[i]);
Guido van Rossum795e1892000-02-17 15:19:15 +0000368 }
369 else {
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000370 int len = FormatMessageW(
Guido van Rossum795e1892000-02-17 15:19:15 +0000371 FORMAT_MESSAGE_ALLOCATE_BUFFER |
372 FORMAT_MESSAGE_FROM_SYSTEM |
373 FORMAT_MESSAGE_IGNORE_INSERTS,
374 NULL, /* no message source */
375 i,
376 MAKELANGID(LANG_NEUTRAL,
377 SUBLANG_DEFAULT),
378 /* Default language */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000379 (LPWSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000380 0, /* size not used */
381 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000382 if (len==0) {
Brett Cannonbf364092006-03-01 04:25:17 +0000383 /* Only ever seen this in out-of-mem
Mark Hammond3d61a062002-10-04 00:13:02 +0000384 situations */
Mark Hammond3d61a062002-10-04 00:13:02 +0000385 s_buf = NULL;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000386 message = PyUnicode_FromFormat("Windows Error 0x%X", i);
Mark Hammond3d61a062002-10-04 00:13:02 +0000387 } else {
Mark Hammond3d61a062002-10-04 00:13:02 +0000388 /* remove trailing cr/lf and dots */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000389 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
390 s_buf[--len] = L'\0';
391 message = PyUnicode_FromUnicode(s_buf, len);
Mark Hammond3d61a062002-10-04 00:13:02 +0000392 }
Guido van Rossum795e1892000-02-17 15:19:15 +0000393 }
Guido van Rossum743007d1999-04-21 15:27:31 +0000394 }
Martin v. Löwis3484a182002-03-09 12:07:51 +0000395#endif /* Unix/Windows */
396#endif /* PLAN 9*/
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000397
398 if (message == NULL)
399 {
400#ifdef MS_WINDOWS
401 LocalFree(s_buf);
402#endif
403 return NULL;
404 }
405
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000406 if (filenameObject != NULL)
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000407 v = Py_BuildValue("(iOO)", i, message, filenameObject);
Guido van Rossume0e59821998-10-14 20:38:13 +0000408 else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000409 v = Py_BuildValue("(iO)", i, message);
410 Py_DECREF(message);
411
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000412 if (v != NULL) {
Guido van Rossum373c8691997-04-29 18:22:47 +0000413 PyErr_SetObject(exc, v);
414 Py_DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000415 }
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000416#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000417 LocalFree(s_buf);
Guido van Rossum743007d1999-04-21 15:27:31 +0000418#endif
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000419 return NULL;
420}
Guido van Rossum743007d1999-04-21 15:27:31 +0000421
Barry Warsaw97d95151998-07-23 16:05:56 +0000422
423PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000424PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000425{
Neal Norwitzcd795962007-08-24 19:54:13 +0000426 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000427 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000428 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000429 return result;
430}
431
432#ifdef Py_WIN_WIDE_FILENAMES
433PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000434PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000435{
Brett Cannonbf364092006-03-01 04:25:17 +0000436 PyObject *name = filename ?
437 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000438 NULL;
439 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
440 Py_XDECREF(name);
441 return result;
442}
443#endif /* Py_WIN_WIDE_FILENAMES */
444
445PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000446PyErr_SetFromErrno(PyObject *exc)
Barry Warsaw97d95151998-07-23 16:05:56 +0000447{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000448 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
Barry Warsaw97d95151998-07-23 16:05:56 +0000449}
Guido van Rossum683a0721990-10-21 22:09:12 +0000450
Brett Cannonbf364092006-03-01 04:25:17 +0000451#ifdef MS_WINDOWS
Guido van Rossum795e1892000-02-17 15:19:15 +0000452/* Windows specific error code handling */
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000453PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
Thomas Heller085358a2002-07-29 14:27:41 +0000454 PyObject *exc,
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000455 int ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000456 PyObject *filenameObject)
Guido van Rossum795e1892000-02-17 15:19:15 +0000457{
458 int len;
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000459 WCHAR *s_buf = NULL; /* Free via LocalFree */
460 PyObject *message;
Guido van Rossum795e1892000-02-17 15:19:15 +0000461 PyObject *v;
462 DWORD err = (DWORD)ierr;
463 if (err==0) err = GetLastError();
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000464 len = FormatMessageW(
Guido van Rossum795e1892000-02-17 15:19:15 +0000465 /* Error API error */
466 FORMAT_MESSAGE_ALLOCATE_BUFFER |
467 FORMAT_MESSAGE_FROM_SYSTEM |
468 FORMAT_MESSAGE_IGNORE_INSERTS,
469 NULL, /* no message source */
470 err,
471 MAKELANGID(LANG_NEUTRAL,
472 SUBLANG_DEFAULT), /* Default language */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000473 (LPWSTR) &s_buf,
Guido van Rossum795e1892000-02-17 15:19:15 +0000474 0, /* size not used */
475 NULL); /* no args */
Mark Hammond3d61a062002-10-04 00:13:02 +0000476 if (len==0) {
477 /* Only seen this in out of mem situations */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000478 message = PyUnicode_FromFormat("Windows Error 0x%X", err);
Mark Hammond3d61a062002-10-04 00:13:02 +0000479 s_buf = NULL;
480 } else {
Mark Hammond3d61a062002-10-04 00:13:02 +0000481 /* remove trailing cr/lf and dots */
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000482 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
483 s_buf[--len] = L'\0';
484 message = PyUnicode_FromUnicode(s_buf, len);
Mark Hammond3d61a062002-10-04 00:13:02 +0000485 }
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000486
487 if (message == NULL)
488 {
489 LocalFree(s_buf);
490 return NULL;
491 }
492
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000493 if (filenameObject != NULL)
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000494 v = Py_BuildValue("(iOO)", err, message, filenameObject);
Guido van Rossum795e1892000-02-17 15:19:15 +0000495 else
Martin v. Löwis5d12abe2007-09-03 07:40:24 +0000496 v = Py_BuildValue("(iO)", err, message);
497 Py_DECREF(message);
498
Guido van Rossum795e1892000-02-17 15:19:15 +0000499 if (v != NULL) {
Thomas Heller085358a2002-07-29 14:27:41 +0000500 PyErr_SetObject(exc, v);
Guido van Rossum795e1892000-02-17 15:19:15 +0000501 Py_DECREF(v);
502 }
Mark Hammond3d61a062002-10-04 00:13:02 +0000503 LocalFree(s_buf);
Guido van Rossum795e1892000-02-17 15:19:15 +0000504 return NULL;
505}
506
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000507PyObject *PyErr_SetExcFromWindowsErrWithFilename(
508 PyObject *exc,
509 int ierr,
510 const char *filename)
511{
Neal Norwitzcd795962007-08-24 19:54:13 +0000512 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000513 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
514 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000515 name);
516 Py_XDECREF(name);
517 return ret;
518}
519
520#ifdef Py_WIN_WIDE_FILENAMES
521PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
522 PyObject *exc,
523 int ierr,
524 const Py_UNICODE *filename)
525{
Brett Cannonbf364092006-03-01 04:25:17 +0000526 PyObject *name = filename ?
527 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000528 NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000529 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
530 ierr,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000531 name);
532 Py_XDECREF(name);
533 return ret;
534}
535#endif /* Py_WIN_WIDE_FILENAMES */
536
Thomas Heller085358a2002-07-29 14:27:41 +0000537PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
538{
539 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
540}
541
Guido van Rossum795e1892000-02-17 15:19:15 +0000542PyObject *PyErr_SetFromWindowsErr(int ierr)
543{
Thomas Heller085358a2002-07-29 14:27:41 +0000544 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
545 ierr, NULL);
546}
547PyObject *PyErr_SetFromWindowsErrWithFilename(
548 int ierr,
549 const char *filename)
550{
Neal Norwitzcd795962007-08-24 19:54:13 +0000551 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000552 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
553 PyExc_WindowsError,
554 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000555 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000556 return result;
Guido van Rossum795e1892000-02-17 15:19:15 +0000557}
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000558
559#ifdef Py_WIN_WIDE_FILENAMES
560PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
561 int ierr,
562 const Py_UNICODE *filename)
563{
Brett Cannonbf364092006-03-01 04:25:17 +0000564 PyObject *name = filename ?
565 PyUnicode_FromUnicode(filename, wcslen(filename)) :
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000566 NULL;
567 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
568 PyExc_WindowsError,
569 ierr, name);
Mark Hammondda7efaa2002-10-04 00:09:38 +0000570 Py_XDECREF(name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000571 return result;
572}
573#endif /* Py_WIN_WIDE_FILENAMES */
Guido van Rossum795e1892000-02-17 15:19:15 +0000574#endif /* MS_WINDOWS */
575
Guido van Rossum683a0721990-10-21 22:09:12 +0000576void
Neal Norwitzb382b842007-08-24 20:00:37 +0000577_PyErr_BadInternalCall(const char *filename, int lineno)
Fred Drake6d63adf2000-08-24 22:38:39 +0000578{
579 PyErr_Format(PyExc_SystemError,
580 "%s:%d: bad argument to internal function",
581 filename, lineno);
582}
583
584/* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
585 export the entry point for existing object code: */
586#undef PyErr_BadInternalCall
587void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000588PyErr_BadInternalCall(void)
Guido van Rossum683a0721990-10-21 22:09:12 +0000589{
Fred Drake6d63adf2000-08-24 22:38:39 +0000590 PyErr_Format(PyExc_SystemError,
591 "bad argument to internal function");
Guido van Rossum683a0721990-10-21 22:09:12 +0000592}
Fred Drake6d63adf2000-08-24 22:38:39 +0000593#define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
594
Guido van Rossum1548bac1997-02-14 17:09:47 +0000595
596
Guido van Rossum1548bac1997-02-14 17:09:47 +0000597PyObject *
598PyErr_Format(PyObject *exception, const char *format, ...)
Guido van Rossum1548bac1997-02-14 17:09:47 +0000599{
600 va_list vargs;
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000601 PyObject* string;
Guido van Rossum1548bac1997-02-14 17:09:47 +0000602
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000603#ifdef HAVE_STDARG_PROTOTYPES
Guido van Rossum1548bac1997-02-14 17:09:47 +0000604 va_start(vargs, format);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000605#else
606 va_start(vargs);
607#endif
Guido van Rossum1548bac1997-02-14 17:09:47 +0000608
Walter Dörwald573c08c2007-05-25 15:46:59 +0000609 string = PyUnicode_FromFormatV(format, vargs);
Jeremy Hyltonb69a27e2000-09-01 03:49:47 +0000610 PyErr_SetObject(exception, string);
611 Py_XDECREF(string);
Tim Petersc15c4f12001-10-02 21:32:07 +0000612 va_end(vargs);
Guido van Rossum1548bac1997-02-14 17:09:47 +0000613 return NULL;
614}
Guido van Rossum7617e051997-09-16 18:43:50 +0000615
616
Thomas Wouters477c8d52006-05-27 19:21:47 +0000617
Guido van Rossum7617e051997-09-16 18:43:50 +0000618PyObject *
Neal Norwitzb382b842007-08-24 20:00:37 +0000619PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
Guido van Rossum7617e051997-09-16 18:43:50 +0000620{
Neal Norwitzb382b842007-08-24 20:00:37 +0000621 const char *dot;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000622 PyObject *modulename = NULL;
623 PyObject *classname = NULL;
624 PyObject *mydict = NULL;
625 PyObject *bases = NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000626 PyObject *result = NULL;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000627 dot = strrchr(name, '.');
628 if (dot == NULL) {
629 PyErr_SetString(PyExc_SystemError,
630 "PyErr_NewException: name must be module.class");
Guido van Rossum7617e051997-09-16 18:43:50 +0000631 return NULL;
Guido van Rossum7617e051997-09-16 18:43:50 +0000632 }
633 if (base == NULL)
634 base = PyExc_Exception;
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000635 if (dict == NULL) {
636 dict = mydict = PyDict_New();
637 if (dict == NULL)
638 goto failure;
639 }
640 if (PyDict_GetItemString(dict, "__module__") == NULL) {
Neal Norwitzcd795962007-08-24 19:54:13 +0000641 modulename = PyUnicode_FromStringAndSize(name,
Thomas Wouters89f507f2006-12-13 04:49:30 +0000642 (Py_ssize_t)(dot-name));
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000643 if (modulename == NULL)
644 goto failure;
645 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
646 goto failure;
647 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000648 if (PyTuple_Check(base)) {
649 bases = base;
650 /* INCREF as we create a new ref in the else branch */
651 Py_INCREF(bases);
652 } else {
653 bases = PyTuple_Pack(1, base);
654 if (bases == NULL)
655 goto failure;
656 }
657 /* Create a real new-style class. */
Guido van Rossume845c0f2007-11-02 23:07:07 +0000658 result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO",
Thomas Wouters477c8d52006-05-27 19:21:47 +0000659 dot+1, bases, dict);
Guido van Rossum7617e051997-09-16 18:43:50 +0000660 failure:
Guido van Rossum2ac650f1997-10-03 19:50:55 +0000661 Py_XDECREF(bases);
662 Py_XDECREF(mydict);
663 Py_XDECREF(classname);
664 Py_XDECREF(modulename);
Guido van Rossum7617e051997-09-16 18:43:50 +0000665 return result;
666}
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000667
668/* Call when an exception has occurred but there is no way for Python
669 to handle it. Examples: exception in __del__ or during GC. */
670void
671PyErr_WriteUnraisable(PyObject *obj)
672{
673 PyObject *f, *t, *v, *tb;
674 PyErr_Fetch(&t, &v, &tb);
675 f = PySys_GetObject("stderr");
Christian Heimes2be03732007-11-15 02:26:46 +0000676 if (f != NULL && f != Py_None) {
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000677 PyFile_WriteString("Exception ", f);
678 if (t) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679 PyObject* moduleName;
Guido van Rossumd8faa362007-04-27 19:54:29 +0000680 char* className;
681 assert(PyExceptionClass_Check(t));
682 className = PyExceptionClass_Name(t);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000683 if (className != NULL) {
684 char *dot = strrchr(className, '.');
685 if (dot != NULL)
686 className = dot+1;
687 }
688
689 moduleName = PyObject_GetAttrString(t, "__module__");
Brett Cannonbf364092006-03-01 04:25:17 +0000690 if (moduleName == NULL)
691 PyFile_WriteString("<unknown>", f);
692 else {
Neal Norwitzcd795962007-08-24 19:54:13 +0000693 char* modstr = PyUnicode_AsString(moduleName);
Neal Norwitz2633c692007-02-26 22:22:47 +0000694 if (modstr &&
Georg Brandl1a3284e2007-12-02 09:40:06 +0000695 strcmp(modstr, "builtins") != 0)
Brett Cannonbf364092006-03-01 04:25:17 +0000696 {
697 PyFile_WriteString(modstr, f);
698 PyFile_WriteString(".", f);
699 }
700 }
701 if (className == NULL)
702 PyFile_WriteString("<unknown>", f);
703 else
704 PyFile_WriteString(className, f);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000705 if (v && v != Py_None) {
706 PyFile_WriteString(": ", f);
707 PyFile_WriteObject(v, f, 0);
708 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 Py_XDECREF(moduleName);
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000710 }
711 PyFile_WriteString(" in ", f);
712 PyFile_WriteObject(obj, f, 0);
713 PyFile_WriteString(" ignored\n", f);
714 PyErr_Clear(); /* Just in case */
715 }
716 Py_XDECREF(t);
717 Py_XDECREF(v);
718 Py_XDECREF(tb);
719}
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000720
Armin Rigo092381a2003-10-25 14:29:27 +0000721extern PyObject *PyModule_GetWarningsModule(void);
Guido van Rossumcfd42b52000-12-15 21:58:52 +0000722
Guido van Rossum2fd45652001-02-28 21:46:24 +0000723
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000724/* Set file and line information for the current exception.
725 If the exception is not a SyntaxError, also sets additional attributes
726 to make printing of exceptions believe it is a syntax error. */
Guido van Rossum2fd45652001-02-28 21:46:24 +0000727
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000728void
Martin v. Löwis95292d62002-12-11 14:04:59 +0000729PyErr_SyntaxLocation(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000730{
731 PyObject *exc, *v, *tb, *tmp;
732
733 /* add attributes for the line number and filename for the error */
734 PyErr_Fetch(&exc, &v, &tb);
735 PyErr_NormalizeException(&exc, &v, &tb);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000736 /* XXX check that it is, indeed, a syntax error. It might not
737 * be, though. */
Christian Heimes217cfd12007-12-02 14:31:20 +0000738 tmp = PyLong_FromLong(lineno);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000739 if (tmp == NULL)
740 PyErr_Clear();
741 else {
742 if (PyObject_SetAttrString(v, "lineno", tmp))
743 PyErr_Clear();
744 Py_DECREF(tmp);
745 }
746 if (filename != NULL) {
Neal Norwitzcd795962007-08-24 19:54:13 +0000747 tmp = PyUnicode_FromString(filename);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000748 if (tmp == NULL)
749 PyErr_Clear();
750 else {
751 if (PyObject_SetAttrString(v, "filename", tmp))
752 PyErr_Clear();
753 Py_DECREF(tmp);
754 }
755
756 tmp = PyErr_ProgramText(filename, lineno);
757 if (tmp) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000758 if (PyObject_SetAttrString(v, "text", tmp))
759 PyErr_Clear();
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000760 Py_DECREF(tmp);
761 }
762 }
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000763 if (PyObject_SetAttrString(v, "offset", Py_None)) {
764 PyErr_Clear();
765 }
766 if (exc != PyExc_SyntaxError) {
767 if (!PyObject_HasAttrString(v, "msg")) {
768 tmp = PyObject_Str(v);
769 if (tmp) {
770 if (PyObject_SetAttrString(v, "msg", tmp))
771 PyErr_Clear();
772 Py_DECREF(tmp);
773 } else {
774 PyErr_Clear();
775 }
776 }
777 if (!PyObject_HasAttrString(v, "print_file_and_line")) {
778 if (PyObject_SetAttrString(v, "print_file_and_line",
779 Py_None))
780 PyErr_Clear();
781 }
782 }
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000783 PyErr_Restore(exc, v, tb);
784}
785
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000786/* Attempt to load the line of text that the exception refers to. If it
787 fails, it will return NULL but will not set an exception.
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000788
789 XXX The functionality of this function is quite similar to the
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000790 functionality in tb_displayline() in traceback.c. */
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000791
792PyObject *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000793PyErr_ProgramText(const char *filename, int lineno)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000794{
795 FILE *fp;
796 int i;
797 char linebuf[1000];
798
Tim Petersa7444f42006-02-27 23:29:46 +0000799 if (filename == NULL || *filename == '\0' || lineno <= 0)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000800 return NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000801 fp = fopen(filename, "r" PY_STDIOTEXTMODE);
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000802 if (fp == NULL)
803 return NULL;
804 for (i = 0; i < lineno; i++) {
805 char *pLastChar = &linebuf[sizeof(linebuf) - 2];
806 do {
807 *pLastChar = '\0';
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000808 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
809 fp, NULL) == NULL)
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000810 break;
811 /* fgets read *something*; if it didn't get as
812 far as pLastChar, it must have found a newline
Thomas Wouters477c8d52006-05-27 19:21:47 +0000813 or hit the end of the file; if pLastChar is \n,
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000814 it obviously found a newline; else we haven't
815 yet seen a newline, so must continue */
816 } while (*pLastChar != '\0' && *pLastChar != '\n');
817 }
818 fclose(fp);
819 if (i == lineno) {
820 char *p = linebuf;
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000821 PyObject *res;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000822 while (*p == ' ' || *p == '\t' || *p == '\014')
823 p++;
Guido van Rossumebe8f8a2007-10-10 18:53:36 +0000824 res = PyUnicode_FromString(p);
825 if (res == NULL)
826 PyErr_Clear();
827 return res;
Jeremy Hyltonad3d3f22001-02-28 17:47:12 +0000828 }
829 return NULL;
830}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831
832#ifdef __cplusplus
833}
834#endif