blob: 5c824e650cf9c73001ec0b7186dbef4d87f301d1 [file] [log] [blame]
Barry Warsaw675ac282000-05-26 19:05:16 +00001/* This module provides the suite of standard class-based exceptions for
2 * Python's builtin module. This is a complete C implementation of what,
3 * in Python 1.5.2, was contained in the exceptions.py module. The problem
4 * there was that if exceptions.py could not be imported for some reason,
5 * the entire interpreter would abort.
6 *
7 * By moving the exceptions into C and statically linking, we can guarantee
8 * that the standard exceptions will always be available.
9 *
Barry Warsaw675ac282000-05-26 19:05:16 +000010 *
11 * written by Fredrik Lundh
12 * modifications, additions, cleanups, and proofreading by Barry Warsaw
13 *
14 * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
15 */
16
Martin v. Löwis5cb69362006-04-14 09:08:42 +000017#define PY_SSIZE_T_CLEAN
Barry Warsaw675ac282000-05-26 19:05:16 +000018#include "Python.h"
Fred Drake185a29b2000-08-15 16:20:36 +000019#include "osdefs.h"
Barry Warsaw675ac282000-05-26 19:05:16 +000020
Tim Petersbf26e072000-07-12 04:02:10 +000021/* Caution: MS Visual C++ 6 errors if a single string literal exceeds
22 * 2Kb. So the module docstring has been broken roughly in half, using
23 * compile-time literal concatenation.
24 */
Skip Montanaro995895f2002-03-28 20:57:51 +000025
26/* NOTE: If the exception class hierarchy changes, don't forget to update
27 * Doc/lib/libexcs.tex!
28 */
29
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000030PyDoc_STRVAR(module__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +000031"Python's standard exception class hierarchy.\n\
32\n\
Brett Cannonbf364092006-03-01 04:25:17 +000033Exceptions found here are defined both in the exceptions module and the \n\
34built-in namespace. It is recommended that user-defined exceptions inherit \n\
Brett Cannon54ac2942006-03-01 22:10:49 +000035from Exception. See the documentation for the exception inheritance hierarchy.\n\
Brett Cannonbf364092006-03-01 04:25:17 +000036"
37
Tim Petersbf26e072000-07-12 04:02:10 +000038 /* keep string pieces "small" */
Brett Cannon54ac2942006-03-01 22:10:49 +000039/* XXX(bcannon): exception hierarchy in Lib/test/exception_hierarchy.txt */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000040);
Barry Warsaw675ac282000-05-26 19:05:16 +000041
42
43/* Helper function for populating a dictionary with method wrappers. */
44static int
Brett Cannonbf364092006-03-01 04:25:17 +000045populate_methods(PyObject *klass, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +000046{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000047 PyObject *module;
48 int status = -1;
49
Barry Warsaw675ac282000-05-26 19:05:16 +000050 if (!methods)
51 return 0;
52
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000053 module = PyString_FromString("exceptions");
54 if (!module)
55 return 0;
Barry Warsaw675ac282000-05-26 19:05:16 +000056 while (methods->ml_name) {
57 /* get a wrapper for the built-in function */
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000058 PyObject *func = PyCFunction_NewEx(methods, NULL, module);
Thomas Wouters0452d1f2000-07-22 18:45:06 +000059 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +000060
61 if (!func)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000062 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000063
64 /* turn the function into an unbound method */
65 if (!(meth = PyMethod_New(func, NULL, klass))) {
66 Py_DECREF(func);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000067 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000068 }
Barry Warsaw9667ed22001-01-23 16:08:34 +000069
Barry Warsaw675ac282000-05-26 19:05:16 +000070 /* add method to dictionary */
Brett Cannonbf364092006-03-01 04:25:17 +000071 status = PyObject_SetAttrString(klass, methods->ml_name, meth);
Barry Warsaw675ac282000-05-26 19:05:16 +000072 Py_DECREF(meth);
73 Py_DECREF(func);
74
75 /* stop now if an error occurred, otherwise do the next method */
76 if (status)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000077 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000078
79 methods++;
80 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000081 status = 0;
82 status:
83 Py_DECREF(module);
84 return status;
Barry Warsaw675ac282000-05-26 19:05:16 +000085}
86
Barry Warsaw9667ed22001-01-23 16:08:34 +000087
Barry Warsaw675ac282000-05-26 19:05:16 +000088
89/* This function is used to create all subsequent exception classes. */
90static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +000091make_class(PyObject **klass, PyObject *base,
92 char *name, PyMethodDef *methods,
93 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +000094{
Thomas Wouters0452d1f2000-07-22 18:45:06 +000095 PyObject *dict = PyDict_New();
96 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +000097 int status = -1;
98
99 if (!dict)
100 return -1;
101
102 /* If an error occurs from here on, goto finally instead of explicitly
103 * returning NULL.
104 */
105
106 if (docstr) {
107 if (!(str = PyString_FromString(docstr)))
108 goto finally;
109 if (PyDict_SetItemString(dict, "__doc__", str))
110 goto finally;
111 }
112
113 if (!(*klass = PyErr_NewException(name, base, dict)))
114 goto finally;
115
Brett Cannonbf364092006-03-01 04:25:17 +0000116 if (populate_methods(*klass, methods)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000117 Py_DECREF(*klass);
118 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000119 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000120 }
121
122 status = 0;
123
124 finally:
125 Py_XDECREF(dict);
126 Py_XDECREF(str);
127 return status;
128}
129
130
131/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000132static PyObject *
133get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000134{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000135 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000136 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000137 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000138 if (PyExc_TypeError) {
139 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000140 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000141 }
142 return NULL;
143 }
144 return self;
145}
146
147
148
149/* Notes on bootstrapping the exception classes.
150 *
151 * First thing we create is the base class for all exceptions, called
Brett Cannonbf364092006-03-01 04:25:17 +0000152 * appropriately BaseException. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000153 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000154 * for TypeError, which can conditionally exist.
155 *
Brett Cannonbf364092006-03-01 04:25:17 +0000156 * Next, Exception is created since it is the common subclass for the rest of
157 * the needed exceptions for this bootstrapping to work. StandardError is
158 * created (which is quite simple) followed by
Barry Warsaw675ac282000-05-26 19:05:16 +0000159 * TypeError, because the instantiation of other exceptions can potentially
160 * throw a TypeError. Once these exceptions are created, all the others
161 * can be created in any order. See the static exctable below for the
162 * explicit bootstrap order.
163 *
Brett Cannonbf364092006-03-01 04:25:17 +0000164 * All classes after BaseException can be created using PyErr_NewException().
Barry Warsaw675ac282000-05-26 19:05:16 +0000165 */
166
Brett Cannonbf364092006-03-01 04:25:17 +0000167PyDoc_STRVAR(BaseException__doc__, "Common base class for all exceptions");
Barry Warsaw675ac282000-05-26 19:05:16 +0000168
Brett Cannonbf364092006-03-01 04:25:17 +0000169/*
170 Set args and message attributes.
171
172 Assumes self and args have already been set properly with set_self, etc.
173*/
174static int
175set_args_and_message(PyObject *self, PyObject *args)
176{
177 PyObject *message_val;
178 Py_ssize_t args_len = PySequence_Length(args);
179
180 if (args_len < 0)
181 return 0;
182
183 /* set args */
184 if (PyObject_SetAttrString(self, "args", args) < 0)
185 return 0;
186
187 /* set message */
188 if (args_len == 1)
189 message_val = PySequence_GetItem(args, 0);
190 else
191 message_val = PyString_FromString("");
192 if (!message_val)
193 return 0;
194
195 if (PyObject_SetAttrString(self, "message", message_val) < 0) {
196 Py_DECREF(message_val);
197 return 0;
198 }
199
200 Py_DECREF(message_val);
201 return 1;
202}
Barry Warsaw675ac282000-05-26 19:05:16 +0000203
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000204static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000205BaseException__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000206{
Barry Warsaw675ac282000-05-26 19:05:16 +0000207 if (!(self = get_self(args)))
208 return NULL;
209
Brett Cannonbf364092006-03-01 04:25:17 +0000210 /* set args and message attribute */
211 args = PySequence_GetSlice(args, 1, PySequence_Length(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000212 if (!args)
213 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000214
Brett Cannonbf364092006-03-01 04:25:17 +0000215 if (!set_args_and_message(self, args)) {
216 Py_DECREF(args);
217 return NULL;
218 }
219
220 Py_DECREF(args);
221 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000222}
223
224
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000225static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000226BaseException__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000227{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000228 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000229
Fred Drake1aba5772000-08-15 15:46:16 +0000230 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000231 return NULL;
232
233 args = PyObject_GetAttrString(self, "args");
234 if (!args)
235 return NULL;
236
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000237 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000238 case 0:
239 out = PyString_FromString("");
240 break;
241 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000242 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000243 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000244 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000245 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000246 Py_DECREF(tmp);
247 }
248 else
249 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000250 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000251 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000252 case -1:
253 PyErr_Clear();
254 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000255 default:
256 out = PyObject_Str(args);
257 break;
258 }
259
260 Py_DECREF(args);
261 return out;
262}
263
Brett Cannonbf364092006-03-01 04:25:17 +0000264#ifdef Py_USING_UNICODE
265static PyObject *
266BaseException__unicode__(PyObject *self, PyObject *args)
267{
268 Py_ssize_t args_len;
269
270 if (!PyArg_ParseTuple(args, "O:__unicode__", &self))
271 return NULL;
272
273 args = PyObject_GetAttrString(self, "args");
274 if (!args)
275 return NULL;
276
277 args_len = PySequence_Size(args);
278 if (args_len < 0) {
279 Py_DECREF(args);
280 return NULL;
281 }
282
283 if (args_len == 0) {
284 Py_DECREF(args);
285 return PyUnicode_FromUnicode(NULL, 0);
286 }
287 else if (args_len == 1) {
288 PyObject *temp = PySequence_GetItem(args, 0);
Brett Cannon46872b12006-03-02 04:31:55 +0000289 PyObject *unicode_obj;
290
Brett Cannonbf364092006-03-01 04:25:17 +0000291 if (!temp) {
292 Py_DECREF(args);
293 return NULL;
294 }
295 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000296 unicode_obj = PyObject_Unicode(temp);
297 Py_DECREF(temp);
298 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000299 }
300 else {
Brett Cannon46872b12006-03-02 04:31:55 +0000301 PyObject *unicode_obj = PyObject_Unicode(args);
302
Brett Cannonbf364092006-03-01 04:25:17 +0000303 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000304 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000305 }
306}
307#endif /* Py_USING_UNICODE */
Barry Warsaw675ac282000-05-26 19:05:16 +0000308
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000309static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000310BaseException__repr__(PyObject *self, PyObject *args)
311{
312 PyObject *args_attr;
313 Py_ssize_t args_len;
314 PyObject *repr_suffix;
315 PyObject *repr;
316
317 if (!PyArg_ParseTuple(args, "O:__repr__", &self))
318 return NULL;
319
320 args_attr = PyObject_GetAttrString(self, "args");
321 if (!args_attr)
322 return NULL;
323
324 args_len = PySequence_Length(args_attr);
325 if (args_len < 0) {
326 Py_DECREF(args_attr);
327 return NULL;
328 }
329
330 if (args_len == 0) {
331 Py_DECREF(args_attr);
332 repr_suffix = PyString_FromString("()");
333 if (!repr_suffix)
334 return NULL;
335 }
336 else {
Neal Norwitz9742f272006-03-03 19:13:57 +0000337 PyObject *args_repr = PyObject_Repr(args_attr);
Brett Cannonbf364092006-03-01 04:25:17 +0000338 Py_DECREF(args_attr);
339 if (!args_repr)
340 return NULL;
341
342 repr_suffix = args_repr;
Brett Cannonbf364092006-03-01 04:25:17 +0000343 }
344
345 repr = PyString_FromString(self->ob_type->tp_name);
346 if (!repr) {
347 Py_DECREF(repr_suffix);
348 return NULL;
349 }
350
351 PyString_ConcatAndDel(&repr, repr_suffix);
352 return repr;
353}
354
355static PyObject *
356BaseException__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000357{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000358 PyObject *out;
359 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000360
Fred Drake1aba5772000-08-15 15:46:16 +0000361 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000362 return NULL;
363
364 args = PyObject_GetAttrString(self, "args");
365 if (!args)
366 return NULL;
367
368 out = PyObject_GetItem(args, index);
369 Py_DECREF(args);
370 return out;
371}
372
373
374static PyMethodDef
Brett Cannonbf364092006-03-01 04:25:17 +0000375BaseException_methods[] = {
376 /* methods for the BaseException class */
377 {"__getitem__", BaseException__getitem__, METH_VARARGS},
378 {"__repr__", BaseException__repr__, METH_VARARGS},
379 {"__str__", BaseException__str__, METH_VARARGS},
380#ifdef Py_USING_UNICODE
381 {"__unicode__", BaseException__unicode__, METH_VARARGS},
382#endif /* Py_USING_UNICODE */
383 {"__init__", BaseException__init__, METH_VARARGS},
384 {NULL, NULL }
Barry Warsaw675ac282000-05-26 19:05:16 +0000385};
386
387
388static int
Brett Cannonbf364092006-03-01 04:25:17 +0000389make_BaseException(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000390{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000391 PyObject *dict = PyDict_New();
392 PyObject *str = NULL;
393 PyObject *name = NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000394 PyObject *emptytuple = NULL;
395 PyObject *argstuple = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000396 int status = -1;
397
398 if (!dict)
399 return -1;
400
401 /* If an error occurs from here on, goto finally instead of explicitly
402 * returning NULL.
403 */
404
405 if (!(str = PyString_FromString(modulename)))
406 goto finally;
407 if (PyDict_SetItemString(dict, "__module__", str))
408 goto finally;
409 Py_DECREF(str);
Brett Cannonbf364092006-03-01 04:25:17 +0000410
411 if (!(str = PyString_FromString(BaseException__doc__)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000412 goto finally;
413 if (PyDict_SetItemString(dict, "__doc__", str))
414 goto finally;
415
Brett Cannonbf364092006-03-01 04:25:17 +0000416 if (!(name = PyString_FromString("BaseException")))
Barry Warsaw675ac282000-05-26 19:05:16 +0000417 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000418
Brett Cannonbf364092006-03-01 04:25:17 +0000419 if (!(emptytuple = PyTuple_New(0)))
420 goto finally;
421
422 if (!(argstuple = PyTuple_Pack(3, name, emptytuple, dict)))
423 goto finally;
424
425 if (!(PyExc_BaseException = PyType_Type.tp_new(&PyType_Type, argstuple,
426 NULL)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000427 goto finally;
428
429 /* Now populate the dictionary with the method suite */
Brett Cannonbf364092006-03-01 04:25:17 +0000430 if (populate_methods(PyExc_BaseException, BaseException_methods))
431 /* Don't need to reclaim PyExc_BaseException here because that'll
Barry Warsaw675ac282000-05-26 19:05:16 +0000432 * happen during interpreter shutdown.
433 */
434 goto finally;
435
436 status = 0;
437
438 finally:
439 Py_XDECREF(dict);
440 Py_XDECREF(str);
441 Py_XDECREF(name);
Brett Cannonbf364092006-03-01 04:25:17 +0000442 Py_XDECREF(emptytuple);
443 Py_XDECREF(argstuple);
Barry Warsaw675ac282000-05-26 19:05:16 +0000444 return status;
445}
446
447
448
Brett Cannonbf364092006-03-01 04:25:17 +0000449PyDoc_STRVAR(Exception__doc__, "Common base class for all non-exit exceptions.");
450
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000451PyDoc_STRVAR(StandardError__doc__,
Brett Cannonbf364092006-03-01 04:25:17 +0000452"Base class for all standard Python exceptions that do not represent"
453"interpreter exiting.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000454
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000455PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000457PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000458PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000459
Barry Warsaw675ac282000-05-26 19:05:16 +0000460
461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000462PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000463
464
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000465static PyObject *
466SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000467{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000468 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000469 int status;
470
471 if (!(self = get_self(args)))
472 return NULL;
473
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000474 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000475 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000476
Brett Cannonbf364092006-03-01 04:25:17 +0000477 if (!set_args_and_message(self, args)) {
478 Py_DECREF(args);
479 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000480 }
481
482 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000483 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000484 case 0:
485 Py_INCREF(Py_None);
486 code = Py_None;
487 break;
488 case 1:
489 code = PySequence_GetItem(args, 0);
490 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000491 case -1:
492 PyErr_Clear();
493 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000494 default:
495 Py_INCREF(args);
496 code = args;
497 break;
498 }
499
500 status = PyObject_SetAttrString(self, "code", code);
501 Py_DECREF(code);
502 Py_DECREF(args);
503 if (status < 0)
504 return NULL;
505
Brett Cannonbf364092006-03-01 04:25:17 +0000506 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000507}
508
509
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000510static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000511 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000512 {NULL, NULL}
513};
514
515
516
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000517PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000518
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000519PyDoc_STRVAR(ImportError__doc__,
520"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000521
522
523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000524PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000525
526
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000527static PyObject *
528EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000529{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000530 PyObject *item0 = NULL;
531 PyObject *item1 = NULL;
532 PyObject *item2 = NULL;
533 PyObject *subslice = NULL;
534 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000535
536 if (!(self = get_self(args)))
537 return NULL;
538
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000539 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000540 return NULL;
541
Brett Cannonbf364092006-03-01 04:25:17 +0000542 if (!set_args_and_message(self, args)) {
543 Py_DECREF(args);
544 return NULL;
545 }
546
547 if (PyObject_SetAttrString(self, "errno", Py_None) ||
Barry Warsaw675ac282000-05-26 19:05:16 +0000548 PyObject_SetAttrString(self, "strerror", Py_None) ||
549 PyObject_SetAttrString(self, "filename", Py_None))
550 {
551 goto finally;
552 }
553
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000554 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000555 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000556 /* Where a function has a single filename, such as open() or some
557 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
558 * called, giving a third argument which is the filename. But, so
559 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000560 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000561 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000562 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000563 * we hack args so that it only contains two items. This also
564 * means we need our own __str__() which prints out the filename
565 * when it was supplied.
566 */
567 item0 = PySequence_GetItem(args, 0);
568 item1 = PySequence_GetItem(args, 1);
569 item2 = PySequence_GetItem(args, 2);
570 if (!item0 || !item1 || !item2)
571 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000572
Barry Warsaw675ac282000-05-26 19:05:16 +0000573 if (PyObject_SetAttrString(self, "errno", item0) ||
574 PyObject_SetAttrString(self, "strerror", item1) ||
575 PyObject_SetAttrString(self, "filename", item2))
576 {
577 goto finally;
578 }
579
580 subslice = PySequence_GetSlice(args, 0, 2);
581 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
582 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000583 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000584
585 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000586 /* Used when PyErr_SetFromErrno() is called and no filename
587 * argument is given.
588 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000589 item0 = PySequence_GetItem(args, 0);
590 item1 = PySequence_GetItem(args, 1);
591 if (!item0 || !item1)
592 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000593
Barry Warsaw675ac282000-05-26 19:05:16 +0000594 if (PyObject_SetAttrString(self, "errno", item0) ||
595 PyObject_SetAttrString(self, "strerror", item1))
596 {
597 goto finally;
598 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000599 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000600
601 case -1:
602 PyErr_Clear();
603 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000604 }
605
606 Py_INCREF(Py_None);
607 rtnval = Py_None;
608
609 finally:
610 Py_DECREF(args);
611 Py_XDECREF(item0);
612 Py_XDECREF(item1);
613 Py_XDECREF(item2);
614 Py_XDECREF(subslice);
615 return rtnval;
616}
617
618
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000619static PyObject *
620EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000621{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000622 PyObject *originalself = self;
623 PyObject *filename;
624 PyObject *serrno;
625 PyObject *strerror;
626 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000627
Fred Drake1aba5772000-08-15 15:46:16 +0000628 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000629 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000630
Barry Warsaw675ac282000-05-26 19:05:16 +0000631 filename = PyObject_GetAttrString(self, "filename");
632 serrno = PyObject_GetAttrString(self, "errno");
633 strerror = PyObject_GetAttrString(self, "strerror");
634 if (!filename || !serrno || !strerror)
635 goto finally;
636
637 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000638 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
639 PyObject *repr = PyObject_Repr(filename);
640 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000641
642 if (!fmt || !repr || !tuple) {
643 Py_XDECREF(fmt);
644 Py_XDECREF(repr);
645 Py_XDECREF(tuple);
646 goto finally;
647 }
648
649 PyTuple_SET_ITEM(tuple, 0, serrno);
650 PyTuple_SET_ITEM(tuple, 1, strerror);
651 PyTuple_SET_ITEM(tuple, 2, repr);
652
653 rtnval = PyString_Format(fmt, tuple);
654
655 Py_DECREF(fmt);
656 Py_DECREF(tuple);
657 /* already freed because tuple owned only reference */
658 serrno = NULL;
659 strerror = NULL;
660 }
661 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000662 PyObject *fmt = PyString_FromString("[Errno %s] %s");
663 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000664
665 if (!fmt || !tuple) {
666 Py_XDECREF(fmt);
667 Py_XDECREF(tuple);
668 goto finally;
669 }
670
671 PyTuple_SET_ITEM(tuple, 0, serrno);
672 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000673
Barry Warsaw675ac282000-05-26 19:05:16 +0000674 rtnval = PyString_Format(fmt, tuple);
675
676 Py_DECREF(fmt);
677 Py_DECREF(tuple);
678 /* already freed because tuple owned only reference */
679 serrno = NULL;
680 strerror = NULL;
681 }
682 else
683 /* The original Python code said:
684 *
685 * return StandardError.__str__(self)
686 *
687 * but there is no StandardError__str__() function; we happen to
Brett Cannonbf364092006-03-01 04:25:17 +0000688 * know that's just a pass through to BaseException__str__().
Barry Warsaw675ac282000-05-26 19:05:16 +0000689 */
Brett Cannonbf364092006-03-01 04:25:17 +0000690 rtnval = BaseException__str__(originalself, args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000691
692 finally:
693 Py_XDECREF(filename);
694 Py_XDECREF(serrno);
695 Py_XDECREF(strerror);
696 return rtnval;
697}
698
699
700static
701PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000702 {"__init__", EnvironmentError__init__, METH_VARARGS},
703 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000704 {NULL, NULL}
705};
706
707
708
709
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000710PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000711
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000712PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000713
714#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000715PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000716#endif /* MS_WINDOWS */
717
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000718#ifdef __VMS
719static char
720VMSError__doc__[] = "OpenVMS OS system call failed.";
721#endif
722
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000723PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000724
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000725PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000726
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000727PyDoc_STRVAR(NotImplementedError__doc__,
728"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000729
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000730PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000732PyDoc_STRVAR(UnboundLocalError__doc__,
733"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000734
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000735PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000736
737
738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000739PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000740
741
742static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000743SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000744{
Barry Warsaw87bec352000-08-18 05:05:37 +0000745 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000746 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000747
748 /* Additional class-creation time initializations */
749 if (!emptystring ||
750 PyObject_SetAttrString(klass, "msg", emptystring) ||
751 PyObject_SetAttrString(klass, "filename", Py_None) ||
752 PyObject_SetAttrString(klass, "lineno", Py_None) ||
753 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000754 PyObject_SetAttrString(klass, "text", Py_None) ||
755 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000756 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000757 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000758 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000759 Py_XDECREF(emptystring);
760 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000761}
762
763
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000764static PyObject *
765SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000766{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000767 PyObject *rtnval = NULL;
Martin v. Löwis725507b2006-03-07 12:08:51 +0000768 Py_ssize_t lenargs;
Barry Warsaw675ac282000-05-26 19:05:16 +0000769
770 if (!(self = get_self(args)))
771 return NULL;
772
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000773 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000774 return NULL;
775
Brett Cannonbf364092006-03-01 04:25:17 +0000776 if (!set_args_and_message(self, args)) {
777 Py_DECREF(args);
778 return NULL;
779 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000780
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000781 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000782 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000783 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000784 int status;
785
786 if (!item0)
787 goto finally;
788 status = PyObject_SetAttrString(self, "msg", item0);
789 Py_DECREF(item0);
790 if (status)
791 goto finally;
792 }
793 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000794 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000795 PyObject *filename = NULL, *lineno = NULL;
796 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000797 int status = 1;
798
799 if (!info)
800 goto finally;
801
802 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000803 if (filename != NULL) {
804 lineno = PySequence_GetItem(info, 1);
805 if (lineno != NULL) {
806 offset = PySequence_GetItem(info, 2);
807 if (offset != NULL) {
808 text = PySequence_GetItem(info, 3);
809 if (text != NULL) {
810 status =
811 PyObject_SetAttrString(self, "filename", filename)
812 || PyObject_SetAttrString(self, "lineno", lineno)
813 || PyObject_SetAttrString(self, "offset", offset)
814 || PyObject_SetAttrString(self, "text", text);
815 Py_DECREF(text);
816 }
817 Py_DECREF(offset);
818 }
819 Py_DECREF(lineno);
820 }
821 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000822 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000823 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000824
825 if (status)
826 goto finally;
827 }
828 Py_INCREF(Py_None);
829 rtnval = Py_None;
830
831 finally:
832 Py_DECREF(args);
833 return rtnval;
834}
835
836
Fred Drake185a29b2000-08-15 16:20:36 +0000837/* This is called "my_basename" instead of just "basename" to avoid name
838 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
839 defined, and Python does define that. */
840static char *
841my_basename(char *name)
842{
843 char *cp = name;
844 char *result = name;
845
846 if (name == NULL)
847 return "???";
848 while (*cp != '\0') {
849 if (*cp == SEP)
850 result = cp + 1;
851 ++cp;
852 }
853 return result;
854}
855
856
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000857static PyObject *
858SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000859{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000860 PyObject *msg;
861 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000862 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000863
Fred Drake1aba5772000-08-15 15:46:16 +0000864 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000865 return NULL;
866
867 if (!(msg = PyObject_GetAttrString(self, "msg")))
868 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000869
Barry Warsaw675ac282000-05-26 19:05:16 +0000870 str = PyObject_Str(msg);
871 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000872 result = str;
873
874 /* XXX -- do all the additional formatting with filename and
875 lineno here */
876
Guido van Rossum602d4512002-09-03 20:24:09 +0000877 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000878 int have_filename = 0;
879 int have_lineno = 0;
880 char *buffer = NULL;
881
Barry Warsaw77c9f502000-08-16 19:43:17 +0000882 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000883 have_filename = PyString_Check(filename);
884 else
885 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000886
887 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000888 have_lineno = PyInt_Check(lineno);
889 else
890 PyErr_Clear();
891
892 if (have_filename || have_lineno) {
Martin v. Löwis725507b2006-03-07 12:08:51 +0000893 Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64;
Barry Warsaw77c9f502000-08-16 19:43:17 +0000894 if (have_filename)
895 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000896
Anthony Baxtera863d332006-04-11 07:43:46 +0000897 buffer = (char *)PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000898 if (buffer != NULL) {
899 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000900 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
901 PyString_AS_STRING(str),
902 my_basename(PyString_AS_STRING(filename)),
903 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000904 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000905 PyOS_snprintf(buffer, bufsize, "%s (%s)",
906 PyString_AS_STRING(str),
907 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000908 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000909 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
910 PyString_AS_STRING(str),
911 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000912
Fred Drake1aba5772000-08-15 15:46:16 +0000913 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000914 PyMem_FREE(buffer);
915
Fred Drake1aba5772000-08-15 15:46:16 +0000916 if (result == NULL)
917 result = str;
918 else
919 Py_DECREF(str);
920 }
921 }
922 Py_XDECREF(filename);
923 Py_XDECREF(lineno);
924 }
925 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000926}
927
928
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000929static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000930 {"__init__", SyntaxError__init__, METH_VARARGS},
931 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000932 {NULL, NULL}
933};
934
935
Guido van Rossum602d4512002-09-03 20:24:09 +0000936static PyObject *
937KeyError__str__(PyObject *self, PyObject *args)
938{
939 PyObject *argsattr;
940 PyObject *result;
941
942 if (!PyArg_ParseTuple(args, "O:__str__", &self))
943 return NULL;
944
Brett Cannonbf364092006-03-01 04:25:17 +0000945 argsattr = PyObject_GetAttrString(self, "args");
946 if (!argsattr)
947 return NULL;
Guido van Rossum602d4512002-09-03 20:24:09 +0000948
949 /* If args is a tuple of exactly one item, apply repr to args[0].
950 This is done so that e.g. the exception raised by {}[''] prints
951 KeyError: ''
952 rather than the confusing
953 KeyError
954 alone. The downside is that if KeyError is raised with an explanatory
955 string, that string will be displayed in quotes. Too bad.
Brett Cannonbf364092006-03-01 04:25:17 +0000956 If args is anything else, use the default BaseException__str__().
Guido van Rossum602d4512002-09-03 20:24:09 +0000957 */
958 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
959 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
960 result = PyObject_Repr(key);
961 }
962 else
Brett Cannonbf364092006-03-01 04:25:17 +0000963 result = BaseException__str__(self, args);
Guido van Rossum602d4512002-09-03 20:24:09 +0000964
965 Py_DECREF(argsattr);
966 return result;
967}
968
969static PyMethodDef KeyError_methods[] = {
970 {"__str__", KeyError__str__, METH_VARARGS},
971 {NULL, NULL}
972};
973
974
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000975#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000976static
Martin v. Löwis18e16552006-02-15 17:27:45 +0000977int get_int(PyObject *exc, const char *name, Py_ssize_t *value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000978{
979 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
980
981 if (!attr)
982 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000983 if (PyInt_Check(attr)) {
984 *value = PyInt_AS_LONG(attr);
985 } else if (PyLong_Check(attr)) {
986 *value = (size_t)PyLong_AsLongLong(attr);
987 if (*value == -1) {
988 Py_DECREF(attr);
989 return -1;
990 }
991 } else {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000992 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000993 Py_DECREF(attr);
994 return -1;
995 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000996 Py_DECREF(attr);
997 return 0;
998}
999
1000
1001static
Martin v. Löwis18e16552006-02-15 17:27:45 +00001002int set_ssize_t(PyObject *exc, const char *name, Py_ssize_t value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001003{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001004 PyObject *obj = PyInt_FromSsize_t(value);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001005 int result;
1006
1007 if (!obj)
1008 return -1;
1009 result = PyObject_SetAttrString(exc, (char *)name, obj);
1010 Py_DECREF(obj);
1011 return result;
1012}
1013
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001014static
1015PyObject *get_string(PyObject *exc, const char *name)
1016{
1017 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1018
1019 if (!attr)
1020 return NULL;
1021 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001022 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001023 Py_DECREF(attr);
1024 return NULL;
1025 }
1026 return attr;
1027}
1028
1029
1030static
1031int set_string(PyObject *exc, const char *name, const char *value)
1032{
1033 PyObject *obj = PyString_FromString(value);
1034 int result;
1035
1036 if (!obj)
1037 return -1;
1038 result = PyObject_SetAttrString(exc, (char *)name, obj);
1039 Py_DECREF(obj);
1040 return result;
1041}
1042
1043
1044static
1045PyObject *get_unicode(PyObject *exc, const char *name)
1046{
1047 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1048
1049 if (!attr)
1050 return NULL;
1051 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001052 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001053 Py_DECREF(attr);
1054 return NULL;
1055 }
1056 return attr;
1057}
1058
1059PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1060{
1061 return get_string(exc, "encoding");
1062}
1063
1064PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1065{
1066 return get_string(exc, "encoding");
1067}
1068
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001069PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
1070{
1071 return get_unicode(exc, "object");
1072}
1073
1074PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1075{
1076 return get_string(exc, "object");
1077}
1078
1079PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1080{
1081 return get_unicode(exc, "object");
1082}
1083
Martin v. Löwis18e16552006-02-15 17:27:45 +00001084int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001085{
1086 if (!get_int(exc, "start", start)) {
1087 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001088 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001089 if (!object)
1090 return -1;
1091 size = PyUnicode_GET_SIZE(object);
1092 if (*start<0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001093 *start = 0; /*XXX check for values <0*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001094 if (*start>=size)
1095 *start = size-1;
1096 Py_DECREF(object);
1097 return 0;
1098 }
1099 return -1;
1100}
1101
1102
Martin v. Löwis18e16552006-02-15 17:27:45 +00001103int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001104{
1105 if (!get_int(exc, "start", start)) {
1106 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001107 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001108 if (!object)
1109 return -1;
1110 size = PyString_GET_SIZE(object);
1111 if (*start<0)
1112 *start = 0;
1113 if (*start>=size)
1114 *start = size-1;
1115 Py_DECREF(object);
1116 return 0;
1117 }
1118 return -1;
1119}
1120
1121
Martin v. Löwis18e16552006-02-15 17:27:45 +00001122int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001123{
1124 return PyUnicodeEncodeError_GetStart(exc, start);
1125}
1126
1127
Martin v. Löwis18e16552006-02-15 17:27:45 +00001128int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001129{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001130 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001131}
1132
1133
Martin v. Löwis18e16552006-02-15 17:27:45 +00001134int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001135{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001136 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001137}
1138
1139
Martin v. Löwis18e16552006-02-15 17:27:45 +00001140int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001141{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001142 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001143}
1144
1145
Martin v. Löwis18e16552006-02-15 17:27:45 +00001146int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001147{
1148 if (!get_int(exc, "end", end)) {
1149 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001150 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001151 if (!object)
1152 return -1;
1153 size = PyUnicode_GET_SIZE(object);
1154 if (*end<1)
1155 *end = 1;
1156 if (*end>size)
1157 *end = size;
1158 Py_DECREF(object);
1159 return 0;
1160 }
1161 return -1;
1162}
1163
1164
Martin v. Löwis18e16552006-02-15 17:27:45 +00001165int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001166{
1167 if (!get_int(exc, "end", end)) {
1168 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001169 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001170 if (!object)
1171 return -1;
1172 size = PyString_GET_SIZE(object);
1173 if (*end<1)
1174 *end = 1;
1175 if (*end>size)
1176 *end = size;
1177 Py_DECREF(object);
1178 return 0;
1179 }
1180 return -1;
1181}
1182
1183
Martin v. Löwis18e16552006-02-15 17:27:45 +00001184int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001185{
1186 return PyUnicodeEncodeError_GetEnd(exc, start);
1187}
1188
1189
Martin v. Löwis18e16552006-02-15 17:27:45 +00001190int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001191{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001192 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001193}
1194
1195
Martin v. Löwis18e16552006-02-15 17:27:45 +00001196int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001197{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001198 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001199}
1200
1201
Martin v. Löwis18e16552006-02-15 17:27:45 +00001202int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001203{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001204 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001205}
1206
1207
1208PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1209{
1210 return get_string(exc, "reason");
1211}
1212
1213
1214PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1215{
1216 return get_string(exc, "reason");
1217}
1218
1219
1220PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1221{
1222 return get_string(exc, "reason");
1223}
1224
1225
1226int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1227{
1228 return set_string(exc, "reason", reason);
1229}
1230
1231
1232int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1233{
1234 return set_string(exc, "reason", reason);
1235}
1236
1237
1238int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1239{
1240 return set_string(exc, "reason", reason);
1241}
1242
1243
1244static PyObject *
1245UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1246{
1247 PyObject *rtnval = NULL;
1248 PyObject *encoding;
1249 PyObject *object;
1250 PyObject *start;
1251 PyObject *end;
1252 PyObject *reason;
1253
1254 if (!(self = get_self(args)))
1255 return NULL;
1256
1257 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1258 return NULL;
1259
Brett Cannonbf364092006-03-01 04:25:17 +00001260 if (!set_args_and_message(self, args)) {
1261 Py_DECREF(args);
1262 return NULL;
1263 }
1264
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001265 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1266 &PyString_Type, &encoding,
1267 objecttype, &object,
1268 &PyInt_Type, &start,
1269 &PyInt_Type, &end,
1270 &PyString_Type, &reason))
Walter Dörwalde98147a2003-08-14 20:59:07 +00001271 goto finally;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001272
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001273 if (PyObject_SetAttrString(self, "encoding", encoding))
1274 goto finally;
1275 if (PyObject_SetAttrString(self, "object", object))
1276 goto finally;
1277 if (PyObject_SetAttrString(self, "start", start))
1278 goto finally;
1279 if (PyObject_SetAttrString(self, "end", end))
1280 goto finally;
1281 if (PyObject_SetAttrString(self, "reason", reason))
1282 goto finally;
1283
1284 Py_INCREF(Py_None);
1285 rtnval = Py_None;
1286
1287 finally:
1288 Py_DECREF(args);
1289 return rtnval;
1290}
1291
1292
1293static PyObject *
1294UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1295{
1296 return UnicodeError__init__(self, args, &PyUnicode_Type);
1297}
1298
1299static PyObject *
1300UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1301{
1302 PyObject *encodingObj = NULL;
1303 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001304 Py_ssize_t start;
1305 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001306 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001307 PyObject *result = NULL;
1308
1309 self = arg;
1310
1311 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1312 goto error;
1313
1314 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1315 goto error;
1316
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001317 if (PyUnicodeEncodeError_GetStart(self, &start))
1318 goto error;
1319
1320 if (PyUnicodeEncodeError_GetEnd(self, &end))
1321 goto error;
1322
1323 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1324 goto error;
1325
1326 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001327 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001328 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001329 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001330 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001331 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001332 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001333 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001334 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1335 result = PyString_FromFormat(
1336 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001337 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001338 badchar_str,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001339 start,
1340 PyString_AS_STRING(reasonObj)
1341 );
1342 }
1343 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001344 result = PyString_FromFormat(
1345 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001346 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001347 start,
1348 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001349 PyString_AS_STRING(reasonObj)
1350 );
1351 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001352
1353error:
1354 Py_XDECREF(reasonObj);
1355 Py_XDECREF(objectObj);
1356 Py_XDECREF(encodingObj);
1357 return result;
1358}
1359
1360static PyMethodDef UnicodeEncodeError_methods[] = {
1361 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1362 {"__str__", UnicodeEncodeError__str__, METH_O},
1363 {NULL, NULL}
1364};
1365
1366
1367PyObject * PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001368 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1369 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001370{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001371 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001372 encoding, object, length, start, end, reason);
1373}
1374
1375
1376static PyObject *
1377UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1378{
1379 return UnicodeError__init__(self, args, &PyString_Type);
1380}
1381
1382static PyObject *
1383UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1384{
1385 PyObject *encodingObj = NULL;
1386 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001387 Py_ssize_t start;
1388 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001389 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001390 PyObject *result = NULL;
1391
1392 self = arg;
1393
1394 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1395 goto error;
1396
1397 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1398 goto error;
1399
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001400 if (PyUnicodeDecodeError_GetStart(self, &start))
1401 goto error;
1402
1403 if (PyUnicodeDecodeError_GetEnd(self, &end))
1404 goto error;
1405
1406 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1407 goto error;
1408
1409 if (end==start+1) {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001410 /* FromFormat does not support %02x, so format that separately */
1411 char byte[4];
1412 PyOS_snprintf(byte, sizeof(byte), "%02x",
1413 ((int)PyString_AS_STRING(objectObj)[start])&0xff);
1414 result = PyString_FromFormat(
1415 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001416 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001417 byte,
1418 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001419 PyString_AS_STRING(reasonObj)
1420 );
1421 }
1422 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001423 result = PyString_FromFormat(
1424 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001425 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001426 start,
1427 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001428 PyString_AS_STRING(reasonObj)
1429 );
1430 }
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001431
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001432
1433error:
1434 Py_XDECREF(reasonObj);
1435 Py_XDECREF(objectObj);
1436 Py_XDECREF(encodingObj);
1437 return result;
1438}
1439
1440static PyMethodDef UnicodeDecodeError_methods[] = {
1441 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1442 {"__str__", UnicodeDecodeError__str__, METH_O},
1443 {NULL, NULL}
1444};
1445
1446
1447PyObject * PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001448 const char *encoding, const char *object, Py_ssize_t length,
1449 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001450{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001451 assert(length < INT_MAX);
1452 assert(start < INT_MAX);
1453 assert(end < INT_MAX);
Martin v. Löwis5cb69362006-04-14 09:08:42 +00001454 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",
1455 encoding, object, length, start, end, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001456}
1457
1458
1459static PyObject *
1460UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1461{
1462 PyObject *rtnval = NULL;
1463 PyObject *object;
1464 PyObject *start;
1465 PyObject *end;
1466 PyObject *reason;
1467
1468 if (!(self = get_self(args)))
1469 return NULL;
1470
1471 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1472 return NULL;
1473
Brett Cannonbf364092006-03-01 04:25:17 +00001474 if (!set_args_and_message(self, args)) {
1475 Py_DECREF(args);
1476 return NULL;
1477 }
1478
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001479 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1480 &PyUnicode_Type, &object,
1481 &PyInt_Type, &start,
1482 &PyInt_Type, &end,
1483 &PyString_Type, &reason))
1484 goto finally;
1485
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001486 if (PyObject_SetAttrString(self, "object", object))
1487 goto finally;
1488 if (PyObject_SetAttrString(self, "start", start))
1489 goto finally;
1490 if (PyObject_SetAttrString(self, "end", end))
1491 goto finally;
1492 if (PyObject_SetAttrString(self, "reason", reason))
1493 goto finally;
1494
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001495 rtnval = Py_None;
Brett Cannonbf364092006-03-01 04:25:17 +00001496 Py_INCREF(rtnval);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001497
1498 finally:
1499 Py_DECREF(args);
1500 return rtnval;
1501}
1502
1503
1504static PyObject *
1505UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1506{
1507 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001508 Py_ssize_t start;
1509 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001510 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001511 PyObject *result = NULL;
1512
1513 self = arg;
1514
1515 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1516 goto error;
1517
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001518 if (PyUnicodeTranslateError_GetStart(self, &start))
1519 goto error;
1520
1521 if (PyUnicodeTranslateError_GetEnd(self, &end))
1522 goto error;
1523
1524 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1525 goto error;
1526
1527 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001528 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001529 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001530 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001531 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001532 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001533 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001534 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001535 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1536 result = PyString_FromFormat(
1537 "can't translate character u'\\%s' in position %zd: %.400s",
1538 badchar_str,
1539 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001540 PyString_AS_STRING(reasonObj)
1541 );
1542 }
1543 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001544 result = PyString_FromFormat(
1545 "can't translate characters in position %zd-%zd: %.400s",
1546 start,
1547 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001548 PyString_AS_STRING(reasonObj)
1549 );
1550 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001551
1552error:
1553 Py_XDECREF(reasonObj);
1554 Py_XDECREF(objectObj);
1555 return result;
1556}
1557
1558static PyMethodDef UnicodeTranslateError_methods[] = {
1559 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1560 {"__str__", UnicodeTranslateError__str__, METH_O},
1561 {NULL, NULL}
1562};
1563
1564
1565PyObject * PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001566 const Py_UNICODE *object, Py_ssize_t length,
1567 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001568{
Martin v. Löwis5cb69362006-04-14 09:08:42 +00001569 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001570 object, length, start, end, reason);
1571}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001572#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001573
1574
Barry Warsaw675ac282000-05-26 19:05:16 +00001575
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001576/* Exception doc strings */
1577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001578PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001579
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001580PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001581
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001582PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001584PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001585
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001586PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001587
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001588PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001589
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001590PyDoc_STRVAR(ZeroDivisionError__doc__,
1591"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001592
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001593PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001594
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001595PyDoc_STRVAR(ValueError__doc__,
1596"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001599
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001600#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001601PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1602
1603PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1604
1605PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001606#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001608PyDoc_STRVAR(SystemError__doc__,
1609"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001610\n\
1611Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001612the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001613
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001614PyDoc_STRVAR(ReferenceError__doc__,
1615"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001617PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001618
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001619PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001620
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001621PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001622
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001623/* Warning category docstrings */
1624
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001625PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001627PyDoc_STRVAR(UserWarning__doc__,
1628"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001630PyDoc_STRVAR(DeprecationWarning__doc__,
1631"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001632
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001633PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001634"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001635"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001636
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001637PyDoc_STRVAR(SyntaxWarning__doc__,
1638"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001640PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001641"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643PyDoc_STRVAR(RuntimeWarning__doc__,
1644"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001645
Barry Warsaw9f007392002-08-14 15:51:29 +00001646PyDoc_STRVAR(FutureWarning__doc__,
1647"Base class for warnings about constructs that will change semantically "
1648"in the future.");
1649
Barry Warsaw675ac282000-05-26 19:05:16 +00001650
1651
1652/* module global functions */
1653static PyMethodDef functions[] = {
1654 /* Sentinel */
1655 {NULL, NULL}
1656};
1657
1658
1659
1660/* Global C API defined exceptions */
1661
Brett Cannonbf364092006-03-01 04:25:17 +00001662PyObject *PyExc_BaseException;
Barry Warsaw675ac282000-05-26 19:05:16 +00001663PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001664PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001665PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001666PyObject *PyExc_StandardError;
1667PyObject *PyExc_ArithmeticError;
1668PyObject *PyExc_LookupError;
1669
1670PyObject *PyExc_AssertionError;
1671PyObject *PyExc_AttributeError;
1672PyObject *PyExc_EOFError;
1673PyObject *PyExc_FloatingPointError;
1674PyObject *PyExc_EnvironmentError;
1675PyObject *PyExc_IOError;
1676PyObject *PyExc_OSError;
1677PyObject *PyExc_ImportError;
1678PyObject *PyExc_IndexError;
1679PyObject *PyExc_KeyError;
1680PyObject *PyExc_KeyboardInterrupt;
1681PyObject *PyExc_MemoryError;
1682PyObject *PyExc_NameError;
1683PyObject *PyExc_OverflowError;
1684PyObject *PyExc_RuntimeError;
1685PyObject *PyExc_NotImplementedError;
1686PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001687PyObject *PyExc_IndentationError;
1688PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001689PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001690PyObject *PyExc_SystemError;
1691PyObject *PyExc_SystemExit;
1692PyObject *PyExc_UnboundLocalError;
1693PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001694PyObject *PyExc_UnicodeEncodeError;
1695PyObject *PyExc_UnicodeDecodeError;
1696PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001697PyObject *PyExc_TypeError;
1698PyObject *PyExc_ValueError;
1699PyObject *PyExc_ZeroDivisionError;
1700#ifdef MS_WINDOWS
1701PyObject *PyExc_WindowsError;
1702#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001703#ifdef __VMS
1704PyObject *PyExc_VMSError;
1705#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001706
1707/* Pre-computed MemoryError instance. Best to create this as early as
Brett Cannonbf364092006-03-01 04:25:17 +00001708 * possible and not wait until a MemoryError is actually raised!
Barry Warsaw675ac282000-05-26 19:05:16 +00001709 */
1710PyObject *PyExc_MemoryErrorInst;
1711
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001712/* Predefined warning categories */
1713PyObject *PyExc_Warning;
1714PyObject *PyExc_UserWarning;
1715PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001716PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001717PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001718/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001719PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001720PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001721PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001722
Barry Warsaw675ac282000-05-26 19:05:16 +00001723
1724
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001725/* mapping between exception names and their PyObject ** */
1726static struct {
1727 char *name;
1728 PyObject **exc;
1729 PyObject **base; /* NULL == PyExc_StandardError */
1730 char *docstr;
1731 PyMethodDef *methods;
1732 int (*classinit)(PyObject *);
1733} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001734 /*
Brett Cannonbf364092006-03-01 04:25:17 +00001735 * The first four classes MUST appear in exactly this order
Barry Warsaw675ac282000-05-26 19:05:16 +00001736 */
Brett Cannonbf364092006-03-01 04:25:17 +00001737 {"BaseException", &PyExc_BaseException},
1738 {"Exception", &PyExc_Exception, &PyExc_BaseException, Exception__doc__},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001739 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1740 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001741 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1742 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001743 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1744 StandardError__doc__},
1745 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1746 /*
1747 * The rest appear in depth-first order of the hierarchy
1748 */
Brett Cannonbf364092006-03-01 04:25:17 +00001749 {"SystemExit", &PyExc_SystemExit, &PyExc_BaseException, SystemExit__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +00001750 SystemExit_methods},
Brett Cannonbf364092006-03-01 04:25:17 +00001751 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, &PyExc_BaseException,
1752 KeyboardInterrupt__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001753 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1754 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1755 EnvironmentError_methods},
1756 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1757 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1758#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001759 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001760 WindowsError__doc__},
1761#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001762#ifdef __VMS
1763 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1764 VMSError__doc__},
1765#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001766 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1767 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1768 {"NotImplementedError", &PyExc_NotImplementedError,
1769 &PyExc_RuntimeError, NotImplementedError__doc__},
1770 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1771 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1772 UnboundLocalError__doc__},
1773 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1774 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1775 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001776 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1777 IndentationError__doc__},
1778 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1779 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001780 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1781 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1782 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1783 IndexError__doc__},
1784 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001785 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001786 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1787 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1788 OverflowError__doc__},
1789 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1790 ZeroDivisionError__doc__},
1791 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1792 FloatingPointError__doc__},
1793 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1794 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001795#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001796 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1797 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1798 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1799 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1800 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1801 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001802#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001803 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001804 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1805 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001806 /* Warning categories */
1807 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1808 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1809 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1810 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001811 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1812 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001813 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001814 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001815 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1816 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001817 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1818 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001819 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1820 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001821 /* Sentinel */
1822 {NULL}
1823};
1824
1825
1826
Mark Hammonda2905272002-07-29 13:42:14 +00001827void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001828_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001829{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001830 char *modulename = "exceptions";
Martin v. Löwis18e16552006-02-15 17:27:45 +00001831 Py_ssize_t modnamesz = strlen(modulename);
Barry Warsaw675ac282000-05-26 19:05:16 +00001832 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001833 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001834
Tim Peters6d6c1a32001-08-02 04:15:00 +00001835 me = Py_InitModule(modulename, functions);
1836 if (me == NULL)
1837 goto err;
1838 mydict = PyModule_GetDict(me);
1839 if (mydict == NULL)
1840 goto err;
1841 bltinmod = PyImport_ImportModule("__builtin__");
1842 if (bltinmod == NULL)
1843 goto err;
1844 bdict = PyModule_GetDict(bltinmod);
1845 if (bdict == NULL)
1846 goto err;
1847 doc = PyString_FromString(module__doc__);
1848 if (doc == NULL)
1849 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001850
Tim Peters6d6c1a32001-08-02 04:15:00 +00001851 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001852 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001853 if (i < 0) {
1854 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001855 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001856 return;
1857 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001858
1859 /* This is the base class of all exceptions, so make it first. */
Brett Cannonbf364092006-03-01 04:25:17 +00001860 if (make_BaseException(modulename) ||
1861 PyDict_SetItemString(mydict, "BaseException", PyExc_BaseException) ||
1862 PyDict_SetItemString(bdict, "BaseException", PyExc_BaseException))
Barry Warsaw675ac282000-05-26 19:05:16 +00001863 {
Brett Cannonbf364092006-03-01 04:25:17 +00001864 Py_FatalError("Base class `BaseException' could not be created.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001865 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001866
Barry Warsaw675ac282000-05-26 19:05:16 +00001867 /* Now we can programmatically create all the remaining exceptions.
1868 * Remember to start the loop at 1 to skip Exceptions.
1869 */
1870 for (i=1; exctable[i].name; i++) {
1871 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001872 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1873 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001874
1875 (void)strcpy(cname, modulename);
1876 (void)strcat(cname, ".");
1877 (void)strcat(cname, exctable[i].name);
1878
1879 if (exctable[i].base == 0)
1880 base = PyExc_StandardError;
1881 else
1882 base = *exctable[i].base;
1883
1884 status = make_class(exctable[i].exc, base, cname,
1885 exctable[i].methods,
1886 exctable[i].docstr);
1887
1888 PyMem_DEL(cname);
1889
1890 if (status)
1891 Py_FatalError("Standard exception classes could not be created.");
1892
1893 if (exctable[i].classinit) {
1894 status = (*exctable[i].classinit)(*exctable[i].exc);
1895 if (status)
1896 Py_FatalError("An exception class could not be initialized.");
1897 }
1898
1899 /* Now insert the class into both this module and the __builtin__
1900 * module.
1901 */
1902 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1903 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1904 {
1905 Py_FatalError("Module dictionary insertion problem.");
1906 }
1907 }
1908
1909 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001910 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00001911 if (!args ||
1912 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1913 {
1914 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1915 }
1916 Py_DECREF(args);
1917
1918 /* We're done with __builtin__ */
1919 Py_DECREF(bltinmod);
1920}
1921
1922
Mark Hammonda2905272002-07-29 13:42:14 +00001923void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001924_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001925{
1926 int i;
1927
1928 Py_XDECREF(PyExc_MemoryErrorInst);
1929 PyExc_MemoryErrorInst = NULL;
1930
1931 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001932 /* clear the class's dictionary, freeing up circular references
1933 * between the class and its methods.
1934 */
1935 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1936 PyDict_Clear(cdict);
1937 Py_DECREF(cdict);
1938
1939 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001940 Py_XDECREF(*exctable[i].exc);
1941 *exctable[i].exc = NULL;
1942 }
1943}