blob: c8c7b69d83364a8f7e49c38f0d92a51c92316152 [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
17#include "Python.h"
Fred Drake185a29b2000-08-15 16:20:36 +000018#include "osdefs.h"
Barry Warsaw675ac282000-05-26 19:05:16 +000019
Tim Petersbf26e072000-07-12 04:02:10 +000020/* Caution: MS Visual C++ 6 errors if a single string literal exceeds
21 * 2Kb. So the module docstring has been broken roughly in half, using
22 * compile-time literal concatenation.
23 */
Skip Montanaro995895f2002-03-28 20:57:51 +000024
25/* NOTE: If the exception class hierarchy changes, don't forget to update
26 * Doc/lib/libexcs.tex!
27 */
28
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029PyDoc_STRVAR(module__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +000030"Python's standard exception class hierarchy.\n\
31\n\
Brett Cannonbf364092006-03-01 04:25:17 +000032Exceptions found here are defined both in the exceptions module and the \n\
33built-in namespace. It is recommended that user-defined exceptions inherit \n\
Brett Cannon54ac2942006-03-01 22:10:49 +000034from Exception. See the documentation for the exception inheritance hierarchy.\n\
Brett Cannonbf364092006-03-01 04:25:17 +000035"
36
Tim Petersbf26e072000-07-12 04:02:10 +000037 /* keep string pieces "small" */
Brett Cannon54ac2942006-03-01 22:10:49 +000038/* XXX(bcannon): exception hierarchy in Lib/test/exception_hierarchy.txt */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039);
Barry Warsaw675ac282000-05-26 19:05:16 +000040
41
42/* Helper function for populating a dictionary with method wrappers. */
43static int
Brett Cannonbf364092006-03-01 04:25:17 +000044populate_methods(PyObject *klass, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +000045{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000046 PyObject *module;
47 int status = -1;
48
Barry Warsaw675ac282000-05-26 19:05:16 +000049 if (!methods)
50 return 0;
51
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000052 module = PyString_FromString("exceptions");
53 if (!module)
54 return 0;
Barry Warsaw675ac282000-05-26 19:05:16 +000055 while (methods->ml_name) {
56 /* get a wrapper for the built-in function */
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000057 PyObject *func = PyCFunction_NewEx(methods, NULL, module);
Thomas Wouters0452d1f2000-07-22 18:45:06 +000058 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +000059
60 if (!func)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000061 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000062
63 /* turn the function into an unbound method */
64 if (!(meth = PyMethod_New(func, NULL, klass))) {
65 Py_DECREF(func);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000066 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000067 }
Barry Warsaw9667ed22001-01-23 16:08:34 +000068
Barry Warsaw675ac282000-05-26 19:05:16 +000069 /* add method to dictionary */
Brett Cannonbf364092006-03-01 04:25:17 +000070 status = PyObject_SetAttrString(klass, methods->ml_name, meth);
Barry Warsaw675ac282000-05-26 19:05:16 +000071 Py_DECREF(meth);
72 Py_DECREF(func);
73
74 /* stop now if an error occurred, otherwise do the next method */
75 if (status)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000076 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000077
78 methods++;
79 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000080 status = 0;
81 status:
82 Py_DECREF(module);
83 return status;
Barry Warsaw675ac282000-05-26 19:05:16 +000084}
85
Barry Warsaw9667ed22001-01-23 16:08:34 +000086
Barry Warsaw675ac282000-05-26 19:05:16 +000087
88/* This function is used to create all subsequent exception classes. */
89static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +000090make_class(PyObject **klass, PyObject *base,
91 char *name, PyMethodDef *methods,
92 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +000093{
Thomas Wouters0452d1f2000-07-22 18:45:06 +000094 PyObject *dict = PyDict_New();
95 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +000096 int status = -1;
97
98 if (!dict)
99 return -1;
100
101 /* If an error occurs from here on, goto finally instead of explicitly
102 * returning NULL.
103 */
104
105 if (docstr) {
106 if (!(str = PyString_FromString(docstr)))
107 goto finally;
108 if (PyDict_SetItemString(dict, "__doc__", str))
109 goto finally;
110 }
111
112 if (!(*klass = PyErr_NewException(name, base, dict)))
113 goto finally;
114
Brett Cannonbf364092006-03-01 04:25:17 +0000115 if (populate_methods(*klass, methods)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000116 Py_DECREF(*klass);
117 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000118 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000119 }
120
121 status = 0;
122
123 finally:
124 Py_XDECREF(dict);
125 Py_XDECREF(str);
126 return status;
127}
128
129
130/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000131static PyObject *
132get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000133{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000134 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000135 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000136 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000137 if (PyExc_TypeError) {
138 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000139 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000140 }
141 return NULL;
142 }
143 return self;
144}
145
146
147
148/* Notes on bootstrapping the exception classes.
149 *
150 * First thing we create is the base class for all exceptions, called
Brett Cannonbf364092006-03-01 04:25:17 +0000151 * appropriately BaseException. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000152 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000153 * for TypeError, which can conditionally exist.
154 *
Brett Cannonbf364092006-03-01 04:25:17 +0000155 * Next, Exception is created since it is the common subclass for the rest of
156 * the needed exceptions for this bootstrapping to work. StandardError is
157 * created (which is quite simple) followed by
Barry Warsaw675ac282000-05-26 19:05:16 +0000158 * TypeError, because the instantiation of other exceptions can potentially
159 * throw a TypeError. Once these exceptions are created, all the others
160 * can be created in any order. See the static exctable below for the
161 * explicit bootstrap order.
162 *
Brett Cannonbf364092006-03-01 04:25:17 +0000163 * All classes after BaseException can be created using PyErr_NewException().
Barry Warsaw675ac282000-05-26 19:05:16 +0000164 */
165
Brett Cannonbf364092006-03-01 04:25:17 +0000166PyDoc_STRVAR(BaseException__doc__, "Common base class for all exceptions");
Barry Warsaw675ac282000-05-26 19:05:16 +0000167
Brett Cannonbf364092006-03-01 04:25:17 +0000168/*
169 Set args and message attributes.
170
171 Assumes self and args have already been set properly with set_self, etc.
172*/
173static int
174set_args_and_message(PyObject *self, PyObject *args)
175{
176 PyObject *message_val;
177 Py_ssize_t args_len = PySequence_Length(args);
178
179 if (args_len < 0)
180 return 0;
181
182 /* set args */
183 if (PyObject_SetAttrString(self, "args", args) < 0)
184 return 0;
185
186 /* set message */
187 if (args_len == 1)
188 message_val = PySequence_GetItem(args, 0);
189 else
190 message_val = PyString_FromString("");
191 if (!message_val)
192 return 0;
193
194 if (PyObject_SetAttrString(self, "message", message_val) < 0) {
195 Py_DECREF(message_val);
196 return 0;
197 }
198
199 Py_DECREF(message_val);
200 return 1;
201}
Barry Warsaw675ac282000-05-26 19:05:16 +0000202
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000203static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000204BaseException__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000205{
Barry Warsaw675ac282000-05-26 19:05:16 +0000206 if (!(self = get_self(args)))
207 return NULL;
208
Brett Cannonbf364092006-03-01 04:25:17 +0000209 /* set args and message attribute */
210 args = PySequence_GetSlice(args, 1, PySequence_Length(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000211 if (!args)
212 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000213
Brett Cannonbf364092006-03-01 04:25:17 +0000214 if (!set_args_and_message(self, args)) {
215 Py_DECREF(args);
216 return NULL;
217 }
218
219 Py_DECREF(args);
220 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000221}
222
223
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000224static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000225BaseException__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000226{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000227 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000228
Fred Drake1aba5772000-08-15 15:46:16 +0000229 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000230 return NULL;
231
232 args = PyObject_GetAttrString(self, "args");
233 if (!args)
234 return NULL;
235
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000236 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000237 case 0:
238 out = PyString_FromString("");
239 break;
240 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000241 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000242 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000243 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000244 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000245 Py_DECREF(tmp);
246 }
247 else
248 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000249 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000250 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000251 case -1:
252 PyErr_Clear();
253 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000254 default:
255 out = PyObject_Str(args);
256 break;
257 }
258
259 Py_DECREF(args);
260 return out;
261}
262
Brett Cannonbf364092006-03-01 04:25:17 +0000263#ifdef Py_USING_UNICODE
264static PyObject *
265BaseException__unicode__(PyObject *self, PyObject *args)
266{
267 Py_ssize_t args_len;
268
269 if (!PyArg_ParseTuple(args, "O:__unicode__", &self))
270 return NULL;
271
272 args = PyObject_GetAttrString(self, "args");
273 if (!args)
274 return NULL;
275
276 args_len = PySequence_Size(args);
277 if (args_len < 0) {
278 Py_DECREF(args);
279 return NULL;
280 }
281
282 if (args_len == 0) {
283 Py_DECREF(args);
284 return PyUnicode_FromUnicode(NULL, 0);
285 }
286 else if (args_len == 1) {
287 PyObject *temp = PySequence_GetItem(args, 0);
288 if (!temp) {
289 Py_DECREF(args);
290 return NULL;
291 }
292 Py_DECREF(args);
293 return PyObject_Unicode(temp);
294 }
295 else {
296 Py_DECREF(args);
297 return PyObject_Unicode(args);
298 }
299}
300#endif /* Py_USING_UNICODE */
Barry Warsaw675ac282000-05-26 19:05:16 +0000301
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000302static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000303BaseException__repr__(PyObject *self, PyObject *args)
304{
305 PyObject *args_attr;
306 Py_ssize_t args_len;
307 PyObject *repr_suffix;
308 PyObject *repr;
309
310 if (!PyArg_ParseTuple(args, "O:__repr__", &self))
311 return NULL;
312
313 args_attr = PyObject_GetAttrString(self, "args");
314 if (!args_attr)
315 return NULL;
316
317 args_len = PySequence_Length(args_attr);
318 if (args_len < 0) {
319 Py_DECREF(args_attr);
320 return NULL;
321 }
322
323 if (args_len == 0) {
324 Py_DECREF(args_attr);
325 repr_suffix = PyString_FromString("()");
326 if (!repr_suffix)
327 return NULL;
328 }
329 else {
330 PyObject *args_repr;
331 /*PyObject *right_paren;
332
333 repr_suffix = PyString_FromString("(*");
334 if (!repr_suffix) {
335 Py_DECREF(args_attr);
336 return NULL;
337 }*/
338
339 args_repr = PyObject_Repr(args_attr);
340 Py_DECREF(args_attr);
341 if (!args_repr)
342 return NULL;
343
344 repr_suffix = args_repr;
345
346 /*PyString_ConcatAndDel(&repr_suffix, args_repr);
347 if (!repr_suffix)
348 return NULL;
349
350 right_paren = PyString_FromString(")");
351 if (!right_paren) {
352 Py_DECREF(repr_suffix);
353 return NULL;
354 }
355
356 PyString_ConcatAndDel(&repr_suffix, right_paren);
357 if (!repr_suffix)
358 return NULL;*/
359 }
360
361 repr = PyString_FromString(self->ob_type->tp_name);
362 if (!repr) {
363 Py_DECREF(repr_suffix);
364 return NULL;
365 }
366
367 PyString_ConcatAndDel(&repr, repr_suffix);
368 return repr;
369}
370
371static PyObject *
372BaseException__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000373{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000374 PyObject *out;
375 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000376
Fred Drake1aba5772000-08-15 15:46:16 +0000377 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000378 return NULL;
379
380 args = PyObject_GetAttrString(self, "args");
381 if (!args)
382 return NULL;
383
384 out = PyObject_GetItem(args, index);
385 Py_DECREF(args);
386 return out;
387}
388
389
390static PyMethodDef
Brett Cannonbf364092006-03-01 04:25:17 +0000391BaseException_methods[] = {
392 /* methods for the BaseException class */
393 {"__getitem__", BaseException__getitem__, METH_VARARGS},
394 {"__repr__", BaseException__repr__, METH_VARARGS},
395 {"__str__", BaseException__str__, METH_VARARGS},
396#ifdef Py_USING_UNICODE
397 {"__unicode__", BaseException__unicode__, METH_VARARGS},
398#endif /* Py_USING_UNICODE */
399 {"__init__", BaseException__init__, METH_VARARGS},
400 {NULL, NULL }
Barry Warsaw675ac282000-05-26 19:05:16 +0000401};
402
403
404static int
Brett Cannonbf364092006-03-01 04:25:17 +0000405make_BaseException(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000406{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000407 PyObject *dict = PyDict_New();
408 PyObject *str = NULL;
409 PyObject *name = NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000410 PyObject *emptytuple = NULL;
411 PyObject *argstuple = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000412 int status = -1;
413
414 if (!dict)
415 return -1;
416
417 /* If an error occurs from here on, goto finally instead of explicitly
418 * returning NULL.
419 */
420
421 if (!(str = PyString_FromString(modulename)))
422 goto finally;
423 if (PyDict_SetItemString(dict, "__module__", str))
424 goto finally;
425 Py_DECREF(str);
Brett Cannonbf364092006-03-01 04:25:17 +0000426
427 if (!(str = PyString_FromString(BaseException__doc__)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000428 goto finally;
429 if (PyDict_SetItemString(dict, "__doc__", str))
430 goto finally;
431
Brett Cannonbf364092006-03-01 04:25:17 +0000432 if (!(name = PyString_FromString("BaseException")))
Barry Warsaw675ac282000-05-26 19:05:16 +0000433 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000434
Brett Cannonbf364092006-03-01 04:25:17 +0000435 if (!(emptytuple = PyTuple_New(0)))
436 goto finally;
437
438 if (!(argstuple = PyTuple_Pack(3, name, emptytuple, dict)))
439 goto finally;
440
441 if (!(PyExc_BaseException = PyType_Type.tp_new(&PyType_Type, argstuple,
442 NULL)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000443 goto finally;
444
445 /* Now populate the dictionary with the method suite */
Brett Cannonbf364092006-03-01 04:25:17 +0000446 if (populate_methods(PyExc_BaseException, BaseException_methods))
447 /* Don't need to reclaim PyExc_BaseException here because that'll
Barry Warsaw675ac282000-05-26 19:05:16 +0000448 * happen during interpreter shutdown.
449 */
450 goto finally;
451
452 status = 0;
453
454 finally:
455 Py_XDECREF(dict);
456 Py_XDECREF(str);
457 Py_XDECREF(name);
Brett Cannonbf364092006-03-01 04:25:17 +0000458 Py_XDECREF(emptytuple);
459 Py_XDECREF(argstuple);
Barry Warsaw675ac282000-05-26 19:05:16 +0000460 return status;
461}
462
463
464
Brett Cannonbf364092006-03-01 04:25:17 +0000465PyDoc_STRVAR(Exception__doc__, "Common base class for all non-exit exceptions.");
466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467PyDoc_STRVAR(StandardError__doc__,
Brett Cannonbf364092006-03-01 04:25:17 +0000468"Base class for all standard Python exceptions that do not represent"
469"interpreter exiting.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000470
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000471PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000472
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000473PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000474PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000475
Barry Warsaw675ac282000-05-26 19:05:16 +0000476
477
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000478PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000479
480
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000481static PyObject *
482SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000483{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000484 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000485 int status;
486
487 if (!(self = get_self(args)))
488 return NULL;
489
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000490 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000491 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000492
Brett Cannonbf364092006-03-01 04:25:17 +0000493 if (!set_args_and_message(self, args)) {
494 Py_DECREF(args);
495 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000496 }
497
498 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000499 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000500 case 0:
501 Py_INCREF(Py_None);
502 code = Py_None;
503 break;
504 case 1:
505 code = PySequence_GetItem(args, 0);
506 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000507 case -1:
508 PyErr_Clear();
509 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000510 default:
511 Py_INCREF(args);
512 code = args;
513 break;
514 }
515
516 status = PyObject_SetAttrString(self, "code", code);
517 Py_DECREF(code);
518 Py_DECREF(args);
519 if (status < 0)
520 return NULL;
521
Brett Cannonbf364092006-03-01 04:25:17 +0000522 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000523}
524
525
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000526static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000527 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000528 {NULL, NULL}
529};
530
531
532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000533PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000534
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000535PyDoc_STRVAR(ImportError__doc__,
536"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000537
538
539
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000540PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000541
542
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000543static PyObject *
544EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000545{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000546 PyObject *item0 = NULL;
547 PyObject *item1 = NULL;
548 PyObject *item2 = NULL;
549 PyObject *subslice = NULL;
550 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000551
552 if (!(self = get_self(args)))
553 return NULL;
554
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000555 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000556 return NULL;
557
Brett Cannonbf364092006-03-01 04:25:17 +0000558 if (!set_args_and_message(self, args)) {
559 Py_DECREF(args);
560 return NULL;
561 }
562
563 if (PyObject_SetAttrString(self, "errno", Py_None) ||
Barry Warsaw675ac282000-05-26 19:05:16 +0000564 PyObject_SetAttrString(self, "strerror", Py_None) ||
565 PyObject_SetAttrString(self, "filename", Py_None))
566 {
567 goto finally;
568 }
569
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000570 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000571 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000572 /* Where a function has a single filename, such as open() or some
573 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
574 * called, giving a third argument which is the filename. But, so
575 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000576 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000577 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000578 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000579 * we hack args so that it only contains two items. This also
580 * means we need our own __str__() which prints out the filename
581 * when it was supplied.
582 */
583 item0 = PySequence_GetItem(args, 0);
584 item1 = PySequence_GetItem(args, 1);
585 item2 = PySequence_GetItem(args, 2);
586 if (!item0 || !item1 || !item2)
587 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000588
Barry Warsaw675ac282000-05-26 19:05:16 +0000589 if (PyObject_SetAttrString(self, "errno", item0) ||
590 PyObject_SetAttrString(self, "strerror", item1) ||
591 PyObject_SetAttrString(self, "filename", item2))
592 {
593 goto finally;
594 }
595
596 subslice = PySequence_GetSlice(args, 0, 2);
597 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
598 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000599 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000600
601 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000602 /* Used when PyErr_SetFromErrno() is called and no filename
603 * argument is given.
604 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000605 item0 = PySequence_GetItem(args, 0);
606 item1 = PySequence_GetItem(args, 1);
607 if (!item0 || !item1)
608 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000609
Barry Warsaw675ac282000-05-26 19:05:16 +0000610 if (PyObject_SetAttrString(self, "errno", item0) ||
611 PyObject_SetAttrString(self, "strerror", item1))
612 {
613 goto finally;
614 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000615 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000616
617 case -1:
618 PyErr_Clear();
619 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000620 }
621
622 Py_INCREF(Py_None);
623 rtnval = Py_None;
624
625 finally:
626 Py_DECREF(args);
627 Py_XDECREF(item0);
628 Py_XDECREF(item1);
629 Py_XDECREF(item2);
630 Py_XDECREF(subslice);
631 return rtnval;
632}
633
634
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000635static PyObject *
636EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000637{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000638 PyObject *originalself = self;
639 PyObject *filename;
640 PyObject *serrno;
641 PyObject *strerror;
642 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000643
Fred Drake1aba5772000-08-15 15:46:16 +0000644 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000645 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000646
Barry Warsaw675ac282000-05-26 19:05:16 +0000647 filename = PyObject_GetAttrString(self, "filename");
648 serrno = PyObject_GetAttrString(self, "errno");
649 strerror = PyObject_GetAttrString(self, "strerror");
650 if (!filename || !serrno || !strerror)
651 goto finally;
652
653 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000654 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
655 PyObject *repr = PyObject_Repr(filename);
656 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000657
658 if (!fmt || !repr || !tuple) {
659 Py_XDECREF(fmt);
660 Py_XDECREF(repr);
661 Py_XDECREF(tuple);
662 goto finally;
663 }
664
665 PyTuple_SET_ITEM(tuple, 0, serrno);
666 PyTuple_SET_ITEM(tuple, 1, strerror);
667 PyTuple_SET_ITEM(tuple, 2, repr);
668
669 rtnval = PyString_Format(fmt, tuple);
670
671 Py_DECREF(fmt);
672 Py_DECREF(tuple);
673 /* already freed because tuple owned only reference */
674 serrno = NULL;
675 strerror = NULL;
676 }
677 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000678 PyObject *fmt = PyString_FromString("[Errno %s] %s");
679 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000680
681 if (!fmt || !tuple) {
682 Py_XDECREF(fmt);
683 Py_XDECREF(tuple);
684 goto finally;
685 }
686
687 PyTuple_SET_ITEM(tuple, 0, serrno);
688 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000689
Barry Warsaw675ac282000-05-26 19:05:16 +0000690 rtnval = PyString_Format(fmt, tuple);
691
692 Py_DECREF(fmt);
693 Py_DECREF(tuple);
694 /* already freed because tuple owned only reference */
695 serrno = NULL;
696 strerror = NULL;
697 }
698 else
699 /* The original Python code said:
700 *
701 * return StandardError.__str__(self)
702 *
703 * but there is no StandardError__str__() function; we happen to
Brett Cannonbf364092006-03-01 04:25:17 +0000704 * know that's just a pass through to BaseException__str__().
Barry Warsaw675ac282000-05-26 19:05:16 +0000705 */
Brett Cannonbf364092006-03-01 04:25:17 +0000706 rtnval = BaseException__str__(originalself, args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000707
708 finally:
709 Py_XDECREF(filename);
710 Py_XDECREF(serrno);
711 Py_XDECREF(strerror);
712 return rtnval;
713}
714
715
716static
717PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000718 {"__init__", EnvironmentError__init__, METH_VARARGS},
719 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000720 {NULL, NULL}
721};
722
723
724
725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000726PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000727
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000728PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000729
730#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000731PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000732#endif /* MS_WINDOWS */
733
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000734#ifdef __VMS
735static char
736VMSError__doc__[] = "OpenVMS OS system call failed.";
737#endif
738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000739PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000740
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000741PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000742
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000743PyDoc_STRVAR(NotImplementedError__doc__,
744"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000745
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000746PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000748PyDoc_STRVAR(UnboundLocalError__doc__,
749"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000750
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000751PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000752
753
754
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000755PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000756
757
758static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000759SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000760{
Barry Warsaw87bec352000-08-18 05:05:37 +0000761 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000762 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000763
764 /* Additional class-creation time initializations */
765 if (!emptystring ||
766 PyObject_SetAttrString(klass, "msg", emptystring) ||
767 PyObject_SetAttrString(klass, "filename", Py_None) ||
768 PyObject_SetAttrString(klass, "lineno", Py_None) ||
769 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000770 PyObject_SetAttrString(klass, "text", Py_None) ||
771 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000772 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000773 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000774 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000775 Py_XDECREF(emptystring);
776 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000777}
778
779
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000780static PyObject *
781SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000782{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000783 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000784 int lenargs;
785
786 if (!(self = get_self(args)))
787 return NULL;
788
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000789 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000790 return NULL;
791
Brett Cannonbf364092006-03-01 04:25:17 +0000792 if (!set_args_and_message(self, args)) {
793 Py_DECREF(args);
794 return NULL;
795 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000796
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000797 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000798 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000799 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000800 int status;
801
802 if (!item0)
803 goto finally;
804 status = PyObject_SetAttrString(self, "msg", item0);
805 Py_DECREF(item0);
806 if (status)
807 goto finally;
808 }
809 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000810 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000811 PyObject *filename = NULL, *lineno = NULL;
812 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000813 int status = 1;
814
815 if (!info)
816 goto finally;
817
818 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000819 if (filename != NULL) {
820 lineno = PySequence_GetItem(info, 1);
821 if (lineno != NULL) {
822 offset = PySequence_GetItem(info, 2);
823 if (offset != NULL) {
824 text = PySequence_GetItem(info, 3);
825 if (text != NULL) {
826 status =
827 PyObject_SetAttrString(self, "filename", filename)
828 || PyObject_SetAttrString(self, "lineno", lineno)
829 || PyObject_SetAttrString(self, "offset", offset)
830 || PyObject_SetAttrString(self, "text", text);
831 Py_DECREF(text);
832 }
833 Py_DECREF(offset);
834 }
835 Py_DECREF(lineno);
836 }
837 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000838 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000839 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000840
841 if (status)
842 goto finally;
843 }
844 Py_INCREF(Py_None);
845 rtnval = Py_None;
846
847 finally:
848 Py_DECREF(args);
849 return rtnval;
850}
851
852
Fred Drake185a29b2000-08-15 16:20:36 +0000853/* This is called "my_basename" instead of just "basename" to avoid name
854 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
855 defined, and Python does define that. */
856static char *
857my_basename(char *name)
858{
859 char *cp = name;
860 char *result = name;
861
862 if (name == NULL)
863 return "???";
864 while (*cp != '\0') {
865 if (*cp == SEP)
866 result = cp + 1;
867 ++cp;
868 }
869 return result;
870}
871
872
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000873static PyObject *
874SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000875{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000876 PyObject *msg;
877 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000878 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000879
Fred Drake1aba5772000-08-15 15:46:16 +0000880 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000881 return NULL;
882
883 if (!(msg = PyObject_GetAttrString(self, "msg")))
884 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000885
Barry Warsaw675ac282000-05-26 19:05:16 +0000886 str = PyObject_Str(msg);
887 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000888 result = str;
889
890 /* XXX -- do all the additional formatting with filename and
891 lineno here */
892
Guido van Rossum602d4512002-09-03 20:24:09 +0000893 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000894 int have_filename = 0;
895 int have_lineno = 0;
896 char *buffer = NULL;
897
Barry Warsaw77c9f502000-08-16 19:43:17 +0000898 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000899 have_filename = PyString_Check(filename);
900 else
901 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000902
903 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000904 have_lineno = PyInt_Check(lineno);
905 else
906 PyErr_Clear();
907
908 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000909 int bufsize = PyString_GET_SIZE(str) + 64;
910 if (have_filename)
911 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000912
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000913 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000914 if (buffer != NULL) {
915 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000916 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
917 PyString_AS_STRING(str),
918 my_basename(PyString_AS_STRING(filename)),
919 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000920 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000921 PyOS_snprintf(buffer, bufsize, "%s (%s)",
922 PyString_AS_STRING(str),
923 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000924 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000925 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
926 PyString_AS_STRING(str),
927 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000928
Fred Drake1aba5772000-08-15 15:46:16 +0000929 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000930 PyMem_FREE(buffer);
931
Fred Drake1aba5772000-08-15 15:46:16 +0000932 if (result == NULL)
933 result = str;
934 else
935 Py_DECREF(str);
936 }
937 }
938 Py_XDECREF(filename);
939 Py_XDECREF(lineno);
940 }
941 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000942}
943
944
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000945static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000946 {"__init__", SyntaxError__init__, METH_VARARGS},
947 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000948 {NULL, NULL}
949};
950
951
Guido van Rossum602d4512002-09-03 20:24:09 +0000952static PyObject *
953KeyError__str__(PyObject *self, PyObject *args)
954{
955 PyObject *argsattr;
956 PyObject *result;
957
958 if (!PyArg_ParseTuple(args, "O:__str__", &self))
959 return NULL;
960
Brett Cannonbf364092006-03-01 04:25:17 +0000961 argsattr = PyObject_GetAttrString(self, "args");
962 if (!argsattr)
963 return NULL;
Guido van Rossum602d4512002-09-03 20:24:09 +0000964
965 /* If args is a tuple of exactly one item, apply repr to args[0].
966 This is done so that e.g. the exception raised by {}[''] prints
967 KeyError: ''
968 rather than the confusing
969 KeyError
970 alone. The downside is that if KeyError is raised with an explanatory
971 string, that string will be displayed in quotes. Too bad.
Brett Cannonbf364092006-03-01 04:25:17 +0000972 If args is anything else, use the default BaseException__str__().
Guido van Rossum602d4512002-09-03 20:24:09 +0000973 */
974 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
975 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
976 result = PyObject_Repr(key);
977 }
978 else
Brett Cannonbf364092006-03-01 04:25:17 +0000979 result = BaseException__str__(self, args);
Guido van Rossum602d4512002-09-03 20:24:09 +0000980
981 Py_DECREF(argsattr);
982 return result;
983}
984
985static PyMethodDef KeyError_methods[] = {
986 {"__str__", KeyError__str__, METH_VARARGS},
987 {NULL, NULL}
988};
989
990
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000991#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000992static
Martin v. Löwis18e16552006-02-15 17:27:45 +0000993int get_int(PyObject *exc, const char *name, Py_ssize_t *value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000994{
995 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
996
997 if (!attr)
998 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000999 if (PyInt_Check(attr)) {
1000 *value = PyInt_AS_LONG(attr);
1001 } else if (PyLong_Check(attr)) {
1002 *value = (size_t)PyLong_AsLongLong(attr);
1003 if (*value == -1) {
1004 Py_DECREF(attr);
1005 return -1;
1006 }
1007 } else {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001008 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001009 Py_DECREF(attr);
1010 return -1;
1011 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001012 Py_DECREF(attr);
1013 return 0;
1014}
1015
1016
1017static
Martin v. Löwis18e16552006-02-15 17:27:45 +00001018int set_ssize_t(PyObject *exc, const char *name, Py_ssize_t value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001019{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001020 PyObject *obj = PyInt_FromSsize_t(value);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001021 int result;
1022
1023 if (!obj)
1024 return -1;
1025 result = PyObject_SetAttrString(exc, (char *)name, obj);
1026 Py_DECREF(obj);
1027 return result;
1028}
1029
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001030static
1031PyObject *get_string(PyObject *exc, const char *name)
1032{
1033 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1034
1035 if (!attr)
1036 return NULL;
1037 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001038 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001039 Py_DECREF(attr);
1040 return NULL;
1041 }
1042 return attr;
1043}
1044
1045
1046static
1047int set_string(PyObject *exc, const char *name, const char *value)
1048{
1049 PyObject *obj = PyString_FromString(value);
1050 int result;
1051
1052 if (!obj)
1053 return -1;
1054 result = PyObject_SetAttrString(exc, (char *)name, obj);
1055 Py_DECREF(obj);
1056 return result;
1057}
1058
1059
1060static
1061PyObject *get_unicode(PyObject *exc, const char *name)
1062{
1063 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1064
1065 if (!attr)
1066 return NULL;
1067 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001068 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001069 Py_DECREF(attr);
1070 return NULL;
1071 }
1072 return attr;
1073}
1074
1075PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1076{
1077 return get_string(exc, "encoding");
1078}
1079
1080PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1081{
1082 return get_string(exc, "encoding");
1083}
1084
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001085PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
1086{
1087 return get_unicode(exc, "object");
1088}
1089
1090PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1091{
1092 return get_string(exc, "object");
1093}
1094
1095PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1096{
1097 return get_unicode(exc, "object");
1098}
1099
Martin v. Löwis18e16552006-02-15 17:27:45 +00001100int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001101{
1102 if (!get_int(exc, "start", start)) {
1103 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001104 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001105 if (!object)
1106 return -1;
1107 size = PyUnicode_GET_SIZE(object);
1108 if (*start<0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001109 *start = 0; /*XXX check for values <0*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001110 if (*start>=size)
1111 *start = size-1;
1112 Py_DECREF(object);
1113 return 0;
1114 }
1115 return -1;
1116}
1117
1118
Martin v. Löwis18e16552006-02-15 17:27:45 +00001119int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001120{
1121 if (!get_int(exc, "start", start)) {
1122 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001123 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001124 if (!object)
1125 return -1;
1126 size = PyString_GET_SIZE(object);
1127 if (*start<0)
1128 *start = 0;
1129 if (*start>=size)
1130 *start = size-1;
1131 Py_DECREF(object);
1132 return 0;
1133 }
1134 return -1;
1135}
1136
1137
Martin v. Löwis18e16552006-02-15 17:27:45 +00001138int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001139{
1140 return PyUnicodeEncodeError_GetStart(exc, start);
1141}
1142
1143
Martin v. Löwis18e16552006-02-15 17:27:45 +00001144int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001145{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001146 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001147}
1148
1149
Martin v. Löwis18e16552006-02-15 17:27:45 +00001150int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001151{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001152 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001153}
1154
1155
Martin v. Löwis18e16552006-02-15 17:27:45 +00001156int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001157{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001158 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001159}
1160
1161
Martin v. Löwis18e16552006-02-15 17:27:45 +00001162int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001163{
1164 if (!get_int(exc, "end", end)) {
1165 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001166 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001167 if (!object)
1168 return -1;
1169 size = PyUnicode_GET_SIZE(object);
1170 if (*end<1)
1171 *end = 1;
1172 if (*end>size)
1173 *end = size;
1174 Py_DECREF(object);
1175 return 0;
1176 }
1177 return -1;
1178}
1179
1180
Martin v. Löwis18e16552006-02-15 17:27:45 +00001181int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001182{
1183 if (!get_int(exc, "end", end)) {
1184 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001185 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001186 if (!object)
1187 return -1;
1188 size = PyString_GET_SIZE(object);
1189 if (*end<1)
1190 *end = 1;
1191 if (*end>size)
1192 *end = size;
1193 Py_DECREF(object);
1194 return 0;
1195 }
1196 return -1;
1197}
1198
1199
Martin v. Löwis18e16552006-02-15 17:27:45 +00001200int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001201{
1202 return PyUnicodeEncodeError_GetEnd(exc, start);
1203}
1204
1205
Martin v. Löwis18e16552006-02-15 17:27:45 +00001206int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001207{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001208 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001209}
1210
1211
Martin v. Löwis18e16552006-02-15 17:27:45 +00001212int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001213{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001214 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001215}
1216
1217
Martin v. Löwis18e16552006-02-15 17:27:45 +00001218int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001219{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001220 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001221}
1222
1223
1224PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1225{
1226 return get_string(exc, "reason");
1227}
1228
1229
1230PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1231{
1232 return get_string(exc, "reason");
1233}
1234
1235
1236PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1237{
1238 return get_string(exc, "reason");
1239}
1240
1241
1242int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1243{
1244 return set_string(exc, "reason", reason);
1245}
1246
1247
1248int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1249{
1250 return set_string(exc, "reason", reason);
1251}
1252
1253
1254int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1255{
1256 return set_string(exc, "reason", reason);
1257}
1258
1259
1260static PyObject *
1261UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1262{
1263 PyObject *rtnval = NULL;
1264 PyObject *encoding;
1265 PyObject *object;
1266 PyObject *start;
1267 PyObject *end;
1268 PyObject *reason;
1269
1270 if (!(self = get_self(args)))
1271 return NULL;
1272
1273 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1274 return NULL;
1275
Brett Cannonbf364092006-03-01 04:25:17 +00001276 if (!set_args_and_message(self, args)) {
1277 Py_DECREF(args);
1278 return NULL;
1279 }
1280
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001281 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1282 &PyString_Type, &encoding,
1283 objecttype, &object,
1284 &PyInt_Type, &start,
1285 &PyInt_Type, &end,
1286 &PyString_Type, &reason))
Walter Dörwalde98147a2003-08-14 20:59:07 +00001287 goto finally;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001288
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001289 if (PyObject_SetAttrString(self, "encoding", encoding))
1290 goto finally;
1291 if (PyObject_SetAttrString(self, "object", object))
1292 goto finally;
1293 if (PyObject_SetAttrString(self, "start", start))
1294 goto finally;
1295 if (PyObject_SetAttrString(self, "end", end))
1296 goto finally;
1297 if (PyObject_SetAttrString(self, "reason", reason))
1298 goto finally;
1299
1300 Py_INCREF(Py_None);
1301 rtnval = Py_None;
1302
1303 finally:
1304 Py_DECREF(args);
1305 return rtnval;
1306}
1307
1308
1309static PyObject *
1310UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1311{
1312 return UnicodeError__init__(self, args, &PyUnicode_Type);
1313}
1314
1315static PyObject *
1316UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1317{
1318 PyObject *encodingObj = NULL;
1319 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001320 Py_ssize_t start;
1321 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001322 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001323 PyObject *result = NULL;
1324
1325 self = arg;
1326
1327 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1328 goto error;
1329
1330 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1331 goto error;
1332
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001333 if (PyUnicodeEncodeError_GetStart(self, &start))
1334 goto error;
1335
1336 if (PyUnicodeEncodeError_GetEnd(self, &end))
1337 goto error;
1338
1339 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1340 goto error;
1341
1342 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001343 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001344 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001345 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001346 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001347 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001348 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001349 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001350 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1351 result = PyString_FromFormat(
1352 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001353 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001354 badchar_str,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001355 start,
1356 PyString_AS_STRING(reasonObj)
1357 );
1358 }
1359 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001360 result = PyString_FromFormat(
1361 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001362 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001363 start,
1364 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001365 PyString_AS_STRING(reasonObj)
1366 );
1367 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001368
1369error:
1370 Py_XDECREF(reasonObj);
1371 Py_XDECREF(objectObj);
1372 Py_XDECREF(encodingObj);
1373 return result;
1374}
1375
1376static PyMethodDef UnicodeEncodeError_methods[] = {
1377 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1378 {"__str__", UnicodeEncodeError__str__, METH_O},
1379 {NULL, NULL}
1380};
1381
1382
1383PyObject * PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001384 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1385 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001386{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001387 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001388 encoding, object, length, start, end, reason);
1389}
1390
1391
1392static PyObject *
1393UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1394{
1395 return UnicodeError__init__(self, args, &PyString_Type);
1396}
1397
1398static PyObject *
1399UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1400{
1401 PyObject *encodingObj = NULL;
1402 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001403 Py_ssize_t start;
1404 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001405 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001406 PyObject *result = NULL;
1407
1408 self = arg;
1409
1410 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1411 goto error;
1412
1413 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1414 goto error;
1415
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001416 if (PyUnicodeDecodeError_GetStart(self, &start))
1417 goto error;
1418
1419 if (PyUnicodeDecodeError_GetEnd(self, &end))
1420 goto error;
1421
1422 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1423 goto error;
1424
1425 if (end==start+1) {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001426 /* FromFormat does not support %02x, so format that separately */
1427 char byte[4];
1428 PyOS_snprintf(byte, sizeof(byte), "%02x",
1429 ((int)PyString_AS_STRING(objectObj)[start])&0xff);
1430 result = PyString_FromFormat(
1431 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001432 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001433 byte,
1434 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001435 PyString_AS_STRING(reasonObj)
1436 );
1437 }
1438 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001439 result = PyString_FromFormat(
1440 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001441 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001442 start,
1443 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001444 PyString_AS_STRING(reasonObj)
1445 );
1446 }
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001447
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001448
1449error:
1450 Py_XDECREF(reasonObj);
1451 Py_XDECREF(objectObj);
1452 Py_XDECREF(encodingObj);
1453 return result;
1454}
1455
1456static PyMethodDef UnicodeDecodeError_methods[] = {
1457 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1458 {"__str__", UnicodeDecodeError__str__, METH_O},
1459 {NULL, NULL}
1460};
1461
1462
1463PyObject * PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001464 const char *encoding, const char *object, Py_ssize_t length,
1465 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001466{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001467 assert(length < INT_MAX);
1468 assert(start < INT_MAX);
1469 assert(end < INT_MAX);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001470 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
Martin v. Löwis18e16552006-02-15 17:27:45 +00001471 encoding, object, (int)length, (int)start, (int)end, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001472}
1473
1474
1475static PyObject *
1476UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1477{
1478 PyObject *rtnval = NULL;
1479 PyObject *object;
1480 PyObject *start;
1481 PyObject *end;
1482 PyObject *reason;
1483
1484 if (!(self = get_self(args)))
1485 return NULL;
1486
1487 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1488 return NULL;
1489
Brett Cannonbf364092006-03-01 04:25:17 +00001490 if (!set_args_and_message(self, args)) {
1491 Py_DECREF(args);
1492 return NULL;
1493 }
1494
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001495 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1496 &PyUnicode_Type, &object,
1497 &PyInt_Type, &start,
1498 &PyInt_Type, &end,
1499 &PyString_Type, &reason))
1500 goto finally;
1501
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001502 if (PyObject_SetAttrString(self, "object", object))
1503 goto finally;
1504 if (PyObject_SetAttrString(self, "start", start))
1505 goto finally;
1506 if (PyObject_SetAttrString(self, "end", end))
1507 goto finally;
1508 if (PyObject_SetAttrString(self, "reason", reason))
1509 goto finally;
1510
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001511 rtnval = Py_None;
Brett Cannonbf364092006-03-01 04:25:17 +00001512 Py_INCREF(rtnval);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001513
1514 finally:
1515 Py_DECREF(args);
1516 return rtnval;
1517}
1518
1519
1520static PyObject *
1521UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1522{
1523 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001524 Py_ssize_t start;
1525 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001526 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001527 PyObject *result = NULL;
1528
1529 self = arg;
1530
1531 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1532 goto error;
1533
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001534 if (PyUnicodeTranslateError_GetStart(self, &start))
1535 goto error;
1536
1537 if (PyUnicodeTranslateError_GetEnd(self, &end))
1538 goto error;
1539
1540 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1541 goto error;
1542
1543 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001544 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001545 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001546 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001547 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001548 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001549 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001550 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001551 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1552 result = PyString_FromFormat(
1553 "can't translate character u'\\%s' in position %zd: %.400s",
1554 badchar_str,
1555 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001556 PyString_AS_STRING(reasonObj)
1557 );
1558 }
1559 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001560 result = PyString_FromFormat(
1561 "can't translate characters in position %zd-%zd: %.400s",
1562 start,
1563 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001564 PyString_AS_STRING(reasonObj)
1565 );
1566 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001567
1568error:
1569 Py_XDECREF(reasonObj);
1570 Py_XDECREF(objectObj);
1571 return result;
1572}
1573
1574static PyMethodDef UnicodeTranslateError_methods[] = {
1575 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1576 {"__str__", UnicodeTranslateError__str__, METH_O},
1577 {NULL, NULL}
1578};
1579
1580
1581PyObject * PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001582 const Py_UNICODE *object, Py_ssize_t length,
1583 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001584{
1585 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1586 object, length, start, end, reason);
1587}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001588#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001589
1590
Barry Warsaw675ac282000-05-26 19:05:16 +00001591
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001592/* Exception doc strings */
1593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001594PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001595
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001596PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001599
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001600PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001602PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001604PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001606PyDoc_STRVAR(ZeroDivisionError__doc__,
1607"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001609PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001610
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001611PyDoc_STRVAR(ValueError__doc__,
1612"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001613
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001614PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001615
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001616#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001617PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1618
1619PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1620
1621PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001622#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624PyDoc_STRVAR(SystemError__doc__,
1625"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001626\n\
1627Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001628the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001630PyDoc_STRVAR(ReferenceError__doc__,
1631"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001632
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001633PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001634
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001635PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001636
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001637PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001638
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001639/* Warning category docstrings */
1640
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001641PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643PyDoc_STRVAR(UserWarning__doc__,
1644"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001646PyDoc_STRVAR(DeprecationWarning__doc__,
1647"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001649PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001650"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001651"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001652
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001653PyDoc_STRVAR(SyntaxWarning__doc__,
1654"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001655
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001656PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001657"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001658
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001659PyDoc_STRVAR(RuntimeWarning__doc__,
1660"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001661
Barry Warsaw9f007392002-08-14 15:51:29 +00001662PyDoc_STRVAR(FutureWarning__doc__,
1663"Base class for warnings about constructs that will change semantically "
1664"in the future.");
1665
Barry Warsaw675ac282000-05-26 19:05:16 +00001666
1667
1668/* module global functions */
1669static PyMethodDef functions[] = {
1670 /* Sentinel */
1671 {NULL, NULL}
1672};
1673
1674
1675
1676/* Global C API defined exceptions */
1677
Brett Cannonbf364092006-03-01 04:25:17 +00001678PyObject *PyExc_BaseException;
Barry Warsaw675ac282000-05-26 19:05:16 +00001679PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001680PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001681PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001682PyObject *PyExc_StandardError;
1683PyObject *PyExc_ArithmeticError;
1684PyObject *PyExc_LookupError;
1685
1686PyObject *PyExc_AssertionError;
1687PyObject *PyExc_AttributeError;
1688PyObject *PyExc_EOFError;
1689PyObject *PyExc_FloatingPointError;
1690PyObject *PyExc_EnvironmentError;
1691PyObject *PyExc_IOError;
1692PyObject *PyExc_OSError;
1693PyObject *PyExc_ImportError;
1694PyObject *PyExc_IndexError;
1695PyObject *PyExc_KeyError;
1696PyObject *PyExc_KeyboardInterrupt;
1697PyObject *PyExc_MemoryError;
1698PyObject *PyExc_NameError;
1699PyObject *PyExc_OverflowError;
1700PyObject *PyExc_RuntimeError;
1701PyObject *PyExc_NotImplementedError;
1702PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001703PyObject *PyExc_IndentationError;
1704PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001705PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001706PyObject *PyExc_SystemError;
1707PyObject *PyExc_SystemExit;
1708PyObject *PyExc_UnboundLocalError;
1709PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001710PyObject *PyExc_UnicodeEncodeError;
1711PyObject *PyExc_UnicodeDecodeError;
1712PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001713PyObject *PyExc_TypeError;
1714PyObject *PyExc_ValueError;
1715PyObject *PyExc_ZeroDivisionError;
1716#ifdef MS_WINDOWS
1717PyObject *PyExc_WindowsError;
1718#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001719#ifdef __VMS
1720PyObject *PyExc_VMSError;
1721#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001722
1723/* Pre-computed MemoryError instance. Best to create this as early as
Brett Cannonbf364092006-03-01 04:25:17 +00001724 * possible and not wait until a MemoryError is actually raised!
Barry Warsaw675ac282000-05-26 19:05:16 +00001725 */
1726PyObject *PyExc_MemoryErrorInst;
1727
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001728/* Predefined warning categories */
1729PyObject *PyExc_Warning;
1730PyObject *PyExc_UserWarning;
1731PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001732PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001733PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001734/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001735PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001736PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001737PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001738
Barry Warsaw675ac282000-05-26 19:05:16 +00001739
1740
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001741/* mapping between exception names and their PyObject ** */
1742static struct {
1743 char *name;
1744 PyObject **exc;
1745 PyObject **base; /* NULL == PyExc_StandardError */
1746 char *docstr;
1747 PyMethodDef *methods;
1748 int (*classinit)(PyObject *);
1749} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001750 /*
Brett Cannonbf364092006-03-01 04:25:17 +00001751 * The first four classes MUST appear in exactly this order
Barry Warsaw675ac282000-05-26 19:05:16 +00001752 */
Brett Cannonbf364092006-03-01 04:25:17 +00001753 {"BaseException", &PyExc_BaseException},
1754 {"Exception", &PyExc_Exception, &PyExc_BaseException, Exception__doc__},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001755 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1756 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001757 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1758 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001759 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1760 StandardError__doc__},
1761 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1762 /*
1763 * The rest appear in depth-first order of the hierarchy
1764 */
Brett Cannonbf364092006-03-01 04:25:17 +00001765 {"SystemExit", &PyExc_SystemExit, &PyExc_BaseException, SystemExit__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +00001766 SystemExit_methods},
Brett Cannonbf364092006-03-01 04:25:17 +00001767 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, &PyExc_BaseException,
1768 KeyboardInterrupt__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001769 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1770 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1771 EnvironmentError_methods},
1772 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1773 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1774#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001775 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001776 WindowsError__doc__},
1777#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001778#ifdef __VMS
1779 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1780 VMSError__doc__},
1781#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001782 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1783 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1784 {"NotImplementedError", &PyExc_NotImplementedError,
1785 &PyExc_RuntimeError, NotImplementedError__doc__},
1786 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1787 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1788 UnboundLocalError__doc__},
1789 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1790 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1791 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001792 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1793 IndentationError__doc__},
1794 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1795 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001796 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1797 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1798 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1799 IndexError__doc__},
1800 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001801 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001802 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1803 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1804 OverflowError__doc__},
1805 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1806 ZeroDivisionError__doc__},
1807 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1808 FloatingPointError__doc__},
1809 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1810 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001811#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001812 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1813 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1814 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1815 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1816 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1817 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001818#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001819 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001820 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1821 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001822 /* Warning categories */
1823 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1824 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1825 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1826 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001827 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1828 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001829 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001830 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001831 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1832 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001833 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1834 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001835 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1836 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001837 /* Sentinel */
1838 {NULL}
1839};
1840
1841
1842
Mark Hammonda2905272002-07-29 13:42:14 +00001843void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001844_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001845{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001846 char *modulename = "exceptions";
Martin v. Löwis18e16552006-02-15 17:27:45 +00001847 Py_ssize_t modnamesz = strlen(modulename);
Barry Warsaw675ac282000-05-26 19:05:16 +00001848 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001849 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001850
Tim Peters6d6c1a32001-08-02 04:15:00 +00001851 me = Py_InitModule(modulename, functions);
1852 if (me == NULL)
1853 goto err;
1854 mydict = PyModule_GetDict(me);
1855 if (mydict == NULL)
1856 goto err;
1857 bltinmod = PyImport_ImportModule("__builtin__");
1858 if (bltinmod == NULL)
1859 goto err;
1860 bdict = PyModule_GetDict(bltinmod);
1861 if (bdict == NULL)
1862 goto err;
1863 doc = PyString_FromString(module__doc__);
1864 if (doc == NULL)
1865 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001866
Tim Peters6d6c1a32001-08-02 04:15:00 +00001867 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001868 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001869 if (i < 0) {
1870 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001871 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001872 return;
1873 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001874
1875 /* This is the base class of all exceptions, so make it first. */
Brett Cannonbf364092006-03-01 04:25:17 +00001876 if (make_BaseException(modulename) ||
1877 PyDict_SetItemString(mydict, "BaseException", PyExc_BaseException) ||
1878 PyDict_SetItemString(bdict, "BaseException", PyExc_BaseException))
Barry Warsaw675ac282000-05-26 19:05:16 +00001879 {
Brett Cannonbf364092006-03-01 04:25:17 +00001880 Py_FatalError("Base class `BaseException' could not be created.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001881 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001882
Barry Warsaw675ac282000-05-26 19:05:16 +00001883 /* Now we can programmatically create all the remaining exceptions.
1884 * Remember to start the loop at 1 to skip Exceptions.
1885 */
1886 for (i=1; exctable[i].name; i++) {
1887 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001888 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1889 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001890
1891 (void)strcpy(cname, modulename);
1892 (void)strcat(cname, ".");
1893 (void)strcat(cname, exctable[i].name);
1894
1895 if (exctable[i].base == 0)
1896 base = PyExc_StandardError;
1897 else
1898 base = *exctable[i].base;
1899
1900 status = make_class(exctable[i].exc, base, cname,
1901 exctable[i].methods,
1902 exctable[i].docstr);
1903
1904 PyMem_DEL(cname);
1905
1906 if (status)
1907 Py_FatalError("Standard exception classes could not be created.");
1908
1909 if (exctable[i].classinit) {
1910 status = (*exctable[i].classinit)(*exctable[i].exc);
1911 if (status)
1912 Py_FatalError("An exception class could not be initialized.");
1913 }
1914
1915 /* Now insert the class into both this module and the __builtin__
1916 * module.
1917 */
1918 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1919 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1920 {
1921 Py_FatalError("Module dictionary insertion problem.");
1922 }
1923 }
1924
1925 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001926 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00001927 if (!args ||
1928 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1929 {
1930 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1931 }
1932 Py_DECREF(args);
1933
1934 /* We're done with __builtin__ */
1935 Py_DECREF(bltinmod);
1936}
1937
1938
Mark Hammonda2905272002-07-29 13:42:14 +00001939void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001940_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001941{
1942 int i;
1943
1944 Py_XDECREF(PyExc_MemoryErrorInst);
1945 PyExc_MemoryErrorInst = NULL;
1946
1947 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001948 /* clear the class's dictionary, freeing up circular references
1949 * between the class and its methods.
1950 */
1951 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1952 PyDict_Clear(cdict);
1953 Py_DECREF(cdict);
1954
1955 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001956 Py_XDECREF(*exctable[i].exc);
1957 *exctable[i].exc = NULL;
1958 }
1959}