blob: b146c971b9c87a4c77f9dc261ea19c5fa6288233 [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);
Brett Cannon46872b12006-03-02 04:31:55 +0000288 PyObject *unicode_obj;
289
Brett Cannonbf364092006-03-01 04:25:17 +0000290 if (!temp) {
291 Py_DECREF(args);
292 return NULL;
293 }
294 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000295 unicode_obj = PyObject_Unicode(temp);
296 Py_DECREF(temp);
297 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000298 }
299 else {
Brett Cannon46872b12006-03-02 04:31:55 +0000300 PyObject *unicode_obj = PyObject_Unicode(args);
301
Brett Cannonbf364092006-03-01 04:25:17 +0000302 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000303 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000304 }
305}
306#endif /* Py_USING_UNICODE */
Barry Warsaw675ac282000-05-26 19:05:16 +0000307
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000308static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000309BaseException__repr__(PyObject *self, PyObject *args)
310{
311 PyObject *args_attr;
312 Py_ssize_t args_len;
313 PyObject *repr_suffix;
314 PyObject *repr;
315
316 if (!PyArg_ParseTuple(args, "O:__repr__", &self))
317 return NULL;
318
319 args_attr = PyObject_GetAttrString(self, "args");
320 if (!args_attr)
321 return NULL;
322
323 args_len = PySequence_Length(args_attr);
324 if (args_len < 0) {
325 Py_DECREF(args_attr);
326 return NULL;
327 }
328
329 if (args_len == 0) {
330 Py_DECREF(args_attr);
331 repr_suffix = PyString_FromString("()");
332 if (!repr_suffix)
333 return NULL;
334 }
335 else {
Neal Norwitz9742f272006-03-03 19:13:57 +0000336 PyObject *args_repr = PyObject_Repr(args_attr);
Brett Cannonbf364092006-03-01 04:25:17 +0000337 Py_DECREF(args_attr);
338 if (!args_repr)
339 return NULL;
340
341 repr_suffix = args_repr;
Brett Cannonbf364092006-03-01 04:25:17 +0000342 }
343
344 repr = PyString_FromString(self->ob_type->tp_name);
345 if (!repr) {
346 Py_DECREF(repr_suffix);
347 return NULL;
348 }
349
350 PyString_ConcatAndDel(&repr, repr_suffix);
351 return repr;
352}
353
354static PyObject *
355BaseException__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000356{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000357 PyObject *out;
358 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000359
Fred Drake1aba5772000-08-15 15:46:16 +0000360 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000361 return NULL;
362
363 args = PyObject_GetAttrString(self, "args");
364 if (!args)
365 return NULL;
366
367 out = PyObject_GetItem(args, index);
368 Py_DECREF(args);
369 return out;
370}
371
372
373static PyMethodDef
Brett Cannonbf364092006-03-01 04:25:17 +0000374BaseException_methods[] = {
375 /* methods for the BaseException class */
376 {"__getitem__", BaseException__getitem__, METH_VARARGS},
377 {"__repr__", BaseException__repr__, METH_VARARGS},
378 {"__str__", BaseException__str__, METH_VARARGS},
379#ifdef Py_USING_UNICODE
380 {"__unicode__", BaseException__unicode__, METH_VARARGS},
381#endif /* Py_USING_UNICODE */
382 {"__init__", BaseException__init__, METH_VARARGS},
383 {NULL, NULL }
Barry Warsaw675ac282000-05-26 19:05:16 +0000384};
385
386
387static int
Brett Cannonbf364092006-03-01 04:25:17 +0000388make_BaseException(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000389{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000390 PyObject *dict = PyDict_New();
391 PyObject *str = NULL;
392 PyObject *name = NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000393 PyObject *emptytuple = NULL;
394 PyObject *argstuple = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000395 int status = -1;
396
397 if (!dict)
398 return -1;
399
400 /* If an error occurs from here on, goto finally instead of explicitly
401 * returning NULL.
402 */
403
404 if (!(str = PyString_FromString(modulename)))
405 goto finally;
406 if (PyDict_SetItemString(dict, "__module__", str))
407 goto finally;
408 Py_DECREF(str);
Brett Cannonbf364092006-03-01 04:25:17 +0000409
410 if (!(str = PyString_FromString(BaseException__doc__)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000411 goto finally;
412 if (PyDict_SetItemString(dict, "__doc__", str))
413 goto finally;
414
Brett Cannonbf364092006-03-01 04:25:17 +0000415 if (!(name = PyString_FromString("BaseException")))
Barry Warsaw675ac282000-05-26 19:05:16 +0000416 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000417
Brett Cannonbf364092006-03-01 04:25:17 +0000418 if (!(emptytuple = PyTuple_New(0)))
419 goto finally;
420
421 if (!(argstuple = PyTuple_Pack(3, name, emptytuple, dict)))
422 goto finally;
423
424 if (!(PyExc_BaseException = PyType_Type.tp_new(&PyType_Type, argstuple,
425 NULL)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000426 goto finally;
427
428 /* Now populate the dictionary with the method suite */
Brett Cannonbf364092006-03-01 04:25:17 +0000429 if (populate_methods(PyExc_BaseException, BaseException_methods))
430 /* Don't need to reclaim PyExc_BaseException here because that'll
Barry Warsaw675ac282000-05-26 19:05:16 +0000431 * happen during interpreter shutdown.
432 */
433 goto finally;
434
435 status = 0;
436
437 finally:
438 Py_XDECREF(dict);
439 Py_XDECREF(str);
440 Py_XDECREF(name);
Brett Cannonbf364092006-03-01 04:25:17 +0000441 Py_XDECREF(emptytuple);
442 Py_XDECREF(argstuple);
Barry Warsaw675ac282000-05-26 19:05:16 +0000443 return status;
444}
445
446
447
Brett Cannonbf364092006-03-01 04:25:17 +0000448PyDoc_STRVAR(Exception__doc__, "Common base class for all non-exit exceptions.");
449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000450PyDoc_STRVAR(StandardError__doc__,
Brett Cannonbf364092006-03-01 04:25:17 +0000451"Base class for all standard Python exceptions that do not represent"
452"interpreter exiting.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000453
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000454PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000455
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000456PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000457PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000458
Barry Warsaw675ac282000-05-26 19:05:16 +0000459
460
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000461PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000462
463
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000464static PyObject *
465SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000466{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000467 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000468 int status;
469
470 if (!(self = get_self(args)))
471 return NULL;
472
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000473 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000474 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000475
Brett Cannonbf364092006-03-01 04:25:17 +0000476 if (!set_args_and_message(self, args)) {
477 Py_DECREF(args);
478 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000479 }
480
481 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000482 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000483 case 0:
484 Py_INCREF(Py_None);
485 code = Py_None;
486 break;
487 case 1:
488 code = PySequence_GetItem(args, 0);
489 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000490 case -1:
491 PyErr_Clear();
492 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000493 default:
494 Py_INCREF(args);
495 code = args;
496 break;
497 }
498
499 status = PyObject_SetAttrString(self, "code", code);
500 Py_DECREF(code);
501 Py_DECREF(args);
502 if (status < 0)
503 return NULL;
504
Brett Cannonbf364092006-03-01 04:25:17 +0000505 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000506}
507
508
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000509static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000510 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000511 {NULL, NULL}
512};
513
514
515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000516PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000518PyDoc_STRVAR(ImportError__doc__,
519"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000520
521
522
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000523PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000524
525
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000526static PyObject *
527EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000528{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000529 PyObject *item0 = NULL;
530 PyObject *item1 = NULL;
531 PyObject *item2 = NULL;
532 PyObject *subslice = NULL;
533 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000534
535 if (!(self = get_self(args)))
536 return NULL;
537
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000538 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000539 return NULL;
540
Brett Cannonbf364092006-03-01 04:25:17 +0000541 if (!set_args_and_message(self, args)) {
542 Py_DECREF(args);
543 return NULL;
544 }
545
546 if (PyObject_SetAttrString(self, "errno", Py_None) ||
Barry Warsaw675ac282000-05-26 19:05:16 +0000547 PyObject_SetAttrString(self, "strerror", Py_None) ||
548 PyObject_SetAttrString(self, "filename", Py_None))
549 {
550 goto finally;
551 }
552
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000553 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000554 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000555 /* Where a function has a single filename, such as open() or some
556 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
557 * called, giving a third argument which is the filename. But, so
558 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000559 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000560 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000561 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000562 * we hack args so that it only contains two items. This also
563 * means we need our own __str__() which prints out the filename
564 * when it was supplied.
565 */
566 item0 = PySequence_GetItem(args, 0);
567 item1 = PySequence_GetItem(args, 1);
568 item2 = PySequence_GetItem(args, 2);
569 if (!item0 || !item1 || !item2)
570 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000571
Barry Warsaw675ac282000-05-26 19:05:16 +0000572 if (PyObject_SetAttrString(self, "errno", item0) ||
573 PyObject_SetAttrString(self, "strerror", item1) ||
574 PyObject_SetAttrString(self, "filename", item2))
575 {
576 goto finally;
577 }
578
579 subslice = PySequence_GetSlice(args, 0, 2);
580 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
581 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000582 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000583
584 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000585 /* Used when PyErr_SetFromErrno() is called and no filename
586 * argument is given.
587 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000588 item0 = PySequence_GetItem(args, 0);
589 item1 = PySequence_GetItem(args, 1);
590 if (!item0 || !item1)
591 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000592
Barry Warsaw675ac282000-05-26 19:05:16 +0000593 if (PyObject_SetAttrString(self, "errno", item0) ||
594 PyObject_SetAttrString(self, "strerror", item1))
595 {
596 goto finally;
597 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000598 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000599
600 case -1:
601 PyErr_Clear();
602 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000603 }
604
605 Py_INCREF(Py_None);
606 rtnval = Py_None;
607
608 finally:
609 Py_DECREF(args);
610 Py_XDECREF(item0);
611 Py_XDECREF(item1);
612 Py_XDECREF(item2);
613 Py_XDECREF(subslice);
614 return rtnval;
615}
616
617
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000618static PyObject *
619EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000620{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000621 PyObject *originalself = self;
622 PyObject *filename;
623 PyObject *serrno;
624 PyObject *strerror;
625 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000626
Fred Drake1aba5772000-08-15 15:46:16 +0000627 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000628 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000629
Barry Warsaw675ac282000-05-26 19:05:16 +0000630 filename = PyObject_GetAttrString(self, "filename");
631 serrno = PyObject_GetAttrString(self, "errno");
632 strerror = PyObject_GetAttrString(self, "strerror");
633 if (!filename || !serrno || !strerror)
634 goto finally;
635
636 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000637 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
638 PyObject *repr = PyObject_Repr(filename);
639 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000640
641 if (!fmt || !repr || !tuple) {
642 Py_XDECREF(fmt);
643 Py_XDECREF(repr);
644 Py_XDECREF(tuple);
645 goto finally;
646 }
647
648 PyTuple_SET_ITEM(tuple, 0, serrno);
649 PyTuple_SET_ITEM(tuple, 1, strerror);
650 PyTuple_SET_ITEM(tuple, 2, repr);
651
652 rtnval = PyString_Format(fmt, tuple);
653
654 Py_DECREF(fmt);
655 Py_DECREF(tuple);
656 /* already freed because tuple owned only reference */
657 serrno = NULL;
658 strerror = NULL;
659 }
660 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000661 PyObject *fmt = PyString_FromString("[Errno %s] %s");
662 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000663
664 if (!fmt || !tuple) {
665 Py_XDECREF(fmt);
666 Py_XDECREF(tuple);
667 goto finally;
668 }
669
670 PyTuple_SET_ITEM(tuple, 0, serrno);
671 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000672
Barry Warsaw675ac282000-05-26 19:05:16 +0000673 rtnval = PyString_Format(fmt, tuple);
674
675 Py_DECREF(fmt);
676 Py_DECREF(tuple);
677 /* already freed because tuple owned only reference */
678 serrno = NULL;
679 strerror = NULL;
680 }
681 else
682 /* The original Python code said:
683 *
684 * return StandardError.__str__(self)
685 *
686 * but there is no StandardError__str__() function; we happen to
Brett Cannonbf364092006-03-01 04:25:17 +0000687 * know that's just a pass through to BaseException__str__().
Barry Warsaw675ac282000-05-26 19:05:16 +0000688 */
Brett Cannonbf364092006-03-01 04:25:17 +0000689 rtnval = BaseException__str__(originalself, args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000690
691 finally:
692 Py_XDECREF(filename);
693 Py_XDECREF(serrno);
694 Py_XDECREF(strerror);
695 return rtnval;
696}
697
698
699static
700PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000701 {"__init__", EnvironmentError__init__, METH_VARARGS},
702 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000703 {NULL, NULL}
704};
705
706
707
708
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000709PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000710
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000711PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000712
713#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000714PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000715#endif /* MS_WINDOWS */
716
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000717#ifdef __VMS
718static char
719VMSError__doc__[] = "OpenVMS OS system call failed.";
720#endif
721
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000722PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000723
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000724PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000726PyDoc_STRVAR(NotImplementedError__doc__,
727"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000728
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000729PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000730
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000731PyDoc_STRVAR(UnboundLocalError__doc__,
732"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000734PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000735
736
737
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000738PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000739
740
741static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000742SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000743{
Barry Warsaw87bec352000-08-18 05:05:37 +0000744 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000745 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000746
747 /* Additional class-creation time initializations */
748 if (!emptystring ||
749 PyObject_SetAttrString(klass, "msg", emptystring) ||
750 PyObject_SetAttrString(klass, "filename", Py_None) ||
751 PyObject_SetAttrString(klass, "lineno", Py_None) ||
752 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000753 PyObject_SetAttrString(klass, "text", Py_None) ||
754 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000755 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000756 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000757 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000758 Py_XDECREF(emptystring);
759 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000760}
761
762
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000763static PyObject *
764SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000765{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000766 PyObject *rtnval = NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +0000767 Py_ssize_t lenargs;
Barry Warsaw675ac282000-05-26 19:05:16 +0000768
769 if (!(self = get_self(args)))
770 return NULL;
771
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000772 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000773 return NULL;
774
Brett Cannonbf364092006-03-01 04:25:17 +0000775 if (!set_args_and_message(self, args)) {
776 Py_DECREF(args);
777 return NULL;
778 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000779
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000780 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000781 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000782 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000783 int status;
784
785 if (!item0)
786 goto finally;
787 status = PyObject_SetAttrString(self, "msg", item0);
788 Py_DECREF(item0);
789 if (status)
790 goto finally;
791 }
792 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000793 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000794 PyObject *filename = NULL, *lineno = NULL;
795 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000796 int status = 1;
797
798 if (!info)
799 goto finally;
800
801 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000802 if (filename != NULL) {
803 lineno = PySequence_GetItem(info, 1);
804 if (lineno != NULL) {
805 offset = PySequence_GetItem(info, 2);
806 if (offset != NULL) {
807 text = PySequence_GetItem(info, 3);
808 if (text != NULL) {
809 status =
810 PyObject_SetAttrString(self, "filename", filename)
811 || PyObject_SetAttrString(self, "lineno", lineno)
812 || PyObject_SetAttrString(self, "offset", offset)
813 || PyObject_SetAttrString(self, "text", text);
814 Py_DECREF(text);
815 }
816 Py_DECREF(offset);
817 }
818 Py_DECREF(lineno);
819 }
820 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000821 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000822 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000823
824 if (status)
825 goto finally;
826 }
827 Py_INCREF(Py_None);
828 rtnval = Py_None;
829
830 finally:
831 Py_DECREF(args);
832 return rtnval;
833}
834
835
Fred Drake185a29b2000-08-15 16:20:36 +0000836/* This is called "my_basename" instead of just "basename" to avoid name
837 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
838 defined, and Python does define that. */
839static char *
840my_basename(char *name)
841{
842 char *cp = name;
843 char *result = name;
844
845 if (name == NULL)
846 return "???";
847 while (*cp != '\0') {
848 if (*cp == SEP)
849 result = cp + 1;
850 ++cp;
851 }
852 return result;
853}
854
855
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000856static PyObject *
857SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000858{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000859 PyObject *msg;
860 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000861 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000862
Fred Drake1aba5772000-08-15 15:46:16 +0000863 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000864 return NULL;
865
866 if (!(msg = PyObject_GetAttrString(self, "msg")))
867 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000868
Barry Warsaw675ac282000-05-26 19:05:16 +0000869 str = PyObject_Str(msg);
870 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000871 result = str;
872
873 /* XXX -- do all the additional formatting with filename and
874 lineno here */
875
Guido van Rossum602d4512002-09-03 20:24:09 +0000876 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000877 int have_filename = 0;
878 int have_lineno = 0;
879 char *buffer = NULL;
880
Barry Warsaw77c9f502000-08-16 19:43:17 +0000881 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000882 have_filename = PyString_Check(filename);
883 else
884 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000885
886 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000887 have_lineno = PyInt_Check(lineno);
888 else
889 PyErr_Clear();
890
891 if (have_filename || have_lineno) {
Martin v. Löwis725507b2006-03-07 12:08:51 +0000892 Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64;
Barry Warsaw77c9f502000-08-16 19:43:17 +0000893 if (have_filename)
894 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000895
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000896 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000897 if (buffer != NULL) {
898 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000899 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
900 PyString_AS_STRING(str),
901 my_basename(PyString_AS_STRING(filename)),
902 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000903 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000904 PyOS_snprintf(buffer, bufsize, "%s (%s)",
905 PyString_AS_STRING(str),
906 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000907 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000908 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
909 PyString_AS_STRING(str),
910 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000911
Fred Drake1aba5772000-08-15 15:46:16 +0000912 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000913 PyMem_FREE(buffer);
914
Fred Drake1aba5772000-08-15 15:46:16 +0000915 if (result == NULL)
916 result = str;
917 else
918 Py_DECREF(str);
919 }
920 }
921 Py_XDECREF(filename);
922 Py_XDECREF(lineno);
923 }
924 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000925}
926
927
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000928static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000929 {"__init__", SyntaxError__init__, METH_VARARGS},
930 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000931 {NULL, NULL}
932};
933
934
Guido van Rossum602d4512002-09-03 20:24:09 +0000935static PyObject *
936KeyError__str__(PyObject *self, PyObject *args)
937{
938 PyObject *argsattr;
939 PyObject *result;
940
941 if (!PyArg_ParseTuple(args, "O:__str__", &self))
942 return NULL;
943
Brett Cannonbf364092006-03-01 04:25:17 +0000944 argsattr = PyObject_GetAttrString(self, "args");
945 if (!argsattr)
946 return NULL;
Guido van Rossum602d4512002-09-03 20:24:09 +0000947
948 /* If args is a tuple of exactly one item, apply repr to args[0].
949 This is done so that e.g. the exception raised by {}[''] prints
950 KeyError: ''
951 rather than the confusing
952 KeyError
953 alone. The downside is that if KeyError is raised with an explanatory
954 string, that string will be displayed in quotes. Too bad.
Brett Cannonbf364092006-03-01 04:25:17 +0000955 If args is anything else, use the default BaseException__str__().
Guido van Rossum602d4512002-09-03 20:24:09 +0000956 */
957 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
958 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
959 result = PyObject_Repr(key);
960 }
961 else
Brett Cannonbf364092006-03-01 04:25:17 +0000962 result = BaseException__str__(self, args);
Guido van Rossum602d4512002-09-03 20:24:09 +0000963
964 Py_DECREF(argsattr);
965 return result;
966}
967
968static PyMethodDef KeyError_methods[] = {
969 {"__str__", KeyError__str__, METH_VARARGS},
970 {NULL, NULL}
971};
972
973
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000974#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000975static
Martin v. Löwis18e16552006-02-15 17:27:45 +0000976int get_int(PyObject *exc, const char *name, Py_ssize_t *value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000977{
978 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
979
980 if (!attr)
981 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000982 if (PyInt_Check(attr)) {
983 *value = PyInt_AS_LONG(attr);
984 } else if (PyLong_Check(attr)) {
985 *value = (size_t)PyLong_AsLongLong(attr);
986 if (*value == -1) {
987 Py_DECREF(attr);
988 return -1;
989 }
990 } else {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000991 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000992 Py_DECREF(attr);
993 return -1;
994 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000995 Py_DECREF(attr);
996 return 0;
997}
998
999
1000static
Martin v. Löwis18e16552006-02-15 17:27:45 +00001001int set_ssize_t(PyObject *exc, const char *name, Py_ssize_t value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001002{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001003 PyObject *obj = PyInt_FromSsize_t(value);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001004 int result;
1005
1006 if (!obj)
1007 return -1;
1008 result = PyObject_SetAttrString(exc, (char *)name, obj);
1009 Py_DECREF(obj);
1010 return result;
1011}
1012
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001013static
1014PyObject *get_string(PyObject *exc, const char *name)
1015{
1016 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1017
1018 if (!attr)
1019 return NULL;
1020 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001021 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001022 Py_DECREF(attr);
1023 return NULL;
1024 }
1025 return attr;
1026}
1027
1028
1029static
1030int set_string(PyObject *exc, const char *name, const char *value)
1031{
1032 PyObject *obj = PyString_FromString(value);
1033 int result;
1034
1035 if (!obj)
1036 return -1;
1037 result = PyObject_SetAttrString(exc, (char *)name, obj);
1038 Py_DECREF(obj);
1039 return result;
1040}
1041
1042
1043static
1044PyObject *get_unicode(PyObject *exc, const char *name)
1045{
1046 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1047
1048 if (!attr)
1049 return NULL;
1050 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001051 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001052 Py_DECREF(attr);
1053 return NULL;
1054 }
1055 return attr;
1056}
1057
1058PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1059{
1060 return get_string(exc, "encoding");
1061}
1062
1063PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1064{
1065 return get_string(exc, "encoding");
1066}
1067
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001068PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
1069{
1070 return get_unicode(exc, "object");
1071}
1072
1073PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1074{
1075 return get_string(exc, "object");
1076}
1077
1078PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1079{
1080 return get_unicode(exc, "object");
1081}
1082
Martin v. Löwis18e16552006-02-15 17:27:45 +00001083int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001084{
1085 if (!get_int(exc, "start", start)) {
1086 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001087 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001088 if (!object)
1089 return -1;
1090 size = PyUnicode_GET_SIZE(object);
1091 if (*start<0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001092 *start = 0; /*XXX check for values <0*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001093 if (*start>=size)
1094 *start = size-1;
1095 Py_DECREF(object);
1096 return 0;
1097 }
1098 return -1;
1099}
1100
1101
Martin v. Löwis18e16552006-02-15 17:27:45 +00001102int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001103{
1104 if (!get_int(exc, "start", start)) {
1105 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001106 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001107 if (!object)
1108 return -1;
1109 size = PyString_GET_SIZE(object);
1110 if (*start<0)
1111 *start = 0;
1112 if (*start>=size)
1113 *start = size-1;
1114 Py_DECREF(object);
1115 return 0;
1116 }
1117 return -1;
1118}
1119
1120
Martin v. Löwis18e16552006-02-15 17:27:45 +00001121int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001122{
1123 return PyUnicodeEncodeError_GetStart(exc, start);
1124}
1125
1126
Martin v. Löwis18e16552006-02-15 17:27:45 +00001127int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001128{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001129 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001130}
1131
1132
Martin v. Löwis18e16552006-02-15 17:27:45 +00001133int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001134{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001135 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001136}
1137
1138
Martin v. Löwis18e16552006-02-15 17:27:45 +00001139int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001140{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001141 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001142}
1143
1144
Martin v. Löwis18e16552006-02-15 17:27:45 +00001145int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001146{
1147 if (!get_int(exc, "end", end)) {
1148 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001149 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001150 if (!object)
1151 return -1;
1152 size = PyUnicode_GET_SIZE(object);
1153 if (*end<1)
1154 *end = 1;
1155 if (*end>size)
1156 *end = size;
1157 Py_DECREF(object);
1158 return 0;
1159 }
1160 return -1;
1161}
1162
1163
Martin v. Löwis18e16552006-02-15 17:27:45 +00001164int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001165{
1166 if (!get_int(exc, "end", end)) {
1167 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001168 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001169 if (!object)
1170 return -1;
1171 size = PyString_GET_SIZE(object);
1172 if (*end<1)
1173 *end = 1;
1174 if (*end>size)
1175 *end = size;
1176 Py_DECREF(object);
1177 return 0;
1178 }
1179 return -1;
1180}
1181
1182
Martin v. Löwis18e16552006-02-15 17:27:45 +00001183int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001184{
1185 return PyUnicodeEncodeError_GetEnd(exc, start);
1186}
1187
1188
Martin v. Löwis18e16552006-02-15 17:27:45 +00001189int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001190{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001191 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001192}
1193
1194
Martin v. Löwis18e16552006-02-15 17:27:45 +00001195int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001196{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001197 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001198}
1199
1200
Martin v. Löwis18e16552006-02-15 17:27:45 +00001201int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001202{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001203 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001204}
1205
1206
1207PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1208{
1209 return get_string(exc, "reason");
1210}
1211
1212
1213PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1214{
1215 return get_string(exc, "reason");
1216}
1217
1218
1219PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1220{
1221 return get_string(exc, "reason");
1222}
1223
1224
1225int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1226{
1227 return set_string(exc, "reason", reason);
1228}
1229
1230
1231int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1232{
1233 return set_string(exc, "reason", reason);
1234}
1235
1236
1237int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1238{
1239 return set_string(exc, "reason", reason);
1240}
1241
1242
1243static PyObject *
1244UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1245{
1246 PyObject *rtnval = NULL;
1247 PyObject *encoding;
1248 PyObject *object;
1249 PyObject *start;
1250 PyObject *end;
1251 PyObject *reason;
1252
1253 if (!(self = get_self(args)))
1254 return NULL;
1255
1256 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1257 return NULL;
1258
Brett Cannonbf364092006-03-01 04:25:17 +00001259 if (!set_args_and_message(self, args)) {
1260 Py_DECREF(args);
1261 return NULL;
1262 }
1263
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001264 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1265 &PyString_Type, &encoding,
1266 objecttype, &object,
1267 &PyInt_Type, &start,
1268 &PyInt_Type, &end,
1269 &PyString_Type, &reason))
Walter Dörwalde98147a2003-08-14 20:59:07 +00001270 goto finally;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001271
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001272 if (PyObject_SetAttrString(self, "encoding", encoding))
1273 goto finally;
1274 if (PyObject_SetAttrString(self, "object", object))
1275 goto finally;
1276 if (PyObject_SetAttrString(self, "start", start))
1277 goto finally;
1278 if (PyObject_SetAttrString(self, "end", end))
1279 goto finally;
1280 if (PyObject_SetAttrString(self, "reason", reason))
1281 goto finally;
1282
1283 Py_INCREF(Py_None);
1284 rtnval = Py_None;
1285
1286 finally:
1287 Py_DECREF(args);
1288 return rtnval;
1289}
1290
1291
1292static PyObject *
1293UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1294{
1295 return UnicodeError__init__(self, args, &PyUnicode_Type);
1296}
1297
1298static PyObject *
1299UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1300{
1301 PyObject *encodingObj = NULL;
1302 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001303 Py_ssize_t start;
1304 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001305 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001306 PyObject *result = NULL;
1307
1308 self = arg;
1309
1310 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1311 goto error;
1312
1313 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1314 goto error;
1315
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001316 if (PyUnicodeEncodeError_GetStart(self, &start))
1317 goto error;
1318
1319 if (PyUnicodeEncodeError_GetEnd(self, &end))
1320 goto error;
1321
1322 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1323 goto error;
1324
1325 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001326 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001327 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001328 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001329 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001330 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001331 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001332 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001333 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1334 result = PyString_FromFormat(
1335 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001336 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001337 badchar_str,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001338 start,
1339 PyString_AS_STRING(reasonObj)
1340 );
1341 }
1342 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001343 result = PyString_FromFormat(
1344 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001345 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001346 start,
1347 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001348 PyString_AS_STRING(reasonObj)
1349 );
1350 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001351
1352error:
1353 Py_XDECREF(reasonObj);
1354 Py_XDECREF(objectObj);
1355 Py_XDECREF(encodingObj);
1356 return result;
1357}
1358
1359static PyMethodDef UnicodeEncodeError_methods[] = {
1360 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1361 {"__str__", UnicodeEncodeError__str__, METH_O},
1362 {NULL, NULL}
1363};
1364
1365
1366PyObject * PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001367 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1368 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001369{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001370 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001371 encoding, object, length, start, end, reason);
1372}
1373
1374
1375static PyObject *
1376UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1377{
1378 return UnicodeError__init__(self, args, &PyString_Type);
1379}
1380
1381static PyObject *
1382UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1383{
1384 PyObject *encodingObj = NULL;
1385 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001386 Py_ssize_t start;
1387 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001388 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001389 PyObject *result = NULL;
1390
1391 self = arg;
1392
1393 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1394 goto error;
1395
1396 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1397 goto error;
1398
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001399 if (PyUnicodeDecodeError_GetStart(self, &start))
1400 goto error;
1401
1402 if (PyUnicodeDecodeError_GetEnd(self, &end))
1403 goto error;
1404
1405 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1406 goto error;
1407
1408 if (end==start+1) {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001409 /* FromFormat does not support %02x, so format that separately */
1410 char byte[4];
1411 PyOS_snprintf(byte, sizeof(byte), "%02x",
1412 ((int)PyString_AS_STRING(objectObj)[start])&0xff);
1413 result = PyString_FromFormat(
1414 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001415 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001416 byte,
1417 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001418 PyString_AS_STRING(reasonObj)
1419 );
1420 }
1421 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001422 result = PyString_FromFormat(
1423 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001424 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001425 start,
1426 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001427 PyString_AS_STRING(reasonObj)
1428 );
1429 }
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001430
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001431
1432error:
1433 Py_XDECREF(reasonObj);
1434 Py_XDECREF(objectObj);
1435 Py_XDECREF(encodingObj);
1436 return result;
1437}
1438
1439static PyMethodDef UnicodeDecodeError_methods[] = {
1440 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1441 {"__str__", UnicodeDecodeError__str__, METH_O},
1442 {NULL, NULL}
1443};
1444
1445
1446PyObject * PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001447 const char *encoding, const char *object, Py_ssize_t length,
1448 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001449{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001450 assert(length < INT_MAX);
1451 assert(start < INT_MAX);
1452 assert(end < INT_MAX);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001453 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
Martin v. Löwis18e16552006-02-15 17:27:45 +00001454 encoding, object, (int)length, (int)start, (int)end, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001455}
1456
1457
1458static PyObject *
1459UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1460{
1461 PyObject *rtnval = NULL;
1462 PyObject *object;
1463 PyObject *start;
1464 PyObject *end;
1465 PyObject *reason;
1466
1467 if (!(self = get_self(args)))
1468 return NULL;
1469
1470 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1471 return NULL;
1472
Brett Cannonbf364092006-03-01 04:25:17 +00001473 if (!set_args_and_message(self, args)) {
1474 Py_DECREF(args);
1475 return NULL;
1476 }
1477
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001478 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1479 &PyUnicode_Type, &object,
1480 &PyInt_Type, &start,
1481 &PyInt_Type, &end,
1482 &PyString_Type, &reason))
1483 goto finally;
1484
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001485 if (PyObject_SetAttrString(self, "object", object))
1486 goto finally;
1487 if (PyObject_SetAttrString(self, "start", start))
1488 goto finally;
1489 if (PyObject_SetAttrString(self, "end", end))
1490 goto finally;
1491 if (PyObject_SetAttrString(self, "reason", reason))
1492 goto finally;
1493
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001494 rtnval = Py_None;
Brett Cannonbf364092006-03-01 04:25:17 +00001495 Py_INCREF(rtnval);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001496
1497 finally:
1498 Py_DECREF(args);
1499 return rtnval;
1500}
1501
1502
1503static PyObject *
1504UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1505{
1506 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001507 Py_ssize_t start;
1508 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001509 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001510 PyObject *result = NULL;
1511
1512 self = arg;
1513
1514 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1515 goto error;
1516
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001517 if (PyUnicodeTranslateError_GetStart(self, &start))
1518 goto error;
1519
1520 if (PyUnicodeTranslateError_GetEnd(self, &end))
1521 goto error;
1522
1523 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1524 goto error;
1525
1526 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001527 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001528 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001529 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001530 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001531 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001532 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001533 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001534 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1535 result = PyString_FromFormat(
1536 "can't translate character u'\\%s' in position %zd: %.400s",
1537 badchar_str,
1538 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001539 PyString_AS_STRING(reasonObj)
1540 );
1541 }
1542 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001543 result = PyString_FromFormat(
1544 "can't translate characters in position %zd-%zd: %.400s",
1545 start,
1546 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001547 PyString_AS_STRING(reasonObj)
1548 );
1549 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001550
1551error:
1552 Py_XDECREF(reasonObj);
1553 Py_XDECREF(objectObj);
1554 return result;
1555}
1556
1557static PyMethodDef UnicodeTranslateError_methods[] = {
1558 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1559 {"__str__", UnicodeTranslateError__str__, METH_O},
1560 {NULL, NULL}
1561};
1562
1563
1564PyObject * PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001565 const Py_UNICODE *object, Py_ssize_t length,
1566 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001567{
1568 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1569 object, length, start, end, reason);
1570}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001571#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001572
1573
Barry Warsaw675ac282000-05-26 19:05:16 +00001574
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001575/* Exception doc strings */
1576
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001577PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001578
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001579PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001581PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001582
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001583PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001585PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001586
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001587PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001588
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589PyDoc_STRVAR(ZeroDivisionError__doc__,
1590"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001591
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001592PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001593
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001594PyDoc_STRVAR(ValueError__doc__,
1595"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001596
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001597PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001598
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001599#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001600PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1601
1602PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1603
1604PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001605#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001607PyDoc_STRVAR(SystemError__doc__,
1608"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001609\n\
1610Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001611the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001612
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001613PyDoc_STRVAR(ReferenceError__doc__,
1614"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001615
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001616PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001617
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001618PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001619
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001620PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001621
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001622/* Warning category docstrings */
1623
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001625
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001626PyDoc_STRVAR(UserWarning__doc__,
1627"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001628
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001629PyDoc_STRVAR(DeprecationWarning__doc__,
1630"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001631
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001632PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001633"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001634"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001635
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001636PyDoc_STRVAR(SyntaxWarning__doc__,
1637"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001639PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001640"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001641
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001642PyDoc_STRVAR(RuntimeWarning__doc__,
1643"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001644
Barry Warsaw9f007392002-08-14 15:51:29 +00001645PyDoc_STRVAR(FutureWarning__doc__,
1646"Base class for warnings about constructs that will change semantically "
1647"in the future.");
1648
Barry Warsaw675ac282000-05-26 19:05:16 +00001649
1650
1651/* module global functions */
1652static PyMethodDef functions[] = {
1653 /* Sentinel */
1654 {NULL, NULL}
1655};
1656
1657
1658
1659/* Global C API defined exceptions */
1660
Brett Cannonbf364092006-03-01 04:25:17 +00001661PyObject *PyExc_BaseException;
Barry Warsaw675ac282000-05-26 19:05:16 +00001662PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001663PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001664PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001665PyObject *PyExc_StandardError;
1666PyObject *PyExc_ArithmeticError;
1667PyObject *PyExc_LookupError;
1668
1669PyObject *PyExc_AssertionError;
1670PyObject *PyExc_AttributeError;
1671PyObject *PyExc_EOFError;
1672PyObject *PyExc_FloatingPointError;
1673PyObject *PyExc_EnvironmentError;
1674PyObject *PyExc_IOError;
1675PyObject *PyExc_OSError;
1676PyObject *PyExc_ImportError;
1677PyObject *PyExc_IndexError;
1678PyObject *PyExc_KeyError;
1679PyObject *PyExc_KeyboardInterrupt;
1680PyObject *PyExc_MemoryError;
1681PyObject *PyExc_NameError;
1682PyObject *PyExc_OverflowError;
1683PyObject *PyExc_RuntimeError;
1684PyObject *PyExc_NotImplementedError;
1685PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001686PyObject *PyExc_IndentationError;
1687PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001688PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001689PyObject *PyExc_SystemError;
1690PyObject *PyExc_SystemExit;
1691PyObject *PyExc_UnboundLocalError;
1692PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001693PyObject *PyExc_UnicodeEncodeError;
1694PyObject *PyExc_UnicodeDecodeError;
1695PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001696PyObject *PyExc_TypeError;
1697PyObject *PyExc_ValueError;
1698PyObject *PyExc_ZeroDivisionError;
1699#ifdef MS_WINDOWS
1700PyObject *PyExc_WindowsError;
1701#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001702#ifdef __VMS
1703PyObject *PyExc_VMSError;
1704#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001705
1706/* Pre-computed MemoryError instance. Best to create this as early as
Brett Cannonbf364092006-03-01 04:25:17 +00001707 * possible and not wait until a MemoryError is actually raised!
Barry Warsaw675ac282000-05-26 19:05:16 +00001708 */
1709PyObject *PyExc_MemoryErrorInst;
1710
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001711/* Predefined warning categories */
1712PyObject *PyExc_Warning;
1713PyObject *PyExc_UserWarning;
1714PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001715PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001716PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001717/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001718PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001719PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001720PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001721
Barry Warsaw675ac282000-05-26 19:05:16 +00001722
1723
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001724/* mapping between exception names and their PyObject ** */
1725static struct {
1726 char *name;
1727 PyObject **exc;
1728 PyObject **base; /* NULL == PyExc_StandardError */
1729 char *docstr;
1730 PyMethodDef *methods;
1731 int (*classinit)(PyObject *);
1732} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001733 /*
Brett Cannonbf364092006-03-01 04:25:17 +00001734 * The first four classes MUST appear in exactly this order
Barry Warsaw675ac282000-05-26 19:05:16 +00001735 */
Brett Cannonbf364092006-03-01 04:25:17 +00001736 {"BaseException", &PyExc_BaseException},
1737 {"Exception", &PyExc_Exception, &PyExc_BaseException, Exception__doc__},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001738 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1739 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001740 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1741 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001742 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1743 StandardError__doc__},
1744 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1745 /*
1746 * The rest appear in depth-first order of the hierarchy
1747 */
Brett Cannonbf364092006-03-01 04:25:17 +00001748 {"SystemExit", &PyExc_SystemExit, &PyExc_BaseException, SystemExit__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +00001749 SystemExit_methods},
Brett Cannonbf364092006-03-01 04:25:17 +00001750 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, &PyExc_BaseException,
1751 KeyboardInterrupt__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001752 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1753 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1754 EnvironmentError_methods},
1755 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1756 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1757#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001758 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001759 WindowsError__doc__},
1760#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001761#ifdef __VMS
1762 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1763 VMSError__doc__},
1764#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001765 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1766 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1767 {"NotImplementedError", &PyExc_NotImplementedError,
1768 &PyExc_RuntimeError, NotImplementedError__doc__},
1769 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1770 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1771 UnboundLocalError__doc__},
1772 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1773 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1774 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001775 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1776 IndentationError__doc__},
1777 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1778 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001779 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1780 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1781 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1782 IndexError__doc__},
1783 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001784 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001785 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1786 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1787 OverflowError__doc__},
1788 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1789 ZeroDivisionError__doc__},
1790 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1791 FloatingPointError__doc__},
1792 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1793 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001794#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001795 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1796 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1797 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1798 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1799 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1800 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001801#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001802 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001803 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1804 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001805 /* Warning categories */
1806 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1807 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1808 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1809 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001810 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1811 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001812 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001813 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001814 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1815 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001816 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1817 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001818 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1819 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001820 /* Sentinel */
1821 {NULL}
1822};
1823
1824
1825
Mark Hammonda2905272002-07-29 13:42:14 +00001826void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001827_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001828{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001829 char *modulename = "exceptions";
Martin v. Löwis18e16552006-02-15 17:27:45 +00001830 Py_ssize_t modnamesz = strlen(modulename);
Barry Warsaw675ac282000-05-26 19:05:16 +00001831 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001832 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001833
Tim Peters6d6c1a32001-08-02 04:15:00 +00001834 me = Py_InitModule(modulename, functions);
1835 if (me == NULL)
1836 goto err;
1837 mydict = PyModule_GetDict(me);
1838 if (mydict == NULL)
1839 goto err;
1840 bltinmod = PyImport_ImportModule("__builtin__");
1841 if (bltinmod == NULL)
1842 goto err;
1843 bdict = PyModule_GetDict(bltinmod);
1844 if (bdict == NULL)
1845 goto err;
1846 doc = PyString_FromString(module__doc__);
1847 if (doc == NULL)
1848 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001849
Tim Peters6d6c1a32001-08-02 04:15:00 +00001850 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001851 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001852 if (i < 0) {
1853 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001854 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001855 return;
1856 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001857
1858 /* This is the base class of all exceptions, so make it first. */
Brett Cannonbf364092006-03-01 04:25:17 +00001859 if (make_BaseException(modulename) ||
1860 PyDict_SetItemString(mydict, "BaseException", PyExc_BaseException) ||
1861 PyDict_SetItemString(bdict, "BaseException", PyExc_BaseException))
Barry Warsaw675ac282000-05-26 19:05:16 +00001862 {
Brett Cannonbf364092006-03-01 04:25:17 +00001863 Py_FatalError("Base class `BaseException' could not be created.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001864 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001865
Barry Warsaw675ac282000-05-26 19:05:16 +00001866 /* Now we can programmatically create all the remaining exceptions.
1867 * Remember to start the loop at 1 to skip Exceptions.
1868 */
1869 for (i=1; exctable[i].name; i++) {
1870 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001871 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1872 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001873
1874 (void)strcpy(cname, modulename);
1875 (void)strcat(cname, ".");
1876 (void)strcat(cname, exctable[i].name);
1877
1878 if (exctable[i].base == 0)
1879 base = PyExc_StandardError;
1880 else
1881 base = *exctable[i].base;
1882
1883 status = make_class(exctable[i].exc, base, cname,
1884 exctable[i].methods,
1885 exctable[i].docstr);
1886
1887 PyMem_DEL(cname);
1888
1889 if (status)
1890 Py_FatalError("Standard exception classes could not be created.");
1891
1892 if (exctable[i].classinit) {
1893 status = (*exctable[i].classinit)(*exctable[i].exc);
1894 if (status)
1895 Py_FatalError("An exception class could not be initialized.");
1896 }
1897
1898 /* Now insert the class into both this module and the __builtin__
1899 * module.
1900 */
1901 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1902 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1903 {
1904 Py_FatalError("Module dictionary insertion problem.");
1905 }
1906 }
1907
1908 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001909 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00001910 if (!args ||
1911 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1912 {
1913 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1914 }
1915 Py_DECREF(args);
1916
1917 /* We're done with __builtin__ */
1918 Py_DECREF(bltinmod);
1919}
1920
1921
Mark Hammonda2905272002-07-29 13:42:14 +00001922void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001923_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001924{
1925 int i;
1926
1927 Py_XDECREF(PyExc_MemoryErrorInst);
1928 PyExc_MemoryErrorInst = NULL;
1929
1930 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001931 /* clear the class's dictionary, freeing up circular references
1932 * between the class and its methods.
1933 */
1934 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1935 PyDict_Clear(cdict);
1936 Py_DECREF(cdict);
1937
1938 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001939 Py_XDECREF(*exctable[i].exc);
1940 *exctable[i].exc = NULL;
1941 }
1942}