blob: 24ea25ddee0b848ac26554bd2732c98b34b6ae45 [file] [log] [blame]
Barry Warsaw675ac282000-05-26 19:05:16 +00001/* This module provides the suite of standard class-based exceptions for
2 * Python's builtin module. This is a complete C implementation of what,
3 * in Python 1.5.2, was contained in the exceptions.py module. The problem
4 * there was that if exceptions.py could not be imported for some reason,
5 * the entire interpreter would abort.
6 *
7 * By moving the exceptions into C and statically linking, we can guarantee
8 * that the standard exceptions will always be available.
9 *
10 * history:
11 * 98-08-19 fl created (for pyexe)
12 * 00-02-08 fl updated for 1.5.2
13 * 26-May-2000 baw vetted for Python 1.6
14 *
15 * written by Fredrik Lundh
16 * modifications, additions, cleanups, and proofreading by Barry Warsaw
17 *
18 * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
19 */
20
21#include "Python.h"
Fred Drake185a29b2000-08-15 16:20:36 +000022#include "osdefs.h"
Barry Warsaw675ac282000-05-26 19:05:16 +000023
Tim Petersbf26e072000-07-12 04:02:10 +000024/* Caution: MS Visual C++ 6 errors if a single string literal exceeds
25 * 2Kb. So the module docstring has been broken roughly in half, using
26 * compile-time literal concatenation.
27 */
Skip Montanaro995895f2002-03-28 20:57:51 +000028
29/* NOTE: If the exception class hierarchy changes, don't forget to update
30 * Doc/lib/libexcs.tex!
31 */
32
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000033PyDoc_STRVAR(module__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +000034"Python's standard exception class hierarchy.\n\
35\n\
36Before Python 1.5, the standard exceptions were all simple string objects.\n\
37In Python 1.5, the standard exceptions were converted to classes organized\n\
38into a relatively flat hierarchy. String-based standard exceptions were\n\
39optional, or used as a fallback if some problem occurred while importing\n\
40the exception module. With Python 1.6, optional string-based standard\n\
41exceptions were removed (along with the -X command line flag).\n\
42\n\
43The class exceptions were implemented in such a way as to be almost\n\
44completely backward compatible. Some tricky uses of IOError could\n\
45potentially have broken, but by Python 1.6, all of these should have\n\
46been fixed. As of Python 1.6, the class-based standard exceptions are\n\
47now implemented in C, and are guaranteed to exist in the Python\n\
48interpreter.\n\
49\n\
50Here is a rundown of the class hierarchy. The classes found here are\n\
51inserted into both the exceptions module and the `built-in' module. It is\n\
52recommended that user defined class based exceptions be derived from the\n\
Tim Petersbf26e072000-07-12 04:02:10 +000053`Exception' class, although this is currently not enforced.\n"
54 /* keep string pieces "small" */
55"\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000056Exception\n\
57 |\n\
58 +-- SystemExit\n\
Guido van Rossum59d1d2b2001-04-20 19:13:02 +000059 +-- StopIteration\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000060 +-- StandardError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000061 | |\n\
62 | +-- KeyboardInterrupt\n\
63 | +-- ImportError\n\
64 | +-- EnvironmentError\n\
65 | | |\n\
66 | | +-- IOError\n\
67 | | +-- OSError\n\
68 | | |\n\
69 | | +-- WindowsError\n\
70 | |\n\
71 | +-- EOFError\n\
72 | +-- RuntimeError\n\
73 | | |\n\
74 | | +-- NotImplementedError\n\
75 | |\n\
76 | +-- NameError\n\
77 | | |\n\
78 | | +-- UnboundLocalError\n\
79 | |\n\
80 | +-- AttributeError\n\
81 | +-- SyntaxError\n\
82 | | |\n\
83 | | +-- IndentationError\n\
84 | | |\n\
85 | | +-- TabError\n\
86 | |\n\
87 | +-- TypeError\n\
88 | +-- AssertionError\n\
89 | +-- LookupError\n\
90 | | |\n\
91 | | +-- IndexError\n\
92 | | +-- KeyError\n\
93 | |\n\
94 | +-- ArithmeticError\n\
95 | | |\n\
96 | | +-- OverflowError\n\
97 | | +-- ZeroDivisionError\n\
98 | | +-- FloatingPointError\n\
99 | |\n\
100 | +-- ValueError\n\
101 | | |\n\
102 | | +-- UnicodeError\n\
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000103 | | |\n\
104 | | +-- UnicodeEncodeError\n\
105 | | +-- UnicodeDecodeError\n\
106 | | +-- UnicodeTranslateError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000107 | |\n\
Fred Drakebb9fa212001-10-05 21:50:08 +0000108 | +-- ReferenceError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000109 | +-- SystemError\n\
110 | +-- MemoryError\n\
111 |\n\
112 +---Warning\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000113 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000114 +-- UserWarning\n\
115 +-- DeprecationWarning\n\
Neal Norwitzd68f5172002-05-29 15:54:55 +0000116 +-- PendingDeprecationWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000117 +-- SyntaxWarning\n\
Guido van Rossumae347b32001-08-23 02:56:07 +0000118 +-- OverflowWarning\n\
Barry Warsaw9f007392002-08-14 15:51:29 +0000119 +-- RuntimeWarning\n\
120 +-- FutureWarning"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000121);
Barry Warsaw675ac282000-05-26 19:05:16 +0000122
123
124/* Helper function for populating a dictionary with method wrappers. */
125static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000126populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000127{
128 if (!methods)
129 return 0;
130
131 while (methods->ml_name) {
132 /* get a wrapper for the built-in function */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000133 PyObject *func = PyCFunction_New(methods, NULL);
134 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000135 int status;
136
137 if (!func)
138 return -1;
139
140 /* turn the function into an unbound method */
141 if (!(meth = PyMethod_New(func, NULL, klass))) {
142 Py_DECREF(func);
143 return -1;
144 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000145
Barry Warsaw675ac282000-05-26 19:05:16 +0000146 /* add method to dictionary */
147 status = PyDict_SetItemString(dict, methods->ml_name, meth);
148 Py_DECREF(meth);
149 Py_DECREF(func);
150
151 /* stop now if an error occurred, otherwise do the next method */
152 if (status)
153 return status;
154
155 methods++;
156 }
157 return 0;
158}
159
Barry Warsaw9667ed22001-01-23 16:08:34 +0000160
Barry Warsaw675ac282000-05-26 19:05:16 +0000161
162/* This function is used to create all subsequent exception classes. */
163static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000164make_class(PyObject **klass, PyObject *base,
165 char *name, PyMethodDef *methods,
166 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000167{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000168 PyObject *dict = PyDict_New();
169 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000170 int status = -1;
171
172 if (!dict)
173 return -1;
174
175 /* If an error occurs from here on, goto finally instead of explicitly
176 * returning NULL.
177 */
178
179 if (docstr) {
180 if (!(str = PyString_FromString(docstr)))
181 goto finally;
182 if (PyDict_SetItemString(dict, "__doc__", str))
183 goto finally;
184 }
185
186 if (!(*klass = PyErr_NewException(name, base, dict)))
187 goto finally;
188
189 if (populate_methods(*klass, dict, methods)) {
190 Py_DECREF(*klass);
191 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000192 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000193 }
194
195 status = 0;
196
197 finally:
198 Py_XDECREF(dict);
199 Py_XDECREF(str);
200 return status;
201}
202
203
204/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000205static PyObject *
206get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000207{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000208 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000209 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000210 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000211 if (PyExc_TypeError) {
212 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000213 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000214 }
215 return NULL;
216 }
217 return self;
218}
219
220
221
222/* Notes on bootstrapping the exception classes.
223 *
224 * First thing we create is the base class for all exceptions, called
225 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000226 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000227 * for TypeError, which can conditionally exist.
228 *
229 * Next, StandardError is created (which is quite simple) followed by
230 * TypeError, because the instantiation of other exceptions can potentially
231 * throw a TypeError. Once these exceptions are created, all the others
232 * can be created in any order. See the static exctable below for the
233 * explicit bootstrap order.
234 *
235 * All classes after Exception can be created using PyErr_NewException().
236 */
237
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000238PyDoc_STRVAR(Exception__doc__, "Common base class for all exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000239
240
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000241static PyObject *
242Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000243{
244 int status;
245
246 if (!(self = get_self(args)))
247 return NULL;
248
249 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000250 /* XXX size is only a hint */
251 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000252 if (!args)
253 return NULL;
254 status = PyObject_SetAttrString(self, "args", args);
255 Py_DECREF(args);
256 if (status < 0)
257 return NULL;
258
259 Py_INCREF(Py_None);
260 return Py_None;
261}
262
263
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000264static PyObject *
265Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000266{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000267 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000268
Fred Drake1aba5772000-08-15 15:46:16 +0000269 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000270 return NULL;
271
272 args = PyObject_GetAttrString(self, "args");
273 if (!args)
274 return NULL;
275
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000276 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000277 case 0:
278 out = PyString_FromString("");
279 break;
280 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000281 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000282 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000283 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000284 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000285 Py_DECREF(tmp);
286 }
287 else
288 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000289 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000290 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000291 default:
292 out = PyObject_Str(args);
293 break;
294 }
295
296 Py_DECREF(args);
297 return out;
298}
299
300
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000301static PyObject *
302Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000303{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000304 PyObject *out;
305 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000306
Fred Drake1aba5772000-08-15 15:46:16 +0000307 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000308 return NULL;
309
310 args = PyObject_GetAttrString(self, "args");
311 if (!args)
312 return NULL;
313
314 out = PyObject_GetItem(args, index);
315 Py_DECREF(args);
316 return out;
317}
318
319
320static PyMethodDef
321Exception_methods[] = {
322 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000323 { "__getitem__", Exception__getitem__, METH_VARARGS},
324 { "__str__", Exception__str__, METH_VARARGS},
325 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000326 { NULL, NULL }
327};
328
329
330static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000331make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000332{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000333 PyObject *dict = PyDict_New();
334 PyObject *str = NULL;
335 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000336 int status = -1;
337
338 if (!dict)
339 return -1;
340
341 /* If an error occurs from here on, goto finally instead of explicitly
342 * returning NULL.
343 */
344
345 if (!(str = PyString_FromString(modulename)))
346 goto finally;
347 if (PyDict_SetItemString(dict, "__module__", str))
348 goto finally;
349 Py_DECREF(str);
350 if (!(str = PyString_FromString(Exception__doc__)))
351 goto finally;
352 if (PyDict_SetItemString(dict, "__doc__", str))
353 goto finally;
354
355 if (!(name = PyString_FromString("Exception")))
356 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000357
Barry Warsaw675ac282000-05-26 19:05:16 +0000358 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
359 goto finally;
360
361 /* Now populate the dictionary with the method suite */
362 if (populate_methods(PyExc_Exception, dict, Exception_methods))
363 /* Don't need to reclaim PyExc_Exception here because that'll
364 * happen during interpreter shutdown.
365 */
366 goto finally;
367
368 status = 0;
369
370 finally:
371 Py_XDECREF(dict);
372 Py_XDECREF(str);
373 Py_XDECREF(name);
374 return status;
375}
376
377
378
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000379PyDoc_STRVAR(StandardError__doc__,
380"Base class for all standard Python exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000381
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000382PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000383
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000384PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000385
Barry Warsaw675ac282000-05-26 19:05:16 +0000386
387
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000388PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000389
390
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000391static PyObject *
392SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000393{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000394 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000395 int status;
396
397 if (!(self = get_self(args)))
398 return NULL;
399
400 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000401 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000402 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000403
Barry Warsaw675ac282000-05-26 19:05:16 +0000404 status = PyObject_SetAttrString(self, "args", args);
405 if (status < 0) {
406 Py_DECREF(args);
407 return NULL;
408 }
409
410 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000411 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000412 case 0:
413 Py_INCREF(Py_None);
414 code = Py_None;
415 break;
416 case 1:
417 code = PySequence_GetItem(args, 0);
418 break;
419 default:
420 Py_INCREF(args);
421 code = args;
422 break;
423 }
424
425 status = PyObject_SetAttrString(self, "code", code);
426 Py_DECREF(code);
427 Py_DECREF(args);
428 if (status < 0)
429 return NULL;
430
431 Py_INCREF(Py_None);
432 return Py_None;
433}
434
435
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000436static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000437 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000438 {NULL, NULL}
439};
440
441
442
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000443PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000444
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000445PyDoc_STRVAR(ImportError__doc__,
446"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000447
448
449
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000450PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000451
452
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000453static PyObject *
454EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000455{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000456 PyObject *item0 = NULL;
457 PyObject *item1 = NULL;
458 PyObject *item2 = NULL;
459 PyObject *subslice = NULL;
460 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000461
462 if (!(self = get_self(args)))
463 return NULL;
464
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000465 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000466 return NULL;
467
468 if (PyObject_SetAttrString(self, "args", args) ||
469 PyObject_SetAttrString(self, "errno", Py_None) ||
470 PyObject_SetAttrString(self, "strerror", Py_None) ||
471 PyObject_SetAttrString(self, "filename", Py_None))
472 {
473 goto finally;
474 }
475
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000476 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000477 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000478 /* Where a function has a single filename, such as open() or some
479 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
480 * called, giving a third argument which is the filename. But, so
481 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000482 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000483 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000484 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000485 * we hack args so that it only contains two items. This also
486 * means we need our own __str__() which prints out the filename
487 * when it was supplied.
488 */
489 item0 = PySequence_GetItem(args, 0);
490 item1 = PySequence_GetItem(args, 1);
491 item2 = PySequence_GetItem(args, 2);
492 if (!item0 || !item1 || !item2)
493 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000494
Barry Warsaw675ac282000-05-26 19:05:16 +0000495 if (PyObject_SetAttrString(self, "errno", item0) ||
496 PyObject_SetAttrString(self, "strerror", item1) ||
497 PyObject_SetAttrString(self, "filename", item2))
498 {
499 goto finally;
500 }
501
502 subslice = PySequence_GetSlice(args, 0, 2);
503 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
504 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000505 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000506
507 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000508 /* Used when PyErr_SetFromErrno() is called and no filename
509 * argument is given.
510 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000511 item0 = PySequence_GetItem(args, 0);
512 item1 = PySequence_GetItem(args, 1);
513 if (!item0 || !item1)
514 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000515
Barry Warsaw675ac282000-05-26 19:05:16 +0000516 if (PyObject_SetAttrString(self, "errno", item0) ||
517 PyObject_SetAttrString(self, "strerror", item1))
518 {
519 goto finally;
520 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000521 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000522 }
523
524 Py_INCREF(Py_None);
525 rtnval = Py_None;
526
527 finally:
528 Py_DECREF(args);
529 Py_XDECREF(item0);
530 Py_XDECREF(item1);
531 Py_XDECREF(item2);
532 Py_XDECREF(subslice);
533 return rtnval;
534}
535
536
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000537static PyObject *
538EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000539{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000540 PyObject *originalself = self;
541 PyObject *filename;
542 PyObject *serrno;
543 PyObject *strerror;
544 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000545
Fred Drake1aba5772000-08-15 15:46:16 +0000546 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000547 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000548
Barry Warsaw675ac282000-05-26 19:05:16 +0000549 filename = PyObject_GetAttrString(self, "filename");
550 serrno = PyObject_GetAttrString(self, "errno");
551 strerror = PyObject_GetAttrString(self, "strerror");
552 if (!filename || !serrno || !strerror)
553 goto finally;
554
555 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000556 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
557 PyObject *repr = PyObject_Repr(filename);
558 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000559
560 if (!fmt || !repr || !tuple) {
561 Py_XDECREF(fmt);
562 Py_XDECREF(repr);
563 Py_XDECREF(tuple);
564 goto finally;
565 }
566
567 PyTuple_SET_ITEM(tuple, 0, serrno);
568 PyTuple_SET_ITEM(tuple, 1, strerror);
569 PyTuple_SET_ITEM(tuple, 2, repr);
570
571 rtnval = PyString_Format(fmt, tuple);
572
573 Py_DECREF(fmt);
574 Py_DECREF(tuple);
575 /* already freed because tuple owned only reference */
576 serrno = NULL;
577 strerror = NULL;
578 }
579 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000580 PyObject *fmt = PyString_FromString("[Errno %s] %s");
581 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000582
583 if (!fmt || !tuple) {
584 Py_XDECREF(fmt);
585 Py_XDECREF(tuple);
586 goto finally;
587 }
588
589 PyTuple_SET_ITEM(tuple, 0, serrno);
590 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000591
Barry Warsaw675ac282000-05-26 19:05:16 +0000592 rtnval = PyString_Format(fmt, tuple);
593
594 Py_DECREF(fmt);
595 Py_DECREF(tuple);
596 /* already freed because tuple owned only reference */
597 serrno = NULL;
598 strerror = NULL;
599 }
600 else
601 /* The original Python code said:
602 *
603 * return StandardError.__str__(self)
604 *
605 * but there is no StandardError__str__() function; we happen to
606 * know that's just a pass through to Exception__str__().
607 */
608 rtnval = Exception__str__(originalself, args);
609
610 finally:
611 Py_XDECREF(filename);
612 Py_XDECREF(serrno);
613 Py_XDECREF(strerror);
614 return rtnval;
615}
616
617
618static
619PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000620 {"__init__", EnvironmentError__init__, METH_VARARGS},
621 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000622 {NULL, NULL}
623};
624
625
626
627
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000628PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000630PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000631
632#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000633PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000634#endif /* MS_WINDOWS */
635
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000636PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000637
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000638PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000640PyDoc_STRVAR(NotImplementedError__doc__,
641"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000643PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000644
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000645PyDoc_STRVAR(UnboundLocalError__doc__,
646"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000647
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000648PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000649
650
651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000652PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000653
654
655static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000656SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000657{
Barry Warsaw87bec352000-08-18 05:05:37 +0000658 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000659 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000660
661 /* Additional class-creation time initializations */
662 if (!emptystring ||
663 PyObject_SetAttrString(klass, "msg", emptystring) ||
664 PyObject_SetAttrString(klass, "filename", Py_None) ||
665 PyObject_SetAttrString(klass, "lineno", Py_None) ||
666 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000667 PyObject_SetAttrString(klass, "text", Py_None) ||
668 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000669 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000670 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000671 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000672 Py_XDECREF(emptystring);
673 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000674}
675
676
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000677static PyObject *
678SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000679{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000680 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000681 int lenargs;
682
683 if (!(self = get_self(args)))
684 return NULL;
685
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000686 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000687 return NULL;
688
689 if (PyObject_SetAttrString(self, "args", args))
690 goto finally;
691
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000692 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000693 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000694 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000695 int status;
696
697 if (!item0)
698 goto finally;
699 status = PyObject_SetAttrString(self, "msg", item0);
700 Py_DECREF(item0);
701 if (status)
702 goto finally;
703 }
704 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000705 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000706 PyObject *filename = NULL, *lineno = NULL;
707 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000708 int status = 1;
709
710 if (!info)
711 goto finally;
712
713 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000714 if (filename != NULL) {
715 lineno = PySequence_GetItem(info, 1);
716 if (lineno != NULL) {
717 offset = PySequence_GetItem(info, 2);
718 if (offset != NULL) {
719 text = PySequence_GetItem(info, 3);
720 if (text != NULL) {
721 status =
722 PyObject_SetAttrString(self, "filename", filename)
723 || PyObject_SetAttrString(self, "lineno", lineno)
724 || PyObject_SetAttrString(self, "offset", offset)
725 || PyObject_SetAttrString(self, "text", text);
726 Py_DECREF(text);
727 }
728 Py_DECREF(offset);
729 }
730 Py_DECREF(lineno);
731 }
732 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000733 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000734 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000735
736 if (status)
737 goto finally;
738 }
739 Py_INCREF(Py_None);
740 rtnval = Py_None;
741
742 finally:
743 Py_DECREF(args);
744 return rtnval;
745}
746
747
Fred Drake185a29b2000-08-15 16:20:36 +0000748/* This is called "my_basename" instead of just "basename" to avoid name
749 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
750 defined, and Python does define that. */
751static char *
752my_basename(char *name)
753{
754 char *cp = name;
755 char *result = name;
756
757 if (name == NULL)
758 return "???";
759 while (*cp != '\0') {
760 if (*cp == SEP)
761 result = cp + 1;
762 ++cp;
763 }
764 return result;
765}
766
767
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000768static PyObject *
769SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000770{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000771 PyObject *msg;
772 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000773 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000774
Fred Drake1aba5772000-08-15 15:46:16 +0000775 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000776 return NULL;
777
778 if (!(msg = PyObject_GetAttrString(self, "msg")))
779 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000780
Barry Warsaw675ac282000-05-26 19:05:16 +0000781 str = PyObject_Str(msg);
782 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000783 result = str;
784
785 /* XXX -- do all the additional formatting with filename and
786 lineno here */
787
788 if (PyString_Check(str)) {
789 int have_filename = 0;
790 int have_lineno = 0;
791 char *buffer = NULL;
792
Barry Warsaw77c9f502000-08-16 19:43:17 +0000793 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000794 have_filename = PyString_Check(filename);
795 else
796 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000797
798 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000799 have_lineno = PyInt_Check(lineno);
800 else
801 PyErr_Clear();
802
803 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000804 int bufsize = PyString_GET_SIZE(str) + 64;
805 if (have_filename)
806 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000807
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000808 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000809 if (buffer != NULL) {
810 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000811 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
812 PyString_AS_STRING(str),
813 my_basename(PyString_AS_STRING(filename)),
814 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000815 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000816 PyOS_snprintf(buffer, bufsize, "%s (%s)",
817 PyString_AS_STRING(str),
818 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000819 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000820 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
821 PyString_AS_STRING(str),
822 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000823
Fred Drake1aba5772000-08-15 15:46:16 +0000824 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000825 PyMem_FREE(buffer);
826
Fred Drake1aba5772000-08-15 15:46:16 +0000827 if (result == NULL)
828 result = str;
829 else
830 Py_DECREF(str);
831 }
832 }
833 Py_XDECREF(filename);
834 Py_XDECREF(lineno);
835 }
836 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000837}
838
839
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000840static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000841 {"__init__", SyntaxError__init__, METH_VARARGS},
842 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000843 {NULL, NULL}
844};
845
846
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000847static
848int get_int(PyObject *exc, const char *name, int *value)
849{
850 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
851
852 if (!attr)
853 return -1;
854 if (!PyInt_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000855 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000856 Py_DECREF(attr);
857 return -1;
858 }
859 *value = PyInt_AS_LONG(attr);
860 Py_DECREF(attr);
861 return 0;
862}
863
864
865static
866int set_int(PyObject *exc, const char *name, int value)
867{
868 PyObject *obj = PyInt_FromLong(value);
869 int result;
870
871 if (!obj)
872 return -1;
873 result = PyObject_SetAttrString(exc, (char *)name, obj);
874 Py_DECREF(obj);
875 return result;
876}
877
878
879static
880PyObject *get_string(PyObject *exc, const char *name)
881{
882 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
883
884 if (!attr)
885 return NULL;
886 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000887 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000888 Py_DECREF(attr);
889 return NULL;
890 }
891 return attr;
892}
893
894
895static
896int set_string(PyObject *exc, const char *name, const char *value)
897{
898 PyObject *obj = PyString_FromString(value);
899 int result;
900
901 if (!obj)
902 return -1;
903 result = PyObject_SetAttrString(exc, (char *)name, obj);
904 Py_DECREF(obj);
905 return result;
906}
907
908
909static
910PyObject *get_unicode(PyObject *exc, const char *name)
911{
912 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
913
914 if (!attr)
915 return NULL;
916 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000917 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000918 Py_DECREF(attr);
919 return NULL;
920 }
921 return attr;
922}
923
924PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
925{
926 return get_string(exc, "encoding");
927}
928
929PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
930{
931 return get_string(exc, "encoding");
932}
933
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000934PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
935{
936 return get_unicode(exc, "object");
937}
938
939PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
940{
941 return get_string(exc, "object");
942}
943
944PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
945{
946 return get_unicode(exc, "object");
947}
948
949int PyUnicodeEncodeError_GetStart(PyObject *exc, int *start)
950{
951 if (!get_int(exc, "start", start)) {
952 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
953 int size;
954 if (!object)
955 return -1;
956 size = PyUnicode_GET_SIZE(object);
957 if (*start<0)
958 *start = 0;
959 if (*start>=size)
960 *start = size-1;
961 Py_DECREF(object);
962 return 0;
963 }
964 return -1;
965}
966
967
968int PyUnicodeDecodeError_GetStart(PyObject *exc, int *start)
969{
970 if (!get_int(exc, "start", start)) {
971 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
972 int size;
973 if (!object)
974 return -1;
975 size = PyString_GET_SIZE(object);
976 if (*start<0)
977 *start = 0;
978 if (*start>=size)
979 *start = size-1;
980 Py_DECREF(object);
981 return 0;
982 }
983 return -1;
984}
985
986
987int PyUnicodeTranslateError_GetStart(PyObject *exc, int *start)
988{
989 return PyUnicodeEncodeError_GetStart(exc, start);
990}
991
992
993int PyUnicodeEncodeError_SetStart(PyObject *exc, int start)
994{
995 return set_int(exc, "start", start);
996}
997
998
999int PyUnicodeDecodeError_SetStart(PyObject *exc, int start)
1000{
1001 return set_int(exc, "start", start);
1002}
1003
1004
1005int PyUnicodeTranslateError_SetStart(PyObject *exc, int start)
1006{
1007 return set_int(exc, "start", start);
1008}
1009
1010
1011int PyUnicodeEncodeError_GetEnd(PyObject *exc, int *end)
1012{
1013 if (!get_int(exc, "end", end)) {
1014 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
1015 int size;
1016 if (!object)
1017 return -1;
1018 size = PyUnicode_GET_SIZE(object);
1019 if (*end<1)
1020 *end = 1;
1021 if (*end>size)
1022 *end = size;
1023 Py_DECREF(object);
1024 return 0;
1025 }
1026 return -1;
1027}
1028
1029
1030int PyUnicodeDecodeError_GetEnd(PyObject *exc, int *end)
1031{
1032 if (!get_int(exc, "end", end)) {
1033 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
1034 int size;
1035 if (!object)
1036 return -1;
1037 size = PyString_GET_SIZE(object);
1038 if (*end<1)
1039 *end = 1;
1040 if (*end>size)
1041 *end = size;
1042 Py_DECREF(object);
1043 return 0;
1044 }
1045 return -1;
1046}
1047
1048
1049int PyUnicodeTranslateError_GetEnd(PyObject *exc, int *start)
1050{
1051 return PyUnicodeEncodeError_GetEnd(exc, start);
1052}
1053
1054
1055int PyUnicodeEncodeError_SetEnd(PyObject *exc, int end)
1056{
1057 return set_int(exc, "end", end);
1058}
1059
1060
1061int PyUnicodeDecodeError_SetEnd(PyObject *exc, int end)
1062{
1063 return set_int(exc, "end", end);
1064}
1065
1066
1067int PyUnicodeTranslateError_SetEnd(PyObject *exc, int end)
1068{
1069 return set_int(exc, "end", end);
1070}
1071
1072
1073PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1074{
1075 return get_string(exc, "reason");
1076}
1077
1078
1079PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1080{
1081 return get_string(exc, "reason");
1082}
1083
1084
1085PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1086{
1087 return get_string(exc, "reason");
1088}
1089
1090
1091int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1092{
1093 return set_string(exc, "reason", reason);
1094}
1095
1096
1097int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1098{
1099 return set_string(exc, "reason", reason);
1100}
1101
1102
1103int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1104{
1105 return set_string(exc, "reason", reason);
1106}
1107
1108
1109static PyObject *
1110UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1111{
1112 PyObject *rtnval = NULL;
1113 PyObject *encoding;
1114 PyObject *object;
1115 PyObject *start;
1116 PyObject *end;
1117 PyObject *reason;
1118
1119 if (!(self = get_self(args)))
1120 return NULL;
1121
1122 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1123 return NULL;
1124
1125 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1126 &PyString_Type, &encoding,
1127 objecttype, &object,
1128 &PyInt_Type, &start,
1129 &PyInt_Type, &end,
1130 &PyString_Type, &reason))
1131 return NULL;
1132
1133 if (PyObject_SetAttrString(self, "args", args))
1134 goto finally;
1135
1136 if (PyObject_SetAttrString(self, "encoding", encoding))
1137 goto finally;
1138 if (PyObject_SetAttrString(self, "object", object))
1139 goto finally;
1140 if (PyObject_SetAttrString(self, "start", start))
1141 goto finally;
1142 if (PyObject_SetAttrString(self, "end", end))
1143 goto finally;
1144 if (PyObject_SetAttrString(self, "reason", reason))
1145 goto finally;
1146
1147 Py_INCREF(Py_None);
1148 rtnval = Py_None;
1149
1150 finally:
1151 Py_DECREF(args);
1152 return rtnval;
1153}
1154
1155
1156static PyObject *
1157UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1158{
1159 return UnicodeError__init__(self, args, &PyUnicode_Type);
1160}
1161
1162static PyObject *
1163UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1164{
1165 PyObject *encodingObj = NULL;
1166 PyObject *objectObj = NULL;
1167 int length;
1168 int start;
1169 int end;
1170 PyObject *reasonObj = NULL;
1171 char buffer[1000];
1172 PyObject *result = NULL;
1173
1174 self = arg;
1175
1176 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1177 goto error;
1178
1179 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1180 goto error;
1181
1182 length = PyUnicode_GET_SIZE(objectObj);
1183
1184 if (PyUnicodeEncodeError_GetStart(self, &start))
1185 goto error;
1186
1187 if (PyUnicodeEncodeError_GetEnd(self, &end))
1188 goto error;
1189
1190 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1191 goto error;
1192
1193 if (end==start+1) {
1194 PyOS_snprintf(buffer, sizeof(buffer),
1195 "'%.400s' codec can't encode character '\\u%x' in position %d: %.400s",
1196 PyString_AS_STRING(encodingObj),
1197 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1198 start,
1199 PyString_AS_STRING(reasonObj)
1200 );
1201 }
1202 else {
1203 PyOS_snprintf(buffer, sizeof(buffer),
1204 "'%.400s' codec can't encode characters in position %d-%d: %.400s",
1205 PyString_AS_STRING(encodingObj),
1206 start,
1207 end-1,
1208 PyString_AS_STRING(reasonObj)
1209 );
1210 }
1211 result = PyString_FromString(buffer);
1212
1213error:
1214 Py_XDECREF(reasonObj);
1215 Py_XDECREF(objectObj);
1216 Py_XDECREF(encodingObj);
1217 return result;
1218}
1219
1220static PyMethodDef UnicodeEncodeError_methods[] = {
1221 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1222 {"__str__", UnicodeEncodeError__str__, METH_O},
1223 {NULL, NULL}
1224};
1225
1226
1227PyObject * PyUnicodeEncodeError_Create(
1228 const char *encoding, const Py_UNICODE *object, int length,
1229 int start, int end, const char *reason)
1230{
1231 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#iis",
1232 encoding, object, length, start, end, reason);
1233}
1234
1235
1236static PyObject *
1237UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1238{
1239 return UnicodeError__init__(self, args, &PyString_Type);
1240}
1241
1242static PyObject *
1243UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1244{
1245 PyObject *encodingObj = NULL;
1246 PyObject *objectObj = NULL;
1247 int length;
1248 int start;
1249 int end;
1250 PyObject *reasonObj = NULL;
1251 char buffer[1000];
1252 PyObject *result = NULL;
1253
1254 self = arg;
1255
1256 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1257 goto error;
1258
1259 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1260 goto error;
1261
1262 length = PyString_GET_SIZE(objectObj);
1263
1264 if (PyUnicodeDecodeError_GetStart(self, &start))
1265 goto error;
1266
1267 if (PyUnicodeDecodeError_GetEnd(self, &end))
1268 goto error;
1269
1270 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1271 goto error;
1272
1273 if (end==start+1) {
1274 PyOS_snprintf(buffer, sizeof(buffer),
1275 "'%.400s' codec can't decode byte 0x%x in position %d: %.400s",
1276 PyString_AS_STRING(encodingObj),
1277 ((int)PyString_AS_STRING(objectObj)[start])&0xff,
1278 start,
1279 PyString_AS_STRING(reasonObj)
1280 );
1281 }
1282 else {
1283 PyOS_snprintf(buffer, sizeof(buffer),
1284 "'%.400s' codec can't decode bytes in position %d-%d: %.400s",
1285 PyString_AS_STRING(encodingObj),
1286 start,
1287 end-1,
1288 PyString_AS_STRING(reasonObj)
1289 );
1290 }
1291 result = PyString_FromString(buffer);
1292
1293error:
1294 Py_XDECREF(reasonObj);
1295 Py_XDECREF(objectObj);
1296 Py_XDECREF(encodingObj);
1297 return result;
1298}
1299
1300static PyMethodDef UnicodeDecodeError_methods[] = {
1301 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1302 {"__str__", UnicodeDecodeError__str__, METH_O},
1303 {NULL, NULL}
1304};
1305
1306
1307PyObject * PyUnicodeDecodeError_Create(
1308 const char *encoding, const char *object, int length,
1309 int start, int end, const char *reason)
1310{
1311 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
1312 encoding, object, length, start, end, reason);
1313}
1314
1315
1316static PyObject *
1317UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1318{
1319 PyObject *rtnval = NULL;
1320 PyObject *object;
1321 PyObject *start;
1322 PyObject *end;
1323 PyObject *reason;
1324
1325 if (!(self = get_self(args)))
1326 return NULL;
1327
1328 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1329 return NULL;
1330
1331 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1332 &PyUnicode_Type, &object,
1333 &PyInt_Type, &start,
1334 &PyInt_Type, &end,
1335 &PyString_Type, &reason))
1336 goto finally;
1337
1338 if (PyObject_SetAttrString(self, "args", args))
1339 goto finally;
1340
1341 if (PyObject_SetAttrString(self, "object", object))
1342 goto finally;
1343 if (PyObject_SetAttrString(self, "start", start))
1344 goto finally;
1345 if (PyObject_SetAttrString(self, "end", end))
1346 goto finally;
1347 if (PyObject_SetAttrString(self, "reason", reason))
1348 goto finally;
1349
1350 Py_INCREF(Py_None);
1351 rtnval = Py_None;
1352
1353 finally:
1354 Py_DECREF(args);
1355 return rtnval;
1356}
1357
1358
1359static PyObject *
1360UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1361{
1362 PyObject *objectObj = NULL;
1363 int length;
1364 int start;
1365 int end;
1366 PyObject *reasonObj = NULL;
1367 char buffer[1000];
1368 PyObject *result = NULL;
1369
1370 self = arg;
1371
1372 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1373 goto error;
1374
1375 length = PyUnicode_GET_SIZE(objectObj);
1376
1377 if (PyUnicodeTranslateError_GetStart(self, &start))
1378 goto error;
1379
1380 if (PyUnicodeTranslateError_GetEnd(self, &end))
1381 goto error;
1382
1383 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1384 goto error;
1385
1386 if (end==start+1) {
1387 PyOS_snprintf(buffer, sizeof(buffer),
1388 "can't translate character '\\u%x' in position %d: %.400s",
1389 (int)PyUnicode_AS_UNICODE(objectObj)[start],
1390 start,
1391 PyString_AS_STRING(reasonObj)
1392 );
1393 }
1394 else {
1395 PyOS_snprintf(buffer, sizeof(buffer),
1396 "can't translate characters in position %d-%d: %.400s",
1397 start,
1398 end-1,
1399 PyString_AS_STRING(reasonObj)
1400 );
1401 }
1402 result = PyString_FromString(buffer);
1403
1404error:
1405 Py_XDECREF(reasonObj);
1406 Py_XDECREF(objectObj);
1407 return result;
1408}
1409
1410static PyMethodDef UnicodeTranslateError_methods[] = {
1411 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1412 {"__str__", UnicodeTranslateError__str__, METH_O},
1413 {NULL, NULL}
1414};
1415
1416
1417PyObject * PyUnicodeTranslateError_Create(
1418 const Py_UNICODE *object, int length,
1419 int start, int end, const char *reason)
1420{
1421 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1422 object, length, start, end, reason);
1423}
1424
1425
Barry Warsaw675ac282000-05-26 19:05:16 +00001426
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001427/* Exception doc strings */
1428
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001429PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001430
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001431PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001432
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001433PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001434
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001435PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001436
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001437PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001438
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001439PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001440
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001441PyDoc_STRVAR(ZeroDivisionError__doc__,
1442"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001443
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001444PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001445
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001446PyDoc_STRVAR(ValueError__doc__,
1447"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001448
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001449PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001450
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001451PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1452
1453PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1454
1455PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
1456
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001457PyDoc_STRVAR(SystemError__doc__,
1458"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001459\n\
1460Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001461the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001462
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001463PyDoc_STRVAR(ReferenceError__doc__,
1464"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001465
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001466PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001467
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001468PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001469
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001470PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001471
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001472/* Warning category docstrings */
1473
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001474PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001475
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001476PyDoc_STRVAR(UserWarning__doc__,
1477"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001479PyDoc_STRVAR(DeprecationWarning__doc__,
1480"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001481
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001482PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001483"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001484"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001485
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001486PyDoc_STRVAR(SyntaxWarning__doc__,
1487"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001488
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001489PyDoc_STRVAR(OverflowWarning__doc__,
1490"Base class for warnings about numeric overflow.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001491
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001492PyDoc_STRVAR(RuntimeWarning__doc__,
1493"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001494
Barry Warsaw9f007392002-08-14 15:51:29 +00001495PyDoc_STRVAR(FutureWarning__doc__,
1496"Base class for warnings about constructs that will change semantically "
1497"in the future.");
1498
Barry Warsaw675ac282000-05-26 19:05:16 +00001499
1500
1501/* module global functions */
1502static PyMethodDef functions[] = {
1503 /* Sentinel */
1504 {NULL, NULL}
1505};
1506
1507
1508
1509/* Global C API defined exceptions */
1510
1511PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001512PyObject *PyExc_StopIteration;
Barry Warsaw675ac282000-05-26 19:05:16 +00001513PyObject *PyExc_StandardError;
1514PyObject *PyExc_ArithmeticError;
1515PyObject *PyExc_LookupError;
1516
1517PyObject *PyExc_AssertionError;
1518PyObject *PyExc_AttributeError;
1519PyObject *PyExc_EOFError;
1520PyObject *PyExc_FloatingPointError;
1521PyObject *PyExc_EnvironmentError;
1522PyObject *PyExc_IOError;
1523PyObject *PyExc_OSError;
1524PyObject *PyExc_ImportError;
1525PyObject *PyExc_IndexError;
1526PyObject *PyExc_KeyError;
1527PyObject *PyExc_KeyboardInterrupt;
1528PyObject *PyExc_MemoryError;
1529PyObject *PyExc_NameError;
1530PyObject *PyExc_OverflowError;
1531PyObject *PyExc_RuntimeError;
1532PyObject *PyExc_NotImplementedError;
1533PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001534PyObject *PyExc_IndentationError;
1535PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001536PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001537PyObject *PyExc_SystemError;
1538PyObject *PyExc_SystemExit;
1539PyObject *PyExc_UnboundLocalError;
1540PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001541PyObject *PyExc_UnicodeEncodeError;
1542PyObject *PyExc_UnicodeDecodeError;
1543PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001544PyObject *PyExc_TypeError;
1545PyObject *PyExc_ValueError;
1546PyObject *PyExc_ZeroDivisionError;
1547#ifdef MS_WINDOWS
1548PyObject *PyExc_WindowsError;
1549#endif
1550
1551/* Pre-computed MemoryError instance. Best to create this as early as
1552 * possibly and not wait until a MemoryError is actually raised!
1553 */
1554PyObject *PyExc_MemoryErrorInst;
1555
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001556/* Predefined warning categories */
1557PyObject *PyExc_Warning;
1558PyObject *PyExc_UserWarning;
1559PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001560PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001561PyObject *PyExc_SyntaxWarning;
Guido van Rossumae347b32001-08-23 02:56:07 +00001562PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001563PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001564PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001565
Barry Warsaw675ac282000-05-26 19:05:16 +00001566
1567
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001568/* mapping between exception names and their PyObject ** */
1569static struct {
1570 char *name;
1571 PyObject **exc;
1572 PyObject **base; /* NULL == PyExc_StandardError */
1573 char *docstr;
1574 PyMethodDef *methods;
1575 int (*classinit)(PyObject *);
1576} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001577 /*
1578 * The first three classes MUST appear in exactly this order
1579 */
1580 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001581 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1582 StopIteration__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001583 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1584 StandardError__doc__},
1585 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1586 /*
1587 * The rest appear in depth-first order of the hierarchy
1588 */
1589 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1590 SystemExit_methods},
1591 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1592 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1593 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1594 EnvironmentError_methods},
1595 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1596 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1597#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001598 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001599 WindowsError__doc__},
1600#endif /* MS_WINDOWS */
1601 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1602 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1603 {"NotImplementedError", &PyExc_NotImplementedError,
1604 &PyExc_RuntimeError, NotImplementedError__doc__},
1605 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1606 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1607 UnboundLocalError__doc__},
1608 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1609 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1610 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001611 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1612 IndentationError__doc__},
1613 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1614 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001615 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1616 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1617 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1618 IndexError__doc__},
1619 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
1620 KeyError__doc__},
1621 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1622 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1623 OverflowError__doc__},
1624 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1625 ZeroDivisionError__doc__},
1626 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1627 FloatingPointError__doc__},
1628 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1629 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001630 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1631 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1632 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1633 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1634 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1635 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Fred Drakebb9fa212001-10-05 21:50:08 +00001636 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001637 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1638 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001639 /* Warning categories */
1640 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1641 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1642 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1643 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001644 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1645 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001646 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Guido van Rossumae347b32001-08-23 02:56:07 +00001647 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1648 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001649 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1650 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001651 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1652 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001653 /* Sentinel */
1654 {NULL}
1655};
1656
1657
1658
Mark Hammonda2905272002-07-29 13:42:14 +00001659void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001660_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001661{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001662 char *modulename = "exceptions";
Barry Warsaw675ac282000-05-26 19:05:16 +00001663 int modnamesz = strlen(modulename);
1664 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001665 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001666
Tim Peters6d6c1a32001-08-02 04:15:00 +00001667 me = Py_InitModule(modulename, functions);
1668 if (me == NULL)
1669 goto err;
1670 mydict = PyModule_GetDict(me);
1671 if (mydict == NULL)
1672 goto err;
1673 bltinmod = PyImport_ImportModule("__builtin__");
1674 if (bltinmod == NULL)
1675 goto err;
1676 bdict = PyModule_GetDict(bltinmod);
1677 if (bdict == NULL)
1678 goto err;
1679 doc = PyString_FromString(module__doc__);
1680 if (doc == NULL)
1681 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001682
Tim Peters6d6c1a32001-08-02 04:15:00 +00001683 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001684 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001685 if (i < 0) {
1686 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001687 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001688 return;
1689 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001690
1691 /* This is the base class of all exceptions, so make it first. */
1692 if (make_Exception(modulename) ||
1693 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1694 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1695 {
1696 Py_FatalError("Base class `Exception' could not be created.");
1697 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001698
Barry Warsaw675ac282000-05-26 19:05:16 +00001699 /* Now we can programmatically create all the remaining exceptions.
1700 * Remember to start the loop at 1 to skip Exceptions.
1701 */
1702 for (i=1; exctable[i].name; i++) {
1703 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001704 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1705 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001706
1707 (void)strcpy(cname, modulename);
1708 (void)strcat(cname, ".");
1709 (void)strcat(cname, exctable[i].name);
1710
1711 if (exctable[i].base == 0)
1712 base = PyExc_StandardError;
1713 else
1714 base = *exctable[i].base;
1715
1716 status = make_class(exctable[i].exc, base, cname,
1717 exctable[i].methods,
1718 exctable[i].docstr);
1719
1720 PyMem_DEL(cname);
1721
1722 if (status)
1723 Py_FatalError("Standard exception classes could not be created.");
1724
1725 if (exctable[i].classinit) {
1726 status = (*exctable[i].classinit)(*exctable[i].exc);
1727 if (status)
1728 Py_FatalError("An exception class could not be initialized.");
1729 }
1730
1731 /* Now insert the class into both this module and the __builtin__
1732 * module.
1733 */
1734 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1735 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1736 {
1737 Py_FatalError("Module dictionary insertion problem.");
1738 }
1739 }
1740
1741 /* Now we need to pre-allocate a MemoryError instance */
1742 args = Py_BuildValue("()");
1743 if (!args ||
1744 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1745 {
1746 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1747 }
1748 Py_DECREF(args);
1749
1750 /* We're done with __builtin__ */
1751 Py_DECREF(bltinmod);
1752}
1753
1754
Mark Hammonda2905272002-07-29 13:42:14 +00001755void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001756_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001757{
1758 int i;
1759
1760 Py_XDECREF(PyExc_MemoryErrorInst);
1761 PyExc_MemoryErrorInst = NULL;
1762
1763 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001764 /* clear the class's dictionary, freeing up circular references
1765 * between the class and its methods.
1766 */
1767 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1768 PyDict_Clear(cdict);
1769 Py_DECREF(cdict);
1770
1771 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001772 Py_XDECREF(*exctable[i].exc);
1773 *exctable[i].exc = NULL;
1774 }
1775}