blob: 59f209176675593f257d4378a19fada837cfd230 [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 *
10 * history:
11 * 98-08-19 fl created (for pyexe)
12 * 00-02-08 fl updated for 1.5.2
13 * 26-May-2000 baw vetted for Python 1.6
Brett Cannonbf364092006-03-01 04:25:17 +000014 * XXX
Barry Warsaw675ac282000-05-26 19:05:16 +000015 *
16 * written by Fredrik Lundh
17 * modifications, additions, cleanups, and proofreading by Barry Warsaw
18 *
19 * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
20 */
21
22#include "Python.h"
Fred Drake185a29b2000-08-15 16:20:36 +000023#include "osdefs.h"
Barry Warsaw675ac282000-05-26 19:05:16 +000024
Tim Petersbf26e072000-07-12 04:02:10 +000025/* Caution: MS Visual C++ 6 errors if a single string literal exceeds
26 * 2Kb. So the module docstring has been broken roughly in half, using
27 * compile-time literal concatenation.
28 */
Skip Montanaro995895f2002-03-28 20:57:51 +000029
30/* NOTE: If the exception class hierarchy changes, don't forget to update
31 * Doc/lib/libexcs.tex!
32 */
33
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000034PyDoc_STRVAR(module__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +000035"Python's standard exception class hierarchy.\n\
36\n\
Brett Cannonbf364092006-03-01 04:25:17 +000037Exceptions found here are defined both in the exceptions module and the \n\
38built-in namespace. It is recommended that user-defined exceptions inherit \n\
39from Exception.\n\
40"
41
Tim Petersbf26e072000-07-12 04:02:10 +000042 /* keep string pieces "small" */
Brett Cannonbf364092006-03-01 04:25:17 +000043/* XXX exception hierarchy from Lib/test/exception_hierarchy.txt */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000044);
Barry Warsaw675ac282000-05-26 19:05:16 +000045
46
47/* Helper function for populating a dictionary with method wrappers. */
48static int
Brett Cannonbf364092006-03-01 04:25:17 +000049populate_methods(PyObject *klass, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +000050{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000051 PyObject *module;
52 int status = -1;
53
Barry Warsaw675ac282000-05-26 19:05:16 +000054 if (!methods)
55 return 0;
56
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000057 module = PyString_FromString("exceptions");
58 if (!module)
59 return 0;
Barry Warsaw675ac282000-05-26 19:05:16 +000060 while (methods->ml_name) {
61 /* get a wrapper for the built-in function */
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000062 PyObject *func = PyCFunction_NewEx(methods, NULL, module);
Thomas Wouters0452d1f2000-07-22 18:45:06 +000063 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +000064
65 if (!func)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000066 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000067
68 /* turn the function into an unbound method */
69 if (!(meth = PyMethod_New(func, NULL, klass))) {
70 Py_DECREF(func);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000071 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000072 }
Barry Warsaw9667ed22001-01-23 16:08:34 +000073
Barry Warsaw675ac282000-05-26 19:05:16 +000074 /* add method to dictionary */
Brett Cannonbf364092006-03-01 04:25:17 +000075 status = PyObject_SetAttrString(klass, methods->ml_name, meth);
Barry Warsaw675ac282000-05-26 19:05:16 +000076 Py_DECREF(meth);
77 Py_DECREF(func);
78
79 /* stop now if an error occurred, otherwise do the next method */
80 if (status)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000081 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000082
83 methods++;
84 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000085 status = 0;
86 status:
87 Py_DECREF(module);
88 return status;
Barry Warsaw675ac282000-05-26 19:05:16 +000089}
90
Barry Warsaw9667ed22001-01-23 16:08:34 +000091
Barry Warsaw675ac282000-05-26 19:05:16 +000092
93/* This function is used to create all subsequent exception classes. */
94static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +000095make_class(PyObject **klass, PyObject *base,
96 char *name, PyMethodDef *methods,
97 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +000098{
Thomas Wouters0452d1f2000-07-22 18:45:06 +000099 PyObject *dict = PyDict_New();
100 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000101 int status = -1;
102
103 if (!dict)
104 return -1;
105
106 /* If an error occurs from here on, goto finally instead of explicitly
107 * returning NULL.
108 */
109
110 if (docstr) {
111 if (!(str = PyString_FromString(docstr)))
112 goto finally;
113 if (PyDict_SetItemString(dict, "__doc__", str))
114 goto finally;
115 }
116
117 if (!(*klass = PyErr_NewException(name, base, dict)))
118 goto finally;
119
Brett Cannonbf364092006-03-01 04:25:17 +0000120 if (populate_methods(*klass, methods)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000121 Py_DECREF(*klass);
122 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000123 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000124 }
125
126 status = 0;
127
128 finally:
129 Py_XDECREF(dict);
130 Py_XDECREF(str);
131 return status;
132}
133
134
135/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000136static PyObject *
137get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000138{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000139 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000140 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000141 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000142 if (PyExc_TypeError) {
143 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000144 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000145 }
146 return NULL;
147 }
148 return self;
149}
150
151
152
153/* Notes on bootstrapping the exception classes.
154 *
155 * First thing we create is the base class for all exceptions, called
Brett Cannonbf364092006-03-01 04:25:17 +0000156 * appropriately BaseException. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000157 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000158 * for TypeError, which can conditionally exist.
159 *
Brett Cannonbf364092006-03-01 04:25:17 +0000160 * Next, Exception is created since it is the common subclass for the rest of
161 * the needed exceptions for this bootstrapping to work. StandardError is
162 * created (which is quite simple) followed by
Barry Warsaw675ac282000-05-26 19:05:16 +0000163 * TypeError, because the instantiation of other exceptions can potentially
164 * throw a TypeError. Once these exceptions are created, all the others
165 * can be created in any order. See the static exctable below for the
166 * explicit bootstrap order.
167 *
Brett Cannonbf364092006-03-01 04:25:17 +0000168 * All classes after BaseException can be created using PyErr_NewException().
Barry Warsaw675ac282000-05-26 19:05:16 +0000169 */
170
Brett Cannonbf364092006-03-01 04:25:17 +0000171PyDoc_STRVAR(BaseException__doc__, "Common base class for all exceptions");
Barry Warsaw675ac282000-05-26 19:05:16 +0000172
Brett Cannonbf364092006-03-01 04:25:17 +0000173/*
174 Set args and message attributes.
175
176 Assumes self and args have already been set properly with set_self, etc.
177*/
178static int
179set_args_and_message(PyObject *self, PyObject *args)
180{
181 PyObject *message_val;
182 Py_ssize_t args_len = PySequence_Length(args);
183
184 if (args_len < 0)
185 return 0;
186
187 /* set args */
188 if (PyObject_SetAttrString(self, "args", args) < 0)
189 return 0;
190
191 /* set message */
192 if (args_len == 1)
193 message_val = PySequence_GetItem(args, 0);
194 else
195 message_val = PyString_FromString("");
196 if (!message_val)
197 return 0;
198
199 if (PyObject_SetAttrString(self, "message", message_val) < 0) {
200 Py_DECREF(message_val);
201 return 0;
202 }
203
204 Py_DECREF(message_val);
205 return 1;
206}
Barry Warsaw675ac282000-05-26 19:05:16 +0000207
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000208static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000209BaseException__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000210{
Barry Warsaw675ac282000-05-26 19:05:16 +0000211 if (!(self = get_self(args)))
212 return NULL;
213
Brett Cannonbf364092006-03-01 04:25:17 +0000214 /* set args and message attribute */
215 args = PySequence_GetSlice(args, 1, PySequence_Length(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000216 if (!args)
217 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000218
Brett Cannonbf364092006-03-01 04:25:17 +0000219 if (!set_args_and_message(self, args)) {
220 Py_DECREF(args);
221 return NULL;
222 }
223
224 Py_DECREF(args);
225 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000226}
227
228
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000229static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000230BaseException__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000231{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000232 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000233
Fred Drake1aba5772000-08-15 15:46:16 +0000234 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000235 return NULL;
236
237 args = PyObject_GetAttrString(self, "args");
238 if (!args)
239 return NULL;
240
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000241 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000242 case 0:
243 out = PyString_FromString("");
244 break;
245 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000246 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000247 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000248 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000249 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000250 Py_DECREF(tmp);
251 }
252 else
253 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000254 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000255 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000256 case -1:
257 PyErr_Clear();
258 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000259 default:
260 out = PyObject_Str(args);
261 break;
262 }
263
264 Py_DECREF(args);
265 return out;
266}
267
Brett Cannonbf364092006-03-01 04:25:17 +0000268#ifdef Py_USING_UNICODE
269static PyObject *
270BaseException__unicode__(PyObject *self, PyObject *args)
271{
272 Py_ssize_t args_len;
273
274 if (!PyArg_ParseTuple(args, "O:__unicode__", &self))
275 return NULL;
276
277 args = PyObject_GetAttrString(self, "args");
278 if (!args)
279 return NULL;
280
281 args_len = PySequence_Size(args);
282 if (args_len < 0) {
283 Py_DECREF(args);
284 return NULL;
285 }
286
287 if (args_len == 0) {
288 Py_DECREF(args);
289 return PyUnicode_FromUnicode(NULL, 0);
290 }
291 else if (args_len == 1) {
292 PyObject *temp = PySequence_GetItem(args, 0);
293 if (!temp) {
294 Py_DECREF(args);
295 return NULL;
296 }
297 Py_DECREF(args);
298 return PyObject_Unicode(temp);
299 }
300 else {
301 Py_DECREF(args);
302 return PyObject_Unicode(args);
303 }
304}
305#endif /* Py_USING_UNICODE */
Barry Warsaw675ac282000-05-26 19:05:16 +0000306
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000307static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000308BaseException__repr__(PyObject *self, PyObject *args)
309{
310 PyObject *args_attr;
311 Py_ssize_t args_len;
312 PyObject *repr_suffix;
313 PyObject *repr;
314
315 if (!PyArg_ParseTuple(args, "O:__repr__", &self))
316 return NULL;
317
318 args_attr = PyObject_GetAttrString(self, "args");
319 if (!args_attr)
320 return NULL;
321
322 args_len = PySequence_Length(args_attr);
323 if (args_len < 0) {
324 Py_DECREF(args_attr);
325 return NULL;
326 }
327
328 if (args_len == 0) {
329 Py_DECREF(args_attr);
330 repr_suffix = PyString_FromString("()");
331 if (!repr_suffix)
332 return NULL;
333 }
334 else {
335 PyObject *args_repr;
336 /*PyObject *right_paren;
337
338 repr_suffix = PyString_FromString("(*");
339 if (!repr_suffix) {
340 Py_DECREF(args_attr);
341 return NULL;
342 }*/
343
344 args_repr = PyObject_Repr(args_attr);
345 Py_DECREF(args_attr);
346 if (!args_repr)
347 return NULL;
348
349 repr_suffix = args_repr;
350
351 /*PyString_ConcatAndDel(&repr_suffix, args_repr);
352 if (!repr_suffix)
353 return NULL;
354
355 right_paren = PyString_FromString(")");
356 if (!right_paren) {
357 Py_DECREF(repr_suffix);
358 return NULL;
359 }
360
361 PyString_ConcatAndDel(&repr_suffix, right_paren);
362 if (!repr_suffix)
363 return NULL;*/
364 }
365
366 repr = PyString_FromString(self->ob_type->tp_name);
367 if (!repr) {
368 Py_DECREF(repr_suffix);
369 return NULL;
370 }
371
372 PyString_ConcatAndDel(&repr, repr_suffix);
373 return repr;
374}
375
376static PyObject *
377BaseException__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000378{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000379 PyObject *out;
380 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000381
Fred Drake1aba5772000-08-15 15:46:16 +0000382 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000383 return NULL;
384
385 args = PyObject_GetAttrString(self, "args");
386 if (!args)
387 return NULL;
388
389 out = PyObject_GetItem(args, index);
390 Py_DECREF(args);
391 return out;
392}
393
394
395static PyMethodDef
Brett Cannonbf364092006-03-01 04:25:17 +0000396BaseException_methods[] = {
397 /* methods for the BaseException class */
398 {"__getitem__", BaseException__getitem__, METH_VARARGS},
399 {"__repr__", BaseException__repr__, METH_VARARGS},
400 {"__str__", BaseException__str__, METH_VARARGS},
401#ifdef Py_USING_UNICODE
402 {"__unicode__", BaseException__unicode__, METH_VARARGS},
403#endif /* Py_USING_UNICODE */
404 {"__init__", BaseException__init__, METH_VARARGS},
405 {NULL, NULL }
Barry Warsaw675ac282000-05-26 19:05:16 +0000406};
407
408
409static int
Brett Cannonbf364092006-03-01 04:25:17 +0000410make_BaseException(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000411{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000412 PyObject *dict = PyDict_New();
413 PyObject *str = NULL;
414 PyObject *name = NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000415 PyObject *emptytuple = NULL;
416 PyObject *argstuple = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000417 int status = -1;
418
419 if (!dict)
420 return -1;
421
422 /* If an error occurs from here on, goto finally instead of explicitly
423 * returning NULL.
424 */
425
426 if (!(str = PyString_FromString(modulename)))
427 goto finally;
428 if (PyDict_SetItemString(dict, "__module__", str))
429 goto finally;
430 Py_DECREF(str);
Brett Cannonbf364092006-03-01 04:25:17 +0000431
432 if (!(str = PyString_FromString(BaseException__doc__)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000433 goto finally;
434 if (PyDict_SetItemString(dict, "__doc__", str))
435 goto finally;
436
Brett Cannonbf364092006-03-01 04:25:17 +0000437 if (!(name = PyString_FromString("BaseException")))
Barry Warsaw675ac282000-05-26 19:05:16 +0000438 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000439
Brett Cannonbf364092006-03-01 04:25:17 +0000440 if (!(emptytuple = PyTuple_New(0)))
441 goto finally;
442
443 if (!(argstuple = PyTuple_Pack(3, name, emptytuple, dict)))
444 goto finally;
445
446 if (!(PyExc_BaseException = PyType_Type.tp_new(&PyType_Type, argstuple,
447 NULL)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000448 goto finally;
449
450 /* Now populate the dictionary with the method suite */
Brett Cannonbf364092006-03-01 04:25:17 +0000451 if (populate_methods(PyExc_BaseException, BaseException_methods))
452 /* Don't need to reclaim PyExc_BaseException here because that'll
Barry Warsaw675ac282000-05-26 19:05:16 +0000453 * happen during interpreter shutdown.
454 */
455 goto finally;
456
457 status = 0;
458
459 finally:
460 Py_XDECREF(dict);
461 Py_XDECREF(str);
462 Py_XDECREF(name);
Brett Cannonbf364092006-03-01 04:25:17 +0000463 Py_XDECREF(emptytuple);
464 Py_XDECREF(argstuple);
Barry Warsaw675ac282000-05-26 19:05:16 +0000465 return status;
466}
467
468
469
Brett Cannonbf364092006-03-01 04:25:17 +0000470PyDoc_STRVAR(Exception__doc__, "Common base class for all non-exit exceptions.");
471
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000472PyDoc_STRVAR(StandardError__doc__,
Brett Cannonbf364092006-03-01 04:25:17 +0000473"Base class for all standard Python exceptions that do not represent"
474"interpreter exiting.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000475
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000476PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000479PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000480
Barry Warsaw675ac282000-05-26 19:05:16 +0000481
482
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000483PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000484
485
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000486static PyObject *
487SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000488{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000489 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000490 int status;
491
492 if (!(self = get_self(args)))
493 return NULL;
494
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000495 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000496 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000497
Brett Cannonbf364092006-03-01 04:25:17 +0000498 if (!set_args_and_message(self, args)) {
499 Py_DECREF(args);
500 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000501 }
502
503 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000504 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000505 case 0:
506 Py_INCREF(Py_None);
507 code = Py_None;
508 break;
509 case 1:
510 code = PySequence_GetItem(args, 0);
511 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000512 case -1:
513 PyErr_Clear();
514 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000515 default:
516 Py_INCREF(args);
517 code = args;
518 break;
519 }
520
521 status = PyObject_SetAttrString(self, "code", code);
522 Py_DECREF(code);
523 Py_DECREF(args);
524 if (status < 0)
525 return NULL;
526
Brett Cannonbf364092006-03-01 04:25:17 +0000527 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000528}
529
530
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000531static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000532 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000533 {NULL, NULL}
534};
535
536
537
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000538PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000539
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000540PyDoc_STRVAR(ImportError__doc__,
541"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000542
543
544
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000545PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000546
547
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000548static PyObject *
549EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000550{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000551 PyObject *item0 = NULL;
552 PyObject *item1 = NULL;
553 PyObject *item2 = NULL;
554 PyObject *subslice = NULL;
555 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000556
557 if (!(self = get_self(args)))
558 return NULL;
559
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000560 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000561 return NULL;
562
Brett Cannonbf364092006-03-01 04:25:17 +0000563 if (!set_args_and_message(self, args)) {
564 Py_DECREF(args);
565 return NULL;
566 }
567
568 if (PyObject_SetAttrString(self, "errno", Py_None) ||
Barry Warsaw675ac282000-05-26 19:05:16 +0000569 PyObject_SetAttrString(self, "strerror", Py_None) ||
570 PyObject_SetAttrString(self, "filename", Py_None))
571 {
572 goto finally;
573 }
574
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000575 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000576 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000577 /* Where a function has a single filename, such as open() or some
578 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
579 * called, giving a third argument which is the filename. But, so
580 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000581 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000582 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000583 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000584 * we hack args so that it only contains two items. This also
585 * means we need our own __str__() which prints out the filename
586 * when it was supplied.
587 */
588 item0 = PySequence_GetItem(args, 0);
589 item1 = PySequence_GetItem(args, 1);
590 item2 = PySequence_GetItem(args, 2);
591 if (!item0 || !item1 || !item2)
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 PyObject_SetAttrString(self, "filename", item2))
597 {
598 goto finally;
599 }
600
601 subslice = PySequence_GetSlice(args, 0, 2);
602 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
603 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000604 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000605
606 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000607 /* Used when PyErr_SetFromErrno() is called and no filename
608 * argument is given.
609 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000610 item0 = PySequence_GetItem(args, 0);
611 item1 = PySequence_GetItem(args, 1);
612 if (!item0 || !item1)
613 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000614
Barry Warsaw675ac282000-05-26 19:05:16 +0000615 if (PyObject_SetAttrString(self, "errno", item0) ||
616 PyObject_SetAttrString(self, "strerror", item1))
617 {
618 goto finally;
619 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000620 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000621
622 case -1:
623 PyErr_Clear();
624 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000625 }
626
627 Py_INCREF(Py_None);
628 rtnval = Py_None;
629
630 finally:
631 Py_DECREF(args);
632 Py_XDECREF(item0);
633 Py_XDECREF(item1);
634 Py_XDECREF(item2);
635 Py_XDECREF(subslice);
636 return rtnval;
637}
638
639
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000640static PyObject *
641EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000642{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000643 PyObject *originalself = self;
644 PyObject *filename;
645 PyObject *serrno;
646 PyObject *strerror;
647 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000648
Fred Drake1aba5772000-08-15 15:46:16 +0000649 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000650 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000651
Barry Warsaw675ac282000-05-26 19:05:16 +0000652 filename = PyObject_GetAttrString(self, "filename");
653 serrno = PyObject_GetAttrString(self, "errno");
654 strerror = PyObject_GetAttrString(self, "strerror");
655 if (!filename || !serrno || !strerror)
656 goto finally;
657
658 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000659 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
660 PyObject *repr = PyObject_Repr(filename);
661 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000662
663 if (!fmt || !repr || !tuple) {
664 Py_XDECREF(fmt);
665 Py_XDECREF(repr);
666 Py_XDECREF(tuple);
667 goto finally;
668 }
669
670 PyTuple_SET_ITEM(tuple, 0, serrno);
671 PyTuple_SET_ITEM(tuple, 1, strerror);
672 PyTuple_SET_ITEM(tuple, 2, repr);
673
674 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 if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000683 PyObject *fmt = PyString_FromString("[Errno %s] %s");
684 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000685
686 if (!fmt || !tuple) {
687 Py_XDECREF(fmt);
688 Py_XDECREF(tuple);
689 goto finally;
690 }
691
692 PyTuple_SET_ITEM(tuple, 0, serrno);
693 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000694
Barry Warsaw675ac282000-05-26 19:05:16 +0000695 rtnval = PyString_Format(fmt, tuple);
696
697 Py_DECREF(fmt);
698 Py_DECREF(tuple);
699 /* already freed because tuple owned only reference */
700 serrno = NULL;
701 strerror = NULL;
702 }
703 else
704 /* The original Python code said:
705 *
706 * return StandardError.__str__(self)
707 *
708 * but there is no StandardError__str__() function; we happen to
Brett Cannonbf364092006-03-01 04:25:17 +0000709 * know that's just a pass through to BaseException__str__().
Barry Warsaw675ac282000-05-26 19:05:16 +0000710 */
Brett Cannonbf364092006-03-01 04:25:17 +0000711 rtnval = BaseException__str__(originalself, args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000712
713 finally:
714 Py_XDECREF(filename);
715 Py_XDECREF(serrno);
716 Py_XDECREF(strerror);
717 return rtnval;
718}
719
720
721static
722PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000723 {"__init__", EnvironmentError__init__, METH_VARARGS},
724 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000725 {NULL, NULL}
726};
727
728
729
730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000731PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000732
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000733PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000734
735#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000736PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000737#endif /* MS_WINDOWS */
738
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000739#ifdef __VMS
740static char
741VMSError__doc__[] = "OpenVMS OS system call failed.";
742#endif
743
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000744PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000746PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000748PyDoc_STRVAR(NotImplementedError__doc__,
749"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000751PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000752
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000753PyDoc_STRVAR(UnboundLocalError__doc__,
754"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000756PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000757
758
759
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000760PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000761
762
763static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000764SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000765{
Barry Warsaw87bec352000-08-18 05:05:37 +0000766 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000767 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000768
769 /* Additional class-creation time initializations */
770 if (!emptystring ||
771 PyObject_SetAttrString(klass, "msg", emptystring) ||
772 PyObject_SetAttrString(klass, "filename", Py_None) ||
773 PyObject_SetAttrString(klass, "lineno", Py_None) ||
774 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000775 PyObject_SetAttrString(klass, "text", Py_None) ||
776 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000777 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000778 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000779 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000780 Py_XDECREF(emptystring);
781 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000782}
783
784
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000785static PyObject *
786SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000787{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000788 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000789 int lenargs;
790
791 if (!(self = get_self(args)))
792 return NULL;
793
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000794 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000795 return NULL;
796
Brett Cannonbf364092006-03-01 04:25:17 +0000797 if (!set_args_and_message(self, args)) {
798 Py_DECREF(args);
799 return NULL;
800 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000801
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000802 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000803 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000804 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000805 int status;
806
807 if (!item0)
808 goto finally;
809 status = PyObject_SetAttrString(self, "msg", item0);
810 Py_DECREF(item0);
811 if (status)
812 goto finally;
813 }
814 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000815 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000816 PyObject *filename = NULL, *lineno = NULL;
817 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000818 int status = 1;
819
820 if (!info)
821 goto finally;
822
823 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000824 if (filename != NULL) {
825 lineno = PySequence_GetItem(info, 1);
826 if (lineno != NULL) {
827 offset = PySequence_GetItem(info, 2);
828 if (offset != NULL) {
829 text = PySequence_GetItem(info, 3);
830 if (text != NULL) {
831 status =
832 PyObject_SetAttrString(self, "filename", filename)
833 || PyObject_SetAttrString(self, "lineno", lineno)
834 || PyObject_SetAttrString(self, "offset", offset)
835 || PyObject_SetAttrString(self, "text", text);
836 Py_DECREF(text);
837 }
838 Py_DECREF(offset);
839 }
840 Py_DECREF(lineno);
841 }
842 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000843 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000844 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000845
846 if (status)
847 goto finally;
848 }
849 Py_INCREF(Py_None);
850 rtnval = Py_None;
851
852 finally:
853 Py_DECREF(args);
854 return rtnval;
855}
856
857
Fred Drake185a29b2000-08-15 16:20:36 +0000858/* This is called "my_basename" instead of just "basename" to avoid name
859 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
860 defined, and Python does define that. */
861static char *
862my_basename(char *name)
863{
864 char *cp = name;
865 char *result = name;
866
867 if (name == NULL)
868 return "???";
869 while (*cp != '\0') {
870 if (*cp == SEP)
871 result = cp + 1;
872 ++cp;
873 }
874 return result;
875}
876
877
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000878static PyObject *
879SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000880{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000881 PyObject *msg;
882 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000883 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000884
Fred Drake1aba5772000-08-15 15:46:16 +0000885 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000886 return NULL;
887
888 if (!(msg = PyObject_GetAttrString(self, "msg")))
889 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000890
Barry Warsaw675ac282000-05-26 19:05:16 +0000891 str = PyObject_Str(msg);
892 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000893 result = str;
894
895 /* XXX -- do all the additional formatting with filename and
896 lineno here */
897
Guido van Rossum602d4512002-09-03 20:24:09 +0000898 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000899 int have_filename = 0;
900 int have_lineno = 0;
901 char *buffer = NULL;
902
Barry Warsaw77c9f502000-08-16 19:43:17 +0000903 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000904 have_filename = PyString_Check(filename);
905 else
906 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000907
908 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000909 have_lineno = PyInt_Check(lineno);
910 else
911 PyErr_Clear();
912
913 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000914 int bufsize = PyString_GET_SIZE(str) + 64;
915 if (have_filename)
916 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000917
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000918 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000919 if (buffer != NULL) {
920 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000921 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
922 PyString_AS_STRING(str),
923 my_basename(PyString_AS_STRING(filename)),
924 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000925 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000926 PyOS_snprintf(buffer, bufsize, "%s (%s)",
927 PyString_AS_STRING(str),
928 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000929 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000930 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
931 PyString_AS_STRING(str),
932 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000933
Fred Drake1aba5772000-08-15 15:46:16 +0000934 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000935 PyMem_FREE(buffer);
936
Fred Drake1aba5772000-08-15 15:46:16 +0000937 if (result == NULL)
938 result = str;
939 else
940 Py_DECREF(str);
941 }
942 }
943 Py_XDECREF(filename);
944 Py_XDECREF(lineno);
945 }
946 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000947}
948
949
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000950static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000951 {"__init__", SyntaxError__init__, METH_VARARGS},
952 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000953 {NULL, NULL}
954};
955
956
Guido van Rossum602d4512002-09-03 20:24:09 +0000957static PyObject *
958KeyError__str__(PyObject *self, PyObject *args)
959{
960 PyObject *argsattr;
961 PyObject *result;
962
963 if (!PyArg_ParseTuple(args, "O:__str__", &self))
964 return NULL;
965
Brett Cannonbf364092006-03-01 04:25:17 +0000966 argsattr = PyObject_GetAttrString(self, "args");
967 if (!argsattr)
968 return NULL;
Guido van Rossum602d4512002-09-03 20:24:09 +0000969
970 /* If args is a tuple of exactly one item, apply repr to args[0].
971 This is done so that e.g. the exception raised by {}[''] prints
972 KeyError: ''
973 rather than the confusing
974 KeyError
975 alone. The downside is that if KeyError is raised with an explanatory
976 string, that string will be displayed in quotes. Too bad.
Brett Cannonbf364092006-03-01 04:25:17 +0000977 If args is anything else, use the default BaseException__str__().
Guido van Rossum602d4512002-09-03 20:24:09 +0000978 */
979 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
980 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
981 result = PyObject_Repr(key);
982 }
983 else
Brett Cannonbf364092006-03-01 04:25:17 +0000984 result = BaseException__str__(self, args);
Guido van Rossum602d4512002-09-03 20:24:09 +0000985
986 Py_DECREF(argsattr);
987 return result;
988}
989
990static PyMethodDef KeyError_methods[] = {
991 {"__str__", KeyError__str__, METH_VARARGS},
992 {NULL, NULL}
993};
994
995
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000996#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000997static
Martin v. Löwis18e16552006-02-15 17:27:45 +0000998int get_int(PyObject *exc, const char *name, Py_ssize_t *value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000999{
1000 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1001
1002 if (!attr)
1003 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001004 if (PyInt_Check(attr)) {
1005 *value = PyInt_AS_LONG(attr);
1006 } else if (PyLong_Check(attr)) {
1007 *value = (size_t)PyLong_AsLongLong(attr);
1008 if (*value == -1) {
1009 Py_DECREF(attr);
1010 return -1;
1011 }
1012 } else {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001013 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001014 Py_DECREF(attr);
1015 return -1;
1016 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001017 Py_DECREF(attr);
1018 return 0;
1019}
1020
1021
1022static
Martin v. Löwis18e16552006-02-15 17:27:45 +00001023int set_ssize_t(PyObject *exc, const char *name, Py_ssize_t value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001024{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001025 PyObject *obj = PyInt_FromSsize_t(value);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001026 int result;
1027
1028 if (!obj)
1029 return -1;
1030 result = PyObject_SetAttrString(exc, (char *)name, obj);
1031 Py_DECREF(obj);
1032 return result;
1033}
1034
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001035static
1036PyObject *get_string(PyObject *exc, const char *name)
1037{
1038 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1039
1040 if (!attr)
1041 return NULL;
1042 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001043 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001044 Py_DECREF(attr);
1045 return NULL;
1046 }
1047 return attr;
1048}
1049
1050
1051static
1052int set_string(PyObject *exc, const char *name, const char *value)
1053{
1054 PyObject *obj = PyString_FromString(value);
1055 int result;
1056
1057 if (!obj)
1058 return -1;
1059 result = PyObject_SetAttrString(exc, (char *)name, obj);
1060 Py_DECREF(obj);
1061 return result;
1062}
1063
1064
1065static
1066PyObject *get_unicode(PyObject *exc, const char *name)
1067{
1068 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1069
1070 if (!attr)
1071 return NULL;
1072 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001073 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001074 Py_DECREF(attr);
1075 return NULL;
1076 }
1077 return attr;
1078}
1079
1080PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1081{
1082 return get_string(exc, "encoding");
1083}
1084
1085PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1086{
1087 return get_string(exc, "encoding");
1088}
1089
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001090PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
1091{
1092 return get_unicode(exc, "object");
1093}
1094
1095PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1096{
1097 return get_string(exc, "object");
1098}
1099
1100PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1101{
1102 return get_unicode(exc, "object");
1103}
1104
Martin v. Löwis18e16552006-02-15 17:27:45 +00001105int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001106{
1107 if (!get_int(exc, "start", start)) {
1108 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001109 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001110 if (!object)
1111 return -1;
1112 size = PyUnicode_GET_SIZE(object);
1113 if (*start<0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001114 *start = 0; /*XXX check for values <0*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001115 if (*start>=size)
1116 *start = size-1;
1117 Py_DECREF(object);
1118 return 0;
1119 }
1120 return -1;
1121}
1122
1123
Martin v. Löwis18e16552006-02-15 17:27:45 +00001124int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001125{
1126 if (!get_int(exc, "start", start)) {
1127 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001128 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001129 if (!object)
1130 return -1;
1131 size = PyString_GET_SIZE(object);
1132 if (*start<0)
1133 *start = 0;
1134 if (*start>=size)
1135 *start = size-1;
1136 Py_DECREF(object);
1137 return 0;
1138 }
1139 return -1;
1140}
1141
1142
Martin v. Löwis18e16552006-02-15 17:27:45 +00001143int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001144{
1145 return PyUnicodeEncodeError_GetStart(exc, start);
1146}
1147
1148
Martin v. Löwis18e16552006-02-15 17:27:45 +00001149int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001150{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001151 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001152}
1153
1154
Martin v. Löwis18e16552006-02-15 17:27:45 +00001155int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001156{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001157 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001158}
1159
1160
Martin v. Löwis18e16552006-02-15 17:27:45 +00001161int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001162{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001163 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001164}
1165
1166
Martin v. Löwis18e16552006-02-15 17:27:45 +00001167int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001168{
1169 if (!get_int(exc, "end", end)) {
1170 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001171 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001172 if (!object)
1173 return -1;
1174 size = PyUnicode_GET_SIZE(object);
1175 if (*end<1)
1176 *end = 1;
1177 if (*end>size)
1178 *end = size;
1179 Py_DECREF(object);
1180 return 0;
1181 }
1182 return -1;
1183}
1184
1185
Martin v. Löwis18e16552006-02-15 17:27:45 +00001186int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001187{
1188 if (!get_int(exc, "end", end)) {
1189 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001190 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001191 if (!object)
1192 return -1;
1193 size = PyString_GET_SIZE(object);
1194 if (*end<1)
1195 *end = 1;
1196 if (*end>size)
1197 *end = size;
1198 Py_DECREF(object);
1199 return 0;
1200 }
1201 return -1;
1202}
1203
1204
Martin v. Löwis18e16552006-02-15 17:27:45 +00001205int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001206{
1207 return PyUnicodeEncodeError_GetEnd(exc, start);
1208}
1209
1210
Martin v. Löwis18e16552006-02-15 17:27:45 +00001211int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001212{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001213 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001214}
1215
1216
Martin v. Löwis18e16552006-02-15 17:27:45 +00001217int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001218{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001219 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001220}
1221
1222
Martin v. Löwis18e16552006-02-15 17:27:45 +00001223int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001224{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001225 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001226}
1227
1228
1229PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1230{
1231 return get_string(exc, "reason");
1232}
1233
1234
1235PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1236{
1237 return get_string(exc, "reason");
1238}
1239
1240
1241PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1242{
1243 return get_string(exc, "reason");
1244}
1245
1246
1247int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1248{
1249 return set_string(exc, "reason", reason);
1250}
1251
1252
1253int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1254{
1255 return set_string(exc, "reason", reason);
1256}
1257
1258
1259int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1260{
1261 return set_string(exc, "reason", reason);
1262}
1263
1264
1265static PyObject *
1266UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1267{
1268 PyObject *rtnval = NULL;
1269 PyObject *encoding;
1270 PyObject *object;
1271 PyObject *start;
1272 PyObject *end;
1273 PyObject *reason;
1274
1275 if (!(self = get_self(args)))
1276 return NULL;
1277
1278 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1279 return NULL;
1280
Brett Cannonbf364092006-03-01 04:25:17 +00001281 if (!set_args_and_message(self, args)) {
1282 Py_DECREF(args);
1283 return NULL;
1284 }
1285
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001286 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1287 &PyString_Type, &encoding,
1288 objecttype, &object,
1289 &PyInt_Type, &start,
1290 &PyInt_Type, &end,
1291 &PyString_Type, &reason))
Walter Dörwalde98147a2003-08-14 20:59:07 +00001292 goto finally;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001293
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001294 if (PyObject_SetAttrString(self, "encoding", encoding))
1295 goto finally;
1296 if (PyObject_SetAttrString(self, "object", object))
1297 goto finally;
1298 if (PyObject_SetAttrString(self, "start", start))
1299 goto finally;
1300 if (PyObject_SetAttrString(self, "end", end))
1301 goto finally;
1302 if (PyObject_SetAttrString(self, "reason", reason))
1303 goto finally;
1304
1305 Py_INCREF(Py_None);
1306 rtnval = Py_None;
1307
1308 finally:
1309 Py_DECREF(args);
1310 return rtnval;
1311}
1312
1313
1314static PyObject *
1315UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1316{
1317 return UnicodeError__init__(self, args, &PyUnicode_Type);
1318}
1319
1320static PyObject *
1321UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1322{
1323 PyObject *encodingObj = NULL;
1324 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001325 Py_ssize_t start;
1326 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001327 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001328 PyObject *result = NULL;
1329
1330 self = arg;
1331
1332 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1333 goto error;
1334
1335 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1336 goto error;
1337
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001338 if (PyUnicodeEncodeError_GetStart(self, &start))
1339 goto error;
1340
1341 if (PyUnicodeEncodeError_GetEnd(self, &end))
1342 goto error;
1343
1344 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1345 goto error;
1346
1347 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001348 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001349 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001350 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001351 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001352 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001353 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001354 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001355 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1356 result = PyString_FromFormat(
1357 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001358 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001359 badchar_str,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001360 start,
1361 PyString_AS_STRING(reasonObj)
1362 );
1363 }
1364 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001365 result = PyString_FromFormat(
1366 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001367 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001368 start,
1369 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001370 PyString_AS_STRING(reasonObj)
1371 );
1372 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001373
1374error:
1375 Py_XDECREF(reasonObj);
1376 Py_XDECREF(objectObj);
1377 Py_XDECREF(encodingObj);
1378 return result;
1379}
1380
1381static PyMethodDef UnicodeEncodeError_methods[] = {
1382 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1383 {"__str__", UnicodeEncodeError__str__, METH_O},
1384 {NULL, NULL}
1385};
1386
1387
1388PyObject * PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001389 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1390 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001391{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001392 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001393 encoding, object, length, start, end, reason);
1394}
1395
1396
1397static PyObject *
1398UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1399{
1400 return UnicodeError__init__(self, args, &PyString_Type);
1401}
1402
1403static PyObject *
1404UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1405{
1406 PyObject *encodingObj = NULL;
1407 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001408 Py_ssize_t start;
1409 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001410 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001411 PyObject *result = NULL;
1412
1413 self = arg;
1414
1415 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1416 goto error;
1417
1418 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1419 goto error;
1420
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001421 if (PyUnicodeDecodeError_GetStart(self, &start))
1422 goto error;
1423
1424 if (PyUnicodeDecodeError_GetEnd(self, &end))
1425 goto error;
1426
1427 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1428 goto error;
1429
1430 if (end==start+1) {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001431 /* FromFormat does not support %02x, so format that separately */
1432 char byte[4];
1433 PyOS_snprintf(byte, sizeof(byte), "%02x",
1434 ((int)PyString_AS_STRING(objectObj)[start])&0xff);
1435 result = PyString_FromFormat(
1436 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001437 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001438 byte,
1439 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001440 PyString_AS_STRING(reasonObj)
1441 );
1442 }
1443 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001444 result = PyString_FromFormat(
1445 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001446 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001447 start,
1448 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001449 PyString_AS_STRING(reasonObj)
1450 );
1451 }
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001452
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001453
1454error:
1455 Py_XDECREF(reasonObj);
1456 Py_XDECREF(objectObj);
1457 Py_XDECREF(encodingObj);
1458 return result;
1459}
1460
1461static PyMethodDef UnicodeDecodeError_methods[] = {
1462 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1463 {"__str__", UnicodeDecodeError__str__, METH_O},
1464 {NULL, NULL}
1465};
1466
1467
1468PyObject * PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001469 const char *encoding, const char *object, Py_ssize_t length,
1470 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001471{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001472 assert(length < INT_MAX);
1473 assert(start < INT_MAX);
1474 assert(end < INT_MAX);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001475 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
Martin v. Löwis18e16552006-02-15 17:27:45 +00001476 encoding, object, (int)length, (int)start, (int)end, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001477}
1478
1479
1480static PyObject *
1481UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1482{
1483 PyObject *rtnval = NULL;
1484 PyObject *object;
1485 PyObject *start;
1486 PyObject *end;
1487 PyObject *reason;
1488
1489 if (!(self = get_self(args)))
1490 return NULL;
1491
1492 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1493 return NULL;
1494
Brett Cannonbf364092006-03-01 04:25:17 +00001495 if (!set_args_and_message(self, args)) {
1496 Py_DECREF(args);
1497 return NULL;
1498 }
1499
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001500 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1501 &PyUnicode_Type, &object,
1502 &PyInt_Type, &start,
1503 &PyInt_Type, &end,
1504 &PyString_Type, &reason))
1505 goto finally;
1506
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001507 if (PyObject_SetAttrString(self, "object", object))
1508 goto finally;
1509 if (PyObject_SetAttrString(self, "start", start))
1510 goto finally;
1511 if (PyObject_SetAttrString(self, "end", end))
1512 goto finally;
1513 if (PyObject_SetAttrString(self, "reason", reason))
1514 goto finally;
1515
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001516 rtnval = Py_None;
Brett Cannonbf364092006-03-01 04:25:17 +00001517 Py_INCREF(rtnval);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001518
1519 finally:
1520 Py_DECREF(args);
1521 return rtnval;
1522}
1523
1524
1525static PyObject *
1526UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1527{
1528 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001529 Py_ssize_t start;
1530 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001531 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001532 PyObject *result = NULL;
1533
1534 self = arg;
1535
1536 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1537 goto error;
1538
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001539 if (PyUnicodeTranslateError_GetStart(self, &start))
1540 goto error;
1541
1542 if (PyUnicodeTranslateError_GetEnd(self, &end))
1543 goto error;
1544
1545 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1546 goto error;
1547
1548 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001549 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001550 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001551 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001552 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001553 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001554 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001555 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001556 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1557 result = PyString_FromFormat(
1558 "can't translate character u'\\%s' in position %zd: %.400s",
1559 badchar_str,
1560 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001561 PyString_AS_STRING(reasonObj)
1562 );
1563 }
1564 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001565 result = PyString_FromFormat(
1566 "can't translate characters in position %zd-%zd: %.400s",
1567 start,
1568 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001569 PyString_AS_STRING(reasonObj)
1570 );
1571 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001572
1573error:
1574 Py_XDECREF(reasonObj);
1575 Py_XDECREF(objectObj);
1576 return result;
1577}
1578
1579static PyMethodDef UnicodeTranslateError_methods[] = {
1580 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1581 {"__str__", UnicodeTranslateError__str__, METH_O},
1582 {NULL, NULL}
1583};
1584
1585
1586PyObject * PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001587 const Py_UNICODE *object, Py_ssize_t length,
1588 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001589{
1590 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1591 object, length, start, end, reason);
1592}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001593#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001594
1595
Barry Warsaw675ac282000-05-26 19:05:16 +00001596
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001597/* Exception doc strings */
1598
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001599PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001600
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001601PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001602
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001603PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001604
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001605PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001607PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001609PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001610
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001611PyDoc_STRVAR(ZeroDivisionError__doc__,
1612"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001613
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001614PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001616PyDoc_STRVAR(ValueError__doc__,
1617"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001619PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001620
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001621#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001622PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1623
1624PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1625
1626PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001627#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001629PyDoc_STRVAR(SystemError__doc__,
1630"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001631\n\
1632Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001633the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001634
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001635PyDoc_STRVAR(ReferenceError__doc__,
1636"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001638PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001640PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001642PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001643
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001644/* Warning category docstrings */
1645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001646PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001648PyDoc_STRVAR(UserWarning__doc__,
1649"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001650
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001651PyDoc_STRVAR(DeprecationWarning__doc__,
1652"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001653
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001654PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001655"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001656"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001658PyDoc_STRVAR(SyntaxWarning__doc__,
1659"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001660
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001661PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001662"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001664PyDoc_STRVAR(RuntimeWarning__doc__,
1665"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001666
Barry Warsaw9f007392002-08-14 15:51:29 +00001667PyDoc_STRVAR(FutureWarning__doc__,
1668"Base class for warnings about constructs that will change semantically "
1669"in the future.");
1670
Barry Warsaw675ac282000-05-26 19:05:16 +00001671
1672
1673/* module global functions */
1674static PyMethodDef functions[] = {
1675 /* Sentinel */
1676 {NULL, NULL}
1677};
1678
1679
1680
1681/* Global C API defined exceptions */
1682
Brett Cannonbf364092006-03-01 04:25:17 +00001683PyObject *PyExc_BaseException;
Barry Warsaw675ac282000-05-26 19:05:16 +00001684PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001685PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001686PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001687PyObject *PyExc_StandardError;
1688PyObject *PyExc_ArithmeticError;
1689PyObject *PyExc_LookupError;
1690
1691PyObject *PyExc_AssertionError;
1692PyObject *PyExc_AttributeError;
1693PyObject *PyExc_EOFError;
1694PyObject *PyExc_FloatingPointError;
1695PyObject *PyExc_EnvironmentError;
1696PyObject *PyExc_IOError;
1697PyObject *PyExc_OSError;
1698PyObject *PyExc_ImportError;
1699PyObject *PyExc_IndexError;
1700PyObject *PyExc_KeyError;
1701PyObject *PyExc_KeyboardInterrupt;
1702PyObject *PyExc_MemoryError;
1703PyObject *PyExc_NameError;
1704PyObject *PyExc_OverflowError;
1705PyObject *PyExc_RuntimeError;
1706PyObject *PyExc_NotImplementedError;
1707PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001708PyObject *PyExc_IndentationError;
1709PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001710PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001711PyObject *PyExc_SystemError;
1712PyObject *PyExc_SystemExit;
1713PyObject *PyExc_UnboundLocalError;
1714PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001715PyObject *PyExc_UnicodeEncodeError;
1716PyObject *PyExc_UnicodeDecodeError;
1717PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001718PyObject *PyExc_TypeError;
1719PyObject *PyExc_ValueError;
1720PyObject *PyExc_ZeroDivisionError;
1721#ifdef MS_WINDOWS
1722PyObject *PyExc_WindowsError;
1723#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001724#ifdef __VMS
1725PyObject *PyExc_VMSError;
1726#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001727
1728/* Pre-computed MemoryError instance. Best to create this as early as
Brett Cannonbf364092006-03-01 04:25:17 +00001729 * possible and not wait until a MemoryError is actually raised!
Barry Warsaw675ac282000-05-26 19:05:16 +00001730 */
1731PyObject *PyExc_MemoryErrorInst;
1732
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001733/* Predefined warning categories */
1734PyObject *PyExc_Warning;
1735PyObject *PyExc_UserWarning;
1736PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001737PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001738PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001739/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001740PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001741PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001742PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001743
Barry Warsaw675ac282000-05-26 19:05:16 +00001744
1745
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001746/* mapping between exception names and their PyObject ** */
1747static struct {
1748 char *name;
1749 PyObject **exc;
1750 PyObject **base; /* NULL == PyExc_StandardError */
1751 char *docstr;
1752 PyMethodDef *methods;
1753 int (*classinit)(PyObject *);
1754} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001755 /*
Brett Cannonbf364092006-03-01 04:25:17 +00001756 * The first four classes MUST appear in exactly this order
Barry Warsaw675ac282000-05-26 19:05:16 +00001757 */
Brett Cannonbf364092006-03-01 04:25:17 +00001758 {"BaseException", &PyExc_BaseException},
1759 {"Exception", &PyExc_Exception, &PyExc_BaseException, Exception__doc__},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001760 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1761 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001762 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1763 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001764 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1765 StandardError__doc__},
1766 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1767 /*
1768 * The rest appear in depth-first order of the hierarchy
1769 */
Brett Cannonbf364092006-03-01 04:25:17 +00001770 {"SystemExit", &PyExc_SystemExit, &PyExc_BaseException, SystemExit__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +00001771 SystemExit_methods},
Brett Cannonbf364092006-03-01 04:25:17 +00001772 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, &PyExc_BaseException,
1773 KeyboardInterrupt__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001774 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1775 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1776 EnvironmentError_methods},
1777 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1778 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1779#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001780 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001781 WindowsError__doc__},
1782#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001783#ifdef __VMS
1784 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1785 VMSError__doc__},
1786#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001787 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1788 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1789 {"NotImplementedError", &PyExc_NotImplementedError,
1790 &PyExc_RuntimeError, NotImplementedError__doc__},
1791 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1792 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1793 UnboundLocalError__doc__},
1794 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1795 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1796 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001797 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1798 IndentationError__doc__},
1799 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1800 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001801 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1802 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1803 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1804 IndexError__doc__},
1805 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001806 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001807 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1808 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1809 OverflowError__doc__},
1810 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1811 ZeroDivisionError__doc__},
1812 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1813 FloatingPointError__doc__},
1814 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1815 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001816#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001817 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1818 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1819 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1820 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1821 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1822 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001823#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001824 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001825 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1826 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001827 /* Warning categories */
1828 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1829 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1830 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1831 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001832 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1833 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001834 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001835 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001836 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1837 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001838 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1839 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001840 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1841 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001842 /* Sentinel */
1843 {NULL}
1844};
1845
1846
1847
Mark Hammonda2905272002-07-29 13:42:14 +00001848void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001849_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001850{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001851 char *modulename = "exceptions";
Martin v. Löwis18e16552006-02-15 17:27:45 +00001852 Py_ssize_t modnamesz = strlen(modulename);
Barry Warsaw675ac282000-05-26 19:05:16 +00001853 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001854 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001855
Tim Peters6d6c1a32001-08-02 04:15:00 +00001856 me = Py_InitModule(modulename, functions);
1857 if (me == NULL)
1858 goto err;
1859 mydict = PyModule_GetDict(me);
1860 if (mydict == NULL)
1861 goto err;
1862 bltinmod = PyImport_ImportModule("__builtin__");
1863 if (bltinmod == NULL)
1864 goto err;
1865 bdict = PyModule_GetDict(bltinmod);
1866 if (bdict == NULL)
1867 goto err;
1868 doc = PyString_FromString(module__doc__);
1869 if (doc == NULL)
1870 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001871
Tim Peters6d6c1a32001-08-02 04:15:00 +00001872 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001873 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001874 if (i < 0) {
1875 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001876 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001877 return;
1878 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001879
1880 /* This is the base class of all exceptions, so make it first. */
Brett Cannonbf364092006-03-01 04:25:17 +00001881 if (make_BaseException(modulename) ||
1882 PyDict_SetItemString(mydict, "BaseException", PyExc_BaseException) ||
1883 PyDict_SetItemString(bdict, "BaseException", PyExc_BaseException))
Barry Warsaw675ac282000-05-26 19:05:16 +00001884 {
Brett Cannonbf364092006-03-01 04:25:17 +00001885 Py_FatalError("Base class `BaseException' could not be created.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001886 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001887
Barry Warsaw675ac282000-05-26 19:05:16 +00001888 /* Now we can programmatically create all the remaining exceptions.
1889 * Remember to start the loop at 1 to skip Exceptions.
1890 */
1891 for (i=1; exctable[i].name; i++) {
1892 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001893 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1894 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001895
1896 (void)strcpy(cname, modulename);
1897 (void)strcat(cname, ".");
1898 (void)strcat(cname, exctable[i].name);
1899
1900 if (exctable[i].base == 0)
1901 base = PyExc_StandardError;
1902 else
1903 base = *exctable[i].base;
1904
1905 status = make_class(exctable[i].exc, base, cname,
1906 exctable[i].methods,
1907 exctable[i].docstr);
1908
1909 PyMem_DEL(cname);
1910
1911 if (status)
1912 Py_FatalError("Standard exception classes could not be created.");
1913
1914 if (exctable[i].classinit) {
1915 status = (*exctable[i].classinit)(*exctable[i].exc);
1916 if (status)
1917 Py_FatalError("An exception class could not be initialized.");
1918 }
1919
1920 /* Now insert the class into both this module and the __builtin__
1921 * module.
1922 */
1923 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1924 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1925 {
1926 Py_FatalError("Module dictionary insertion problem.");
1927 }
1928 }
1929
1930 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001931 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00001932 if (!args ||
1933 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1934 {
1935 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1936 }
1937 Py_DECREF(args);
1938
1939 /* We're done with __builtin__ */
1940 Py_DECREF(bltinmod);
1941}
1942
1943
Mark Hammonda2905272002-07-29 13:42:14 +00001944void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001945_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001946{
1947 int i;
1948
1949 Py_XDECREF(PyExc_MemoryErrorInst);
1950 PyExc_MemoryErrorInst = NULL;
1951
1952 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001953 /* clear the class's dictionary, freeing up circular references
1954 * between the class and its methods.
1955 */
1956 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1957 PyDict_Clear(cdict);
1958 Py_DECREF(cdict);
1959
1960 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001961 Py_XDECREF(*exctable[i].exc);
1962 *exctable[i].exc = NULL;
1963 }
1964}