blob: 331811500d8efa175e7358dcd1742a01e513bce1 [file] [log] [blame]
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001/*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */
6
Thomas Wouters477c8d52006-05-27 19:21:47 +00007#define PY_SSIZE_T_CLEAN
8#include <Python.h>
9#include "structmember.h"
10#include "osdefs.h"
11
Thomas Wouters477c8d52006-05-27 19:21:47 +000012
Antoine Pitrou6b4883d2011-10-12 02:54:14 +020013/* Compatibility aliases */
14PyObject *PyExc_EnvironmentError = NULL;
15PyObject *PyExc_IOError = NULL;
16#ifdef MS_WINDOWS
17PyObject *PyExc_WindowsError = NULL;
18#endif
19#ifdef __VMS
20PyObject *PyExc_VMSError = NULL;
21#endif
22
23/* The dict map from errno codes to OSError subclasses */
24static PyObject *errnomap = NULL;
25
26
Thomas Wouters477c8d52006-05-27 19:21:47 +000027/* NOTE: If the exception class hierarchy changes, don't forget to update
28 * Lib/test/exception_hierarchy.txt
29 */
30
Thomas Wouters477c8d52006-05-27 19:21:47 +000031/*
32 * BaseException
33 */
34static PyObject *
35BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
36{
37 PyBaseExceptionObject *self;
38
39 self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +000040 if (!self)
41 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000042 /* the dict is created on the fly in PyObject_GenericSetAttr */
Guido van Rossumebe3e162007-05-17 18:20:34 +000043 self->dict = NULL;
Collin Winter1966f1c2007-09-01 20:26:44 +000044 self->traceback = self->cause = self->context = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000045
46 self->args = PyTuple_New(0);
47 if (!self->args) {
48 Py_DECREF(self);
49 return NULL;
50 }
51
Thomas Wouters477c8d52006-05-27 19:21:47 +000052 return (PyObject *)self;
53}
54
55static int
56BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
57{
Christian Heimes90aa7642007-12-19 02:45:37 +000058 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000059 return -1;
60
Antoine Pitroue0e27352011-12-15 14:31:28 +010061 Py_XDECREF(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +000062 self->args = args;
63 Py_INCREF(self->args);
64
Thomas Wouters477c8d52006-05-27 19:21:47 +000065 return 0;
66}
67
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000068static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000069BaseException_clear(PyBaseExceptionObject *self)
70{
71 Py_CLEAR(self->dict);
72 Py_CLEAR(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000073 Py_CLEAR(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000074 Py_CLEAR(self->cause);
75 Py_CLEAR(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000076 return 0;
77}
78
79static void
80BaseException_dealloc(PyBaseExceptionObject *self)
81{
Thomas Wouters89f507f2006-12-13 04:49:30 +000082 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000083 BaseException_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +000084 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +000085}
86
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000087static int
Thomas Wouters477c8d52006-05-27 19:21:47 +000088BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
89{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000090 Py_VISIT(self->dict);
Thomas Wouters477c8d52006-05-27 19:21:47 +000091 Py_VISIT(self->args);
Collin Winter828f04a2007-08-31 00:04:24 +000092 Py_VISIT(self->traceback);
Collin Winter1966f1c2007-09-01 20:26:44 +000093 Py_VISIT(self->cause);
94 Py_VISIT(self->context);
Thomas Wouters477c8d52006-05-27 19:21:47 +000095 return 0;
96}
97
98static PyObject *
99BaseException_str(PyBaseExceptionObject *self)
100{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000101 switch (PyTuple_GET_SIZE(self->args)) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000102 case 0:
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000103 return PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104 case 1:
Thomas Heller519a0422007-11-15 20:48:54 +0000105 return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +0000106 default:
Thomas Heller519a0422007-11-15 20:48:54 +0000107 return PyObject_Str(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109}
110
111static PyObject *
112BaseException_repr(PyBaseExceptionObject *self)
113{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114 char *name;
115 char *dot;
116
Christian Heimes90aa7642007-12-19 02:45:37 +0000117 name = (char *)Py_TYPE(self)->tp_name;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000118 dot = strrchr(name, '.');
119 if (dot != NULL) name = dot+1;
120
Walter Dörwald7569dfe2007-05-19 21:49:49 +0000121 return PyUnicode_FromFormat("%s%R", name, self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000122}
123
124/* Pickling support */
125static PyObject *
126BaseException_reduce(PyBaseExceptionObject *self)
127{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000128 if (self->args && self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000129 return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000130 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000131 return PyTuple_Pack(2, Py_TYPE(self), self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000132}
133
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000134/*
135 * Needed for backward compatibility, since exceptions used to store
136 * all their attributes in the __dict__. Code is taken from cPickle's
137 * load_build function.
138 */
139static PyObject *
140BaseException_setstate(PyObject *self, PyObject *state)
141{
142 PyObject *d_key, *d_value;
143 Py_ssize_t i = 0;
144
145 if (state != Py_None) {
146 if (!PyDict_Check(state)) {
147 PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
148 return NULL;
149 }
150 while (PyDict_Next(state, &i, &d_key, &d_value)) {
151 if (PyObject_SetAttr(self, d_key, d_value) < 0)
152 return NULL;
153 }
154 }
155 Py_RETURN_NONE;
156}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000157
Collin Winter828f04a2007-08-31 00:04:24 +0000158static PyObject *
159BaseException_with_traceback(PyObject *self, PyObject *tb) {
160 if (PyException_SetTraceback(self, tb))
161 return NULL;
162
163 Py_INCREF(self);
164 return self;
165}
166
Georg Brandl76941002008-05-05 21:38:47 +0000167PyDoc_STRVAR(with_traceback_doc,
168"Exception.with_traceback(tb) --\n\
169 set self.__traceback__ to tb and return self.");
170
Thomas Wouters477c8d52006-05-27 19:21:47 +0000171
172static PyMethodDef BaseException_methods[] = {
173 {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000174 {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
Georg Brandl76941002008-05-05 21:38:47 +0000175 {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O,
176 with_traceback_doc},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000177 {NULL, NULL, 0, NULL},
178};
179
180
Thomas Wouters477c8d52006-05-27 19:21:47 +0000181static PyObject *
182BaseException_get_dict(PyBaseExceptionObject *self)
183{
184 if (self->dict == NULL) {
185 self->dict = PyDict_New();
186 if (!self->dict)
187 return NULL;
188 }
189 Py_INCREF(self->dict);
190 return self->dict;
191}
192
193static int
194BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)
195{
196 if (val == NULL) {
197 PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
198 return -1;
199 }
200 if (!PyDict_Check(val)) {
201 PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
202 return -1;
203 }
204 Py_CLEAR(self->dict);
205 Py_INCREF(val);
206 self->dict = val;
207 return 0;
208}
209
210static PyObject *
211BaseException_get_args(PyBaseExceptionObject *self)
212{
213 if (self->args == NULL) {
214 Py_INCREF(Py_None);
215 return Py_None;
216 }
217 Py_INCREF(self->args);
218 return self->args;
219}
220
221static int
222BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
223{
224 PyObject *seq;
225 if (val == NULL) {
226 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
227 return -1;
228 }
229 seq = PySequence_Tuple(val);
230 if (!seq) return -1;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000231 Py_CLEAR(self->args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000232 self->args = seq;
233 return 0;
234}
235
Collin Winter828f04a2007-08-31 00:04:24 +0000236static PyObject *
237BaseException_get_tb(PyBaseExceptionObject *self)
238{
239 if (self->traceback == NULL) {
240 Py_INCREF(Py_None);
241 return Py_None;
242 }
243 Py_INCREF(self->traceback);
244 return self->traceback;
245}
246
247static int
248BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb)
249{
250 if (tb == NULL) {
251 PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
252 return -1;
253 }
254 else if (!(tb == Py_None || PyTraceBack_Check(tb))) {
255 PyErr_SetString(PyExc_TypeError,
256 "__traceback__ must be a traceback or None");
257 return -1;
258 }
259
260 Py_XINCREF(tb);
261 Py_XDECREF(self->traceback);
262 self->traceback = tb;
263 return 0;
264}
265
Georg Brandlab6f2f62009-03-31 04:16:10 +0000266static PyObject *
267BaseException_get_context(PyObject *self) {
268 PyObject *res = PyException_GetContext(self);
269 if (res) return res; /* new reference already returned above */
270 Py_RETURN_NONE;
271}
272
273static int
274BaseException_set_context(PyObject *self, PyObject *arg) {
275 if (arg == NULL) {
276 PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted");
277 return -1;
278 } else if (arg == Py_None) {
279 arg = NULL;
280 } else if (!PyExceptionInstance_Check(arg)) {
281 PyErr_SetString(PyExc_TypeError, "exception context must be None "
282 "or derive from BaseException");
283 return -1;
284 } else {
285 /* PyException_SetContext steals this reference */
286 Py_INCREF(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
Georg Brandlab6f2f62009-03-31 04:16:10 +0000288 PyException_SetContext(self, arg);
289 return 0;
290}
291
292static PyObject *
293BaseException_get_cause(PyObject *self) {
294 PyObject *res = PyException_GetCause(self);
295 if (res) return res; /* new reference already returned above */
296 Py_RETURN_NONE;
297}
298
299static int
300BaseException_set_cause(PyObject *self, PyObject *arg) {
301 if (arg == NULL) {
302 PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
303 return -1;
304 } else if (arg == Py_None) {
305 arg = NULL;
306 } else if (!PyExceptionInstance_Check(arg)) {
307 PyErr_SetString(PyExc_TypeError, "exception cause must be None "
308 "or derive from BaseException");
309 return -1;
310 } else {
311 /* PyException_SetCause steals this reference */
312 Py_INCREF(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 }
Georg Brandlab6f2f62009-03-31 04:16:10 +0000314 PyException_SetCause(self, arg);
315 return 0;
316}
317
Guido van Rossum360e4b82007-05-14 22:51:27 +0000318
Thomas Wouters477c8d52006-05-27 19:21:47 +0000319static PyGetSetDef BaseException_getset[] = {
320 {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},
321 {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
Collin Winter828f04a2007-08-31 00:04:24 +0000322 {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
Georg Brandlab6f2f62009-03-31 04:16:10 +0000323 {"__context__", (getter)BaseException_get_context,
324 (setter)BaseException_set_context, PyDoc_STR("exception context")},
325 {"__cause__", (getter)BaseException_get_cause,
326 (setter)BaseException_set_cause, PyDoc_STR("exception cause")},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000327 {NULL},
328};
329
330
Collin Winter828f04a2007-08-31 00:04:24 +0000331PyObject *
332PyException_GetTraceback(PyObject *self) {
333 PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
334 Py_XINCREF(base_self->traceback);
335 return base_self->traceback;
336}
337
338
339int
340PyException_SetTraceback(PyObject *self, PyObject *tb) {
341 return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
342}
343
344PyObject *
345PyException_GetCause(PyObject *self) {
346 PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
347 Py_XINCREF(cause);
348 return cause;
349}
350
351/* Steals a reference to cause */
352void
353PyException_SetCause(PyObject *self, PyObject *cause) {
354 PyObject *old_cause = ((PyBaseExceptionObject *)self)->cause;
355 ((PyBaseExceptionObject *)self)->cause = cause;
356 Py_XDECREF(old_cause);
357}
358
359PyObject *
360PyException_GetContext(PyObject *self) {
361 PyObject *context = ((PyBaseExceptionObject *)self)->context;
362 Py_XINCREF(context);
363 return context;
364}
365
366/* Steals a reference to context */
367void
368PyException_SetContext(PyObject *self, PyObject *context) {
369 PyObject *old_context = ((PyBaseExceptionObject *)self)->context;
370 ((PyBaseExceptionObject *)self)->context = context;
371 Py_XDECREF(old_context);
372}
373
374
Thomas Wouters477c8d52006-05-27 19:21:47 +0000375static PyTypeObject _PyExc_BaseException = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000376 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +0000377 "BaseException", /*tp_name*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000378 sizeof(PyBaseExceptionObject), /*tp_basicsize*/
379 0, /*tp_itemsize*/
380 (destructor)BaseException_dealloc, /*tp_dealloc*/
381 0, /*tp_print*/
382 0, /*tp_getattr*/
383 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000384 0, /* tp_reserved; */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000385 (reprfunc)BaseException_repr, /*tp_repr*/
386 0, /*tp_as_number*/
Brett Cannonba7bf492007-02-27 00:15:55 +0000387 0, /*tp_as_sequence*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000388 0, /*tp_as_mapping*/
389 0, /*tp_hash */
390 0, /*tp_call*/
391 (reprfunc)BaseException_str, /*tp_str*/
392 PyObject_GenericGetAttr, /*tp_getattro*/
393 PyObject_GenericSetAttr, /*tp_setattro*/
394 0, /*tp_as_buffer*/
Thomas Wouters27d517b2007-02-25 20:39:11 +0000395 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
Thomas Wouters477c8d52006-05-27 19:21:47 +0000397 PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
398 (traverseproc)BaseException_traverse, /* tp_traverse */
399 (inquiry)BaseException_clear, /* tp_clear */
400 0, /* tp_richcompare */
401 0, /* tp_weaklistoffset */
402 0, /* tp_iter */
403 0, /* tp_iternext */
404 BaseException_methods, /* tp_methods */
Georg Brandlab6f2f62009-03-31 04:16:10 +0000405 0, /* tp_members */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000406 BaseException_getset, /* tp_getset */
407 0, /* tp_base */
408 0, /* tp_dict */
409 0, /* tp_descr_get */
410 0, /* tp_descr_set */
411 offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
412 (initproc)BaseException_init, /* tp_init */
413 0, /* tp_alloc */
414 BaseException_new, /* tp_new */
415};
416/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
417from the previous implmentation and also allowing Python objects to be used
418in the API */
419PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
420
421/* note these macros omit the last semicolon so the macro invocation may
422 * include it and not look strange.
423 */
424#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
425static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000426 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000427 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000428 sizeof(PyBaseExceptionObject), \
429 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
430 0, 0, 0, 0, 0, 0, 0, \
431 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
432 PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
433 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
434 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
435 (initproc)BaseException_init, 0, BaseException_new,\
436}; \
437PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
438
439#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
440static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000441 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000442 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000443 sizeof(Py ## EXCSTORE ## Object), \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000444 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000445 0, 0, 0, 0, 0, \
446 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000447 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
448 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000449 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200450 (initproc)EXCSTORE ## _init, 0, 0, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000451}; \
452PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
453
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200454#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCNEW, \
455 EXCMETHODS, EXCMEMBERS, EXCGETSET, \
456 EXCSTR, EXCDOC) \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000457static PyTypeObject _PyExc_ ## EXCNAME = { \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000458 PyVarObject_HEAD_INIT(NULL, 0) \
Neal Norwitz2633c692007-02-26 22:22:47 +0000459 # EXCNAME, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000460 sizeof(Py ## EXCSTORE ## Object), 0, \
461 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
462 (reprfunc)EXCSTR, 0, 0, 0, \
463 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
464 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
465 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200466 EXCMEMBERS, EXCGETSET, &_ ## EXCBASE, \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000467 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200468 (initproc)EXCSTORE ## _init, 0, EXCNEW,\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000469}; \
470PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
471
472
473/*
474 * Exception extends BaseException
475 */
476SimpleExtendsException(PyExc_BaseException, Exception,
477 "Common base class for all non-exit exceptions.");
478
479
480/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000481 * TypeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000482 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000483SimpleExtendsException(PyExc_Exception, TypeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000484 "Inappropriate argument type.");
485
486
487/*
488 * StopIteration extends Exception
489 */
490SimpleExtendsException(PyExc_Exception, StopIteration,
Georg Brandla18af4e2007-04-21 15:47:16 +0000491 "Signal the end from iterator.__next__().");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000492
493
494/*
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000495 * GeneratorExit extends BaseException
Thomas Wouters477c8d52006-05-27 19:21:47 +0000496 */
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000497SimpleExtendsException(PyExc_BaseException, GeneratorExit,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000498 "Request that a generator exit.");
499
500
501/*
502 * SystemExit extends BaseException
503 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000504
505static int
506SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
507{
508 Py_ssize_t size = PyTuple_GET_SIZE(args);
509
510 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
511 return -1;
512
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000513 if (size == 0)
514 return 0;
515 Py_CLEAR(self->code);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000516 if (size == 1)
517 self->code = PyTuple_GET_ITEM(args, 0);
Victor Stinner92236e52011-05-26 14:25:54 +0200518 else /* size > 1 */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000519 self->code = args;
520 Py_INCREF(self->code);
521 return 0;
522}
523
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000524static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000525SystemExit_clear(PySystemExitObject *self)
526{
527 Py_CLEAR(self->code);
528 return BaseException_clear((PyBaseExceptionObject *)self);
529}
530
531static void
532SystemExit_dealloc(PySystemExitObject *self)
533{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000534 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000535 SystemExit_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000536 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000537}
538
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000539static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000540SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
541{
542 Py_VISIT(self->code);
543 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
544}
545
546static PyMemberDef SystemExit_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000547 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
548 PyDoc_STR("exception code")},
549 {NULL} /* Sentinel */
550};
551
552ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200553 0, 0, SystemExit_members, 0, 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000554 "Request to exit from the interpreter.");
555
556/*
557 * KeyboardInterrupt extends BaseException
558 */
559SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
560 "Program interrupted by user.");
561
562
563/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000564 * ImportError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000565 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000566SimpleExtendsException(PyExc_Exception, ImportError,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000567 "Import can't find module, or can't find name in module.");
568
569
570/*
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200571 * OSError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +0000572 */
573
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200574#ifdef MS_WINDOWS
575#include "errmap.h"
576#endif
577
Thomas Wouters477c8d52006-05-27 19:21:47 +0000578/* Where a function has a single filename, such as open() or some
579 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
580 * called, giving a third argument which is the filename. But, so
581 * that old code using in-place unpacking doesn't break, e.g.:
582 *
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200583 * except OSError, (errno, strerror):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000584 *
585 * we hack args so that it only contains two items. This also
586 * means we need our own __str__() which prints out the filename
587 * when it was supplied.
588 */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200589
Antoine Pitroue0e27352011-12-15 14:31:28 +0100590/* This function doesn't cleanup on error, the caller should */
591static int
592oserror_parse_args(PyObject **p_args,
593 PyObject **myerrno, PyObject **strerror,
594 PyObject **filename
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200595#ifdef MS_WINDOWS
Antoine Pitroue0e27352011-12-15 14:31:28 +0100596 , PyObject **winerror
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200597#endif
Antoine Pitroue0e27352011-12-15 14:31:28 +0100598 )
599{
600 Py_ssize_t nargs;
601 PyObject *args = *p_args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000602
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200603 nargs = PyTuple_GET_SIZE(args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000604
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200605#ifdef MS_WINDOWS
606 if (nargs >= 2 && nargs <= 4) {
607 if (!PyArg_UnpackTuple(args, "OSError", 2, 4,
Antoine Pitroue0e27352011-12-15 14:31:28 +0100608 myerrno, strerror, filename, winerror))
609 return -1;
610 if (*winerror && PyLong_Check(*winerror)) {
611 long errcode, winerrcode;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200612 PyObject *newargs;
613 Py_ssize_t i;
614
Antoine Pitroue0e27352011-12-15 14:31:28 +0100615 winerrcode = PyLong_AsLong(*winerror);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200616 if (winerrcode == -1 && PyErr_Occurred())
Antoine Pitroue0e27352011-12-15 14:31:28 +0100617 return -1;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200618 /* Set errno to the corresponding POSIX errno (overriding
619 first argument). Windows Socket error codes (>= 10000)
620 have the same value as their POSIX counterparts.
621 */
622 if (winerrcode < 10000)
623 errcode = winerror_to_errno(winerrcode);
624 else
625 errcode = winerrcode;
Antoine Pitroue0e27352011-12-15 14:31:28 +0100626 *myerrno = PyLong_FromLong(errcode);
627 if (!*myerrno)
628 return -1;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200629 newargs = PyTuple_New(nargs);
630 if (!newargs)
Antoine Pitroue0e27352011-12-15 14:31:28 +0100631 return -1;
632 PyTuple_SET_ITEM(newargs, 0, *myerrno);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200633 for (i = 1; i < nargs; i++) {
634 PyObject *val = PyTuple_GET_ITEM(args, i);
635 Py_INCREF(val);
636 PyTuple_SET_ITEM(newargs, i, val);
637 }
638 Py_DECREF(args);
Antoine Pitroue0e27352011-12-15 14:31:28 +0100639 args = *p_args = newargs;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200640 }
641 }
642#else
643 if (nargs >= 2 && nargs <= 3) {
644 if (!PyArg_UnpackTuple(args, "OSError", 2, 3,
Antoine Pitroue0e27352011-12-15 14:31:28 +0100645 myerrno, strerror, filename))
646 return -1;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200647 }
648#endif
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000649
Antoine Pitroue0e27352011-12-15 14:31:28 +0100650 return 0;
651}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000652
Antoine Pitroue0e27352011-12-15 14:31:28 +0100653static int
654oserror_init(PyOSErrorObject *self, PyObject **p_args,
655 PyObject *myerrno, PyObject *strerror,
656 PyObject *filename
657#ifdef MS_WINDOWS
658 , PyObject *winerror
659#endif
660 )
661{
662 PyObject *args = *p_args;
663 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000664
665 /* self->filename will remain Py_None otherwise */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200666 if (filename && filename != Py_None) {
Antoine Pitroue0e27352011-12-15 14:31:28 +0100667 if (Py_TYPE(self) == (PyTypeObject *) PyExc_BlockingIOError &&
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200668 PyNumber_Check(filename)) {
669 /* BlockingIOError's 3rd argument can be the number of
670 * characters written.
671 */
672 self->written = PyNumber_AsSsize_t(filename, PyExc_ValueError);
673 if (self->written == -1 && PyErr_Occurred())
Antoine Pitroue0e27352011-12-15 14:31:28 +0100674 return -1;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200675 }
676 else {
677 Py_INCREF(filename);
678 self->filename = filename;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200680 if (nargs >= 2 && nargs <= 3) {
681 /* filename is removed from the args tuple (for compatibility
682 purposes, see test_exceptions.py) */
Antoine Pitroue0e27352011-12-15 14:31:28 +0100683 PyObject *subslice = PyTuple_GetSlice(args, 0, 2);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200684 if (!subslice)
Antoine Pitroue0e27352011-12-15 14:31:28 +0100685 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000686
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200687 Py_DECREF(args); /* replacing args */
Antoine Pitroue0e27352011-12-15 14:31:28 +0100688 *p_args = args = subslice;
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200689 }
690 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000691 }
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200692 Py_XINCREF(myerrno);
693 self->myerrno = myerrno;
694
695 Py_XINCREF(strerror);
696 self->strerror = strerror;
697
698#ifdef MS_WINDOWS
699 Py_XINCREF(winerror);
700 self->winerror = winerror;
701#endif
702
Antoine Pitroue0e27352011-12-15 14:31:28 +0100703 /* Steals the reference to args */
704 self->args = args;
705 args = NULL;
706
707 return 0;
708}
709
710static PyObject *
711OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
712static int
713OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds);
714
715static int
716oserror_use_init(PyTypeObject *type)
717{
718 /* When __init__ is defined in a OSError subclass, we want any
719 extraneous argument to __new__ to be ignored. The only reasonable
720 solution, given __new__ takes a variable number of arguments,
721 is to defer arg parsing and initialization to __init__.
722
723 But when __new__ is overriden as well, it should call our __new__
724 with the right arguments.
725
726 (see http://bugs.python.org/issue12555#msg148829 )
727 */
728 if (type->tp_init != (initproc) OSError_init &&
729 type->tp_new == (newfunc) OSError_new) {
730 assert((PyObject *) type != PyExc_OSError);
731 return 1;
732 }
733 return 0;
734}
735
736static PyObject *
737OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
738{
739 PyOSErrorObject *self = NULL;
740 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
741#ifdef MS_WINDOWS
742 PyObject *winerror = NULL;
743#endif
744
745 if (!oserror_use_init(type)) {
746 if (!_PyArg_NoKeywords(type->tp_name, kwds))
747 return NULL;
748
749 Py_INCREF(args);
750 if (oserror_parse_args(&args, &myerrno, &strerror, &filename
751#ifdef MS_WINDOWS
752 , &winerror
753#endif
754 ))
755 goto error;
756
757 if (myerrno && PyLong_Check(myerrno) &&
758 errnomap && (PyObject *) type == PyExc_OSError) {
759 PyObject *newtype;
760 newtype = PyDict_GetItem(errnomap, myerrno);
761 if (newtype) {
762 assert(PyType_Check(newtype));
763 type = (PyTypeObject *) newtype;
764 }
765 else if (PyErr_Occurred())
766 goto error;
767 }
768 }
769
770 self = (PyOSErrorObject *) type->tp_alloc(type, 0);
771 if (!self)
772 goto error;
773
774 self->dict = NULL;
775 self->traceback = self->cause = self->context = NULL;
776 self->written = -1;
777
778 if (!oserror_use_init(type)) {
779 if (oserror_init(self, &args, myerrno, strerror, filename
780#ifdef MS_WINDOWS
781 , winerror
782#endif
783 ))
784 goto error;
785 }
786
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200787 return (PyObject *) self;
788
789error:
790 Py_XDECREF(args);
791 Py_XDECREF(self);
792 return NULL;
793}
794
795static int
Antoine Pitroue0e27352011-12-15 14:31:28 +0100796OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200797{
Antoine Pitroue0e27352011-12-15 14:31:28 +0100798 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
799#ifdef MS_WINDOWS
800 PyObject *winerror = NULL;
801#endif
802
803 if (!oserror_use_init(Py_TYPE(self)))
804 /* Everything already done in OSError_new */
805 return 0;
806
807 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
808 return -1;
809
810 Py_INCREF(args);
811 if (oserror_parse_args(&args, &myerrno, &strerror, &filename
812#ifdef MS_WINDOWS
813 , &winerror
814#endif
815 ))
816 goto error;
817
818 if (oserror_init(self, &args, myerrno, strerror, filename
819#ifdef MS_WINDOWS
820 , winerror
821#endif
822 ))
823 goto error;
824
Thomas Wouters477c8d52006-05-27 19:21:47 +0000825 return 0;
Antoine Pitroue0e27352011-12-15 14:31:28 +0100826
827error:
828 Py_XDECREF(args);
829 return -1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000830}
831
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000832static int
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200833OSError_clear(PyOSErrorObject *self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000834{
835 Py_CLEAR(self->myerrno);
836 Py_CLEAR(self->strerror);
837 Py_CLEAR(self->filename);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200838#ifdef MS_WINDOWS
839 Py_CLEAR(self->winerror);
840#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000841 return BaseException_clear((PyBaseExceptionObject *)self);
842}
843
844static void
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200845OSError_dealloc(PyOSErrorObject *self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000846{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000847 _PyObject_GC_UNTRACK(self);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200848 OSError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +0000849 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000850}
851
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000852static int
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200853OSError_traverse(PyOSErrorObject *self, visitproc visit,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000854 void *arg)
855{
856 Py_VISIT(self->myerrno);
857 Py_VISIT(self->strerror);
858 Py_VISIT(self->filename);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200859#ifdef MS_WINDOWS
860 Py_VISIT(self->winerror);
861#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000862 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
863}
864
865static PyObject *
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200866OSError_str(PyOSErrorObject *self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000867{
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200868#ifdef MS_WINDOWS
869 /* If available, winerror has the priority over myerrno */
870 if (self->winerror && self->filename)
871 return PyUnicode_FromFormat("[Error %S] %S: %R",
872 self->winerror ? self->winerror: Py_None,
873 self->strerror ? self->strerror: Py_None,
874 self->filename);
875 if (self->winerror && self->strerror)
876 return PyUnicode_FromFormat("[Error %S] %S",
877 self->winerror ? self->winerror: Py_None,
878 self->strerror ? self->strerror: Py_None);
879#endif
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000880 if (self->filename)
881 return PyUnicode_FromFormat("[Errno %S] %S: %R",
882 self->myerrno ? self->myerrno: Py_None,
883 self->strerror ? self->strerror: Py_None,
884 self->filename);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200885 if (self->myerrno && self->strerror)
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +0000886 return PyUnicode_FromFormat("[Errno %S] %S",
887 self->myerrno ? self->myerrno: Py_None,
888 self->strerror ? self->strerror: Py_None);
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200889 return BaseException_str((PyBaseExceptionObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000890}
891
Thomas Wouters477c8d52006-05-27 19:21:47 +0000892static PyObject *
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200893OSError_reduce(PyOSErrorObject *self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000894{
895 PyObject *args = self->args;
896 PyObject *res = NULL, *tmp;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000897
Thomas Wouters477c8d52006-05-27 19:21:47 +0000898 /* self->args is only the first two real arguments if there was a
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200899 * file name given to OSError. */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000900 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000901 args = PyTuple_New(3);
902 if (!args) return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000903
904 tmp = PyTuple_GET_ITEM(self->args, 0);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000905 Py_INCREF(tmp);
906 PyTuple_SET_ITEM(args, 0, tmp);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000907
908 tmp = PyTuple_GET_ITEM(self->args, 1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000909 Py_INCREF(tmp);
910 PyTuple_SET_ITEM(args, 1, tmp);
911
912 Py_INCREF(self->filename);
913 PyTuple_SET_ITEM(args, 2, self->filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000914 } else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000915 Py_INCREF(args);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000916
917 if (self->dict)
Christian Heimes90aa7642007-12-19 02:45:37 +0000918 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000919 else
Christian Heimes90aa7642007-12-19 02:45:37 +0000920 res = PyTuple_Pack(2, Py_TYPE(self), args);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000921 Py_DECREF(args);
922 return res;
923}
924
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200925static PyObject *
926OSError_written_get(PyOSErrorObject *self, void *context)
927{
928 if (self->written == -1) {
929 PyErr_SetString(PyExc_AttributeError, "characters_written");
930 return NULL;
931 }
932 return PyLong_FromSsize_t(self->written);
933}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000934
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200935static int
936OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
937{
938 Py_ssize_t n;
939 n = PyNumber_AsSsize_t(arg, PyExc_ValueError);
940 if (n == -1 && PyErr_Occurred())
941 return -1;
942 self->written = n;
943 return 0;
944}
945
946static PyMemberDef OSError_members[] = {
947 {"errno", T_OBJECT, offsetof(PyOSErrorObject, myerrno), 0,
948 PyDoc_STR("POSIX exception code")},
949 {"strerror", T_OBJECT, offsetof(PyOSErrorObject, strerror), 0,
950 PyDoc_STR("exception strerror")},
951 {"filename", T_OBJECT, offsetof(PyOSErrorObject, filename), 0,
952 PyDoc_STR("exception filename")},
953#ifdef MS_WINDOWS
954 {"winerror", T_OBJECT, offsetof(PyOSErrorObject, winerror), 0,
955 PyDoc_STR("Win32 exception code")},
956#endif
957 {NULL} /* Sentinel */
958};
959
960static PyMethodDef OSError_methods[] = {
961 {"__reduce__", (PyCFunction)OSError_reduce, METH_NOARGS},
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962 {NULL}
963};
964
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200965static PyGetSetDef OSError_getset[] = {
966 {"characters_written", (getter) OSError_written_get,
967 (setter) OSError_written_set, NULL},
968 {NULL}
969};
970
971
972ComplexExtendsException(PyExc_Exception, OSError,
973 OSError, OSError_new,
974 OSError_methods, OSError_members, OSError_getset,
975 OSError_str,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000976 "Base class for I/O related errors.");
977
978
979/*
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200980 * Various OSError subclasses
Thomas Wouters477c8d52006-05-27 19:21:47 +0000981 */
Antoine Pitrou6b4883d2011-10-12 02:54:14 +0200982MiddlingExtendsException(PyExc_OSError, BlockingIOError, OSError,
983 "I/O operation would block.");
984MiddlingExtendsException(PyExc_OSError, ConnectionError, OSError,
985 "Connection error.");
986MiddlingExtendsException(PyExc_OSError, ChildProcessError, OSError,
987 "Child process error.");
988MiddlingExtendsException(PyExc_ConnectionError, BrokenPipeError, OSError,
989 "Broken pipe.");
990MiddlingExtendsException(PyExc_ConnectionError, ConnectionAbortedError, OSError,
991 "Connection aborted.");
992MiddlingExtendsException(PyExc_ConnectionError, ConnectionRefusedError, OSError,
993 "Connection refused.");
994MiddlingExtendsException(PyExc_ConnectionError, ConnectionResetError, OSError,
995 "Connection reset.");
996MiddlingExtendsException(PyExc_OSError, FileExistsError, OSError,
997 "File already exists.");
998MiddlingExtendsException(PyExc_OSError, FileNotFoundError, OSError,
999 "File not found.");
1000MiddlingExtendsException(PyExc_OSError, IsADirectoryError, OSError,
1001 "Operation doesn't work on directories.");
1002MiddlingExtendsException(PyExc_OSError, NotADirectoryError, OSError,
1003 "Operation only works on directories.");
1004MiddlingExtendsException(PyExc_OSError, InterruptedError, OSError,
1005 "Interrupted by signal.");
1006MiddlingExtendsException(PyExc_OSError, PermissionError, OSError,
1007 "Not enough permissions.");
1008MiddlingExtendsException(PyExc_OSError, ProcessLookupError, OSError,
1009 "Process not found.");
1010MiddlingExtendsException(PyExc_OSError, TimeoutError, OSError,
1011 "Timeout expired.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001012
1013/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001014 * EOFError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001015 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001016SimpleExtendsException(PyExc_Exception, EOFError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001017 "Read beyond end of file.");
1018
1019
1020/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001021 * RuntimeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001022 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001023SimpleExtendsException(PyExc_Exception, RuntimeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001024 "Unspecified run-time error.");
1025
1026
1027/*
1028 * NotImplementedError extends RuntimeError
1029 */
1030SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
1031 "Method or function hasn't been implemented yet.");
1032
1033/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001034 * NameError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001035 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001036SimpleExtendsException(PyExc_Exception, NameError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001037 "Name not found globally.");
1038
1039/*
1040 * UnboundLocalError extends NameError
1041 */
1042SimpleExtendsException(PyExc_NameError, UnboundLocalError,
1043 "Local name referenced but not bound to a value.");
1044
1045/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001046 * AttributeError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001047 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001048SimpleExtendsException(PyExc_Exception, AttributeError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001049 "Attribute not found.");
1050
1051
1052/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001053 * SyntaxError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001054 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001055
1056static int
1057SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
1058{
1059 PyObject *info = NULL;
1060 Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1061
1062 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1063 return -1;
1064
1065 if (lenargs >= 1) {
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001066 Py_CLEAR(self->msg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001067 self->msg = PyTuple_GET_ITEM(args, 0);
1068 Py_INCREF(self->msg);
1069 }
1070 if (lenargs == 2) {
1071 info = PyTuple_GET_ITEM(args, 1);
1072 info = PySequence_Tuple(info);
1073 if (!info) return -1;
1074
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001075 if (PyTuple_GET_SIZE(info) != 4) {
1076 /* not a very good error message, but it's what Python 2.4 gives */
1077 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1078 Py_DECREF(info);
1079 return -1;
1080 }
1081
1082 Py_CLEAR(self->filename);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001083 self->filename = PyTuple_GET_ITEM(info, 0);
1084 Py_INCREF(self->filename);
1085
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001086 Py_CLEAR(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001087 self->lineno = PyTuple_GET_ITEM(info, 1);
1088 Py_INCREF(self->lineno);
1089
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001090 Py_CLEAR(self->offset);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001091 self->offset = PyTuple_GET_ITEM(info, 2);
1092 Py_INCREF(self->offset);
1093
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001094 Py_CLEAR(self->text);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001095 self->text = PyTuple_GET_ITEM(info, 3);
1096 Py_INCREF(self->text);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001097
1098 Py_DECREF(info);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001099 }
1100 return 0;
1101}
1102
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001103static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001104SyntaxError_clear(PySyntaxErrorObject *self)
1105{
1106 Py_CLEAR(self->msg);
1107 Py_CLEAR(self->filename);
1108 Py_CLEAR(self->lineno);
1109 Py_CLEAR(self->offset);
1110 Py_CLEAR(self->text);
1111 Py_CLEAR(self->print_file_and_line);
1112 return BaseException_clear((PyBaseExceptionObject *)self);
1113}
1114
1115static void
1116SyntaxError_dealloc(PySyntaxErrorObject *self)
1117{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001118 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001119 SyntaxError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +00001120 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001121}
1122
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001123static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001124SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1125{
1126 Py_VISIT(self->msg);
1127 Py_VISIT(self->filename);
1128 Py_VISIT(self->lineno);
1129 Py_VISIT(self->offset);
1130 Py_VISIT(self->text);
1131 Py_VISIT(self->print_file_and_line);
1132 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1133}
1134
1135/* This is called "my_basename" instead of just "basename" to avoid name
1136 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1137 defined, and Python does define that. */
Victor Stinner6237daf2010-04-28 17:26:19 +00001138static PyObject*
1139my_basename(PyObject *name)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001140{
Victor Stinner6237daf2010-04-28 17:26:19 +00001141 Py_ssize_t i, size, offset;
Victor Stinner31392e72011-10-05 20:14:23 +02001142 int kind;
1143 void *data;
1144
1145 if (PyUnicode_READY(name))
1146 return NULL;
1147 kind = PyUnicode_KIND(name);
1148 data = PyUnicode_DATA(name);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001149 size = PyUnicode_GET_LENGTH(name);
Victor Stinner6237daf2010-04-28 17:26:19 +00001150 offset = 0;
1151 for(i=0; i < size; i++) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001152 if (PyUnicode_READ(kind, data, i) == SEP)
Victor Stinner6237daf2010-04-28 17:26:19 +00001153 offset = i + 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001154 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001155 if (offset != 0)
1156 return PyUnicode_Substring(name, offset, size);
1157 else {
Victor Stinner6237daf2010-04-28 17:26:19 +00001158 Py_INCREF(name);
1159 return name;
1160 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001161}
1162
1163
1164static PyObject *
1165SyntaxError_str(PySyntaxErrorObject *self)
1166{
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001167 int have_lineno = 0;
Victor Stinner6237daf2010-04-28 17:26:19 +00001168 PyObject *filename;
1169 PyObject *result;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001170 /* Below, we always ignore overflow errors, just printing -1.
1171 Still, we cannot allow an OverflowError to be raised, so
1172 we need to call PyLong_AsLongAndOverflow. */
1173 int overflow;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001174
1175 /* XXX -- do all the additional formatting with filename and
1176 lineno here */
1177
Neal Norwitzed2b7392007-08-26 04:51:10 +00001178 if (self->filename && PyUnicode_Check(self->filename)) {
Victor Stinner6237daf2010-04-28 17:26:19 +00001179 filename = my_basename(self->filename);
1180 if (filename == NULL)
1181 return NULL;
1182 } else {
1183 filename = NULL;
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001184 }
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001185 have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001186
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001187 if (!filename && !have_lineno)
Thomas Heller519a0422007-11-15 20:48:54 +00001188 return PyObject_Str(self->msg ? self->msg : Py_None);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001189
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001190 if (filename && have_lineno)
Victor Stinner6237daf2010-04-28 17:26:19 +00001191 result = PyUnicode_FromFormat("%S (%U, line %ld)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001192 self->msg ? self->msg : Py_None,
Victor Stinner6237daf2010-04-28 17:26:19 +00001193 filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Martin v. Löwis10a60b32007-07-18 02:28:27 +00001195 else if (filename)
Victor Stinner6237daf2010-04-28 17:26:19 +00001196 result = PyUnicode_FromFormat("%S (%U)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001197 self->msg ? self->msg : Py_None,
Victor Stinner6237daf2010-04-28 17:26:19 +00001198 filename);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001199 else /* only have_lineno */
Victor Stinner6237daf2010-04-28 17:26:19 +00001200 result = PyUnicode_FromFormat("%S (line %ld)",
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001201 self->msg ? self->msg : Py_None,
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00001202 PyLong_AsLongAndOverflow(self->lineno, &overflow));
Victor Stinner6237daf2010-04-28 17:26:19 +00001203 Py_XDECREF(filename);
1204 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001205}
1206
1207static PyMemberDef SyntaxError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001208 {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1209 PyDoc_STR("exception msg")},
1210 {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1211 PyDoc_STR("exception filename")},
1212 {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1213 PyDoc_STR("exception lineno")},
1214 {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1215 PyDoc_STR("exception offset")},
1216 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1217 PyDoc_STR("exception text")},
1218 {"print_file_and_line", T_OBJECT,
1219 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1220 PyDoc_STR("exception print_file_and_line")},
1221 {NULL} /* Sentinel */
1222};
1223
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001224ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001225 0, 0, SyntaxError_members, 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001226 SyntaxError_str, "Invalid syntax.");
1227
1228
1229/*
1230 * IndentationError extends SyntaxError
1231 */
1232MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
1233 "Improper indentation.");
1234
1235
1236/*
1237 * TabError extends IndentationError
1238 */
1239MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
1240 "Improper mixture of spaces and tabs.");
1241
1242
1243/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001244 * LookupError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001245 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001246SimpleExtendsException(PyExc_Exception, LookupError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001247 "Base class for lookup errors.");
1248
1249
1250/*
1251 * IndexError extends LookupError
1252 */
1253SimpleExtendsException(PyExc_LookupError, IndexError,
1254 "Sequence index out of range.");
1255
1256
1257/*
1258 * KeyError extends LookupError
1259 */
1260static PyObject *
1261KeyError_str(PyBaseExceptionObject *self)
1262{
1263 /* If args is a tuple of exactly one item, apply repr to args[0].
1264 This is done so that e.g. the exception raised by {}[''] prints
1265 KeyError: ''
1266 rather than the confusing
1267 KeyError
1268 alone. The downside is that if KeyError is raised with an explanatory
1269 string, that string will be displayed in quotes. Too bad.
1270 If args is anything else, use the default BaseException__str__().
1271 */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001272 if (PyTuple_GET_SIZE(self->args) == 1) {
Walter Dörwaldf5bec7c2007-05-26 15:03:32 +00001273 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001274 }
1275 return BaseException_str(self);
1276}
1277
1278ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02001279 0, 0, 0, 0, KeyError_str, "Mapping key not found.");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001280
1281
1282/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001283 * ValueError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001284 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001285SimpleExtendsException(PyExc_Exception, ValueError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001286 "Inappropriate argument value (of correct type).");
1287
1288/*
1289 * UnicodeError extends ValueError
1290 */
1291
1292SimpleExtendsException(PyExc_ValueError, UnicodeError,
1293 "Unicode related error.");
1294
Thomas Wouters477c8d52006-05-27 19:21:47 +00001295static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001296get_string(PyObject *attr, const char *name)
Walter Dörwald612344f2007-05-04 19:28:21 +00001297{
1298 if (!attr) {
1299 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1300 return NULL;
1301 }
1302
Christian Heimes72b710a2008-05-26 13:28:38 +00001303 if (!PyBytes_Check(attr)) {
Walter Dörwald612344f2007-05-04 19:28:21 +00001304 PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1305 return NULL;
1306 }
1307 Py_INCREF(attr);
1308 return attr;
1309}
1310
1311static PyObject *
Thomas Wouters477c8d52006-05-27 19:21:47 +00001312get_unicode(PyObject *attr, const char *name)
1313{
1314 if (!attr) {
1315 PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1316 return NULL;
1317 }
1318
1319 if (!PyUnicode_Check(attr)) {
1320 PyErr_Format(PyExc_TypeError,
1321 "%.200s attribute must be unicode", name);
1322 return NULL;
1323 }
1324 Py_INCREF(attr);
1325 return attr;
1326}
1327
Walter Dörwaldd2034312007-05-18 16:29:38 +00001328static int
1329set_unicodefromstring(PyObject **attr, const char *value)
1330{
1331 PyObject *obj = PyUnicode_FromString(value);
1332 if (!obj)
1333 return -1;
1334 Py_CLEAR(*attr);
1335 *attr = obj;
1336 return 0;
1337}
1338
Thomas Wouters477c8d52006-05-27 19:21:47 +00001339PyObject *
1340PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1341{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001342 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001343}
1344
1345PyObject *
1346PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1347{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001348 return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001349}
1350
1351PyObject *
1352PyUnicodeEncodeError_GetObject(PyObject *exc)
1353{
1354 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1355}
1356
1357PyObject *
1358PyUnicodeDecodeError_GetObject(PyObject *exc)
1359{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001360 return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001361}
1362
1363PyObject *
1364PyUnicodeTranslateError_GetObject(PyObject *exc)
1365{
1366 return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1367}
1368
1369int
1370PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1371{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001372 Py_ssize_t size;
1373 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1374 "object");
1375 if (!obj)
1376 return -1;
1377 *start = ((PyUnicodeErrorObject *)exc)->start;
Victor Stinner9e30aa52011-11-21 02:49:52 +01001378 size = PyUnicode_GET_LENGTH(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001379 if (*start<0)
1380 *start = 0; /*XXX check for values <0*/
1381 if (*start>=size)
1382 *start = size-1;
1383 Py_DECREF(obj);
1384 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001385}
1386
1387
1388int
1389PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1390{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001391 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001392 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001393 if (!obj)
1394 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001395 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001396 *start = ((PyUnicodeErrorObject *)exc)->start;
1397 if (*start<0)
1398 *start = 0;
1399 if (*start>=size)
1400 *start = size-1;
1401 Py_DECREF(obj);
1402 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001403}
1404
1405
1406int
1407PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1408{
1409 return PyUnicodeEncodeError_GetStart(exc, start);
1410}
1411
1412
1413int
1414PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1415{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001416 ((PyUnicodeErrorObject *)exc)->start = start;
1417 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001418}
1419
1420
1421int
1422PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1423{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001424 ((PyUnicodeErrorObject *)exc)->start = start;
1425 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001426}
1427
1428
1429int
1430PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1431{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001432 ((PyUnicodeErrorObject *)exc)->start = start;
1433 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001434}
1435
1436
1437int
1438PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1439{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001440 Py_ssize_t size;
1441 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1442 "object");
1443 if (!obj)
1444 return -1;
1445 *end = ((PyUnicodeErrorObject *)exc)->end;
Victor Stinner9e30aa52011-11-21 02:49:52 +01001446 size = PyUnicode_GET_LENGTH(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001447 if (*end<1)
1448 *end = 1;
1449 if (*end>size)
1450 *end = size;
1451 Py_DECREF(obj);
1452 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001453}
1454
1455
1456int
1457PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1458{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001459 Py_ssize_t size;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001460 PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001461 if (!obj)
1462 return -1;
Christian Heimes72b710a2008-05-26 13:28:38 +00001463 size = PyBytes_GET_SIZE(obj);
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001464 *end = ((PyUnicodeErrorObject *)exc)->end;
1465 if (*end<1)
1466 *end = 1;
1467 if (*end>size)
1468 *end = size;
1469 Py_DECREF(obj);
1470 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001471}
1472
1473
1474int
1475PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1476{
1477 return PyUnicodeEncodeError_GetEnd(exc, start);
1478}
1479
1480
1481int
1482PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1483{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001484 ((PyUnicodeErrorObject *)exc)->end = end;
1485 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001486}
1487
1488
1489int
1490PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1491{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001492 ((PyUnicodeErrorObject *)exc)->end = end;
1493 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001494}
1495
1496
1497int
1498PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1499{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001500 ((PyUnicodeErrorObject *)exc)->end = end;
1501 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001502}
1503
1504PyObject *
1505PyUnicodeEncodeError_GetReason(PyObject *exc)
1506{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001507 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001508}
1509
1510
1511PyObject *
1512PyUnicodeDecodeError_GetReason(PyObject *exc)
1513{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001514 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001515}
1516
1517
1518PyObject *
1519PyUnicodeTranslateError_GetReason(PyObject *exc)
1520{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001521 return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
Thomas Wouters477c8d52006-05-27 19:21:47 +00001522}
1523
1524
1525int
1526PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1527{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001528 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1529 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001530}
1531
1532
1533int
1534PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1535{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001536 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1537 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001538}
1539
1540
1541int
1542PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1543{
Walter Dörwaldd2034312007-05-18 16:29:38 +00001544 return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1545 reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001546}
1547
1548
Thomas Wouters477c8d52006-05-27 19:21:47 +00001549static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001550UnicodeError_clear(PyUnicodeErrorObject *self)
1551{
1552 Py_CLEAR(self->encoding);
1553 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001554 Py_CLEAR(self->reason);
1555 return BaseException_clear((PyBaseExceptionObject *)self);
1556}
1557
1558static void
1559UnicodeError_dealloc(PyUnicodeErrorObject *self)
1560{
Thomas Wouters89f507f2006-12-13 04:49:30 +00001561 _PyObject_GC_UNTRACK(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001562 UnicodeError_clear(self);
Christian Heimes90aa7642007-12-19 02:45:37 +00001563 Py_TYPE(self)->tp_free((PyObject *)self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001564}
1565
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001566static int
Thomas Wouters477c8d52006-05-27 19:21:47 +00001567UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1568{
1569 Py_VISIT(self->encoding);
1570 Py_VISIT(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001571 Py_VISIT(self->reason);
1572 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1573}
1574
1575static PyMemberDef UnicodeError_members[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +00001576 {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1577 PyDoc_STR("exception encoding")},
1578 {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1579 PyDoc_STR("exception object")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001580 {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001581 PyDoc_STR("exception start")},
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001582 {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001583 PyDoc_STR("exception end")},
1584 {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1585 PyDoc_STR("exception reason")},
1586 {NULL} /* Sentinel */
1587};
1588
1589
1590/*
1591 * UnicodeEncodeError extends UnicodeError
1592 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001593
1594static int
1595UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1596{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001597 PyUnicodeErrorObject *err;
1598
Thomas Wouters477c8d52006-05-27 19:21:47 +00001599 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1600 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001601
1602 err = (PyUnicodeErrorObject *)self;
1603
1604 Py_CLEAR(err->encoding);
1605 Py_CLEAR(err->object);
1606 Py_CLEAR(err->reason);
1607
1608 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1609 &PyUnicode_Type, &err->encoding,
1610 &PyUnicode_Type, &err->object,
1611 &err->start,
1612 &err->end,
1613 &PyUnicode_Type, &err->reason)) {
1614 err->encoding = err->object = err->reason = NULL;
1615 return -1;
1616 }
1617
Martin v. Löwisb09af032011-11-04 11:16:41 +01001618 if (PyUnicode_READY(err->object) < -1) {
1619 err->encoding = NULL;
1620 return -1;
1621 }
1622
Guido van Rossum98297ee2007-11-06 21:34:58 +00001623 Py_INCREF(err->encoding);
1624 Py_INCREF(err->object);
1625 Py_INCREF(err->reason);
1626
1627 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001628}
1629
1630static PyObject *
1631UnicodeEncodeError_str(PyObject *self)
1632{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001633 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001634 PyObject *result = NULL;
1635 PyObject *reason_str = NULL;
1636 PyObject *encoding_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001637
Eric Smith0facd772010-02-24 15:42:29 +00001638 /* Get reason and encoding as strings, which they might not be if
1639 they've been modified after we were contructed. */
1640 reason_str = PyObject_Str(uself->reason);
1641 if (reason_str == NULL)
1642 goto done;
1643 encoding_str = PyObject_Str(uself->encoding);
1644 if (encoding_str == NULL)
1645 goto done;
1646
Victor Stinnerda1ddf32011-11-20 22:50:23 +01001647 if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) {
1648 Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
Walter Dörwald787b03b2007-06-05 13:29:29 +00001649 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001650 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001651 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001652 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001653 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001654 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001655 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
Eric Smith0facd772010-02-24 15:42:29 +00001656 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001657 fmt,
Eric Smith0facd772010-02-24 15:42:29 +00001658 encoding_str,
Victor Stinnerda1ddf32011-11-20 22:50:23 +01001659 (int)badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001660 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001661 reason_str);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001662 }
Eric Smith0facd772010-02-24 15:42:29 +00001663 else {
1664 result = PyUnicode_FromFormat(
1665 "'%U' codec can't encode characters in position %zd-%zd: %U",
1666 encoding_str,
1667 uself->start,
1668 uself->end-1,
1669 reason_str);
1670 }
1671done:
1672 Py_XDECREF(reason_str);
1673 Py_XDECREF(encoding_str);
1674 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001675}
1676
1677static PyTypeObject _PyExc_UnicodeEncodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001678 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001679 "UnicodeEncodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001680 sizeof(PyUnicodeErrorObject), 0,
1681 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1682 (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1683 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001684 PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1685 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001686 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001687 (initproc)UnicodeEncodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001688};
1689PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1690
1691PyObject *
1692PyUnicodeEncodeError_Create(
1693 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1694 Py_ssize_t start, Py_ssize_t end, const char *reason)
1695{
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001696 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001697 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001698}
1699
1700
1701/*
1702 * UnicodeDecodeError extends UnicodeError
1703 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001704
1705static int
1706UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1707{
Guido van Rossum98297ee2007-11-06 21:34:58 +00001708 PyUnicodeErrorObject *ude;
1709 const char *data;
1710 Py_ssize_t size;
1711
Thomas Wouters477c8d52006-05-27 19:21:47 +00001712 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1713 return -1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001714
1715 ude = (PyUnicodeErrorObject *)self;
1716
1717 Py_CLEAR(ude->encoding);
1718 Py_CLEAR(ude->object);
1719 Py_CLEAR(ude->reason);
1720
1721 if (!PyArg_ParseTuple(args, "O!OnnO!",
1722 &PyUnicode_Type, &ude->encoding,
1723 &ude->object,
1724 &ude->start,
1725 &ude->end,
1726 &PyUnicode_Type, &ude->reason)) {
1727 ude->encoding = ude->object = ude->reason = NULL;
1728 return -1;
1729 }
1730
Christian Heimes72b710a2008-05-26 13:28:38 +00001731 if (!PyBytes_Check(ude->object)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001732 if (PyObject_AsReadBuffer(ude->object, (const void **)&data, &size)) {
1733 ude->encoding = ude->object = ude->reason = NULL;
1734 return -1;
1735 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001736 ude->object = PyBytes_FromStringAndSize(data, size);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001737 }
1738 else {
1739 Py_INCREF(ude->object);
1740 }
1741
1742 Py_INCREF(ude->encoding);
1743 Py_INCREF(ude->reason);
1744
1745 return 0;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001746}
1747
1748static PyObject *
1749UnicodeDecodeError_str(PyObject *self)
1750{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001751 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001752 PyObject *result = NULL;
1753 PyObject *reason_str = NULL;
1754 PyObject *encoding_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001755
Eric Smith0facd772010-02-24 15:42:29 +00001756 /* Get reason and encoding as strings, which they might not be if
1757 they've been modified after we were contructed. */
1758 reason_str = PyObject_Str(uself->reason);
1759 if (reason_str == NULL)
1760 goto done;
1761 encoding_str = PyObject_Str(uself->encoding);
1762 if (encoding_str == NULL)
1763 goto done;
1764
1765 if (uself->start < PyBytes_GET_SIZE(uself->object) && uself->end == uself->start+1) {
Christian Heimes72b710a2008-05-26 13:28:38 +00001766 int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
Eric Smith0facd772010-02-24 15:42:29 +00001767 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001768 "'%U' codec can't decode byte 0x%02x in position %zd: %U",
Eric Smith0facd772010-02-24 15:42:29 +00001769 encoding_str,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001770 byte,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001771 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001772 reason_str);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001773 }
Eric Smith0facd772010-02-24 15:42:29 +00001774 else {
1775 result = PyUnicode_FromFormat(
1776 "'%U' codec can't decode bytes in position %zd-%zd: %U",
1777 encoding_str,
1778 uself->start,
1779 uself->end-1,
1780 reason_str
1781 );
1782 }
1783done:
1784 Py_XDECREF(reason_str);
1785 Py_XDECREF(encoding_str);
1786 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001787}
1788
1789static PyTypeObject _PyExc_UnicodeDecodeError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001790 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001791 "UnicodeDecodeError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001792 sizeof(PyUnicodeErrorObject), 0,
1793 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1794 (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
1795 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001796 PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
1797 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001798 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001799 (initproc)UnicodeDecodeError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001800};
1801PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
1802
1803PyObject *
1804PyUnicodeDecodeError_Create(
1805 const char *encoding, const char *object, Py_ssize_t length,
1806 Py_ssize_t start, Py_ssize_t end, const char *reason)
1807{
Victor Stinner7eeb5b52010-06-07 19:57:46 +00001808 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001809 encoding, object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001810}
1811
1812
1813/*
1814 * UnicodeTranslateError extends UnicodeError
1815 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001816
1817static int
1818UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
1819 PyObject *kwds)
1820{
1821 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1822 return -1;
1823
1824 Py_CLEAR(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001825 Py_CLEAR(self->reason);
1826
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001827 if (!PyArg_ParseTuple(args, "O!nnO!",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001828 &PyUnicode_Type, &self->object,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001829 &self->start,
1830 &self->end,
Walter Dörwaldd2034312007-05-18 16:29:38 +00001831 &PyUnicode_Type, &self->reason)) {
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001832 self->object = self->reason = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001833 return -1;
1834 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001835
Thomas Wouters477c8d52006-05-27 19:21:47 +00001836 Py_INCREF(self->object);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001837 Py_INCREF(self->reason);
1838
1839 return 0;
1840}
1841
1842
1843static PyObject *
1844UnicodeTranslateError_str(PyObject *self)
1845{
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001846 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
Eric Smith0facd772010-02-24 15:42:29 +00001847 PyObject *result = NULL;
1848 PyObject *reason_str = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001849
Eric Smith0facd772010-02-24 15:42:29 +00001850 /* Get reason as a string, which it might not be if it's been
1851 modified after we were contructed. */
1852 reason_str = PyObject_Str(uself->reason);
1853 if (reason_str == NULL)
1854 goto done;
1855
Victor Stinner53b33e72011-11-21 01:17:27 +01001856 if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) {
1857 Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
Walter Dörwald787b03b2007-06-05 13:29:29 +00001858 const char *fmt;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001859 if (badchar <= 0xff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001860 fmt = "can't translate character '\\x%02x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001861 else if (badchar <= 0xffff)
Walter Dörwald32a4c712007-06-20 09:25:34 +00001862 fmt = "can't translate character '\\u%04x' in position %zd: %U";
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001863 else
Walter Dörwald32a4c712007-06-20 09:25:34 +00001864 fmt = "can't translate character '\\U%08x' in position %zd: %U";
Benjamin Petersonc5f4e1e2010-02-25 01:22:28 +00001865 result = PyUnicode_FromFormat(
Walter Dörwald787b03b2007-06-05 13:29:29 +00001866 fmt,
Victor Stinner53b33e72011-11-21 01:17:27 +01001867 (int)badchar,
Guido van Rossum7eaf8222007-06-18 17:58:50 +00001868 uself->start,
Eric Smith0facd772010-02-24 15:42:29 +00001869 reason_str
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001870 );
Eric Smith0facd772010-02-24 15:42:29 +00001871 } else {
1872 result = PyUnicode_FromFormat(
1873 "can't translate characters in position %zd-%zd: %U",
1874 uself->start,
1875 uself->end-1,
1876 reason_str
1877 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001878 }
Eric Smith0facd772010-02-24 15:42:29 +00001879done:
1880 Py_XDECREF(reason_str);
1881 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001882}
1883
1884static PyTypeObject _PyExc_UnicodeTranslateError = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001885 PyVarObject_HEAD_INIT(NULL, 0)
Neal Norwitz2633c692007-02-26 22:22:47 +00001886 "UnicodeTranslateError",
Thomas Wouters477c8d52006-05-27 19:21:47 +00001887 sizeof(PyUnicodeErrorObject), 0,
1888 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1889 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1890 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Thomas Wouters89f507f2006-12-13 04:49:30 +00001891 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001892 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1893 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001894 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001895};
1896PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
1897
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001898/* Deprecated. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001899PyObject *
1900PyUnicodeTranslateError_Create(
1901 const Py_UNICODE *object, Py_ssize_t length,
1902 Py_ssize_t start, Py_ssize_t end, const char *reason)
1903{
1904 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00001905 object, length, start, end, reason);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001906}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001907
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001908PyObject *
1909_PyUnicodeTranslateError_Create(
1910 PyObject *object,
1911 Py_ssize_t start, Py_ssize_t end, const char *reason)
1912{
1913 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "Ons",
1914 object, start, end, reason);
1915}
Thomas Wouters477c8d52006-05-27 19:21:47 +00001916
1917/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001918 * AssertionError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001919 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001920SimpleExtendsException(PyExc_Exception, AssertionError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001921 "Assertion failed.");
1922
1923
1924/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001925 * ArithmeticError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001926 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001927SimpleExtendsException(PyExc_Exception, ArithmeticError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001928 "Base class for arithmetic errors.");
1929
1930
1931/*
1932 * FloatingPointError extends ArithmeticError
1933 */
1934SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
1935 "Floating point operation failed.");
1936
1937
1938/*
1939 * OverflowError extends ArithmeticError
1940 */
1941SimpleExtendsException(PyExc_ArithmeticError, OverflowError,
1942 "Result too large to be represented.");
1943
1944
1945/*
1946 * ZeroDivisionError extends ArithmeticError
1947 */
1948SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
1949 "Second argument to a division or modulo operation was zero.");
1950
1951
1952/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001953 * SystemError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001954 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001955SimpleExtendsException(PyExc_Exception, SystemError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001956 "Internal error in the Python interpreter.\n"
1957 "\n"
1958 "Please report this to the Python maintainer, along with the traceback,\n"
1959 "the Python version, and the hardware/OS platform and version.");
1960
1961
1962/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001963 * ReferenceError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001964 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001965SimpleExtendsException(PyExc_Exception, ReferenceError,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001966 "Weak ref proxy used after referent went away.");
1967
1968
1969/*
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001970 * MemoryError extends Exception
Thomas Wouters477c8d52006-05-27 19:21:47 +00001971 */
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00001972
1973#define MEMERRORS_SAVE 16
1974static PyBaseExceptionObject *memerrors_freelist = NULL;
1975static int memerrors_numfree = 0;
1976
1977static PyObject *
1978MemoryError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1979{
1980 PyBaseExceptionObject *self;
1981
1982 if (type != (PyTypeObject *) PyExc_MemoryError)
1983 return BaseException_new(type, args, kwds);
1984 if (memerrors_freelist == NULL)
1985 return BaseException_new(type, args, kwds);
1986 /* Fetch object from freelist and revive it */
1987 self = memerrors_freelist;
1988 self->args = PyTuple_New(0);
1989 /* This shouldn't happen since the empty tuple is persistent */
1990 if (self->args == NULL)
1991 return NULL;
1992 memerrors_freelist = (PyBaseExceptionObject *) self->dict;
1993 memerrors_numfree--;
1994 self->dict = NULL;
1995 _Py_NewReference((PyObject *)self);
1996 _PyObject_GC_TRACK(self);
1997 return (PyObject *)self;
1998}
1999
2000static void
2001MemoryError_dealloc(PyBaseExceptionObject *self)
2002{
2003 _PyObject_GC_UNTRACK(self);
2004 BaseException_clear(self);
2005 if (memerrors_numfree >= MEMERRORS_SAVE)
2006 Py_TYPE(self)->tp_free((PyObject *)self);
2007 else {
2008 self->dict = (PyObject *) memerrors_freelist;
2009 memerrors_freelist = self;
2010 memerrors_numfree++;
2011 }
2012}
2013
2014static void
2015preallocate_memerrors(void)
2016{
2017 /* We create enough MemoryErrors and then decref them, which will fill
2018 up the freelist. */
2019 int i;
2020 PyObject *errors[MEMERRORS_SAVE];
2021 for (i = 0; i < MEMERRORS_SAVE; i++) {
2022 errors[i] = MemoryError_new((PyTypeObject *) PyExc_MemoryError,
2023 NULL, NULL);
2024 if (!errors[i])
2025 Py_FatalError("Could not preallocate MemoryError object");
2026 }
2027 for (i = 0; i < MEMERRORS_SAVE; i++) {
2028 Py_DECREF(errors[i]);
2029 }
2030}
2031
2032static void
2033free_preallocated_memerrors(void)
2034{
2035 while (memerrors_freelist != NULL) {
2036 PyObject *self = (PyObject *) memerrors_freelist;
2037 memerrors_freelist = (PyBaseExceptionObject *) memerrors_freelist->dict;
2038 Py_TYPE(self)->tp_free((PyObject *)self);
2039 }
2040}
2041
2042
2043static PyTypeObject _PyExc_MemoryError = {
2044 PyVarObject_HEAD_INIT(NULL, 0)
2045 "MemoryError",
2046 sizeof(PyBaseExceptionObject),
2047 0, (destructor)MemoryError_dealloc, 0, 0, 0, 0, 0, 0, 0,
2048 0, 0, 0, 0, 0, 0, 0,
2049 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
2050 PyDoc_STR("Out of memory."), (traverseproc)BaseException_traverse,
2051 (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception,
2052 0, 0, 0, offsetof(PyBaseExceptionObject, dict),
2053 (initproc)BaseException_init, 0, MemoryError_new
2054};
2055PyObject *PyExc_MemoryError = (PyObject *) &_PyExc_MemoryError;
2056
Thomas Wouters477c8d52006-05-27 19:21:47 +00002057
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00002058/*
2059 * BufferError extends Exception
2060 */
2061SimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
2062
Thomas Wouters477c8d52006-05-27 19:21:47 +00002063
2064/* Warning category docstrings */
2065
2066/*
2067 * Warning extends Exception
2068 */
2069SimpleExtendsException(PyExc_Exception, Warning,
2070 "Base class for warning categories.");
2071
2072
2073/*
2074 * UserWarning extends Warning
2075 */
2076SimpleExtendsException(PyExc_Warning, UserWarning,
2077 "Base class for warnings generated by user code.");
2078
2079
2080/*
2081 * DeprecationWarning extends Warning
2082 */
2083SimpleExtendsException(PyExc_Warning, DeprecationWarning,
2084 "Base class for warnings about deprecated features.");
2085
2086
2087/*
2088 * PendingDeprecationWarning extends Warning
2089 */
2090SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
2091 "Base class for warnings about features which will be deprecated\n"
2092 "in the future.");
2093
2094
2095/*
2096 * SyntaxWarning extends Warning
2097 */
2098SimpleExtendsException(PyExc_Warning, SyntaxWarning,
2099 "Base class for warnings about dubious syntax.");
2100
2101
2102/*
2103 * RuntimeWarning extends Warning
2104 */
2105SimpleExtendsException(PyExc_Warning, RuntimeWarning,
2106 "Base class for warnings about dubious runtime behavior.");
2107
2108
2109/*
2110 * FutureWarning extends Warning
2111 */
2112SimpleExtendsException(PyExc_Warning, FutureWarning,
2113 "Base class for warnings about constructs that will change semantically\n"
2114 "in the future.");
2115
2116
2117/*
2118 * ImportWarning extends Warning
2119 */
2120SimpleExtendsException(PyExc_Warning, ImportWarning,
2121 "Base class for warnings about probable mistakes in module imports");
2122
2123
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002124/*
2125 * UnicodeWarning extends Warning
2126 */
2127SimpleExtendsException(PyExc_Warning, UnicodeWarning,
2128 "Base class for warnings about Unicode related problems, mostly\n"
2129 "related to conversion problems.");
2130
Georg Brandl08be72d2010-10-24 15:11:22 +00002131
Guido van Rossum98297ee2007-11-06 21:34:58 +00002132/*
2133 * BytesWarning extends Warning
2134 */
2135SimpleExtendsException(PyExc_Warning, BytesWarning,
2136 "Base class for warnings about bytes and buffer related problems, mostly\n"
2137 "related to conversion from str or comparing to str.");
2138
2139
Georg Brandl08be72d2010-10-24 15:11:22 +00002140/*
2141 * ResourceWarning extends Warning
2142 */
2143SimpleExtendsException(PyExc_Warning, ResourceWarning,
2144 "Base class for warnings about resource usage.");
2145
2146
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002147
Thomas Wouters89d996e2007-09-08 17:39:28 +00002148/* Pre-computed RuntimeError instance for when recursion depth is reached.
2149 Meant to be used when normalizing the exception for exceeding the recursion
2150 depth will cause its own infinite recursion.
2151*/
2152PyObject *PyExc_RecursionErrorInst = NULL;
2153
Thomas Wouters477c8d52006-05-27 19:21:47 +00002154#define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
2155 Py_FatalError("exceptions bootstrapping error.");
2156
2157#define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00002158 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
2159 Py_FatalError("Module dictionary insertion problem.");
2160
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002161#define INIT_ALIAS(NAME, TYPE) Py_INCREF(PyExc_ ## TYPE); \
2162 PyExc_ ## NAME = PyExc_ ## TYPE; \
2163 if (PyDict_SetItemString(bdict, # NAME, PyExc_ ## NAME)) \
2164 Py_FatalError("Module dictionary insertion problem.");
2165
2166#define ADD_ERRNO(TYPE, CODE) { \
2167 PyObject *_code = PyLong_FromLong(CODE); \
2168 assert(_PyObject_RealIsSubclass(PyExc_ ## TYPE, PyExc_OSError)); \
2169 if (!_code || PyDict_SetItem(errnomap, _code, PyExc_ ## TYPE)) \
2170 Py_FatalError("errmap insertion problem."); \
2171 }
2172
2173#ifdef MS_WINDOWS
2174#include <Winsock2.h>
2175#if defined(WSAEALREADY) && !defined(EALREADY)
2176#define EALREADY WSAEALREADY
2177#endif
2178#if defined(WSAECONNABORTED) && !defined(ECONNABORTED)
2179#define ECONNABORTED WSAECONNABORTED
2180#endif
2181#if defined(WSAECONNREFUSED) && !defined(ECONNREFUSED)
2182#define ECONNREFUSED WSAECONNREFUSED
2183#endif
2184#if defined(WSAECONNRESET) && !defined(ECONNRESET)
2185#define ECONNRESET WSAECONNRESET
2186#endif
2187#if defined(WSAEINPROGRESS) && !defined(EINPROGRESS)
2188#define EINPROGRESS WSAEINPROGRESS
2189#endif
2190#if defined(WSAESHUTDOWN) && !defined(ESHUTDOWN)
2191#define ESHUTDOWN WSAESHUTDOWN
2192#endif
2193#if defined(WSAETIMEDOUT) && !defined(ETIMEDOUT)
2194#define ETIMEDOUT WSAETIMEDOUT
2195#endif
2196#if defined(WSAEWOULDBLOCK) && !defined(EWOULDBLOCK)
2197#define EWOULDBLOCK WSAEWOULDBLOCK
2198#endif
2199#endif /* MS_WINDOWS */
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002200
Martin v. Löwis1a214512008-06-11 05:26:20 +00002201void
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002202_PyExc_Init(void)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002203{
Neal Norwitz2633c692007-02-26 22:22:47 +00002204 PyObject *bltinmod, *bdict;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002205
2206 PRE_INIT(BaseException)
2207 PRE_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002208 PRE_INIT(TypeError)
2209 PRE_INIT(StopIteration)
2210 PRE_INIT(GeneratorExit)
2211 PRE_INIT(SystemExit)
2212 PRE_INIT(KeyboardInterrupt)
2213 PRE_INIT(ImportError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002214 PRE_INIT(OSError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002215 PRE_INIT(EOFError)
2216 PRE_INIT(RuntimeError)
2217 PRE_INIT(NotImplementedError)
2218 PRE_INIT(NameError)
2219 PRE_INIT(UnboundLocalError)
2220 PRE_INIT(AttributeError)
2221 PRE_INIT(SyntaxError)
2222 PRE_INIT(IndentationError)
2223 PRE_INIT(TabError)
2224 PRE_INIT(LookupError)
2225 PRE_INIT(IndexError)
2226 PRE_INIT(KeyError)
2227 PRE_INIT(ValueError)
2228 PRE_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002229 PRE_INIT(UnicodeEncodeError)
2230 PRE_INIT(UnicodeDecodeError)
2231 PRE_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002232 PRE_INIT(AssertionError)
2233 PRE_INIT(ArithmeticError)
2234 PRE_INIT(FloatingPointError)
2235 PRE_INIT(OverflowError)
2236 PRE_INIT(ZeroDivisionError)
2237 PRE_INIT(SystemError)
2238 PRE_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00002239 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002240 PRE_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00002241 PRE_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002242 PRE_INIT(Warning)
2243 PRE_INIT(UserWarning)
2244 PRE_INIT(DeprecationWarning)
2245 PRE_INIT(PendingDeprecationWarning)
2246 PRE_INIT(SyntaxWarning)
2247 PRE_INIT(RuntimeWarning)
2248 PRE_INIT(FutureWarning)
2249 PRE_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002250 PRE_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00002251 PRE_INIT(BytesWarning)
Georg Brandl08be72d2010-10-24 15:11:22 +00002252 PRE_INIT(ResourceWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002253
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002254 /* OSError subclasses */
2255 PRE_INIT(ConnectionError);
2256
2257 PRE_INIT(BlockingIOError);
2258 PRE_INIT(BrokenPipeError);
2259 PRE_INIT(ChildProcessError);
2260 PRE_INIT(ConnectionAbortedError);
2261 PRE_INIT(ConnectionRefusedError);
2262 PRE_INIT(ConnectionResetError);
2263 PRE_INIT(FileExistsError);
2264 PRE_INIT(FileNotFoundError);
2265 PRE_INIT(IsADirectoryError);
2266 PRE_INIT(NotADirectoryError);
2267 PRE_INIT(InterruptedError);
2268 PRE_INIT(PermissionError);
2269 PRE_INIT(ProcessLookupError);
2270 PRE_INIT(TimeoutError);
2271
Georg Brandl1a3284e2007-12-02 09:40:06 +00002272 bltinmod = PyImport_ImportModule("builtins");
Thomas Wouters477c8d52006-05-27 19:21:47 +00002273 if (bltinmod == NULL)
2274 Py_FatalError("exceptions bootstrapping error.");
2275 bdict = PyModule_GetDict(bltinmod);
2276 if (bdict == NULL)
2277 Py_FatalError("exceptions bootstrapping error.");
2278
2279 POST_INIT(BaseException)
2280 POST_INIT(Exception)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002281 POST_INIT(TypeError)
2282 POST_INIT(StopIteration)
2283 POST_INIT(GeneratorExit)
2284 POST_INIT(SystemExit)
2285 POST_INIT(KeyboardInterrupt)
2286 POST_INIT(ImportError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002287 POST_INIT(OSError)
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002288 INIT_ALIAS(EnvironmentError, OSError)
2289 INIT_ALIAS(IOError, OSError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002290#ifdef MS_WINDOWS
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002291 INIT_ALIAS(WindowsError, OSError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002292#endif
2293#ifdef __VMS
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002294 INIT_ALIAS(VMSError, OSError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002295#endif
2296 POST_INIT(EOFError)
2297 POST_INIT(RuntimeError)
2298 POST_INIT(NotImplementedError)
2299 POST_INIT(NameError)
2300 POST_INIT(UnboundLocalError)
2301 POST_INIT(AttributeError)
2302 POST_INIT(SyntaxError)
2303 POST_INIT(IndentationError)
2304 POST_INIT(TabError)
2305 POST_INIT(LookupError)
2306 POST_INIT(IndexError)
2307 POST_INIT(KeyError)
2308 POST_INIT(ValueError)
2309 POST_INIT(UnicodeError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002310 POST_INIT(UnicodeEncodeError)
2311 POST_INIT(UnicodeDecodeError)
2312 POST_INIT(UnicodeTranslateError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002313 POST_INIT(AssertionError)
2314 POST_INIT(ArithmeticError)
2315 POST_INIT(FloatingPointError)
2316 POST_INIT(OverflowError)
2317 POST_INIT(ZeroDivisionError)
2318 POST_INIT(SystemError)
2319 POST_INIT(ReferenceError)
Neal Norwitzfaa54a32007-08-19 04:23:20 +00002320 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002321 POST_INIT(MemoryError)
Benjamin Peterson2b968d62008-07-05 23:38:30 +00002322 POST_INIT(BufferError)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002323 POST_INIT(Warning)
2324 POST_INIT(UserWarning)
2325 POST_INIT(DeprecationWarning)
2326 POST_INIT(PendingDeprecationWarning)
2327 POST_INIT(SyntaxWarning)
2328 POST_INIT(RuntimeWarning)
2329 POST_INIT(FutureWarning)
2330 POST_INIT(ImportWarning)
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00002331 POST_INIT(UnicodeWarning)
Guido van Rossum98297ee2007-11-06 21:34:58 +00002332 POST_INIT(BytesWarning)
Georg Brandl08be72d2010-10-24 15:11:22 +00002333 POST_INIT(ResourceWarning)
Thomas Wouters477c8d52006-05-27 19:21:47 +00002334
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002335 errnomap = PyDict_New();
2336 if (!errnomap)
2337 Py_FatalError("Cannot allocate map from errnos to OSError subclasses");
2338
2339 /* OSError subclasses */
2340 POST_INIT(ConnectionError);
2341
2342 POST_INIT(BlockingIOError);
2343 ADD_ERRNO(BlockingIOError, EAGAIN);
2344 ADD_ERRNO(BlockingIOError, EALREADY);
2345 ADD_ERRNO(BlockingIOError, EINPROGRESS);
2346 ADD_ERRNO(BlockingIOError, EWOULDBLOCK);
2347 POST_INIT(BrokenPipeError);
2348 ADD_ERRNO(BrokenPipeError, EPIPE);
2349 ADD_ERRNO(BrokenPipeError, ESHUTDOWN);
2350 POST_INIT(ChildProcessError);
2351 ADD_ERRNO(ChildProcessError, ECHILD);
2352 POST_INIT(ConnectionAbortedError);
2353 ADD_ERRNO(ConnectionAbortedError, ECONNABORTED);
2354 POST_INIT(ConnectionRefusedError);
2355 ADD_ERRNO(ConnectionRefusedError, ECONNREFUSED);
2356 POST_INIT(ConnectionResetError);
2357 ADD_ERRNO(ConnectionResetError, ECONNRESET);
2358 POST_INIT(FileExistsError);
2359 ADD_ERRNO(FileExistsError, EEXIST);
2360 POST_INIT(FileNotFoundError);
2361 ADD_ERRNO(FileNotFoundError, ENOENT);
2362 POST_INIT(IsADirectoryError);
2363 ADD_ERRNO(IsADirectoryError, EISDIR);
2364 POST_INIT(NotADirectoryError);
2365 ADD_ERRNO(NotADirectoryError, ENOTDIR);
2366 POST_INIT(InterruptedError);
2367 ADD_ERRNO(InterruptedError, EINTR);
2368 POST_INIT(PermissionError);
2369 ADD_ERRNO(PermissionError, EACCES);
2370 ADD_ERRNO(PermissionError, EPERM);
2371 POST_INIT(ProcessLookupError);
2372 ADD_ERRNO(ProcessLookupError, ESRCH);
2373 POST_INIT(TimeoutError);
2374 ADD_ERRNO(TimeoutError, ETIMEDOUT);
2375
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00002376 preallocate_memerrors();
Thomas Wouters477c8d52006-05-27 19:21:47 +00002377
Thomas Wouters89d996e2007-09-08 17:39:28 +00002378 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2379 if (!PyExc_RecursionErrorInst)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2381 "recursion errors");
Thomas Wouters89d996e2007-09-08 17:39:28 +00002382 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002383 PyBaseExceptionObject *err_inst =
2384 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2385 PyObject *args_tuple;
2386 PyObject *exc_message;
2387 exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
2388 if (!exc_message)
2389 Py_FatalError("cannot allocate argument for RuntimeError "
2390 "pre-allocation");
2391 args_tuple = PyTuple_Pack(1, exc_message);
2392 if (!args_tuple)
2393 Py_FatalError("cannot allocate tuple for RuntimeError "
2394 "pre-allocation");
2395 Py_DECREF(exc_message);
2396 if (BaseException_init(err_inst, args_tuple, NULL))
2397 Py_FatalError("init of pre-allocated RuntimeError failed");
2398 Py_DECREF(args_tuple);
Thomas Wouters89d996e2007-09-08 17:39:28 +00002399 }
2400
Thomas Wouters477c8d52006-05-27 19:21:47 +00002401 Py_DECREF(bltinmod);
2402}
2403
2404void
2405_PyExc_Fini(void)
2406{
Benjamin Peterson78565b22009-06-28 19:19:51 +00002407 Py_CLEAR(PyExc_RecursionErrorInst);
Antoine Pitrou07e20ef2010-10-28 22:56:58 +00002408 free_preallocated_memerrors();
Antoine Pitrou6b4883d2011-10-12 02:54:14 +02002409 Py_CLEAR(errnomap);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002410}