blob: 7cf1580504f23e854e4d12a2156f4a324b391388 [file] [log] [blame]
Barry Warsaw675ac282000-05-26 19:05:16 +00001/* This module provides the suite of standard class-based exceptions for
2 * Python's builtin module. This is a complete C implementation of what,
3 * in Python 1.5.2, was contained in the exceptions.py module. The problem
4 * there was that if exceptions.py could not be imported for some reason,
5 * the entire interpreter would abort.
6 *
7 * By moving the exceptions into C and statically linking, we can guarantee
8 * that the standard exceptions will always be available.
9 *
Barry Warsaw675ac282000-05-26 19:05:16 +000010 *
11 * written by Fredrik Lundh
12 * modifications, additions, cleanups, and proofreading by Barry Warsaw
13 *
14 * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
15 */
16
Martin v. Löwis5cb69362006-04-14 09:08:42 +000017#define PY_SSIZE_T_CLEAN
Barry Warsaw675ac282000-05-26 19:05:16 +000018#include "Python.h"
Fred Drake185a29b2000-08-15 16:20:36 +000019#include "osdefs.h"
Barry Warsaw675ac282000-05-26 19:05:16 +000020
Tim Petersbf26e072000-07-12 04:02:10 +000021/* Caution: MS Visual C++ 6 errors if a single string literal exceeds
22 * 2Kb. So the module docstring has been broken roughly in half, using
23 * compile-time literal concatenation.
24 */
Skip Montanaro995895f2002-03-28 20:57:51 +000025
26/* NOTE: If the exception class hierarchy changes, don't forget to update
27 * Doc/lib/libexcs.tex!
28 */
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030PyDoc_STRVAR(module__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +000031"Python's standard exception class hierarchy.\n\
32\n\
Brett Cannonbf364092006-03-01 04:25:17 +000033Exceptions found here are defined both in the exceptions module and the \n\
34built-in namespace. It is recommended that user-defined exceptions inherit \n\
Brett Cannon54ac2942006-03-01 22:10:49 +000035from Exception. See the documentation for the exception inheritance hierarchy.\n\
Brett Cannonbf364092006-03-01 04:25:17 +000036"
37
Tim Petersbf26e072000-07-12 04:02:10 +000038 /* keep string pieces "small" */
Brett Cannon54ac2942006-03-01 22:10:49 +000039/* XXX(bcannon): exception hierarchy in Lib/test/exception_hierarchy.txt */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000040);
Barry Warsaw675ac282000-05-26 19:05:16 +000041
42
43/* Helper function for populating a dictionary with method wrappers. */
44static int
Brett Cannonbf364092006-03-01 04:25:17 +000045populate_methods(PyObject *klass, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +000046{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000047 PyObject *module;
48 int status = -1;
49
Barry Warsaw675ac282000-05-26 19:05:16 +000050 if (!methods)
51 return 0;
52
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000053 module = PyString_FromString("exceptions");
54 if (!module)
55 return 0;
Barry Warsaw675ac282000-05-26 19:05:16 +000056 while (methods->ml_name) {
57 /* get a wrapper for the built-in function */
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000058 PyObject *func = PyCFunction_NewEx(methods, NULL, module);
Thomas Wouters0452d1f2000-07-22 18:45:06 +000059 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +000060
61 if (!func)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000062 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000063
64 /* turn the function into an unbound method */
65 if (!(meth = PyMethod_New(func, NULL, klass))) {
66 Py_DECREF(func);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000067 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000068 }
Barry Warsaw9667ed22001-01-23 16:08:34 +000069
Barry Warsaw675ac282000-05-26 19:05:16 +000070 /* add method to dictionary */
Brett Cannonbf364092006-03-01 04:25:17 +000071 status = PyObject_SetAttrString(klass, methods->ml_name, meth);
Barry Warsaw675ac282000-05-26 19:05:16 +000072 Py_DECREF(meth);
73 Py_DECREF(func);
74
75 /* stop now if an error occurred, otherwise do the next method */
76 if (status)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000077 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000078
79 methods++;
80 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000081 status = 0;
82 status:
83 Py_DECREF(module);
84 return status;
Barry Warsaw675ac282000-05-26 19:05:16 +000085}
86
Barry Warsaw9667ed22001-01-23 16:08:34 +000087
Barry Warsaw675ac282000-05-26 19:05:16 +000088
89/* This function is used to create all subsequent exception classes. */
90static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +000091make_class(PyObject **klass, PyObject *base,
92 char *name, PyMethodDef *methods,
93 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +000094{
Thomas Wouters0452d1f2000-07-22 18:45:06 +000095 PyObject *dict = PyDict_New();
96 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +000097 int status = -1;
98
99 if (!dict)
100 return -1;
101
102 /* If an error occurs from here on, goto finally instead of explicitly
103 * returning NULL.
104 */
105
106 if (docstr) {
107 if (!(str = PyString_FromString(docstr)))
108 goto finally;
109 if (PyDict_SetItemString(dict, "__doc__", str))
110 goto finally;
111 }
112
113 if (!(*klass = PyErr_NewException(name, base, dict)))
114 goto finally;
115
Brett Cannonbf364092006-03-01 04:25:17 +0000116 if (populate_methods(*klass, methods)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000117 Py_DECREF(*klass);
118 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000119 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000120 }
121
122 status = 0;
123
124 finally:
125 Py_XDECREF(dict);
126 Py_XDECREF(str);
127 return status;
128}
129
130
131/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000132static PyObject *
133get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000134{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000135 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000136 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000137 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000138 if (PyExc_TypeError) {
139 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000140 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000141 }
142 return NULL;
143 }
144 return self;
145}
146
147
148
149/* Notes on bootstrapping the exception classes.
150 *
151 * First thing we create is the base class for all exceptions, called
Brett Cannonbf364092006-03-01 04:25:17 +0000152 * appropriately BaseException. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000153 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000154 * for TypeError, which can conditionally exist.
155 *
Brett Cannonbf364092006-03-01 04:25:17 +0000156 * Next, Exception is created since it is the common subclass for the rest of
157 * the needed exceptions for this bootstrapping to work. StandardError is
158 * created (which is quite simple) followed by
Barry Warsaw675ac282000-05-26 19:05:16 +0000159 * TypeError, because the instantiation of other exceptions can potentially
160 * throw a TypeError. Once these exceptions are created, all the others
161 * can be created in any order. See the static exctable below for the
162 * explicit bootstrap order.
163 *
Brett Cannonbf364092006-03-01 04:25:17 +0000164 * All classes after BaseException can be created using PyErr_NewException().
Barry Warsaw675ac282000-05-26 19:05:16 +0000165 */
166
Brett Cannonbf364092006-03-01 04:25:17 +0000167PyDoc_STRVAR(BaseException__doc__, "Common base class for all exceptions");
Barry Warsaw675ac282000-05-26 19:05:16 +0000168
Brett Cannonbf364092006-03-01 04:25:17 +0000169/*
170 Set args and message attributes.
171
172 Assumes self and args have already been set properly with set_self, etc.
173*/
174static int
175set_args_and_message(PyObject *self, PyObject *args)
176{
177 PyObject *message_val;
178 Py_ssize_t args_len = PySequence_Length(args);
179
180 if (args_len < 0)
181 return 0;
182
183 /* set args */
184 if (PyObject_SetAttrString(self, "args", args) < 0)
185 return 0;
186
187 /* set message */
188 if (args_len == 1)
189 message_val = PySequence_GetItem(args, 0);
190 else
191 message_val = PyString_FromString("");
192 if (!message_val)
193 return 0;
194
195 if (PyObject_SetAttrString(self, "message", message_val) < 0) {
196 Py_DECREF(message_val);
197 return 0;
198 }
199
200 Py_DECREF(message_val);
201 return 1;
202}
Barry Warsaw675ac282000-05-26 19:05:16 +0000203
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000204static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000205BaseException__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000206{
Barry Warsaw675ac282000-05-26 19:05:16 +0000207 if (!(self = get_self(args)))
208 return NULL;
209
Brett Cannonbf364092006-03-01 04:25:17 +0000210 /* set args and message attribute */
211 args = PySequence_GetSlice(args, 1, PySequence_Length(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000212 if (!args)
213 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000214
Brett Cannonbf364092006-03-01 04:25:17 +0000215 if (!set_args_and_message(self, args)) {
216 Py_DECREF(args);
217 return NULL;
218 }
219
220 Py_DECREF(args);
221 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000222}
223
224
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000225static PyObject *
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000226BaseException__str__(PyObject *_self, PyObject *self)
Barry Warsaw675ac282000-05-26 19:05:16 +0000227{
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000228 PyObject *out, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +0000229
230 args = PyObject_GetAttrString(self, "args");
231 if (!args)
232 return NULL;
233
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000234 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000235 case 0:
236 out = PyString_FromString("");
237 break;
238 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000239 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000240 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000241 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000242 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000243 Py_DECREF(tmp);
244 }
245 else
246 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000247 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000248 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000249 case -1:
250 PyErr_Clear();
251 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000252 default:
253 out = PyObject_Str(args);
254 break;
255 }
256
257 Py_DECREF(args);
258 return out;
259}
260
Brett Cannonbf364092006-03-01 04:25:17 +0000261#ifdef Py_USING_UNICODE
262static PyObject *
263BaseException__unicode__(PyObject *self, PyObject *args)
264{
265 Py_ssize_t args_len;
266
267 if (!PyArg_ParseTuple(args, "O:__unicode__", &self))
268 return NULL;
269
270 args = PyObject_GetAttrString(self, "args");
271 if (!args)
272 return NULL;
273
274 args_len = PySequence_Size(args);
275 if (args_len < 0) {
276 Py_DECREF(args);
277 return NULL;
278 }
279
280 if (args_len == 0) {
281 Py_DECREF(args);
282 return PyUnicode_FromUnicode(NULL, 0);
283 }
284 else if (args_len == 1) {
285 PyObject *temp = PySequence_GetItem(args, 0);
Brett Cannon46872b12006-03-02 04:31:55 +0000286 PyObject *unicode_obj;
287
Brett Cannonbf364092006-03-01 04:25:17 +0000288 if (!temp) {
289 Py_DECREF(args);
290 return NULL;
291 }
292 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000293 unicode_obj = PyObject_Unicode(temp);
294 Py_DECREF(temp);
295 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000296 }
297 else {
Brett Cannon46872b12006-03-02 04:31:55 +0000298 PyObject *unicode_obj = PyObject_Unicode(args);
299
Brett Cannonbf364092006-03-01 04:25:17 +0000300 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000301 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000302 }
303}
304#endif /* Py_USING_UNICODE */
Barry Warsaw675ac282000-05-26 19:05:16 +0000305
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000306static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000307BaseException__repr__(PyObject *self, PyObject *args)
308{
309 PyObject *args_attr;
310 Py_ssize_t args_len;
311 PyObject *repr_suffix;
312 PyObject *repr;
313
314 if (!PyArg_ParseTuple(args, "O:__repr__", &self))
315 return NULL;
316
317 args_attr = PyObject_GetAttrString(self, "args");
318 if (!args_attr)
319 return NULL;
320
321 args_len = PySequence_Length(args_attr);
322 if (args_len < 0) {
323 Py_DECREF(args_attr);
324 return NULL;
325 }
326
327 if (args_len == 0) {
328 Py_DECREF(args_attr);
329 repr_suffix = PyString_FromString("()");
330 if (!repr_suffix)
331 return NULL;
332 }
333 else {
Neal Norwitz9742f272006-03-03 19:13:57 +0000334 PyObject *args_repr = PyObject_Repr(args_attr);
Brett Cannonbf364092006-03-01 04:25:17 +0000335 Py_DECREF(args_attr);
336 if (!args_repr)
337 return NULL;
338
339 repr_suffix = args_repr;
Brett Cannonbf364092006-03-01 04:25:17 +0000340 }
341
342 repr = PyString_FromString(self->ob_type->tp_name);
343 if (!repr) {
344 Py_DECREF(repr_suffix);
345 return NULL;
346 }
347
348 PyString_ConcatAndDel(&repr, repr_suffix);
349 return repr;
350}
351
352static PyObject *
353BaseException__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000354{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000355 PyObject *out;
356 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000357
Fred Drake1aba5772000-08-15 15:46:16 +0000358 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000359 return NULL;
360
361 args = PyObject_GetAttrString(self, "args");
362 if (!args)
363 return NULL;
364
365 out = PyObject_GetItem(args, index);
366 Py_DECREF(args);
367 return out;
368}
369
370
371static PyMethodDef
Brett Cannonbf364092006-03-01 04:25:17 +0000372BaseException_methods[] = {
373 /* methods for the BaseException class */
374 {"__getitem__", BaseException__getitem__, METH_VARARGS},
375 {"__repr__", BaseException__repr__, METH_VARARGS},
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000376 {"__str__", BaseException__str__, METH_O},
Brett Cannonbf364092006-03-01 04:25:17 +0000377#ifdef Py_USING_UNICODE
378 {"__unicode__", BaseException__unicode__, METH_VARARGS},
379#endif /* Py_USING_UNICODE */
380 {"__init__", BaseException__init__, METH_VARARGS},
381 {NULL, NULL }
Barry Warsaw675ac282000-05-26 19:05:16 +0000382};
383
384
385static int
Brett Cannonbf364092006-03-01 04:25:17 +0000386make_BaseException(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000387{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000388 PyObject *dict = PyDict_New();
389 PyObject *str = NULL;
390 PyObject *name = NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000391 PyObject *emptytuple = NULL;
392 PyObject *argstuple = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000393 int status = -1;
394
395 if (!dict)
396 return -1;
397
398 /* If an error occurs from here on, goto finally instead of explicitly
399 * returning NULL.
400 */
401
402 if (!(str = PyString_FromString(modulename)))
403 goto finally;
404 if (PyDict_SetItemString(dict, "__module__", str))
405 goto finally;
406 Py_DECREF(str);
Brett Cannonbf364092006-03-01 04:25:17 +0000407
408 if (!(str = PyString_FromString(BaseException__doc__)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000409 goto finally;
410 if (PyDict_SetItemString(dict, "__doc__", str))
411 goto finally;
412
Brett Cannonbf364092006-03-01 04:25:17 +0000413 if (!(name = PyString_FromString("BaseException")))
Barry Warsaw675ac282000-05-26 19:05:16 +0000414 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000415
Brett Cannonbf364092006-03-01 04:25:17 +0000416 if (!(emptytuple = PyTuple_New(0)))
417 goto finally;
418
419 if (!(argstuple = PyTuple_Pack(3, name, emptytuple, dict)))
420 goto finally;
421
422 if (!(PyExc_BaseException = PyType_Type.tp_new(&PyType_Type, argstuple,
423 NULL)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000424 goto finally;
425
426 /* Now populate the dictionary with the method suite */
Brett Cannonbf364092006-03-01 04:25:17 +0000427 if (populate_methods(PyExc_BaseException, BaseException_methods))
428 /* Don't need to reclaim PyExc_BaseException here because that'll
Barry Warsaw675ac282000-05-26 19:05:16 +0000429 * happen during interpreter shutdown.
430 */
431 goto finally;
432
433 status = 0;
434
435 finally:
436 Py_XDECREF(dict);
437 Py_XDECREF(str);
438 Py_XDECREF(name);
Brett Cannonbf364092006-03-01 04:25:17 +0000439 Py_XDECREF(emptytuple);
440 Py_XDECREF(argstuple);
Barry Warsaw675ac282000-05-26 19:05:16 +0000441 return status;
442}
443
444
445
Brett Cannonbf364092006-03-01 04:25:17 +0000446PyDoc_STRVAR(Exception__doc__, "Common base class for all non-exit exceptions.");
447
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000448PyDoc_STRVAR(StandardError__doc__,
Brett Cannonbf364092006-03-01 04:25:17 +0000449"Base class for all standard Python exceptions that do not represent"
450"interpreter exiting.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000451
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000452PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000454PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000455PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000456
Barry Warsaw675ac282000-05-26 19:05:16 +0000457
458
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000459PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000460
461
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000462static PyObject *
463SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000464{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000465 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000466 int status;
467
468 if (!(self = get_self(args)))
469 return NULL;
470
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000471 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000472 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000473
Brett Cannonbf364092006-03-01 04:25:17 +0000474 if (!set_args_and_message(self, args)) {
475 Py_DECREF(args);
476 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000477 }
478
479 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000480 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000481 case 0:
482 Py_INCREF(Py_None);
483 code = Py_None;
484 break;
485 case 1:
486 code = PySequence_GetItem(args, 0);
487 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000488 case -1:
489 PyErr_Clear();
490 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000491 default:
492 Py_INCREF(args);
493 code = args;
494 break;
495 }
496
497 status = PyObject_SetAttrString(self, "code", code);
498 Py_DECREF(code);
499 Py_DECREF(args);
500 if (status < 0)
501 return NULL;
502
Brett Cannonbf364092006-03-01 04:25:17 +0000503 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000504}
505
506
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000507static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000508 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000509 {NULL, NULL}
510};
511
512
513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000514PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000516PyDoc_STRVAR(ImportError__doc__,
517"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000518
519
520
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000521PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000522
523
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000524static PyObject *
525EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000526{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000527 PyObject *item0 = NULL;
528 PyObject *item1 = NULL;
529 PyObject *item2 = NULL;
530 PyObject *subslice = NULL;
531 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000532
533 if (!(self = get_self(args)))
534 return NULL;
535
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000536 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000537 return NULL;
538
Brett Cannonbf364092006-03-01 04:25:17 +0000539 if (!set_args_and_message(self, args)) {
540 Py_DECREF(args);
541 return NULL;
542 }
543
544 if (PyObject_SetAttrString(self, "errno", Py_None) ||
Barry Warsaw675ac282000-05-26 19:05:16 +0000545 PyObject_SetAttrString(self, "strerror", Py_None) ||
546 PyObject_SetAttrString(self, "filename", Py_None))
547 {
548 goto finally;
549 }
550
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000551 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000552 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000553 /* Where a function has a single filename, such as open() or some
554 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
555 * called, giving a third argument which is the filename. But, so
556 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000557 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000558 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000559 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000560 * we hack args so that it only contains two items. This also
561 * means we need our own __str__() which prints out the filename
562 * when it was supplied.
563 */
564 item0 = PySequence_GetItem(args, 0);
565 item1 = PySequence_GetItem(args, 1);
566 item2 = PySequence_GetItem(args, 2);
567 if (!item0 || !item1 || !item2)
568 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000569
Barry Warsaw675ac282000-05-26 19:05:16 +0000570 if (PyObject_SetAttrString(self, "errno", item0) ||
571 PyObject_SetAttrString(self, "strerror", item1) ||
572 PyObject_SetAttrString(self, "filename", item2))
573 {
574 goto finally;
575 }
576
577 subslice = PySequence_GetSlice(args, 0, 2);
578 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
579 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000580 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000581
582 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000583 /* Used when PyErr_SetFromErrno() is called and no filename
584 * argument is given.
585 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000586 item0 = PySequence_GetItem(args, 0);
587 item1 = PySequence_GetItem(args, 1);
588 if (!item0 || !item1)
589 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000590
Barry Warsaw675ac282000-05-26 19:05:16 +0000591 if (PyObject_SetAttrString(self, "errno", item0) ||
592 PyObject_SetAttrString(self, "strerror", item1))
593 {
594 goto finally;
595 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000596 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000597
598 case -1:
599 PyErr_Clear();
600 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000601 }
602
603 Py_INCREF(Py_None);
604 rtnval = Py_None;
605
606 finally:
607 Py_DECREF(args);
608 Py_XDECREF(item0);
609 Py_XDECREF(item1);
610 Py_XDECREF(item2);
611 Py_XDECREF(subslice);
612 return rtnval;
613}
614
615
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000616static PyObject *
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000617EnvironmentError__str__(PyObject *originalself, PyObject *self)
Barry Warsaw675ac282000-05-26 19:05:16 +0000618{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000619 PyObject *filename;
620 PyObject *serrno;
621 PyObject *strerror;
622 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000623
Barry Warsaw675ac282000-05-26 19:05:16 +0000624 filename = PyObject_GetAttrString(self, "filename");
625 serrno = PyObject_GetAttrString(self, "errno");
626 strerror = PyObject_GetAttrString(self, "strerror");
627 if (!filename || !serrno || !strerror)
628 goto finally;
629
630 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000631 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
632 PyObject *repr = PyObject_Repr(filename);
633 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000634
635 if (!fmt || !repr || !tuple) {
636 Py_XDECREF(fmt);
637 Py_XDECREF(repr);
638 Py_XDECREF(tuple);
639 goto finally;
640 }
641
642 PyTuple_SET_ITEM(tuple, 0, serrno);
643 PyTuple_SET_ITEM(tuple, 1, strerror);
644 PyTuple_SET_ITEM(tuple, 2, repr);
645
646 rtnval = PyString_Format(fmt, tuple);
647
648 Py_DECREF(fmt);
649 Py_DECREF(tuple);
650 /* already freed because tuple owned only reference */
651 serrno = NULL;
652 strerror = NULL;
653 }
654 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000655 PyObject *fmt = PyString_FromString("[Errno %s] %s");
656 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000657
658 if (!fmt || !tuple) {
659 Py_XDECREF(fmt);
660 Py_XDECREF(tuple);
661 goto finally;
662 }
663
664 PyTuple_SET_ITEM(tuple, 0, serrno);
665 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000666
Barry Warsaw675ac282000-05-26 19:05:16 +0000667 rtnval = PyString_Format(fmt, tuple);
668
669 Py_DECREF(fmt);
670 Py_DECREF(tuple);
671 /* already freed because tuple owned only reference */
672 serrno = NULL;
673 strerror = NULL;
674 }
675 else
676 /* The original Python code said:
677 *
678 * return StandardError.__str__(self)
679 *
680 * but there is no StandardError__str__() function; we happen to
Brett Cannonbf364092006-03-01 04:25:17 +0000681 * know that's just a pass through to BaseException__str__().
Barry Warsaw675ac282000-05-26 19:05:16 +0000682 */
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000683 rtnval = BaseException__str__(originalself, self);
Barry Warsaw675ac282000-05-26 19:05:16 +0000684
685 finally:
686 Py_XDECREF(filename);
687 Py_XDECREF(serrno);
688 Py_XDECREF(strerror);
689 return rtnval;
690}
691
692
693static
694PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000695 {"__init__", EnvironmentError__init__, METH_VARARGS},
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000696 {"__str__", EnvironmentError__str__, METH_O},
Barry Warsaw675ac282000-05-26 19:05:16 +0000697 {NULL, NULL}
698};
699
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000700PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000701
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000702PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000703
704#ifdef MS_WINDOWS
Martin v. Löwis879768d2006-05-11 13:28:43 +0000705#include "errmap.h"
706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000707PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Martin v. Löwis879768d2006-05-11 13:28:43 +0000708
709static PyObject *
710WindowsError__init__(PyObject *self, PyObject *args)
711{
712 PyObject *o_errcode, *result;
713 long errcode, posix_errno;
714 result = EnvironmentError__init__(self, args);
715 if (!result)
716 return NULL;
717 self = get_self(args);
718 if (!self)
719 goto failed;
720 /* Set errno to the POSIX errno, and winerror to the Win32
721 error code. */
722 o_errcode = PyObject_GetAttrString(self, "errno");
723 if (!o_errcode)
724 goto failed;
725 errcode = PyInt_AsLong(o_errcode);
726 if (!errcode == -1 && PyErr_Occurred())
727 goto failed;
728 posix_errno = winerror_to_errno(errcode);
729 if (PyObject_SetAttrString(self, "winerror", o_errcode) < 0)
730 goto failed;
731 Py_DECREF(o_errcode);
732 o_errcode = PyInt_FromLong(posix_errno);
733 if (!o_errcode)
734 goto failed;
735 if (PyObject_SetAttrString(self, "errno", o_errcode) < 0)
736 goto failed;
737 Py_DECREF(o_errcode);
738 return result;
739failed:
740 /* Could not set errno. */
741 Py_XDECREF(o_errcode);
Martin v. Löwis879768d2006-05-11 13:28:43 +0000742 Py_DECREF(result);
743 return NULL;
744}
745
746static PyObject *
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000747WindowsError__str__(PyObject *originalself, PyObject *self)
Martin v. Löwis879768d2006-05-11 13:28:43 +0000748{
Martin v. Löwis879768d2006-05-11 13:28:43 +0000749 PyObject *filename;
750 PyObject *serrno;
751 PyObject *strerror;
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000752 PyObject *repr = NULL;
753 PyObject *fmt = NULL;
754 PyObject *tuple = NULL;
Martin v. Löwis879768d2006-05-11 13:28:43 +0000755 PyObject *rtnval = NULL;
756
Martin v. Löwis879768d2006-05-11 13:28:43 +0000757 filename = PyObject_GetAttrString(self, "filename");
758 serrno = PyObject_GetAttrString(self, "winerror");
759 strerror = PyObject_GetAttrString(self, "strerror");
760 if (!filename || !serrno || !strerror)
761 goto finally;
762
763 if (filename != Py_None) {
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000764 fmt = PyString_FromString("[Error %s] %s: %s");
765 repr = PyObject_Repr(filename);
766 if (!fmt || !repr)
Martin v. Löwis879768d2006-05-11 13:28:43 +0000767 goto finally;
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000768
Martin v. Löwis879768d2006-05-11 13:28:43 +0000769
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000770 tuple = PyTuple_Pack(3, serrno, strerror, repr);
771 if (!tuple)
772 goto finally;
Martin v. Löwis879768d2006-05-11 13:28:43 +0000773
774 rtnval = PyString_Format(fmt, tuple);
Martin v. Löwis879768d2006-05-11 13:28:43 +0000775 }
776 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000777 fmt = PyString_FromString("[Error %s] %s");
778 if (!fmt)
Martin v. Löwis879768d2006-05-11 13:28:43 +0000779 goto finally;
Martin v. Löwis879768d2006-05-11 13:28:43 +0000780
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000781 tuple = PyTuple_Pack(2, serrno, strerror);
782 if (!tuple)
783 goto finally;
Martin v. Löwis879768d2006-05-11 13:28:43 +0000784
785 rtnval = PyString_Format(fmt, tuple);
Martin v. Löwis879768d2006-05-11 13:28:43 +0000786 }
787 else
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000788 rtnval = EnvironmentError__str__(originalself, self);
Martin v. Löwis879768d2006-05-11 13:28:43 +0000789
790 finally:
791 Py_XDECREF(filename);
792 Py_XDECREF(serrno);
793 Py_XDECREF(strerror);
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000794 Py_XDECREF(repr);
795 Py_XDECREF(fmt);
796 Py_XDECREF(tuple);
Martin v. Löwis879768d2006-05-11 13:28:43 +0000797 return rtnval;
798}
799
800static
801PyMethodDef WindowsError_methods[] = {
802 {"__init__", WindowsError__init__, METH_VARARGS},
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000803 {"__str__", WindowsError__str__, METH_O},
Martin v. Löwis879768d2006-05-11 13:28:43 +0000804 {NULL, NULL}
805};
Barry Warsaw675ac282000-05-26 19:05:16 +0000806#endif /* MS_WINDOWS */
807
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000808#ifdef __VMS
809static char
810VMSError__doc__[] = "OpenVMS OS system call failed.";
811#endif
812
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000813PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000815PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000816
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000817PyDoc_STRVAR(NotImplementedError__doc__,
818"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000819
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000820PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000821
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000822PyDoc_STRVAR(UnboundLocalError__doc__,
823"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000824
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000825PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000826
827
828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000829PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000830
831
832static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000833SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000834{
Barry Warsaw87bec352000-08-18 05:05:37 +0000835 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000836 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000837
838 /* Additional class-creation time initializations */
839 if (!emptystring ||
840 PyObject_SetAttrString(klass, "msg", emptystring) ||
841 PyObject_SetAttrString(klass, "filename", Py_None) ||
842 PyObject_SetAttrString(klass, "lineno", Py_None) ||
843 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000844 PyObject_SetAttrString(klass, "text", Py_None) ||
845 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000846 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000847 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000848 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000849 Py_XDECREF(emptystring);
850 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000851}
852
853
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000854static PyObject *
855SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000856{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000857 PyObject *rtnval = NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +0000858 Py_ssize_t lenargs;
Barry Warsaw675ac282000-05-26 19:05:16 +0000859
860 if (!(self = get_self(args)))
861 return NULL;
862
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000863 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000864 return NULL;
865
Brett Cannonbf364092006-03-01 04:25:17 +0000866 if (!set_args_and_message(self, args)) {
867 Py_DECREF(args);
868 return NULL;
869 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000870
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000871 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000872 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000873 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000874 int status;
875
876 if (!item0)
877 goto finally;
878 status = PyObject_SetAttrString(self, "msg", item0);
879 Py_DECREF(item0);
880 if (status)
881 goto finally;
882 }
883 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000884 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000885 PyObject *filename = NULL, *lineno = NULL;
886 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000887 int status = 1;
888
889 if (!info)
890 goto finally;
891
892 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000893 if (filename != NULL) {
894 lineno = PySequence_GetItem(info, 1);
895 if (lineno != NULL) {
896 offset = PySequence_GetItem(info, 2);
897 if (offset != NULL) {
898 text = PySequence_GetItem(info, 3);
899 if (text != NULL) {
900 status =
901 PyObject_SetAttrString(self, "filename", filename)
902 || PyObject_SetAttrString(self, "lineno", lineno)
903 || PyObject_SetAttrString(self, "offset", offset)
904 || PyObject_SetAttrString(self, "text", text);
905 Py_DECREF(text);
906 }
907 Py_DECREF(offset);
908 }
909 Py_DECREF(lineno);
910 }
911 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000912 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000913 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000914
915 if (status)
916 goto finally;
917 }
918 Py_INCREF(Py_None);
919 rtnval = Py_None;
920
921 finally:
922 Py_DECREF(args);
923 return rtnval;
924}
925
926
Fred Drake185a29b2000-08-15 16:20:36 +0000927/* This is called "my_basename" instead of just "basename" to avoid name
928 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
929 defined, and Python does define that. */
930static char *
931my_basename(char *name)
932{
933 char *cp = name;
934 char *result = name;
935
936 if (name == NULL)
937 return "???";
938 while (*cp != '\0') {
939 if (*cp == SEP)
940 result = cp + 1;
941 ++cp;
942 }
943 return result;
944}
945
946
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000947static PyObject *
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +0000948SyntaxError__str__(PyObject *_self, PyObject *self)
Barry Warsaw675ac282000-05-26 19:05:16 +0000949{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000950 PyObject *msg;
951 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000952 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000953
Barry Warsaw675ac282000-05-26 19:05:16 +0000954 if (!(msg = PyObject_GetAttrString(self, "msg")))
955 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000956
Barry Warsaw675ac282000-05-26 19:05:16 +0000957 str = PyObject_Str(msg);
958 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000959 result = str;
960
961 /* XXX -- do all the additional formatting with filename and
962 lineno here */
963
Guido van Rossum602d4512002-09-03 20:24:09 +0000964 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000965 int have_filename = 0;
966 int have_lineno = 0;
967 char *buffer = NULL;
968
Barry Warsaw77c9f502000-08-16 19:43:17 +0000969 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000970 have_filename = PyString_Check(filename);
971 else
972 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000973
974 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000975 have_lineno = PyInt_Check(lineno);
976 else
977 PyErr_Clear();
978
979 if (have_filename || have_lineno) {
Martin v. Löwis725507b2006-03-07 12:08:51 +0000980 Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64;
Barry Warsaw77c9f502000-08-16 19:43:17 +0000981 if (have_filename)
982 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000983
Anthony Baxtera863d332006-04-11 07:43:46 +0000984 buffer = (char *)PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000985 if (buffer != NULL) {
986 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000987 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
988 PyString_AS_STRING(str),
989 my_basename(PyString_AS_STRING(filename)),
990 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000991 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000992 PyOS_snprintf(buffer, bufsize, "%s (%s)",
993 PyString_AS_STRING(str),
994 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000995 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000996 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
997 PyString_AS_STRING(str),
998 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000999
Fred Drake1aba5772000-08-15 15:46:16 +00001000 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +00001001 PyMem_FREE(buffer);
1002
Fred Drake1aba5772000-08-15 15:46:16 +00001003 if (result == NULL)
1004 result = str;
1005 else
1006 Py_DECREF(str);
1007 }
1008 }
1009 Py_XDECREF(filename);
1010 Py_XDECREF(lineno);
1011 }
1012 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +00001013}
1014
1015
Guido van Rossumf68d8e52001-04-14 17:55:09 +00001016static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +00001017 {"__init__", SyntaxError__init__, METH_VARARGS},
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +00001018 {"__str__", SyntaxError__str__, METH_O},
Barry Warsaw675ac282000-05-26 19:05:16 +00001019 {NULL, NULL}
1020};
1021
1022
Guido van Rossum602d4512002-09-03 20:24:09 +00001023static PyObject *
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +00001024KeyError__str__(PyObject *_self, PyObject *self)
Guido van Rossum602d4512002-09-03 20:24:09 +00001025{
1026 PyObject *argsattr;
1027 PyObject *result;
1028
Brett Cannonbf364092006-03-01 04:25:17 +00001029 argsattr = PyObject_GetAttrString(self, "args");
1030 if (!argsattr)
1031 return NULL;
Guido van Rossum602d4512002-09-03 20:24:09 +00001032
1033 /* If args is a tuple of exactly one item, apply repr to args[0].
1034 This is done so that e.g. the exception raised by {}[''] prints
1035 KeyError: ''
1036 rather than the confusing
1037 KeyError
1038 alone. The downside is that if KeyError is raised with an explanatory
1039 string, that string will be displayed in quotes. Too bad.
Brett Cannonbf364092006-03-01 04:25:17 +00001040 If args is anything else, use the default BaseException__str__().
Guido van Rossum602d4512002-09-03 20:24:09 +00001041 */
1042 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
1043 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
1044 result = PyObject_Repr(key);
1045 }
1046 else
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +00001047 result = BaseException__str__(_self, self);
Guido van Rossum602d4512002-09-03 20:24:09 +00001048
1049 Py_DECREF(argsattr);
1050 return result;
1051}
1052
1053static PyMethodDef KeyError_methods[] = {
Martin v. Löwis2a0ad4d2006-05-15 09:22:27 +00001054 {"__str__", KeyError__str__, METH_O},
Guido van Rossum602d4512002-09-03 20:24:09 +00001055 {NULL, NULL}
1056};
1057
1058
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001059#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001060static
Martin v. Löwis18e16552006-02-15 17:27:45 +00001061int get_int(PyObject *exc, const char *name, Py_ssize_t *value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001062{
1063 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1064
1065 if (!attr)
1066 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001067 if (PyInt_Check(attr)) {
1068 *value = PyInt_AS_LONG(attr);
1069 } else if (PyLong_Check(attr)) {
1070 *value = (size_t)PyLong_AsLongLong(attr);
1071 if (*value == -1) {
1072 Py_DECREF(attr);
1073 return -1;
1074 }
1075 } else {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001076 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001077 Py_DECREF(attr);
1078 return -1;
1079 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001080 Py_DECREF(attr);
1081 return 0;
1082}
1083
1084
1085static
Martin v. Löwis18e16552006-02-15 17:27:45 +00001086int set_ssize_t(PyObject *exc, const char *name, Py_ssize_t value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001087{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001088 PyObject *obj = PyInt_FromSsize_t(value);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001089 int result;
1090
1091 if (!obj)
1092 return -1;
1093 result = PyObject_SetAttrString(exc, (char *)name, obj);
1094 Py_DECREF(obj);
1095 return result;
1096}
1097
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001098static
1099PyObject *get_string(PyObject *exc, const char *name)
1100{
1101 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1102
1103 if (!attr)
1104 return NULL;
1105 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001106 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001107 Py_DECREF(attr);
1108 return NULL;
1109 }
1110 return attr;
1111}
1112
1113
1114static
1115int set_string(PyObject *exc, const char *name, const char *value)
1116{
1117 PyObject *obj = PyString_FromString(value);
1118 int result;
1119
1120 if (!obj)
1121 return -1;
1122 result = PyObject_SetAttrString(exc, (char *)name, obj);
1123 Py_DECREF(obj);
1124 return result;
1125}
1126
1127
1128static
1129PyObject *get_unicode(PyObject *exc, const char *name)
1130{
1131 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1132
1133 if (!attr)
1134 return NULL;
1135 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001136 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001137 Py_DECREF(attr);
1138 return NULL;
1139 }
1140 return attr;
1141}
1142
1143PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1144{
1145 return get_string(exc, "encoding");
1146}
1147
1148PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1149{
1150 return get_string(exc, "encoding");
1151}
1152
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001153PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
1154{
1155 return get_unicode(exc, "object");
1156}
1157
1158PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1159{
1160 return get_string(exc, "object");
1161}
1162
1163PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1164{
1165 return get_unicode(exc, "object");
1166}
1167
Martin v. Löwis18e16552006-02-15 17:27:45 +00001168int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001169{
1170 if (!get_int(exc, "start", start)) {
1171 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001172 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001173 if (!object)
1174 return -1;
1175 size = PyUnicode_GET_SIZE(object);
1176 if (*start<0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001177 *start = 0; /*XXX check for values <0*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001178 if (*start>=size)
1179 *start = size-1;
1180 Py_DECREF(object);
1181 return 0;
1182 }
1183 return -1;
1184}
1185
1186
Martin v. Löwis18e16552006-02-15 17:27:45 +00001187int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001188{
1189 if (!get_int(exc, "start", start)) {
1190 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001191 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001192 if (!object)
1193 return -1;
1194 size = PyString_GET_SIZE(object);
1195 if (*start<0)
1196 *start = 0;
1197 if (*start>=size)
1198 *start = size-1;
1199 Py_DECREF(object);
1200 return 0;
1201 }
1202 return -1;
1203}
1204
1205
Martin v. Löwis18e16552006-02-15 17:27:45 +00001206int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001207{
1208 return PyUnicodeEncodeError_GetStart(exc, start);
1209}
1210
1211
Martin v. Löwis18e16552006-02-15 17:27:45 +00001212int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001213{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001214 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001215}
1216
1217
Martin v. Löwis18e16552006-02-15 17:27:45 +00001218int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001219{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001220 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001221}
1222
1223
Martin v. Löwis18e16552006-02-15 17:27:45 +00001224int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001225{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001226 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001227}
1228
1229
Martin v. Löwis18e16552006-02-15 17:27:45 +00001230int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001231{
1232 if (!get_int(exc, "end", end)) {
1233 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001234 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001235 if (!object)
1236 return -1;
1237 size = PyUnicode_GET_SIZE(object);
1238 if (*end<1)
1239 *end = 1;
1240 if (*end>size)
1241 *end = size;
1242 Py_DECREF(object);
1243 return 0;
1244 }
1245 return -1;
1246}
1247
1248
Martin v. Löwis18e16552006-02-15 17:27:45 +00001249int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001250{
1251 if (!get_int(exc, "end", end)) {
1252 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001253 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001254 if (!object)
1255 return -1;
1256 size = PyString_GET_SIZE(object);
1257 if (*end<1)
1258 *end = 1;
1259 if (*end>size)
1260 *end = size;
1261 Py_DECREF(object);
1262 return 0;
1263 }
1264 return -1;
1265}
1266
1267
Martin v. Löwis18e16552006-02-15 17:27:45 +00001268int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001269{
1270 return PyUnicodeEncodeError_GetEnd(exc, start);
1271}
1272
1273
Martin v. Löwis18e16552006-02-15 17:27:45 +00001274int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001275{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001276 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001277}
1278
1279
Martin v. Löwis18e16552006-02-15 17:27:45 +00001280int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001281{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001282 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001283}
1284
1285
Martin v. Löwis18e16552006-02-15 17:27:45 +00001286int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001287{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001288 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001289}
1290
1291
1292PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1293{
1294 return get_string(exc, "reason");
1295}
1296
1297
1298PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1299{
1300 return get_string(exc, "reason");
1301}
1302
1303
1304PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1305{
1306 return get_string(exc, "reason");
1307}
1308
1309
1310int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1311{
1312 return set_string(exc, "reason", reason);
1313}
1314
1315
1316int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1317{
1318 return set_string(exc, "reason", reason);
1319}
1320
1321
1322int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1323{
1324 return set_string(exc, "reason", reason);
1325}
1326
1327
1328static PyObject *
1329UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1330{
1331 PyObject *rtnval = NULL;
1332 PyObject *encoding;
1333 PyObject *object;
1334 PyObject *start;
1335 PyObject *end;
1336 PyObject *reason;
1337
1338 if (!(self = get_self(args)))
1339 return NULL;
1340
1341 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1342 return NULL;
1343
Brett Cannonbf364092006-03-01 04:25:17 +00001344 if (!set_args_and_message(self, args)) {
1345 Py_DECREF(args);
1346 return NULL;
1347 }
1348
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001349 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1350 &PyString_Type, &encoding,
1351 objecttype, &object,
1352 &PyInt_Type, &start,
1353 &PyInt_Type, &end,
1354 &PyString_Type, &reason))
Walter Dörwalde98147a2003-08-14 20:59:07 +00001355 goto finally;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001356
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001357 if (PyObject_SetAttrString(self, "encoding", encoding))
1358 goto finally;
1359 if (PyObject_SetAttrString(self, "object", object))
1360 goto finally;
1361 if (PyObject_SetAttrString(self, "start", start))
1362 goto finally;
1363 if (PyObject_SetAttrString(self, "end", end))
1364 goto finally;
1365 if (PyObject_SetAttrString(self, "reason", reason))
1366 goto finally;
1367
1368 Py_INCREF(Py_None);
1369 rtnval = Py_None;
1370
1371 finally:
1372 Py_DECREF(args);
1373 return rtnval;
1374}
1375
1376
1377static PyObject *
1378UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1379{
1380 return UnicodeError__init__(self, args, &PyUnicode_Type);
1381}
1382
1383static PyObject *
1384UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1385{
1386 PyObject *encodingObj = NULL;
1387 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001388 Py_ssize_t start;
1389 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001390 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001391 PyObject *result = NULL;
1392
1393 self = arg;
1394
1395 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1396 goto error;
1397
1398 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1399 goto error;
1400
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001401 if (PyUnicodeEncodeError_GetStart(self, &start))
1402 goto error;
1403
1404 if (PyUnicodeEncodeError_GetEnd(self, &end))
1405 goto error;
1406
1407 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1408 goto error;
1409
1410 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001411 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001412 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001413 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001414 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001415 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001416 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001417 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001418 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1419 result = PyString_FromFormat(
1420 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001421 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001422 badchar_str,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001423 start,
1424 PyString_AS_STRING(reasonObj)
1425 );
1426 }
1427 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001428 result = PyString_FromFormat(
1429 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001430 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001431 start,
1432 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001433 PyString_AS_STRING(reasonObj)
1434 );
1435 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001436
1437error:
1438 Py_XDECREF(reasonObj);
1439 Py_XDECREF(objectObj);
1440 Py_XDECREF(encodingObj);
1441 return result;
1442}
1443
1444static PyMethodDef UnicodeEncodeError_methods[] = {
1445 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1446 {"__str__", UnicodeEncodeError__str__, METH_O},
1447 {NULL, NULL}
1448};
1449
1450
1451PyObject * PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001452 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1453 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001454{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001455 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001456 encoding, object, length, start, end, reason);
1457}
1458
1459
1460static PyObject *
1461UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1462{
1463 return UnicodeError__init__(self, args, &PyString_Type);
1464}
1465
1466static PyObject *
1467UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1468{
1469 PyObject *encodingObj = NULL;
1470 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001471 Py_ssize_t start;
1472 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001473 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001474 PyObject *result = NULL;
1475
1476 self = arg;
1477
1478 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1479 goto error;
1480
1481 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1482 goto error;
1483
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001484 if (PyUnicodeDecodeError_GetStart(self, &start))
1485 goto error;
1486
1487 if (PyUnicodeDecodeError_GetEnd(self, &end))
1488 goto error;
1489
1490 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1491 goto error;
1492
1493 if (end==start+1) {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001494 /* FromFormat does not support %02x, so format that separately */
1495 char byte[4];
1496 PyOS_snprintf(byte, sizeof(byte), "%02x",
1497 ((int)PyString_AS_STRING(objectObj)[start])&0xff);
1498 result = PyString_FromFormat(
1499 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001500 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001501 byte,
1502 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001503 PyString_AS_STRING(reasonObj)
1504 );
1505 }
1506 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001507 result = PyString_FromFormat(
1508 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001509 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001510 start,
1511 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001512 PyString_AS_STRING(reasonObj)
1513 );
1514 }
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001515
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001516
1517error:
1518 Py_XDECREF(reasonObj);
1519 Py_XDECREF(objectObj);
1520 Py_XDECREF(encodingObj);
1521 return result;
1522}
1523
1524static PyMethodDef UnicodeDecodeError_methods[] = {
1525 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1526 {"__str__", UnicodeDecodeError__str__, METH_O},
1527 {NULL, NULL}
1528};
1529
1530
1531PyObject * PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001532 const char *encoding, const char *object, Py_ssize_t length,
1533 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001534{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001535 assert(length < INT_MAX);
1536 assert(start < INT_MAX);
1537 assert(end < INT_MAX);
Martin v. Löwis5cb69362006-04-14 09:08:42 +00001538 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
1539 encoding, object, length, start, end, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001540}
1541
1542
1543static PyObject *
1544UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1545{
1546 PyObject *rtnval = NULL;
1547 PyObject *object;
1548 PyObject *start;
1549 PyObject *end;
1550 PyObject *reason;
1551
1552 if (!(self = get_self(args)))
1553 return NULL;
1554
1555 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1556 return NULL;
1557
Brett Cannonbf364092006-03-01 04:25:17 +00001558 if (!set_args_and_message(self, args)) {
1559 Py_DECREF(args);
1560 return NULL;
1561 }
1562
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001563 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1564 &PyUnicode_Type, &object,
1565 &PyInt_Type, &start,
1566 &PyInt_Type, &end,
1567 &PyString_Type, &reason))
1568 goto finally;
1569
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001570 if (PyObject_SetAttrString(self, "object", object))
1571 goto finally;
1572 if (PyObject_SetAttrString(self, "start", start))
1573 goto finally;
1574 if (PyObject_SetAttrString(self, "end", end))
1575 goto finally;
1576 if (PyObject_SetAttrString(self, "reason", reason))
1577 goto finally;
1578
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001579 rtnval = Py_None;
Brett Cannonbf364092006-03-01 04:25:17 +00001580 Py_INCREF(rtnval);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001581
1582 finally:
1583 Py_DECREF(args);
1584 return rtnval;
1585}
1586
1587
1588static PyObject *
1589UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1590{
1591 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001592 Py_ssize_t start;
1593 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001594 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001595 PyObject *result = NULL;
1596
1597 self = arg;
1598
1599 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1600 goto error;
1601
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001602 if (PyUnicodeTranslateError_GetStart(self, &start))
1603 goto error;
1604
1605 if (PyUnicodeTranslateError_GetEnd(self, &end))
1606 goto error;
1607
1608 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1609 goto error;
1610
1611 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001612 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001613 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001614 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001615 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001616 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001617 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001618 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001619 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1620 result = PyString_FromFormat(
1621 "can't translate character u'\\%s' in position %zd: %.400s",
1622 badchar_str,
1623 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001624 PyString_AS_STRING(reasonObj)
1625 );
1626 }
1627 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001628 result = PyString_FromFormat(
1629 "can't translate characters in position %zd-%zd: %.400s",
1630 start,
1631 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001632 PyString_AS_STRING(reasonObj)
1633 );
1634 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001635
1636error:
1637 Py_XDECREF(reasonObj);
1638 Py_XDECREF(objectObj);
1639 return result;
1640}
1641
1642static PyMethodDef UnicodeTranslateError_methods[] = {
1643 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1644 {"__str__", UnicodeTranslateError__str__, METH_O},
1645 {NULL, NULL}
1646};
1647
1648
1649PyObject * PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001650 const Py_UNICODE *object, Py_ssize_t length,
1651 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001652{
Martin v. Löwis5cb69362006-04-14 09:08:42 +00001653 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001654 object, length, start, end, reason);
1655}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001656#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001657
1658
Barry Warsaw675ac282000-05-26 19:05:16 +00001659
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001660/* Exception doc strings */
1661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001662PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001664PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001665
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001666PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001667
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001668PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001669
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001670PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001671
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001672PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001674PyDoc_STRVAR(ZeroDivisionError__doc__,
1675"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001676
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001677PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001679PyDoc_STRVAR(ValueError__doc__,
1680"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001681
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001682PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001683
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001684#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001685PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1686
1687PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1688
1689PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001690#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001691
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001692PyDoc_STRVAR(SystemError__doc__,
1693"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001694\n\
1695Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001696the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001697
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001698PyDoc_STRVAR(ReferenceError__doc__,
1699"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001700
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001701PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001703PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001704
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001705PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001706
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001707/* Warning category docstrings */
1708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001709PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001711PyDoc_STRVAR(UserWarning__doc__,
1712"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001714PyDoc_STRVAR(DeprecationWarning__doc__,
1715"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001718"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001719"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001721PyDoc_STRVAR(SyntaxWarning__doc__,
1722"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001724PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001725"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001727PyDoc_STRVAR(RuntimeWarning__doc__,
1728"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001729
Barry Warsaw9f007392002-08-14 15:51:29 +00001730PyDoc_STRVAR(FutureWarning__doc__,
1731"Base class for warnings about constructs that will change semantically "
1732"in the future.");
1733
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001734PyDoc_STRVAR(ImportWarning__doc__,
1735"Base class for warnings about probable mistakes in module imports");
Barry Warsaw675ac282000-05-26 19:05:16 +00001736
1737
1738/* module global functions */
1739static PyMethodDef functions[] = {
1740 /* Sentinel */
1741 {NULL, NULL}
1742};
1743
1744
1745
1746/* Global C API defined exceptions */
1747
Brett Cannonbf364092006-03-01 04:25:17 +00001748PyObject *PyExc_BaseException;
Barry Warsaw675ac282000-05-26 19:05:16 +00001749PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001750PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001751PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001752PyObject *PyExc_StandardError;
1753PyObject *PyExc_ArithmeticError;
1754PyObject *PyExc_LookupError;
1755
1756PyObject *PyExc_AssertionError;
1757PyObject *PyExc_AttributeError;
1758PyObject *PyExc_EOFError;
1759PyObject *PyExc_FloatingPointError;
1760PyObject *PyExc_EnvironmentError;
1761PyObject *PyExc_IOError;
1762PyObject *PyExc_OSError;
1763PyObject *PyExc_ImportError;
1764PyObject *PyExc_IndexError;
1765PyObject *PyExc_KeyError;
1766PyObject *PyExc_KeyboardInterrupt;
1767PyObject *PyExc_MemoryError;
1768PyObject *PyExc_NameError;
1769PyObject *PyExc_OverflowError;
1770PyObject *PyExc_RuntimeError;
1771PyObject *PyExc_NotImplementedError;
1772PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001773PyObject *PyExc_IndentationError;
1774PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001775PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001776PyObject *PyExc_SystemError;
1777PyObject *PyExc_SystemExit;
1778PyObject *PyExc_UnboundLocalError;
1779PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001780PyObject *PyExc_UnicodeEncodeError;
1781PyObject *PyExc_UnicodeDecodeError;
1782PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001783PyObject *PyExc_TypeError;
1784PyObject *PyExc_ValueError;
1785PyObject *PyExc_ZeroDivisionError;
1786#ifdef MS_WINDOWS
1787PyObject *PyExc_WindowsError;
1788#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001789#ifdef __VMS
1790PyObject *PyExc_VMSError;
1791#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001792
1793/* Pre-computed MemoryError instance. Best to create this as early as
Brett Cannonbf364092006-03-01 04:25:17 +00001794 * possible and not wait until a MemoryError is actually raised!
Barry Warsaw675ac282000-05-26 19:05:16 +00001795 */
1796PyObject *PyExc_MemoryErrorInst;
1797
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001798/* Predefined warning categories */
1799PyObject *PyExc_Warning;
1800PyObject *PyExc_UserWarning;
1801PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001802PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001803PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001804/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001805PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001806PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001807PyObject *PyExc_FutureWarning;
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001808PyObject *PyExc_ImportWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001809
Barry Warsaw675ac282000-05-26 19:05:16 +00001810
1811
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001812/* mapping between exception names and their PyObject ** */
1813static struct {
1814 char *name;
1815 PyObject **exc;
1816 PyObject **base; /* NULL == PyExc_StandardError */
1817 char *docstr;
1818 PyMethodDef *methods;
1819 int (*classinit)(PyObject *);
1820} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001821 /*
Brett Cannonbf364092006-03-01 04:25:17 +00001822 * The first four classes MUST appear in exactly this order
Barry Warsaw675ac282000-05-26 19:05:16 +00001823 */
Brett Cannonbf364092006-03-01 04:25:17 +00001824 {"BaseException", &PyExc_BaseException},
1825 {"Exception", &PyExc_Exception, &PyExc_BaseException, Exception__doc__},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001826 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1827 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001828 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1829 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001830 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1831 StandardError__doc__},
1832 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1833 /*
1834 * The rest appear in depth-first order of the hierarchy
1835 */
Brett Cannonbf364092006-03-01 04:25:17 +00001836 {"SystemExit", &PyExc_SystemExit, &PyExc_BaseException, SystemExit__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +00001837 SystemExit_methods},
Brett Cannonbf364092006-03-01 04:25:17 +00001838 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, &PyExc_BaseException,
1839 KeyboardInterrupt__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001840 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1841 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1842 EnvironmentError_methods},
1843 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1844 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1845#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001846 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Martin v. Löwis879768d2006-05-11 13:28:43 +00001847WindowsError__doc__, WindowsError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001848#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001849#ifdef __VMS
1850 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1851 VMSError__doc__},
1852#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001853 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1854 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1855 {"NotImplementedError", &PyExc_NotImplementedError,
1856 &PyExc_RuntimeError, NotImplementedError__doc__},
1857 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1858 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1859 UnboundLocalError__doc__},
1860 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1861 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1862 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001863 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1864 IndentationError__doc__},
1865 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1866 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001867 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1868 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1869 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1870 IndexError__doc__},
1871 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001872 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001873 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1874 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1875 OverflowError__doc__},
1876 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1877 ZeroDivisionError__doc__},
1878 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1879 FloatingPointError__doc__},
1880 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1881 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001882#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001883 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1884 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1885 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1886 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1887 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1888 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001889#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001890 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001891 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1892 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001893 /* Warning categories */
1894 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1895 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1896 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1897 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001898 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1899 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001900 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001901 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001902 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1903 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001904 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1905 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001906 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1907 FutureWarning__doc__},
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001908 {"ImportWarning", &PyExc_ImportWarning, &PyExc_Warning,
1909 ImportWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001910 /* Sentinel */
1911 {NULL}
1912};
1913
1914
1915
Mark Hammonda2905272002-07-29 13:42:14 +00001916void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001917_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001918{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001919 char *modulename = "exceptions";
Martin v. Löwis18e16552006-02-15 17:27:45 +00001920 Py_ssize_t modnamesz = strlen(modulename);
Barry Warsaw675ac282000-05-26 19:05:16 +00001921 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001922 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001923
Tim Peters6d6c1a32001-08-02 04:15:00 +00001924 me = Py_InitModule(modulename, functions);
1925 if (me == NULL)
1926 goto err;
1927 mydict = PyModule_GetDict(me);
1928 if (mydict == NULL)
1929 goto err;
1930 bltinmod = PyImport_ImportModule("__builtin__");
1931 if (bltinmod == NULL)
1932 goto err;
1933 bdict = PyModule_GetDict(bltinmod);
1934 if (bdict == NULL)
1935 goto err;
1936 doc = PyString_FromString(module__doc__);
1937 if (doc == NULL)
1938 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001939
Tim Peters6d6c1a32001-08-02 04:15:00 +00001940 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001941 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001942 if (i < 0) {
1943 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001944 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001945 return;
1946 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001947
1948 /* This is the base class of all exceptions, so make it first. */
Brett Cannonbf364092006-03-01 04:25:17 +00001949 if (make_BaseException(modulename) ||
1950 PyDict_SetItemString(mydict, "BaseException", PyExc_BaseException) ||
1951 PyDict_SetItemString(bdict, "BaseException", PyExc_BaseException))
Barry Warsaw675ac282000-05-26 19:05:16 +00001952 {
Brett Cannonbf364092006-03-01 04:25:17 +00001953 Py_FatalError("Base class `BaseException' could not be created.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001954 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001955
Barry Warsaw675ac282000-05-26 19:05:16 +00001956 /* Now we can programmatically create all the remaining exceptions.
1957 * Remember to start the loop at 1 to skip Exceptions.
1958 */
1959 for (i=1; exctable[i].name; i++) {
1960 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001961 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1962 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001963
1964 (void)strcpy(cname, modulename);
1965 (void)strcat(cname, ".");
1966 (void)strcat(cname, exctable[i].name);
1967
1968 if (exctable[i].base == 0)
1969 base = PyExc_StandardError;
1970 else
1971 base = *exctable[i].base;
1972
1973 status = make_class(exctable[i].exc, base, cname,
1974 exctable[i].methods,
1975 exctable[i].docstr);
1976
1977 PyMem_DEL(cname);
1978
1979 if (status)
1980 Py_FatalError("Standard exception classes could not be created.");
1981
1982 if (exctable[i].classinit) {
1983 status = (*exctable[i].classinit)(*exctable[i].exc);
1984 if (status)
1985 Py_FatalError("An exception class could not be initialized.");
1986 }
1987
1988 /* Now insert the class into both this module and the __builtin__
1989 * module.
1990 */
1991 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1992 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1993 {
1994 Py_FatalError("Module dictionary insertion problem.");
1995 }
1996 }
1997
1998 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001999 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00002000 if (!args ||
2001 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
2002 {
2003 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2004 }
2005 Py_DECREF(args);
2006
2007 /* We're done with __builtin__ */
2008 Py_DECREF(bltinmod);
2009}
2010
2011
Mark Hammonda2905272002-07-29 13:42:14 +00002012void
Tim Peters6d6c1a32001-08-02 04:15:00 +00002013_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00002014{
2015 int i;
2016
2017 Py_XDECREF(PyExc_MemoryErrorInst);
2018 PyExc_MemoryErrorInst = NULL;
2019
2020 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00002021 /* clear the class's dictionary, freeing up circular references
2022 * between the class and its methods.
2023 */
2024 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
2025 PyDict_Clear(cdict);
2026 Py_DECREF(cdict);
2027
2028 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00002029 Py_XDECREF(*exctable[i].exc);
2030 *exctable[i].exc = NULL;
2031 }
2032}