blob: 3f949edfab05235f9654c8b613eb50f6a1e767b7 [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 *
Brett Cannonbf364092006-03-01 04:25:17 +0000226BaseException__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000227{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000228 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000229
Fred Drake1aba5772000-08-15 15:46:16 +0000230 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000231 return NULL;
232
233 args = PyObject_GetAttrString(self, "args");
234 if (!args)
235 return NULL;
236
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000237 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000238 case 0:
239 out = PyString_FromString("");
240 break;
241 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000242 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000243 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000244 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000245 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000246 Py_DECREF(tmp);
247 }
248 else
249 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000250 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000251 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000252 case -1:
253 PyErr_Clear();
254 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000255 default:
256 out = PyObject_Str(args);
257 break;
258 }
259
260 Py_DECREF(args);
261 return out;
262}
263
Brett Cannonbf364092006-03-01 04:25:17 +0000264#ifdef Py_USING_UNICODE
265static PyObject *
266BaseException__unicode__(PyObject *self, PyObject *args)
267{
268 Py_ssize_t args_len;
269
270 if (!PyArg_ParseTuple(args, "O:__unicode__", &self))
271 return NULL;
272
273 args = PyObject_GetAttrString(self, "args");
274 if (!args)
275 return NULL;
276
277 args_len = PySequence_Size(args);
278 if (args_len < 0) {
279 Py_DECREF(args);
280 return NULL;
281 }
282
283 if (args_len == 0) {
284 Py_DECREF(args);
285 return PyUnicode_FromUnicode(NULL, 0);
286 }
287 else if (args_len == 1) {
288 PyObject *temp = PySequence_GetItem(args, 0);
Brett Cannon46872b12006-03-02 04:31:55 +0000289 PyObject *unicode_obj;
290
Brett Cannonbf364092006-03-01 04:25:17 +0000291 if (!temp) {
292 Py_DECREF(args);
293 return NULL;
294 }
295 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000296 unicode_obj = PyObject_Unicode(temp);
297 Py_DECREF(temp);
298 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000299 }
300 else {
Brett Cannon46872b12006-03-02 04:31:55 +0000301 PyObject *unicode_obj = PyObject_Unicode(args);
302
Brett Cannonbf364092006-03-01 04:25:17 +0000303 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000304 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000305 }
306}
307#endif /* Py_USING_UNICODE */
Barry Warsaw675ac282000-05-26 19:05:16 +0000308
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000309static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000310BaseException__repr__(PyObject *self, PyObject *args)
311{
312 PyObject *args_attr;
313 Py_ssize_t args_len;
314 PyObject *repr_suffix;
315 PyObject *repr;
316
317 if (!PyArg_ParseTuple(args, "O:__repr__", &self))
318 return NULL;
319
320 args_attr = PyObject_GetAttrString(self, "args");
321 if (!args_attr)
322 return NULL;
323
324 args_len = PySequence_Length(args_attr);
325 if (args_len < 0) {
326 Py_DECREF(args_attr);
327 return NULL;
328 }
329
330 if (args_len == 0) {
331 Py_DECREF(args_attr);
332 repr_suffix = PyString_FromString("()");
333 if (!repr_suffix)
334 return NULL;
335 }
336 else {
Neal Norwitz9742f272006-03-03 19:13:57 +0000337 PyObject *args_repr = PyObject_Repr(args_attr);
Brett Cannonbf364092006-03-01 04:25:17 +0000338 Py_DECREF(args_attr);
339 if (!args_repr)
340 return NULL;
341
342 repr_suffix = args_repr;
Brett Cannonbf364092006-03-01 04:25:17 +0000343 }
344
345 repr = PyString_FromString(self->ob_type->tp_name);
346 if (!repr) {
347 Py_DECREF(repr_suffix);
348 return NULL;
349 }
350
351 PyString_ConcatAndDel(&repr, repr_suffix);
352 return repr;
353}
354
355static PyObject *
356BaseException__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000357{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000358 PyObject *out;
359 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000360
Fred Drake1aba5772000-08-15 15:46:16 +0000361 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000362 return NULL;
363
364 args = PyObject_GetAttrString(self, "args");
365 if (!args)
366 return NULL;
367
368 out = PyObject_GetItem(args, index);
369 Py_DECREF(args);
370 return out;
371}
372
373
374static PyMethodDef
Brett Cannonbf364092006-03-01 04:25:17 +0000375BaseException_methods[] = {
376 /* methods for the BaseException class */
377 {"__getitem__", BaseException__getitem__, METH_VARARGS},
378 {"__repr__", BaseException__repr__, METH_VARARGS},
379 {"__str__", BaseException__str__, METH_VARARGS},
380#ifdef Py_USING_UNICODE
381 {"__unicode__", BaseException__unicode__, METH_VARARGS},
382#endif /* Py_USING_UNICODE */
383 {"__init__", BaseException__init__, METH_VARARGS},
384 {NULL, NULL }
Barry Warsaw675ac282000-05-26 19:05:16 +0000385};
386
387
388static int
Brett Cannonbf364092006-03-01 04:25:17 +0000389make_BaseException(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000390{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000391 PyObject *dict = PyDict_New();
392 PyObject *str = NULL;
393 PyObject *name = NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000394 PyObject *emptytuple = NULL;
395 PyObject *argstuple = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000396 int status = -1;
397
398 if (!dict)
399 return -1;
400
401 /* If an error occurs from here on, goto finally instead of explicitly
402 * returning NULL.
403 */
404
405 if (!(str = PyString_FromString(modulename)))
406 goto finally;
407 if (PyDict_SetItemString(dict, "__module__", str))
408 goto finally;
409 Py_DECREF(str);
Brett Cannonbf364092006-03-01 04:25:17 +0000410
411 if (!(str = PyString_FromString(BaseException__doc__)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000412 goto finally;
413 if (PyDict_SetItemString(dict, "__doc__", str))
414 goto finally;
415
Brett Cannonbf364092006-03-01 04:25:17 +0000416 if (!(name = PyString_FromString("BaseException")))
Barry Warsaw675ac282000-05-26 19:05:16 +0000417 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000418
Brett Cannonbf364092006-03-01 04:25:17 +0000419 if (!(emptytuple = PyTuple_New(0)))
420 goto finally;
421
422 if (!(argstuple = PyTuple_Pack(3, name, emptytuple, dict)))
423 goto finally;
424
425 if (!(PyExc_BaseException = PyType_Type.tp_new(&PyType_Type, argstuple,
426 NULL)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000427 goto finally;
428
429 /* Now populate the dictionary with the method suite */
Brett Cannonbf364092006-03-01 04:25:17 +0000430 if (populate_methods(PyExc_BaseException, BaseException_methods))
431 /* Don't need to reclaim PyExc_BaseException here because that'll
Barry Warsaw675ac282000-05-26 19:05:16 +0000432 * happen during interpreter shutdown.
433 */
434 goto finally;
435
436 status = 0;
437
438 finally:
439 Py_XDECREF(dict);
440 Py_XDECREF(str);
441 Py_XDECREF(name);
Brett Cannonbf364092006-03-01 04:25:17 +0000442 Py_XDECREF(emptytuple);
443 Py_XDECREF(argstuple);
Barry Warsaw675ac282000-05-26 19:05:16 +0000444 return status;
445}
446
447
448
Brett Cannonbf364092006-03-01 04:25:17 +0000449PyDoc_STRVAR(Exception__doc__, "Common base class for all non-exit exceptions.");
450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000451PyDoc_STRVAR(StandardError__doc__,
Brett Cannonbf364092006-03-01 04:25:17 +0000452"Base class for all standard Python exceptions that do not represent"
453"interpreter exiting.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000455PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000457PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000458PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000459
Barry Warsaw675ac282000-05-26 19:05:16 +0000460
461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000462PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000463
464
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000465static PyObject *
466SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000467{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000468 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000469 int status;
470
471 if (!(self = get_self(args)))
472 return NULL;
473
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000474 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000475 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000476
Brett Cannonbf364092006-03-01 04:25:17 +0000477 if (!set_args_and_message(self, args)) {
478 Py_DECREF(args);
479 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000480 }
481
482 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000483 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000484 case 0:
485 Py_INCREF(Py_None);
486 code = Py_None;
487 break;
488 case 1:
489 code = PySequence_GetItem(args, 0);
490 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000491 case -1:
492 PyErr_Clear();
493 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000494 default:
495 Py_INCREF(args);
496 code = args;
497 break;
498 }
499
500 status = PyObject_SetAttrString(self, "code", code);
501 Py_DECREF(code);
502 Py_DECREF(args);
503 if (status < 0)
504 return NULL;
505
Brett Cannonbf364092006-03-01 04:25:17 +0000506 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000507}
508
509
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000510static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000511 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000512 {NULL, NULL}
513};
514
515
516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000519PyDoc_STRVAR(ImportError__doc__,
520"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000521
522
523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000524PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000525
526
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000527static PyObject *
528EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000529{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000530 PyObject *item0 = NULL;
531 PyObject *item1 = NULL;
532 PyObject *item2 = NULL;
533 PyObject *subslice = NULL;
534 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000535
536 if (!(self = get_self(args)))
537 return NULL;
538
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000539 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000540 return NULL;
541
Brett Cannonbf364092006-03-01 04:25:17 +0000542 if (!set_args_and_message(self, args)) {
543 Py_DECREF(args);
544 return NULL;
545 }
546
547 if (PyObject_SetAttrString(self, "errno", Py_None) ||
Barry Warsaw675ac282000-05-26 19:05:16 +0000548 PyObject_SetAttrString(self, "strerror", Py_None) ||
549 PyObject_SetAttrString(self, "filename", Py_None))
550 {
551 goto finally;
552 }
553
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000554 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000555 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000556 /* Where a function has a single filename, such as open() or some
557 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
558 * called, giving a third argument which is the filename. But, so
559 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000560 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000561 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000562 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000563 * we hack args so that it only contains two items. This also
564 * means we need our own __str__() which prints out the filename
565 * when it was supplied.
566 */
567 item0 = PySequence_GetItem(args, 0);
568 item1 = PySequence_GetItem(args, 1);
569 item2 = PySequence_GetItem(args, 2);
570 if (!item0 || !item1 || !item2)
571 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000572
Barry Warsaw675ac282000-05-26 19:05:16 +0000573 if (PyObject_SetAttrString(self, "errno", item0) ||
574 PyObject_SetAttrString(self, "strerror", item1) ||
575 PyObject_SetAttrString(self, "filename", item2))
576 {
577 goto finally;
578 }
579
580 subslice = PySequence_GetSlice(args, 0, 2);
581 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
582 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000583 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000584
585 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000586 /* Used when PyErr_SetFromErrno() is called and no filename
587 * argument is given.
588 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000589 item0 = PySequence_GetItem(args, 0);
590 item1 = PySequence_GetItem(args, 1);
591 if (!item0 || !item1)
592 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000593
Barry Warsaw675ac282000-05-26 19:05:16 +0000594 if (PyObject_SetAttrString(self, "errno", item0) ||
595 PyObject_SetAttrString(self, "strerror", item1))
596 {
597 goto finally;
598 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000599 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000600
601 case -1:
602 PyErr_Clear();
603 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000604 }
605
606 Py_INCREF(Py_None);
607 rtnval = Py_None;
608
609 finally:
610 Py_DECREF(args);
611 Py_XDECREF(item0);
612 Py_XDECREF(item1);
613 Py_XDECREF(item2);
614 Py_XDECREF(subslice);
615 return rtnval;
616}
617
618
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000619static PyObject *
620EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000621{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000622 PyObject *originalself = self;
623 PyObject *filename;
624 PyObject *serrno;
625 PyObject *strerror;
626 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000627
Fred Drake1aba5772000-08-15 15:46:16 +0000628 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000629 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000630
Barry Warsaw675ac282000-05-26 19:05:16 +0000631 filename = PyObject_GetAttrString(self, "filename");
632 serrno = PyObject_GetAttrString(self, "errno");
633 strerror = PyObject_GetAttrString(self, "strerror");
634 if (!filename || !serrno || !strerror)
635 goto finally;
636
637 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000638 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
639 PyObject *repr = PyObject_Repr(filename);
640 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000641
642 if (!fmt || !repr || !tuple) {
643 Py_XDECREF(fmt);
644 Py_XDECREF(repr);
645 Py_XDECREF(tuple);
646 goto finally;
647 }
648
649 PyTuple_SET_ITEM(tuple, 0, serrno);
650 PyTuple_SET_ITEM(tuple, 1, strerror);
651 PyTuple_SET_ITEM(tuple, 2, repr);
652
653 rtnval = PyString_Format(fmt, tuple);
654
655 Py_DECREF(fmt);
656 Py_DECREF(tuple);
657 /* already freed because tuple owned only reference */
658 serrno = NULL;
659 strerror = NULL;
660 }
661 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000662 PyObject *fmt = PyString_FromString("[Errno %s] %s");
663 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000664
665 if (!fmt || !tuple) {
666 Py_XDECREF(fmt);
667 Py_XDECREF(tuple);
668 goto finally;
669 }
670
671 PyTuple_SET_ITEM(tuple, 0, serrno);
672 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000673
Barry Warsaw675ac282000-05-26 19:05:16 +0000674 rtnval = PyString_Format(fmt, tuple);
675
676 Py_DECREF(fmt);
677 Py_DECREF(tuple);
678 /* already freed because tuple owned only reference */
679 serrno = NULL;
680 strerror = NULL;
681 }
682 else
683 /* The original Python code said:
684 *
685 * return StandardError.__str__(self)
686 *
687 * but there is no StandardError__str__() function; we happen to
Brett Cannonbf364092006-03-01 04:25:17 +0000688 * know that's just a pass through to BaseException__str__().
Barry Warsaw675ac282000-05-26 19:05:16 +0000689 */
Brett Cannonbf364092006-03-01 04:25:17 +0000690 rtnval = BaseException__str__(originalself, args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000691
692 finally:
693 Py_XDECREF(filename);
694 Py_XDECREF(serrno);
695 Py_XDECREF(strerror);
696 return rtnval;
697}
698
699
700static
701PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000702 {"__init__", EnvironmentError__init__, METH_VARARGS},
703 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000704 {NULL, NULL}
705};
706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000707PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000709PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000710
711#ifdef MS_WINDOWS
Martin v. Löwis879768d2006-05-11 13:28:43 +0000712#include "errmap.h"
713
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000714PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Martin v. Löwis879768d2006-05-11 13:28:43 +0000715
716static PyObject *
717WindowsError__init__(PyObject *self, PyObject *args)
718{
719 PyObject *o_errcode, *result;
720 long errcode, posix_errno;
721 result = EnvironmentError__init__(self, args);
722 if (!result)
723 return NULL;
724 self = get_self(args);
725 if (!self)
726 goto failed;
727 /* Set errno to the POSIX errno, and winerror to the Win32
728 error code. */
729 o_errcode = PyObject_GetAttrString(self, "errno");
730 if (!o_errcode)
731 goto failed;
732 errcode = PyInt_AsLong(o_errcode);
733 if (!errcode == -1 && PyErr_Occurred())
734 goto failed;
735 posix_errno = winerror_to_errno(errcode);
736 if (PyObject_SetAttrString(self, "winerror", o_errcode) < 0)
737 goto failed;
738 Py_DECREF(o_errcode);
739 o_errcode = PyInt_FromLong(posix_errno);
740 if (!o_errcode)
741 goto failed;
742 if (PyObject_SetAttrString(self, "errno", o_errcode) < 0)
743 goto failed;
744 Py_DECREF(o_errcode);
745 return result;
746failed:
747 /* Could not set errno. */
748 Py_XDECREF(o_errcode);
749 Py_DECREF(self);
750 Py_DECREF(result);
751 return NULL;
752}
753
754static PyObject *
755WindowsError__str__(PyObject *self, PyObject *args)
756{
757 PyObject *originalself = self;
758 PyObject *filename;
759 PyObject *serrno;
760 PyObject *strerror;
761 PyObject *rtnval = NULL;
762
763 if (!PyArg_ParseTuple(args, "O:__str__", &self))
764 return NULL;
765
766 filename = PyObject_GetAttrString(self, "filename");
767 serrno = PyObject_GetAttrString(self, "winerror");
768 strerror = PyObject_GetAttrString(self, "strerror");
769 if (!filename || !serrno || !strerror)
770 goto finally;
771
772 if (filename != Py_None) {
773 PyObject *fmt = PyString_FromString("[Error %s] %s: %s");
774 PyObject *repr = PyObject_Repr(filename);
775 PyObject *tuple = PyTuple_New(3);
776
777 if (!fmt || !repr || !tuple) {
778 Py_XDECREF(fmt);
779 Py_XDECREF(repr);
780 Py_XDECREF(tuple);
781 goto finally;
782 }
783
784 PyTuple_SET_ITEM(tuple, 0, serrno);
785 PyTuple_SET_ITEM(tuple, 1, strerror);
786 PyTuple_SET_ITEM(tuple, 2, repr);
787
788 rtnval = PyString_Format(fmt, tuple);
789
790 Py_DECREF(fmt);
791 Py_DECREF(tuple);
792 /* already freed because tuple owned only reference */
793 serrno = NULL;
794 strerror = NULL;
795 }
796 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
797 PyObject *fmt = PyString_FromString("[Error %s] %s");
798 PyObject *tuple = PyTuple_New(2);
799
800 if (!fmt || !tuple) {
801 Py_XDECREF(fmt);
802 Py_XDECREF(tuple);
803 goto finally;
804 }
805
806 PyTuple_SET_ITEM(tuple, 0, serrno);
807 PyTuple_SET_ITEM(tuple, 1, strerror);
808
809 rtnval = PyString_Format(fmt, tuple);
810
811 Py_DECREF(fmt);
812 Py_DECREF(tuple);
813 /* already freed because tuple owned only reference */
814 serrno = NULL;
815 strerror = NULL;
816 }
817 else
818 rtnval = EnvironmentError__str__(originalself, args);
819
820 finally:
821 Py_XDECREF(filename);
822 Py_XDECREF(serrno);
823 Py_XDECREF(strerror);
824 return rtnval;
825}
826
827static
828PyMethodDef WindowsError_methods[] = {
829 {"__init__", WindowsError__init__, METH_VARARGS},
830 {"__str__", WindowsError__str__, METH_VARARGS},
831 {NULL, NULL}
832};
Barry Warsaw675ac282000-05-26 19:05:16 +0000833#endif /* MS_WINDOWS */
834
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000835#ifdef __VMS
836static char
837VMSError__doc__[] = "OpenVMS OS system call failed.";
838#endif
839
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000840PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000841
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000842PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000843
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000844PyDoc_STRVAR(NotImplementedError__doc__,
845"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000846
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000847PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000848
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000849PyDoc_STRVAR(UnboundLocalError__doc__,
850"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000851
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000852PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000853
854
855
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000856PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000857
858
859static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000860SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000861{
Barry Warsaw87bec352000-08-18 05:05:37 +0000862 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000863 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000864
865 /* Additional class-creation time initializations */
866 if (!emptystring ||
867 PyObject_SetAttrString(klass, "msg", emptystring) ||
868 PyObject_SetAttrString(klass, "filename", Py_None) ||
869 PyObject_SetAttrString(klass, "lineno", Py_None) ||
870 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000871 PyObject_SetAttrString(klass, "text", Py_None) ||
872 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000873 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000874 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000875 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000876 Py_XDECREF(emptystring);
877 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000878}
879
880
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000881static PyObject *
882SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000883{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000884 PyObject *rtnval = NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +0000885 Py_ssize_t lenargs;
Barry Warsaw675ac282000-05-26 19:05:16 +0000886
887 if (!(self = get_self(args)))
888 return NULL;
889
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000890 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000891 return NULL;
892
Brett Cannonbf364092006-03-01 04:25:17 +0000893 if (!set_args_and_message(self, args)) {
894 Py_DECREF(args);
895 return NULL;
896 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000897
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000898 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000899 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000900 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000901 int status;
902
903 if (!item0)
904 goto finally;
905 status = PyObject_SetAttrString(self, "msg", item0);
906 Py_DECREF(item0);
907 if (status)
908 goto finally;
909 }
910 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000911 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000912 PyObject *filename = NULL, *lineno = NULL;
913 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000914 int status = 1;
915
916 if (!info)
917 goto finally;
918
919 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000920 if (filename != NULL) {
921 lineno = PySequence_GetItem(info, 1);
922 if (lineno != NULL) {
923 offset = PySequence_GetItem(info, 2);
924 if (offset != NULL) {
925 text = PySequence_GetItem(info, 3);
926 if (text != NULL) {
927 status =
928 PyObject_SetAttrString(self, "filename", filename)
929 || PyObject_SetAttrString(self, "lineno", lineno)
930 || PyObject_SetAttrString(self, "offset", offset)
931 || PyObject_SetAttrString(self, "text", text);
932 Py_DECREF(text);
933 }
934 Py_DECREF(offset);
935 }
936 Py_DECREF(lineno);
937 }
938 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000939 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000940 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000941
942 if (status)
943 goto finally;
944 }
945 Py_INCREF(Py_None);
946 rtnval = Py_None;
947
948 finally:
949 Py_DECREF(args);
950 return rtnval;
951}
952
953
Fred Drake185a29b2000-08-15 16:20:36 +0000954/* This is called "my_basename" instead of just "basename" to avoid name
955 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
956 defined, and Python does define that. */
957static char *
958my_basename(char *name)
959{
960 char *cp = name;
961 char *result = name;
962
963 if (name == NULL)
964 return "???";
965 while (*cp != '\0') {
966 if (*cp == SEP)
967 result = cp + 1;
968 ++cp;
969 }
970 return result;
971}
972
973
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000974static PyObject *
975SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000976{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000977 PyObject *msg;
978 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000979 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000980
Fred Drake1aba5772000-08-15 15:46:16 +0000981 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000982 return NULL;
983
984 if (!(msg = PyObject_GetAttrString(self, "msg")))
985 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000986
Barry Warsaw675ac282000-05-26 19:05:16 +0000987 str = PyObject_Str(msg);
988 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000989 result = str;
990
991 /* XXX -- do all the additional formatting with filename and
992 lineno here */
993
Guido van Rossum602d4512002-09-03 20:24:09 +0000994 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000995 int have_filename = 0;
996 int have_lineno = 0;
997 char *buffer = NULL;
998
Barry Warsaw77c9f502000-08-16 19:43:17 +0000999 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +00001000 have_filename = PyString_Check(filename);
1001 else
1002 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +00001003
1004 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +00001005 have_lineno = PyInt_Check(lineno);
1006 else
1007 PyErr_Clear();
1008
1009 if (have_filename || have_lineno) {
Martin v. Löwis725507b2006-03-07 12:08:51 +00001010 Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64;
Barry Warsaw77c9f502000-08-16 19:43:17 +00001011 if (have_filename)
1012 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +00001013
Anthony Baxtera863d332006-04-11 07:43:46 +00001014 buffer = (char *)PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +00001015 if (buffer != NULL) {
1016 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +00001017 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
1018 PyString_AS_STRING(str),
1019 my_basename(PyString_AS_STRING(filename)),
1020 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +00001021 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +00001022 PyOS_snprintf(buffer, bufsize, "%s (%s)",
1023 PyString_AS_STRING(str),
1024 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +00001025 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +00001026 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
1027 PyString_AS_STRING(str),
1028 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +00001029
Fred Drake1aba5772000-08-15 15:46:16 +00001030 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +00001031 PyMem_FREE(buffer);
1032
Fred Drake1aba5772000-08-15 15:46:16 +00001033 if (result == NULL)
1034 result = str;
1035 else
1036 Py_DECREF(str);
1037 }
1038 }
1039 Py_XDECREF(filename);
1040 Py_XDECREF(lineno);
1041 }
1042 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +00001043}
1044
1045
Guido van Rossumf68d8e52001-04-14 17:55:09 +00001046static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +00001047 {"__init__", SyntaxError__init__, METH_VARARGS},
1048 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +00001049 {NULL, NULL}
1050};
1051
1052
Guido van Rossum602d4512002-09-03 20:24:09 +00001053static PyObject *
1054KeyError__str__(PyObject *self, PyObject *args)
1055{
1056 PyObject *argsattr;
1057 PyObject *result;
1058
1059 if (!PyArg_ParseTuple(args, "O:__str__", &self))
1060 return NULL;
1061
Brett Cannonbf364092006-03-01 04:25:17 +00001062 argsattr = PyObject_GetAttrString(self, "args");
1063 if (!argsattr)
1064 return NULL;
Guido van Rossum602d4512002-09-03 20:24:09 +00001065
1066 /* If args is a tuple of exactly one item, apply repr to args[0].
1067 This is done so that e.g. the exception raised by {}[''] prints
1068 KeyError: ''
1069 rather than the confusing
1070 KeyError
1071 alone. The downside is that if KeyError is raised with an explanatory
1072 string, that string will be displayed in quotes. Too bad.
Brett Cannonbf364092006-03-01 04:25:17 +00001073 If args is anything else, use the default BaseException__str__().
Guido van Rossum602d4512002-09-03 20:24:09 +00001074 */
1075 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
1076 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
1077 result = PyObject_Repr(key);
1078 }
1079 else
Brett Cannonbf364092006-03-01 04:25:17 +00001080 result = BaseException__str__(self, args);
Guido van Rossum602d4512002-09-03 20:24:09 +00001081
1082 Py_DECREF(argsattr);
1083 return result;
1084}
1085
1086static PyMethodDef KeyError_methods[] = {
1087 {"__str__", KeyError__str__, METH_VARARGS},
1088 {NULL, NULL}
1089};
1090
1091
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001092#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001093static
Martin v. Löwis18e16552006-02-15 17:27:45 +00001094int get_int(PyObject *exc, const char *name, Py_ssize_t *value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001095{
1096 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1097
1098 if (!attr)
1099 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001100 if (PyInt_Check(attr)) {
1101 *value = PyInt_AS_LONG(attr);
1102 } else if (PyLong_Check(attr)) {
1103 *value = (size_t)PyLong_AsLongLong(attr);
1104 if (*value == -1) {
1105 Py_DECREF(attr);
1106 return -1;
1107 }
1108 } else {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001109 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001110 Py_DECREF(attr);
1111 return -1;
1112 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001113 Py_DECREF(attr);
1114 return 0;
1115}
1116
1117
1118static
Martin v. Löwis18e16552006-02-15 17:27:45 +00001119int set_ssize_t(PyObject *exc, const char *name, Py_ssize_t value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001120{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001121 PyObject *obj = PyInt_FromSsize_t(value);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001122 int result;
1123
1124 if (!obj)
1125 return -1;
1126 result = PyObject_SetAttrString(exc, (char *)name, obj);
1127 Py_DECREF(obj);
1128 return result;
1129}
1130
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001131static
1132PyObject *get_string(PyObject *exc, const char *name)
1133{
1134 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1135
1136 if (!attr)
1137 return NULL;
1138 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001139 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001140 Py_DECREF(attr);
1141 return NULL;
1142 }
1143 return attr;
1144}
1145
1146
1147static
1148int set_string(PyObject *exc, const char *name, const char *value)
1149{
1150 PyObject *obj = PyString_FromString(value);
1151 int result;
1152
1153 if (!obj)
1154 return -1;
1155 result = PyObject_SetAttrString(exc, (char *)name, obj);
1156 Py_DECREF(obj);
1157 return result;
1158}
1159
1160
1161static
1162PyObject *get_unicode(PyObject *exc, const char *name)
1163{
1164 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1165
1166 if (!attr)
1167 return NULL;
1168 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001169 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001170 Py_DECREF(attr);
1171 return NULL;
1172 }
1173 return attr;
1174}
1175
1176PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1177{
1178 return get_string(exc, "encoding");
1179}
1180
1181PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1182{
1183 return get_string(exc, "encoding");
1184}
1185
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001186PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
1187{
1188 return get_unicode(exc, "object");
1189}
1190
1191PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1192{
1193 return get_string(exc, "object");
1194}
1195
1196PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1197{
1198 return get_unicode(exc, "object");
1199}
1200
Martin v. Löwis18e16552006-02-15 17:27:45 +00001201int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001202{
1203 if (!get_int(exc, "start", start)) {
1204 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001205 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001206 if (!object)
1207 return -1;
1208 size = PyUnicode_GET_SIZE(object);
1209 if (*start<0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001210 *start = 0; /*XXX check for values <0*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001211 if (*start>=size)
1212 *start = size-1;
1213 Py_DECREF(object);
1214 return 0;
1215 }
1216 return -1;
1217}
1218
1219
Martin v. Löwis18e16552006-02-15 17:27:45 +00001220int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001221{
1222 if (!get_int(exc, "start", start)) {
1223 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001224 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001225 if (!object)
1226 return -1;
1227 size = PyString_GET_SIZE(object);
1228 if (*start<0)
1229 *start = 0;
1230 if (*start>=size)
1231 *start = size-1;
1232 Py_DECREF(object);
1233 return 0;
1234 }
1235 return -1;
1236}
1237
1238
Martin v. Löwis18e16552006-02-15 17:27:45 +00001239int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001240{
1241 return PyUnicodeEncodeError_GetStart(exc, start);
1242}
1243
1244
Martin v. Löwis18e16552006-02-15 17:27:45 +00001245int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001246{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001247 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001248}
1249
1250
Martin v. Löwis18e16552006-02-15 17:27:45 +00001251int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001252{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001253 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001254}
1255
1256
Martin v. Löwis18e16552006-02-15 17:27:45 +00001257int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001258{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001259 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001260}
1261
1262
Martin v. Löwis18e16552006-02-15 17:27:45 +00001263int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001264{
1265 if (!get_int(exc, "end", end)) {
1266 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001267 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001268 if (!object)
1269 return -1;
1270 size = PyUnicode_GET_SIZE(object);
1271 if (*end<1)
1272 *end = 1;
1273 if (*end>size)
1274 *end = size;
1275 Py_DECREF(object);
1276 return 0;
1277 }
1278 return -1;
1279}
1280
1281
Martin v. Löwis18e16552006-02-15 17:27:45 +00001282int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001283{
1284 if (!get_int(exc, "end", end)) {
1285 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001286 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001287 if (!object)
1288 return -1;
1289 size = PyString_GET_SIZE(object);
1290 if (*end<1)
1291 *end = 1;
1292 if (*end>size)
1293 *end = size;
1294 Py_DECREF(object);
1295 return 0;
1296 }
1297 return -1;
1298}
1299
1300
Martin v. Löwis18e16552006-02-15 17:27:45 +00001301int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001302{
1303 return PyUnicodeEncodeError_GetEnd(exc, start);
1304}
1305
1306
Martin v. Löwis18e16552006-02-15 17:27:45 +00001307int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001308{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001309 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001310}
1311
1312
Martin v. Löwis18e16552006-02-15 17:27:45 +00001313int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001314{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001315 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001316}
1317
1318
Martin v. Löwis18e16552006-02-15 17:27:45 +00001319int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001320{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001321 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001322}
1323
1324
1325PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1326{
1327 return get_string(exc, "reason");
1328}
1329
1330
1331PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1332{
1333 return get_string(exc, "reason");
1334}
1335
1336
1337PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1338{
1339 return get_string(exc, "reason");
1340}
1341
1342
1343int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1344{
1345 return set_string(exc, "reason", reason);
1346}
1347
1348
1349int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1350{
1351 return set_string(exc, "reason", reason);
1352}
1353
1354
1355int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1356{
1357 return set_string(exc, "reason", reason);
1358}
1359
1360
1361static PyObject *
1362UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1363{
1364 PyObject *rtnval = NULL;
1365 PyObject *encoding;
1366 PyObject *object;
1367 PyObject *start;
1368 PyObject *end;
1369 PyObject *reason;
1370
1371 if (!(self = get_self(args)))
1372 return NULL;
1373
1374 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1375 return NULL;
1376
Brett Cannonbf364092006-03-01 04:25:17 +00001377 if (!set_args_and_message(self, args)) {
1378 Py_DECREF(args);
1379 return NULL;
1380 }
1381
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001382 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1383 &PyString_Type, &encoding,
1384 objecttype, &object,
1385 &PyInt_Type, &start,
1386 &PyInt_Type, &end,
1387 &PyString_Type, &reason))
Walter Dörwalde98147a2003-08-14 20:59:07 +00001388 goto finally;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001389
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001390 if (PyObject_SetAttrString(self, "encoding", encoding))
1391 goto finally;
1392 if (PyObject_SetAttrString(self, "object", object))
1393 goto finally;
1394 if (PyObject_SetAttrString(self, "start", start))
1395 goto finally;
1396 if (PyObject_SetAttrString(self, "end", end))
1397 goto finally;
1398 if (PyObject_SetAttrString(self, "reason", reason))
1399 goto finally;
1400
1401 Py_INCREF(Py_None);
1402 rtnval = Py_None;
1403
1404 finally:
1405 Py_DECREF(args);
1406 return rtnval;
1407}
1408
1409
1410static PyObject *
1411UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1412{
1413 return UnicodeError__init__(self, args, &PyUnicode_Type);
1414}
1415
1416static PyObject *
1417UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1418{
1419 PyObject *encodingObj = NULL;
1420 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001421 Py_ssize_t start;
1422 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001423 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001424 PyObject *result = NULL;
1425
1426 self = arg;
1427
1428 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1429 goto error;
1430
1431 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1432 goto error;
1433
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001434 if (PyUnicodeEncodeError_GetStart(self, &start))
1435 goto error;
1436
1437 if (PyUnicodeEncodeError_GetEnd(self, &end))
1438 goto error;
1439
1440 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1441 goto error;
1442
1443 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001444 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001445 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001446 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001447 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001448 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001449 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001450 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001451 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1452 result = PyString_FromFormat(
1453 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001454 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001455 badchar_str,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001456 start,
1457 PyString_AS_STRING(reasonObj)
1458 );
1459 }
1460 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001461 result = PyString_FromFormat(
1462 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001463 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001464 start,
1465 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001466 PyString_AS_STRING(reasonObj)
1467 );
1468 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001469
1470error:
1471 Py_XDECREF(reasonObj);
1472 Py_XDECREF(objectObj);
1473 Py_XDECREF(encodingObj);
1474 return result;
1475}
1476
1477static PyMethodDef UnicodeEncodeError_methods[] = {
1478 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1479 {"__str__", UnicodeEncodeError__str__, METH_O},
1480 {NULL, NULL}
1481};
1482
1483
1484PyObject * PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001485 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1486 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001487{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001488 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001489 encoding, object, length, start, end, reason);
1490}
1491
1492
1493static PyObject *
1494UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1495{
1496 return UnicodeError__init__(self, args, &PyString_Type);
1497}
1498
1499static PyObject *
1500UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1501{
1502 PyObject *encodingObj = NULL;
1503 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001504 Py_ssize_t start;
1505 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001506 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001507 PyObject *result = NULL;
1508
1509 self = arg;
1510
1511 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1512 goto error;
1513
1514 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1515 goto error;
1516
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001517 if (PyUnicodeDecodeError_GetStart(self, &start))
1518 goto error;
1519
1520 if (PyUnicodeDecodeError_GetEnd(self, &end))
1521 goto error;
1522
1523 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1524 goto error;
1525
1526 if (end==start+1) {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001527 /* FromFormat does not support %02x, so format that separately */
1528 char byte[4];
1529 PyOS_snprintf(byte, sizeof(byte), "%02x",
1530 ((int)PyString_AS_STRING(objectObj)[start])&0xff);
1531 result = PyString_FromFormat(
1532 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001533 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001534 byte,
1535 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001536 PyString_AS_STRING(reasonObj)
1537 );
1538 }
1539 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001540 result = PyString_FromFormat(
1541 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001542 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001543 start,
1544 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001545 PyString_AS_STRING(reasonObj)
1546 );
1547 }
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001548
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001549
1550error:
1551 Py_XDECREF(reasonObj);
1552 Py_XDECREF(objectObj);
1553 Py_XDECREF(encodingObj);
1554 return result;
1555}
1556
1557static PyMethodDef UnicodeDecodeError_methods[] = {
1558 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1559 {"__str__", UnicodeDecodeError__str__, METH_O},
1560 {NULL, NULL}
1561};
1562
1563
1564PyObject * PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001565 const char *encoding, const char *object, Py_ssize_t length,
1566 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001567{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001568 assert(length < INT_MAX);
1569 assert(start < INT_MAX);
1570 assert(end < INT_MAX);
Martin v. Löwis5cb69362006-04-14 09:08:42 +00001571 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
1572 encoding, object, length, start, end, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001573}
1574
1575
1576static PyObject *
1577UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1578{
1579 PyObject *rtnval = NULL;
1580 PyObject *object;
1581 PyObject *start;
1582 PyObject *end;
1583 PyObject *reason;
1584
1585 if (!(self = get_self(args)))
1586 return NULL;
1587
1588 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1589 return NULL;
1590
Brett Cannonbf364092006-03-01 04:25:17 +00001591 if (!set_args_and_message(self, args)) {
1592 Py_DECREF(args);
1593 return NULL;
1594 }
1595
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001596 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1597 &PyUnicode_Type, &object,
1598 &PyInt_Type, &start,
1599 &PyInt_Type, &end,
1600 &PyString_Type, &reason))
1601 goto finally;
1602
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001603 if (PyObject_SetAttrString(self, "object", object))
1604 goto finally;
1605 if (PyObject_SetAttrString(self, "start", start))
1606 goto finally;
1607 if (PyObject_SetAttrString(self, "end", end))
1608 goto finally;
1609 if (PyObject_SetAttrString(self, "reason", reason))
1610 goto finally;
1611
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001612 rtnval = Py_None;
Brett Cannonbf364092006-03-01 04:25:17 +00001613 Py_INCREF(rtnval);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001614
1615 finally:
1616 Py_DECREF(args);
1617 return rtnval;
1618}
1619
1620
1621static PyObject *
1622UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1623{
1624 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001625 Py_ssize_t start;
1626 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001627 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001628 PyObject *result = NULL;
1629
1630 self = arg;
1631
1632 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1633 goto error;
1634
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001635 if (PyUnicodeTranslateError_GetStart(self, &start))
1636 goto error;
1637
1638 if (PyUnicodeTranslateError_GetEnd(self, &end))
1639 goto error;
1640
1641 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1642 goto error;
1643
1644 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001645 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001646 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001647 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001648 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001649 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001650 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001651 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001652 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1653 result = PyString_FromFormat(
1654 "can't translate character u'\\%s' in position %zd: %.400s",
1655 badchar_str,
1656 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001657 PyString_AS_STRING(reasonObj)
1658 );
1659 }
1660 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001661 result = PyString_FromFormat(
1662 "can't translate characters in position %zd-%zd: %.400s",
1663 start,
1664 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001665 PyString_AS_STRING(reasonObj)
1666 );
1667 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001668
1669error:
1670 Py_XDECREF(reasonObj);
1671 Py_XDECREF(objectObj);
1672 return result;
1673}
1674
1675static PyMethodDef UnicodeTranslateError_methods[] = {
1676 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1677 {"__str__", UnicodeTranslateError__str__, METH_O},
1678 {NULL, NULL}
1679};
1680
1681
1682PyObject * PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001683 const Py_UNICODE *object, Py_ssize_t length,
1684 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001685{
Martin v. Löwis5cb69362006-04-14 09:08:42 +00001686 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001687 object, length, start, end, reason);
1688}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001689#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001690
1691
Barry Warsaw675ac282000-05-26 19:05:16 +00001692
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001693/* Exception doc strings */
1694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001695PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001696
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001697PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001698
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001699PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001700
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001701PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001702
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001703PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001704
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001705PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001706
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001707PyDoc_STRVAR(ZeroDivisionError__doc__,
1708"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001710PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001712PyDoc_STRVAR(ValueError__doc__,
1713"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001714
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001715PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001716
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001717#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001718PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1719
1720PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1721
1722PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001723#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001725PyDoc_STRVAR(SystemError__doc__,
1726"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001727\n\
1728Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001729the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001731PyDoc_STRVAR(ReferenceError__doc__,
1732"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001734PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001735
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001736PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001738PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001739
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001740/* Warning category docstrings */
1741
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001742PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001743
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001744PyDoc_STRVAR(UserWarning__doc__,
1745"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001747PyDoc_STRVAR(DeprecationWarning__doc__,
1748"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001749
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001750PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001751"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001752"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001753
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001754PyDoc_STRVAR(SyntaxWarning__doc__,
1755"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001757PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001758"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001760PyDoc_STRVAR(RuntimeWarning__doc__,
1761"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001762
Barry Warsaw9f007392002-08-14 15:51:29 +00001763PyDoc_STRVAR(FutureWarning__doc__,
1764"Base class for warnings about constructs that will change semantically "
1765"in the future.");
1766
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001767PyDoc_STRVAR(ImportWarning__doc__,
1768"Base class for warnings about probable mistakes in module imports");
Barry Warsaw675ac282000-05-26 19:05:16 +00001769
1770
1771/* module global functions */
1772static PyMethodDef functions[] = {
1773 /* Sentinel */
1774 {NULL, NULL}
1775};
1776
1777
1778
1779/* Global C API defined exceptions */
1780
Brett Cannonbf364092006-03-01 04:25:17 +00001781PyObject *PyExc_BaseException;
Barry Warsaw675ac282000-05-26 19:05:16 +00001782PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001783PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001784PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001785PyObject *PyExc_StandardError;
1786PyObject *PyExc_ArithmeticError;
1787PyObject *PyExc_LookupError;
1788
1789PyObject *PyExc_AssertionError;
1790PyObject *PyExc_AttributeError;
1791PyObject *PyExc_EOFError;
1792PyObject *PyExc_FloatingPointError;
1793PyObject *PyExc_EnvironmentError;
1794PyObject *PyExc_IOError;
1795PyObject *PyExc_OSError;
1796PyObject *PyExc_ImportError;
1797PyObject *PyExc_IndexError;
1798PyObject *PyExc_KeyError;
1799PyObject *PyExc_KeyboardInterrupt;
1800PyObject *PyExc_MemoryError;
1801PyObject *PyExc_NameError;
1802PyObject *PyExc_OverflowError;
1803PyObject *PyExc_RuntimeError;
1804PyObject *PyExc_NotImplementedError;
1805PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001806PyObject *PyExc_IndentationError;
1807PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001808PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001809PyObject *PyExc_SystemError;
1810PyObject *PyExc_SystemExit;
1811PyObject *PyExc_UnboundLocalError;
1812PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001813PyObject *PyExc_UnicodeEncodeError;
1814PyObject *PyExc_UnicodeDecodeError;
1815PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001816PyObject *PyExc_TypeError;
1817PyObject *PyExc_ValueError;
1818PyObject *PyExc_ZeroDivisionError;
1819#ifdef MS_WINDOWS
1820PyObject *PyExc_WindowsError;
1821#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001822#ifdef __VMS
1823PyObject *PyExc_VMSError;
1824#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001825
1826/* Pre-computed MemoryError instance. Best to create this as early as
Brett Cannonbf364092006-03-01 04:25:17 +00001827 * possible and not wait until a MemoryError is actually raised!
Barry Warsaw675ac282000-05-26 19:05:16 +00001828 */
1829PyObject *PyExc_MemoryErrorInst;
1830
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001831/* Predefined warning categories */
1832PyObject *PyExc_Warning;
1833PyObject *PyExc_UserWarning;
1834PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001835PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001836PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001837/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001838PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001839PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001840PyObject *PyExc_FutureWarning;
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001841PyObject *PyExc_ImportWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001842
Barry Warsaw675ac282000-05-26 19:05:16 +00001843
1844
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001845/* mapping between exception names and their PyObject ** */
1846static struct {
1847 char *name;
1848 PyObject **exc;
1849 PyObject **base; /* NULL == PyExc_StandardError */
1850 char *docstr;
1851 PyMethodDef *methods;
1852 int (*classinit)(PyObject *);
1853} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001854 /*
Brett Cannonbf364092006-03-01 04:25:17 +00001855 * The first four classes MUST appear in exactly this order
Barry Warsaw675ac282000-05-26 19:05:16 +00001856 */
Brett Cannonbf364092006-03-01 04:25:17 +00001857 {"BaseException", &PyExc_BaseException},
1858 {"Exception", &PyExc_Exception, &PyExc_BaseException, Exception__doc__},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001859 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1860 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001861 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1862 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001863 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1864 StandardError__doc__},
1865 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1866 /*
1867 * The rest appear in depth-first order of the hierarchy
1868 */
Brett Cannonbf364092006-03-01 04:25:17 +00001869 {"SystemExit", &PyExc_SystemExit, &PyExc_BaseException, SystemExit__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +00001870 SystemExit_methods},
Brett Cannonbf364092006-03-01 04:25:17 +00001871 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, &PyExc_BaseException,
1872 KeyboardInterrupt__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001873 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1874 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1875 EnvironmentError_methods},
1876 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1877 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1878#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001879 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Martin v. Löwis879768d2006-05-11 13:28:43 +00001880WindowsError__doc__, WindowsError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001881#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001882#ifdef __VMS
1883 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1884 VMSError__doc__},
1885#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001886 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1887 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1888 {"NotImplementedError", &PyExc_NotImplementedError,
1889 &PyExc_RuntimeError, NotImplementedError__doc__},
1890 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1891 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1892 UnboundLocalError__doc__},
1893 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1894 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1895 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001896 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1897 IndentationError__doc__},
1898 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1899 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001900 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1901 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1902 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1903 IndexError__doc__},
1904 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001905 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001906 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1907 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1908 OverflowError__doc__},
1909 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1910 ZeroDivisionError__doc__},
1911 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1912 FloatingPointError__doc__},
1913 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1914 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001915#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001916 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1917 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1918 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1919 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1920 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1921 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001922#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001923 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001924 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1925 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001926 /* Warning categories */
1927 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1928 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1929 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1930 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001931 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1932 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001933 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001934 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001935 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1936 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001937 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1938 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001939 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1940 FutureWarning__doc__},
Thomas Wouters9df4e6f2006-04-27 23:13:20 +00001941 {"ImportWarning", &PyExc_ImportWarning, &PyExc_Warning,
1942 ImportWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001943 /* Sentinel */
1944 {NULL}
1945};
1946
1947
1948
Mark Hammonda2905272002-07-29 13:42:14 +00001949void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001950_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001951{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001952 char *modulename = "exceptions";
Martin v. Löwis18e16552006-02-15 17:27:45 +00001953 Py_ssize_t modnamesz = strlen(modulename);
Barry Warsaw675ac282000-05-26 19:05:16 +00001954 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001955 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001956
Tim Peters6d6c1a32001-08-02 04:15:00 +00001957 me = Py_InitModule(modulename, functions);
1958 if (me == NULL)
1959 goto err;
1960 mydict = PyModule_GetDict(me);
1961 if (mydict == NULL)
1962 goto err;
1963 bltinmod = PyImport_ImportModule("__builtin__");
1964 if (bltinmod == NULL)
1965 goto err;
1966 bdict = PyModule_GetDict(bltinmod);
1967 if (bdict == NULL)
1968 goto err;
1969 doc = PyString_FromString(module__doc__);
1970 if (doc == NULL)
1971 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001972
Tim Peters6d6c1a32001-08-02 04:15:00 +00001973 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001974 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001975 if (i < 0) {
1976 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001977 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001978 return;
1979 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001980
1981 /* This is the base class of all exceptions, so make it first. */
Brett Cannonbf364092006-03-01 04:25:17 +00001982 if (make_BaseException(modulename) ||
1983 PyDict_SetItemString(mydict, "BaseException", PyExc_BaseException) ||
1984 PyDict_SetItemString(bdict, "BaseException", PyExc_BaseException))
Barry Warsaw675ac282000-05-26 19:05:16 +00001985 {
Brett Cannonbf364092006-03-01 04:25:17 +00001986 Py_FatalError("Base class `BaseException' could not be created.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001987 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001988
Barry Warsaw675ac282000-05-26 19:05:16 +00001989 /* Now we can programmatically create all the remaining exceptions.
1990 * Remember to start the loop at 1 to skip Exceptions.
1991 */
1992 for (i=1; exctable[i].name; i++) {
1993 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001994 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1995 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001996
1997 (void)strcpy(cname, modulename);
1998 (void)strcat(cname, ".");
1999 (void)strcat(cname, exctable[i].name);
2000
2001 if (exctable[i].base == 0)
2002 base = PyExc_StandardError;
2003 else
2004 base = *exctable[i].base;
2005
2006 status = make_class(exctable[i].exc, base, cname,
2007 exctable[i].methods,
2008 exctable[i].docstr);
2009
2010 PyMem_DEL(cname);
2011
2012 if (status)
2013 Py_FatalError("Standard exception classes could not be created.");
2014
2015 if (exctable[i].classinit) {
2016 status = (*exctable[i].classinit)(*exctable[i].exc);
2017 if (status)
2018 Py_FatalError("An exception class could not be initialized.");
2019 }
2020
2021 /* Now insert the class into both this module and the __builtin__
2022 * module.
2023 */
2024 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
2025 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
2026 {
2027 Py_FatalError("Module dictionary insertion problem.");
2028 }
2029 }
2030
2031 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002032 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00002033 if (!args ||
2034 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
2035 {
2036 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
2037 }
2038 Py_DECREF(args);
2039
2040 /* We're done with __builtin__ */
2041 Py_DECREF(bltinmod);
2042}
2043
2044
Mark Hammonda2905272002-07-29 13:42:14 +00002045void
Tim Peters6d6c1a32001-08-02 04:15:00 +00002046_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00002047{
2048 int i;
2049
2050 Py_XDECREF(PyExc_MemoryErrorInst);
2051 PyExc_MemoryErrorInst = NULL;
2052
2053 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00002054 /* clear the class's dictionary, freeing up circular references
2055 * between the class and its methods.
2056 */
2057 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
2058 PyDict_Clear(cdict);
2059 Py_DECREF(cdict);
2060
2061 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00002062 Py_XDECREF(*exctable[i].exc);
2063 *exctable[i].exc = NULL;
2064 }
2065}