blob: c9e516f598db2c096d99baebc8f3a47129b39c99 [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\
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000060 +-- GeneratorExit\n\
Barry Warsaw675ac282000-05-26 19:05:16 +000061 +-- StandardError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000062 | |\n\
63 | +-- KeyboardInterrupt\n\
64 | +-- ImportError\n\
65 | +-- EnvironmentError\n\
66 | | |\n\
67 | | +-- IOError\n\
68 | | +-- OSError\n\
69 | | |\n\
70 | | +-- WindowsError\n\
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000071 | | +-- VMSError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +000072 | |\n\
73 | +-- EOFError\n\
74 | +-- RuntimeError\n\
75 | | |\n\
76 | | +-- NotImplementedError\n\
77 | |\n\
78 | +-- NameError\n\
79 | | |\n\
80 | | +-- UnboundLocalError\n\
81 | |\n\
82 | +-- AttributeError\n\
83 | +-- SyntaxError\n\
84 | | |\n\
85 | | +-- IndentationError\n\
86 | | |\n\
87 | | +-- TabError\n\
88 | |\n\
89 | +-- TypeError\n\
90 | +-- AssertionError\n\
91 | +-- LookupError\n\
92 | | |\n\
93 | | +-- IndexError\n\
94 | | +-- KeyError\n\
95 | |\n\
96 | +-- ArithmeticError\n\
97 | | |\n\
98 | | +-- OverflowError\n\
99 | | +-- ZeroDivisionError\n\
100 | | +-- FloatingPointError\n\
101 | |\n\
102 | +-- ValueError\n\
103 | | |\n\
104 | | +-- UnicodeError\n\
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000105 | | |\n\
106 | | +-- UnicodeEncodeError\n\
107 | | +-- UnicodeDecodeError\n\
108 | | +-- UnicodeTranslateError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000109 | |\n\
Fred Drakebb9fa212001-10-05 21:50:08 +0000110 | +-- ReferenceError\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000111 | +-- SystemError\n\
112 | +-- MemoryError\n\
113 |\n\
114 +---Warning\n\
Barry Warsaw675ac282000-05-26 19:05:16 +0000115 |\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000116 +-- UserWarning\n\
117 +-- DeprecationWarning\n\
Neal Norwitzd68f5172002-05-29 15:54:55 +0000118 +-- PendingDeprecationWarning\n\
Barry Warsaw9667ed22001-01-23 16:08:34 +0000119 +-- SyntaxWarning\n\
Guido van Rossumae347b32001-08-23 02:56:07 +0000120 +-- OverflowWarning\n\
Barry Warsaw9f007392002-08-14 15:51:29 +0000121 +-- RuntimeWarning\n\
122 +-- FutureWarning"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000123);
Barry Warsaw675ac282000-05-26 19:05:16 +0000124
125
126/* Helper function for populating a dictionary with method wrappers. */
127static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000128populate_methods(PyObject *klass, PyObject *dict, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +0000129{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000130 PyObject *module;
131 int status = -1;
132
Barry Warsaw675ac282000-05-26 19:05:16 +0000133 if (!methods)
134 return 0;
135
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000136 module = PyString_FromString("exceptions");
137 if (!module)
138 return 0;
Barry Warsaw675ac282000-05-26 19:05:16 +0000139 while (methods->ml_name) {
140 /* get a wrapper for the built-in function */
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000141 PyObject *func = PyCFunction_NewEx(methods, NULL, module);
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000142 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +0000143
144 if (!func)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000145 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000146
147 /* turn the function into an unbound method */
148 if (!(meth = PyMethod_New(func, NULL, klass))) {
149 Py_DECREF(func);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000150 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000151 }
Barry Warsaw9667ed22001-01-23 16:08:34 +0000152
Barry Warsaw675ac282000-05-26 19:05:16 +0000153 /* add method to dictionary */
154 status = PyDict_SetItemString(dict, methods->ml_name, meth);
155 Py_DECREF(meth);
156 Py_DECREF(func);
157
158 /* stop now if an error occurred, otherwise do the next method */
159 if (status)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000160 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000161
162 methods++;
163 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +0000164 status = 0;
165 status:
166 Py_DECREF(module);
167 return status;
Barry Warsaw675ac282000-05-26 19:05:16 +0000168}
169
Barry Warsaw9667ed22001-01-23 16:08:34 +0000170
Barry Warsaw675ac282000-05-26 19:05:16 +0000171
172/* This function is used to create all subsequent exception classes. */
173static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000174make_class(PyObject **klass, PyObject *base,
175 char *name, PyMethodDef *methods,
176 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +0000177{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000178 PyObject *dict = PyDict_New();
179 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000180 int status = -1;
181
182 if (!dict)
183 return -1;
184
185 /* If an error occurs from here on, goto finally instead of explicitly
186 * returning NULL.
187 */
188
189 if (docstr) {
190 if (!(str = PyString_FromString(docstr)))
191 goto finally;
192 if (PyDict_SetItemString(dict, "__doc__", str))
193 goto finally;
194 }
195
196 if (!(*klass = PyErr_NewException(name, base, dict)))
197 goto finally;
198
199 if (populate_methods(*klass, dict, methods)) {
200 Py_DECREF(*klass);
201 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000202 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000203 }
204
205 status = 0;
206
207 finally:
208 Py_XDECREF(dict);
209 Py_XDECREF(str);
210 return status;
211}
212
213
214/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000215static PyObject *
216get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000217{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000218 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000219 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000220 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000221 if (PyExc_TypeError) {
222 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000223 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000224 }
225 return NULL;
226 }
227 return self;
228}
229
230
231
232/* Notes on bootstrapping the exception classes.
233 *
234 * First thing we create is the base class for all exceptions, called
235 * appropriately enough: Exception. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000236 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000237 * for TypeError, which can conditionally exist.
238 *
239 * Next, StandardError is created (which is quite simple) followed by
240 * TypeError, because the instantiation of other exceptions can potentially
241 * throw a TypeError. Once these exceptions are created, all the others
242 * can be created in any order. See the static exctable below for the
243 * explicit bootstrap order.
244 *
245 * All classes after Exception can be created using PyErr_NewException().
246 */
247
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000248PyDoc_STRVAR(Exception__doc__, "Common base class for all exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000249
250
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000251static PyObject *
252Exception__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000253{
254 int status;
255
256 if (!(self = get_self(args)))
257 return NULL;
258
259 /* set args attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000260 /* XXX size is only a hint */
261 args = PySequence_GetSlice(args, 1, PySequence_Size(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000262 if (!args)
263 return NULL;
264 status = PyObject_SetAttrString(self, "args", args);
265 Py_DECREF(args);
266 if (status < 0)
267 return NULL;
268
269 Py_INCREF(Py_None);
270 return Py_None;
271}
272
273
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000274static PyObject *
275Exception__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000276{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000277 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000278
Fred Drake1aba5772000-08-15 15:46:16 +0000279 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000280 return NULL;
281
282 args = PyObject_GetAttrString(self, "args");
283 if (!args)
284 return NULL;
285
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000286 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000287 case 0:
288 out = PyString_FromString("");
289 break;
290 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000291 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000292 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000293 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000294 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000295 Py_DECREF(tmp);
296 }
297 else
298 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000299 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000300 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000301 case -1:
302 PyErr_Clear();
303 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000304 default:
305 out = PyObject_Str(args);
306 break;
307 }
308
309 Py_DECREF(args);
310 return out;
311}
312
313
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000314static PyObject *
315Exception__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000316{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000317 PyObject *out;
318 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000319
Fred Drake1aba5772000-08-15 15:46:16 +0000320 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000321 return NULL;
322
323 args = PyObject_GetAttrString(self, "args");
324 if (!args)
325 return NULL;
326
327 out = PyObject_GetItem(args, index);
328 Py_DECREF(args);
329 return out;
330}
331
332
333static PyMethodDef
334Exception_methods[] = {
335 /* methods for the Exception class */
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000336 { "__getitem__", Exception__getitem__, METH_VARARGS},
337 { "__str__", Exception__str__, METH_VARARGS},
338 { "__init__", Exception__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000339 { NULL, NULL }
340};
341
342
343static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000344make_Exception(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000345{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000346 PyObject *dict = PyDict_New();
347 PyObject *str = NULL;
348 PyObject *name = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000349 int status = -1;
350
351 if (!dict)
352 return -1;
353
354 /* If an error occurs from here on, goto finally instead of explicitly
355 * returning NULL.
356 */
357
358 if (!(str = PyString_FromString(modulename)))
359 goto finally;
360 if (PyDict_SetItemString(dict, "__module__", str))
361 goto finally;
362 Py_DECREF(str);
363 if (!(str = PyString_FromString(Exception__doc__)))
364 goto finally;
365 if (PyDict_SetItemString(dict, "__doc__", str))
366 goto finally;
367
368 if (!(name = PyString_FromString("Exception")))
369 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000370
Barry Warsaw675ac282000-05-26 19:05:16 +0000371 if (!(PyExc_Exception = PyClass_New(NULL, dict, name)))
372 goto finally;
373
374 /* Now populate the dictionary with the method suite */
375 if (populate_methods(PyExc_Exception, dict, Exception_methods))
376 /* Don't need to reclaim PyExc_Exception here because that'll
377 * happen during interpreter shutdown.
378 */
379 goto finally;
380
381 status = 0;
382
383 finally:
384 Py_XDECREF(dict);
385 Py_XDECREF(str);
386 Py_XDECREF(name);
387 return status;
388}
389
390
391
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000392PyDoc_STRVAR(StandardError__doc__,
393"Base class for all standard Python exceptions.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000394
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000395PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000396
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000397PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000398PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000399
Barry Warsaw675ac282000-05-26 19:05:16 +0000400
401
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000402PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000403
404
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000405static PyObject *
406SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000407{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000408 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000409 int status;
410
411 if (!(self = get_self(args)))
412 return NULL;
413
414 /* Set args attribute. */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000415 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000416 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000417
Barry Warsaw675ac282000-05-26 19:05:16 +0000418 status = PyObject_SetAttrString(self, "args", args);
419 if (status < 0) {
420 Py_DECREF(args);
421 return NULL;
422 }
423
424 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000425 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000426 case 0:
427 Py_INCREF(Py_None);
428 code = Py_None;
429 break;
430 case 1:
431 code = PySequence_GetItem(args, 0);
432 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000433 case -1:
434 PyErr_Clear();
435 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000436 default:
437 Py_INCREF(args);
438 code = args;
439 break;
440 }
441
442 status = PyObject_SetAttrString(self, "code", code);
443 Py_DECREF(code);
444 Py_DECREF(args);
445 if (status < 0)
446 return NULL;
447
448 Py_INCREF(Py_None);
449 return Py_None;
450}
451
452
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000453static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000454 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000455 {NULL, NULL}
456};
457
458
459
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000460PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000461
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000462PyDoc_STRVAR(ImportError__doc__,
463"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000464
465
466
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000467PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000468
469
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000470static PyObject *
471EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000472{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000473 PyObject *item0 = NULL;
474 PyObject *item1 = NULL;
475 PyObject *item2 = NULL;
476 PyObject *subslice = NULL;
477 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000478
479 if (!(self = get_self(args)))
480 return NULL;
481
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000482 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000483 return NULL;
484
485 if (PyObject_SetAttrString(self, "args", args) ||
486 PyObject_SetAttrString(self, "errno", Py_None) ||
487 PyObject_SetAttrString(self, "strerror", Py_None) ||
488 PyObject_SetAttrString(self, "filename", Py_None))
489 {
490 goto finally;
491 }
492
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000493 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000494 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000495 /* Where a function has a single filename, such as open() or some
496 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
497 * called, giving a third argument which is the filename. But, so
498 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000499 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000500 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000501 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000502 * we hack args so that it only contains two items. This also
503 * means we need our own __str__() which prints out the filename
504 * when it was supplied.
505 */
506 item0 = PySequence_GetItem(args, 0);
507 item1 = PySequence_GetItem(args, 1);
508 item2 = PySequence_GetItem(args, 2);
509 if (!item0 || !item1 || !item2)
510 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000511
Barry Warsaw675ac282000-05-26 19:05:16 +0000512 if (PyObject_SetAttrString(self, "errno", item0) ||
513 PyObject_SetAttrString(self, "strerror", item1) ||
514 PyObject_SetAttrString(self, "filename", item2))
515 {
516 goto finally;
517 }
518
519 subslice = PySequence_GetSlice(args, 0, 2);
520 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
521 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000522 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000523
524 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000525 /* Used when PyErr_SetFromErrno() is called and no filename
526 * argument is given.
527 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000528 item0 = PySequence_GetItem(args, 0);
529 item1 = PySequence_GetItem(args, 1);
530 if (!item0 || !item1)
531 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000532
Barry Warsaw675ac282000-05-26 19:05:16 +0000533 if (PyObject_SetAttrString(self, "errno", item0) ||
534 PyObject_SetAttrString(self, "strerror", item1))
535 {
536 goto finally;
537 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000538 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000539
540 case -1:
541 PyErr_Clear();
542 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000543 }
544
545 Py_INCREF(Py_None);
546 rtnval = Py_None;
547
548 finally:
549 Py_DECREF(args);
550 Py_XDECREF(item0);
551 Py_XDECREF(item1);
552 Py_XDECREF(item2);
553 Py_XDECREF(subslice);
554 return rtnval;
555}
556
557
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000558static PyObject *
559EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000560{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000561 PyObject *originalself = self;
562 PyObject *filename;
563 PyObject *serrno;
564 PyObject *strerror;
565 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000566
Fred Drake1aba5772000-08-15 15:46:16 +0000567 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000568 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000569
Barry Warsaw675ac282000-05-26 19:05:16 +0000570 filename = PyObject_GetAttrString(self, "filename");
571 serrno = PyObject_GetAttrString(self, "errno");
572 strerror = PyObject_GetAttrString(self, "strerror");
573 if (!filename || !serrno || !strerror)
574 goto finally;
575
576 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000577 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
578 PyObject *repr = PyObject_Repr(filename);
579 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000580
581 if (!fmt || !repr || !tuple) {
582 Py_XDECREF(fmt);
583 Py_XDECREF(repr);
584 Py_XDECREF(tuple);
585 goto finally;
586 }
587
588 PyTuple_SET_ITEM(tuple, 0, serrno);
589 PyTuple_SET_ITEM(tuple, 1, strerror);
590 PyTuple_SET_ITEM(tuple, 2, repr);
591
592 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 if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000601 PyObject *fmt = PyString_FromString("[Errno %s] %s");
602 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000603
604 if (!fmt || !tuple) {
605 Py_XDECREF(fmt);
606 Py_XDECREF(tuple);
607 goto finally;
608 }
609
610 PyTuple_SET_ITEM(tuple, 0, serrno);
611 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000612
Barry Warsaw675ac282000-05-26 19:05:16 +0000613 rtnval = PyString_Format(fmt, tuple);
614
615 Py_DECREF(fmt);
616 Py_DECREF(tuple);
617 /* already freed because tuple owned only reference */
618 serrno = NULL;
619 strerror = NULL;
620 }
621 else
622 /* The original Python code said:
623 *
624 * return StandardError.__str__(self)
625 *
626 * but there is no StandardError__str__() function; we happen to
627 * know that's just a pass through to Exception__str__().
628 */
629 rtnval = Exception__str__(originalself, args);
630
631 finally:
632 Py_XDECREF(filename);
633 Py_XDECREF(serrno);
634 Py_XDECREF(strerror);
635 return rtnval;
636}
637
638
639static
640PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000641 {"__init__", EnvironmentError__init__, METH_VARARGS},
642 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000643 {NULL, NULL}
644};
645
646
647
648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000649PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000650
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000651PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000652
653#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000654PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000655#endif /* MS_WINDOWS */
656
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000657#ifdef __VMS
658static char
659VMSError__doc__[] = "OpenVMS OS system call failed.";
660#endif
661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000662PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000664PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000665
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000666PyDoc_STRVAR(NotImplementedError__doc__,
667"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000668
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000669PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000670
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000671PyDoc_STRVAR(UnboundLocalError__doc__,
672"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000673
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000674PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000675
676
677
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000678PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000679
680
681static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000682SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000683{
Barry Warsaw87bec352000-08-18 05:05:37 +0000684 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000685 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000686
687 /* Additional class-creation time initializations */
688 if (!emptystring ||
689 PyObject_SetAttrString(klass, "msg", emptystring) ||
690 PyObject_SetAttrString(klass, "filename", Py_None) ||
691 PyObject_SetAttrString(klass, "lineno", Py_None) ||
692 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000693 PyObject_SetAttrString(klass, "text", Py_None) ||
694 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000695 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000696 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000697 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000698 Py_XDECREF(emptystring);
699 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000700}
701
702
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000703static PyObject *
704SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000705{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000706 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000707 int lenargs;
708
709 if (!(self = get_self(args)))
710 return NULL;
711
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000712 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000713 return NULL;
714
715 if (PyObject_SetAttrString(self, "args", args))
716 goto finally;
717
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000718 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000719 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000720 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000721 int status;
722
723 if (!item0)
724 goto finally;
725 status = PyObject_SetAttrString(self, "msg", item0);
726 Py_DECREF(item0);
727 if (status)
728 goto finally;
729 }
730 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000731 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000732 PyObject *filename = NULL, *lineno = NULL;
733 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000734 int status = 1;
735
736 if (!info)
737 goto finally;
738
739 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000740 if (filename != NULL) {
741 lineno = PySequence_GetItem(info, 1);
742 if (lineno != NULL) {
743 offset = PySequence_GetItem(info, 2);
744 if (offset != NULL) {
745 text = PySequence_GetItem(info, 3);
746 if (text != NULL) {
747 status =
748 PyObject_SetAttrString(self, "filename", filename)
749 || PyObject_SetAttrString(self, "lineno", lineno)
750 || PyObject_SetAttrString(self, "offset", offset)
751 || PyObject_SetAttrString(self, "text", text);
752 Py_DECREF(text);
753 }
754 Py_DECREF(offset);
755 }
756 Py_DECREF(lineno);
757 }
758 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000759 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000760 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000761
762 if (status)
763 goto finally;
764 }
765 Py_INCREF(Py_None);
766 rtnval = Py_None;
767
768 finally:
769 Py_DECREF(args);
770 return rtnval;
771}
772
773
Fred Drake185a29b2000-08-15 16:20:36 +0000774/* This is called "my_basename" instead of just "basename" to avoid name
775 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
776 defined, and Python does define that. */
777static char *
778my_basename(char *name)
779{
780 char *cp = name;
781 char *result = name;
782
783 if (name == NULL)
784 return "???";
785 while (*cp != '\0') {
786 if (*cp == SEP)
787 result = cp + 1;
788 ++cp;
789 }
790 return result;
791}
792
793
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000794static PyObject *
795SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000796{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000797 PyObject *msg;
798 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000799 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000800
Fred Drake1aba5772000-08-15 15:46:16 +0000801 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000802 return NULL;
803
804 if (!(msg = PyObject_GetAttrString(self, "msg")))
805 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000806
Barry Warsaw675ac282000-05-26 19:05:16 +0000807 str = PyObject_Str(msg);
808 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000809 result = str;
810
811 /* XXX -- do all the additional formatting with filename and
812 lineno here */
813
Guido van Rossum602d4512002-09-03 20:24:09 +0000814 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000815 int have_filename = 0;
816 int have_lineno = 0;
817 char *buffer = NULL;
818
Barry Warsaw77c9f502000-08-16 19:43:17 +0000819 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000820 have_filename = PyString_Check(filename);
821 else
822 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000823
824 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000825 have_lineno = PyInt_Check(lineno);
826 else
827 PyErr_Clear();
828
829 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000830 int bufsize = PyString_GET_SIZE(str) + 64;
831 if (have_filename)
832 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000833
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000834 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000835 if (buffer != NULL) {
836 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000837 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
838 PyString_AS_STRING(str),
839 my_basename(PyString_AS_STRING(filename)),
840 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000841 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000842 PyOS_snprintf(buffer, bufsize, "%s (%s)",
843 PyString_AS_STRING(str),
844 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000845 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000846 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
847 PyString_AS_STRING(str),
848 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000849
Fred Drake1aba5772000-08-15 15:46:16 +0000850 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000851 PyMem_FREE(buffer);
852
Fred Drake1aba5772000-08-15 15:46:16 +0000853 if (result == NULL)
854 result = str;
855 else
856 Py_DECREF(str);
857 }
858 }
859 Py_XDECREF(filename);
860 Py_XDECREF(lineno);
861 }
862 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000863}
864
865
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000866static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000867 {"__init__", SyntaxError__init__, METH_VARARGS},
868 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000869 {NULL, NULL}
870};
871
872
Guido van Rossum602d4512002-09-03 20:24:09 +0000873static PyObject *
874KeyError__str__(PyObject *self, PyObject *args)
875{
876 PyObject *argsattr;
877 PyObject *result;
878
879 if (!PyArg_ParseTuple(args, "O:__str__", &self))
880 return NULL;
881
882 if (!(argsattr = PyObject_GetAttrString(self, "args")))
883 return NULL;
884
885 /* If args is a tuple of exactly one item, apply repr to args[0].
886 This is done so that e.g. the exception raised by {}[''] prints
887 KeyError: ''
888 rather than the confusing
889 KeyError
890 alone. The downside is that if KeyError is raised with an explanatory
891 string, that string will be displayed in quotes. Too bad.
892 If args is anything else, use the default Exception__str__().
893 */
894 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
895 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
896 result = PyObject_Repr(key);
897 }
898 else
899 result = Exception__str__(self, args);
900
901 Py_DECREF(argsattr);
902 return result;
903}
904
905static PyMethodDef KeyError_methods[] = {
906 {"__str__", KeyError__str__, METH_VARARGS},
907 {NULL, NULL}
908};
909
910
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000911#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000912static
Martin v. Löwis18e16552006-02-15 17:27:45 +0000913int get_int(PyObject *exc, const char *name, Py_ssize_t *value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000914{
915 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
916
917 if (!attr)
918 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919 if (PyInt_Check(attr)) {
920 *value = PyInt_AS_LONG(attr);
921 } else if (PyLong_Check(attr)) {
922 *value = (size_t)PyLong_AsLongLong(attr);
923 if (*value == -1) {
924 Py_DECREF(attr);
925 return -1;
926 }
927 } else {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000928 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000929 Py_DECREF(attr);
930 return -1;
931 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000932 Py_DECREF(attr);
933 return 0;
934}
935
936
937static
Martin v. Löwis18e16552006-02-15 17:27:45 +0000938int set_ssize_t(PyObject *exc, const char *name, Py_ssize_t value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000939{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000940 PyObject *obj = PyInt_FromSsize_t(value);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000941 int result;
942
943 if (!obj)
944 return -1;
945 result = PyObject_SetAttrString(exc, (char *)name, obj);
946 Py_DECREF(obj);
947 return result;
948}
949
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000950static
951PyObject *get_string(PyObject *exc, const char *name)
952{
953 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
954
955 if (!attr)
956 return NULL;
957 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000958 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000959 Py_DECREF(attr);
960 return NULL;
961 }
962 return attr;
963}
964
965
966static
967int set_string(PyObject *exc, const char *name, const char *value)
968{
969 PyObject *obj = PyString_FromString(value);
970 int result;
971
972 if (!obj)
973 return -1;
974 result = PyObject_SetAttrString(exc, (char *)name, obj);
975 Py_DECREF(obj);
976 return result;
977}
978
979
980static
981PyObject *get_unicode(PyObject *exc, const char *name)
982{
983 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
984
985 if (!attr)
986 return NULL;
987 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +0000988 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000989 Py_DECREF(attr);
990 return NULL;
991 }
992 return attr;
993}
994
995PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
996{
997 return get_string(exc, "encoding");
998}
999
1000PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1001{
1002 return get_string(exc, "encoding");
1003}
1004
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001005PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
1006{
1007 return get_unicode(exc, "object");
1008}
1009
1010PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1011{
1012 return get_string(exc, "object");
1013}
1014
1015PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1016{
1017 return get_unicode(exc, "object");
1018}
1019
Martin v. Löwis18e16552006-02-15 17:27:45 +00001020int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001021{
1022 if (!get_int(exc, "start", start)) {
1023 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001024 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001025 if (!object)
1026 return -1;
1027 size = PyUnicode_GET_SIZE(object);
1028 if (*start<0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001029 *start = 0; /*XXX check for values <0*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001030 if (*start>=size)
1031 *start = size-1;
1032 Py_DECREF(object);
1033 return 0;
1034 }
1035 return -1;
1036}
1037
1038
Martin v. Löwis18e16552006-02-15 17:27:45 +00001039int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001040{
1041 if (!get_int(exc, "start", start)) {
1042 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001043 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001044 if (!object)
1045 return -1;
1046 size = PyString_GET_SIZE(object);
1047 if (*start<0)
1048 *start = 0;
1049 if (*start>=size)
1050 *start = size-1;
1051 Py_DECREF(object);
1052 return 0;
1053 }
1054 return -1;
1055}
1056
1057
Martin v. Löwis18e16552006-02-15 17:27:45 +00001058int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001059{
1060 return PyUnicodeEncodeError_GetStart(exc, start);
1061}
1062
1063
Martin v. Löwis18e16552006-02-15 17:27:45 +00001064int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001065{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001066 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001067}
1068
1069
Martin v. Löwis18e16552006-02-15 17:27:45 +00001070int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001071{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001072 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001073}
1074
1075
Martin v. Löwis18e16552006-02-15 17:27:45 +00001076int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001077{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001078 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001079}
1080
1081
Martin v. Löwis18e16552006-02-15 17:27:45 +00001082int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001083{
1084 if (!get_int(exc, "end", end)) {
1085 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001086 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001087 if (!object)
1088 return -1;
1089 size = PyUnicode_GET_SIZE(object);
1090 if (*end<1)
1091 *end = 1;
1092 if (*end>size)
1093 *end = size;
1094 Py_DECREF(object);
1095 return 0;
1096 }
1097 return -1;
1098}
1099
1100
Martin v. Löwis18e16552006-02-15 17:27:45 +00001101int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001102{
1103 if (!get_int(exc, "end", end)) {
1104 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001105 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001106 if (!object)
1107 return -1;
1108 size = PyString_GET_SIZE(object);
1109 if (*end<1)
1110 *end = 1;
1111 if (*end>size)
1112 *end = size;
1113 Py_DECREF(object);
1114 return 0;
1115 }
1116 return -1;
1117}
1118
1119
Martin v. Löwis18e16552006-02-15 17:27:45 +00001120int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001121{
1122 return PyUnicodeEncodeError_GetEnd(exc, start);
1123}
1124
1125
Martin v. Löwis18e16552006-02-15 17:27:45 +00001126int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001127{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001128 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001129}
1130
1131
Martin v. Löwis18e16552006-02-15 17:27:45 +00001132int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001133{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001134 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001135}
1136
1137
Martin v. Löwis18e16552006-02-15 17:27:45 +00001138int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001139{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001140 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001141}
1142
1143
1144PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1145{
1146 return get_string(exc, "reason");
1147}
1148
1149
1150PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1151{
1152 return get_string(exc, "reason");
1153}
1154
1155
1156PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1157{
1158 return get_string(exc, "reason");
1159}
1160
1161
1162int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1163{
1164 return set_string(exc, "reason", reason);
1165}
1166
1167
1168int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1169{
1170 return set_string(exc, "reason", reason);
1171}
1172
1173
1174int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1175{
1176 return set_string(exc, "reason", reason);
1177}
1178
1179
1180static PyObject *
1181UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1182{
1183 PyObject *rtnval = NULL;
1184 PyObject *encoding;
1185 PyObject *object;
1186 PyObject *start;
1187 PyObject *end;
1188 PyObject *reason;
1189
1190 if (!(self = get_self(args)))
1191 return NULL;
1192
1193 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1194 return NULL;
1195
1196 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1197 &PyString_Type, &encoding,
1198 objecttype, &object,
1199 &PyInt_Type, &start,
1200 &PyInt_Type, &end,
1201 &PyString_Type, &reason))
Walter Dörwalde98147a2003-08-14 20:59:07 +00001202 goto finally;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001203
1204 if (PyObject_SetAttrString(self, "args", args))
1205 goto finally;
1206
1207 if (PyObject_SetAttrString(self, "encoding", encoding))
1208 goto finally;
1209 if (PyObject_SetAttrString(self, "object", object))
1210 goto finally;
1211 if (PyObject_SetAttrString(self, "start", start))
1212 goto finally;
1213 if (PyObject_SetAttrString(self, "end", end))
1214 goto finally;
1215 if (PyObject_SetAttrString(self, "reason", reason))
1216 goto finally;
1217
1218 Py_INCREF(Py_None);
1219 rtnval = Py_None;
1220
1221 finally:
1222 Py_DECREF(args);
1223 return rtnval;
1224}
1225
1226
1227static PyObject *
1228UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1229{
1230 return UnicodeError__init__(self, args, &PyUnicode_Type);
1231}
1232
1233static PyObject *
1234UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1235{
1236 PyObject *encodingObj = NULL;
1237 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001238 Py_ssize_t start;
1239 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001240 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001241 PyObject *result = NULL;
1242
1243 self = arg;
1244
1245 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1246 goto error;
1247
1248 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1249 goto error;
1250
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001251 if (PyUnicodeEncodeError_GetStart(self, &start))
1252 goto error;
1253
1254 if (PyUnicodeEncodeError_GetEnd(self, &end))
1255 goto error;
1256
1257 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1258 goto error;
1259
1260 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001261 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001262 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001263 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001264 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001265 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001266 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001267 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001268 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1269 result = PyString_FromFormat(
1270 "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001271 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001272 badchar_str,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001273 start,
1274 PyString_AS_STRING(reasonObj)
1275 );
1276 }
1277 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001278 result = PyString_FromFormat(
1279 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001280 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001281 start,
1282 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001283 PyString_AS_STRING(reasonObj)
1284 );
1285 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001286
1287error:
1288 Py_XDECREF(reasonObj);
1289 Py_XDECREF(objectObj);
1290 Py_XDECREF(encodingObj);
1291 return result;
1292}
1293
1294static PyMethodDef UnicodeEncodeError_methods[] = {
1295 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1296 {"__str__", UnicodeEncodeError__str__, METH_O},
1297 {NULL, NULL}
1298};
1299
1300
1301PyObject * PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001302 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1303 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001304{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001305 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001306 encoding, object, length, start, end, reason);
1307}
1308
1309
1310static PyObject *
1311UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1312{
1313 return UnicodeError__init__(self, args, &PyString_Type);
1314}
1315
1316static PyObject *
1317UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1318{
1319 PyObject *encodingObj = NULL;
1320 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001321 Py_ssize_t start;
1322 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001323 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001324 PyObject *result = NULL;
1325
1326 self = arg;
1327
1328 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1329 goto error;
1330
1331 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1332 goto error;
1333
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001334 if (PyUnicodeDecodeError_GetStart(self, &start))
1335 goto error;
1336
1337 if (PyUnicodeDecodeError_GetEnd(self, &end))
1338 goto error;
1339
1340 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1341 goto error;
1342
1343 if (end==start+1) {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001344 /* FromFormat does not support %02x, so format that separately */
1345 char byte[4];
1346 PyOS_snprintf(byte, sizeof(byte), "%02x",
1347 ((int)PyString_AS_STRING(objectObj)[start])&0xff);
1348 result = PyString_FromFormat(
1349 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001350 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001351 byte,
1352 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001353 PyString_AS_STRING(reasonObj)
1354 );
1355 }
1356 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001357 result = PyString_FromFormat(
1358 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001359 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001360 start,
1361 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001362 PyString_AS_STRING(reasonObj)
1363 );
1364 }
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001365
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001366
1367error:
1368 Py_XDECREF(reasonObj);
1369 Py_XDECREF(objectObj);
1370 Py_XDECREF(encodingObj);
1371 return result;
1372}
1373
1374static PyMethodDef UnicodeDecodeError_methods[] = {
1375 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1376 {"__str__", UnicodeDecodeError__str__, METH_O},
1377 {NULL, NULL}
1378};
1379
1380
1381PyObject * PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001382 const char *encoding, const char *object, Py_ssize_t length,
1383 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001384{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001385 assert(length < INT_MAX);
1386 assert(start < INT_MAX);
1387 assert(end < INT_MAX);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001388 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
Martin v. Löwis18e16552006-02-15 17:27:45 +00001389 encoding, object, (int)length, (int)start, (int)end, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001390}
1391
1392
1393static PyObject *
1394UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1395{
1396 PyObject *rtnval = NULL;
1397 PyObject *object;
1398 PyObject *start;
1399 PyObject *end;
1400 PyObject *reason;
1401
1402 if (!(self = get_self(args)))
1403 return NULL;
1404
1405 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1406 return NULL;
1407
1408 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1409 &PyUnicode_Type, &object,
1410 &PyInt_Type, &start,
1411 &PyInt_Type, &end,
1412 &PyString_Type, &reason))
1413 goto finally;
1414
1415 if (PyObject_SetAttrString(self, "args", args))
1416 goto finally;
1417
1418 if (PyObject_SetAttrString(self, "object", object))
1419 goto finally;
1420 if (PyObject_SetAttrString(self, "start", start))
1421 goto finally;
1422 if (PyObject_SetAttrString(self, "end", end))
1423 goto finally;
1424 if (PyObject_SetAttrString(self, "reason", reason))
1425 goto finally;
1426
1427 Py_INCREF(Py_None);
1428 rtnval = Py_None;
1429
1430 finally:
1431 Py_DECREF(args);
1432 return rtnval;
1433}
1434
1435
1436static PyObject *
1437UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1438{
1439 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001440 Py_ssize_t start;
1441 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001442 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001443 PyObject *result = NULL;
1444
1445 self = arg;
1446
1447 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1448 goto error;
1449
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001450 if (PyUnicodeTranslateError_GetStart(self, &start))
1451 goto error;
1452
1453 if (PyUnicodeTranslateError_GetEnd(self, &end))
1454 goto error;
1455
1456 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1457 goto error;
1458
1459 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001460 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001461 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001462 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001463 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001464 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001465 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001466 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001467 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1468 result = PyString_FromFormat(
1469 "can't translate character u'\\%s' in position %zd: %.400s",
1470 badchar_str,
1471 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001472 PyString_AS_STRING(reasonObj)
1473 );
1474 }
1475 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001476 result = PyString_FromFormat(
1477 "can't translate characters in position %zd-%zd: %.400s",
1478 start,
1479 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001480 PyString_AS_STRING(reasonObj)
1481 );
1482 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001483
1484error:
1485 Py_XDECREF(reasonObj);
1486 Py_XDECREF(objectObj);
1487 return result;
1488}
1489
1490static PyMethodDef UnicodeTranslateError_methods[] = {
1491 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1492 {"__str__", UnicodeTranslateError__str__, METH_O},
1493 {NULL, NULL}
1494};
1495
1496
1497PyObject * PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001498 const Py_UNICODE *object, Py_ssize_t length,
1499 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001500{
1501 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1502 object, length, start, end, reason);
1503}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001504#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001505
1506
Barry Warsaw675ac282000-05-26 19:05:16 +00001507
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001508/* Exception doc strings */
1509
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001510PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001511
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001512PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001513
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001514PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001516PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001518PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001519
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001520PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(ZeroDivisionError__doc__,
1523"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001524
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001525PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001526
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001527PyDoc_STRVAR(ValueError__doc__,
1528"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001529
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001530PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001531
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001532#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001533PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1534
1535PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1536
1537PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001538#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001539
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001540PyDoc_STRVAR(SystemError__doc__,
1541"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001542\n\
1543Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001544the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001546PyDoc_STRVAR(ReferenceError__doc__,
1547"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001548
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001549PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001550
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001551PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001552
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001553PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001554
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001555/* Warning category docstrings */
1556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001557PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001558
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001559PyDoc_STRVAR(UserWarning__doc__,
1560"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001561
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001562PyDoc_STRVAR(DeprecationWarning__doc__,
1563"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001565PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001566"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001567"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001568
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001569PyDoc_STRVAR(SyntaxWarning__doc__,
1570"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001571
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001572PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001573"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001574
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001575PyDoc_STRVAR(RuntimeWarning__doc__,
1576"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001577
Barry Warsaw9f007392002-08-14 15:51:29 +00001578PyDoc_STRVAR(FutureWarning__doc__,
1579"Base class for warnings about constructs that will change semantically "
1580"in the future.");
1581
Barry Warsaw675ac282000-05-26 19:05:16 +00001582
1583
1584/* module global functions */
1585static PyMethodDef functions[] = {
1586 /* Sentinel */
1587 {NULL, NULL}
1588};
1589
1590
1591
1592/* Global C API defined exceptions */
1593
1594PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001595PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001596PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001597PyObject *PyExc_StandardError;
1598PyObject *PyExc_ArithmeticError;
1599PyObject *PyExc_LookupError;
1600
1601PyObject *PyExc_AssertionError;
1602PyObject *PyExc_AttributeError;
1603PyObject *PyExc_EOFError;
1604PyObject *PyExc_FloatingPointError;
1605PyObject *PyExc_EnvironmentError;
1606PyObject *PyExc_IOError;
1607PyObject *PyExc_OSError;
1608PyObject *PyExc_ImportError;
1609PyObject *PyExc_IndexError;
1610PyObject *PyExc_KeyError;
1611PyObject *PyExc_KeyboardInterrupt;
1612PyObject *PyExc_MemoryError;
1613PyObject *PyExc_NameError;
1614PyObject *PyExc_OverflowError;
1615PyObject *PyExc_RuntimeError;
1616PyObject *PyExc_NotImplementedError;
1617PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001618PyObject *PyExc_IndentationError;
1619PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001620PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001621PyObject *PyExc_SystemError;
1622PyObject *PyExc_SystemExit;
1623PyObject *PyExc_UnboundLocalError;
1624PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001625PyObject *PyExc_UnicodeEncodeError;
1626PyObject *PyExc_UnicodeDecodeError;
1627PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001628PyObject *PyExc_TypeError;
1629PyObject *PyExc_ValueError;
1630PyObject *PyExc_ZeroDivisionError;
1631#ifdef MS_WINDOWS
1632PyObject *PyExc_WindowsError;
1633#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001634#ifdef __VMS
1635PyObject *PyExc_VMSError;
1636#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001637
1638/* Pre-computed MemoryError instance. Best to create this as early as
1639 * possibly and not wait until a MemoryError is actually raised!
1640 */
1641PyObject *PyExc_MemoryErrorInst;
1642
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001643/* Predefined warning categories */
1644PyObject *PyExc_Warning;
1645PyObject *PyExc_UserWarning;
1646PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001647PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001648PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001649/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001650PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001651PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001652PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001653
Barry Warsaw675ac282000-05-26 19:05:16 +00001654
1655
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001656/* mapping between exception names and their PyObject ** */
1657static struct {
1658 char *name;
1659 PyObject **exc;
1660 PyObject **base; /* NULL == PyExc_StandardError */
1661 char *docstr;
1662 PyMethodDef *methods;
1663 int (*classinit)(PyObject *);
1664} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001665 /*
1666 * The first three classes MUST appear in exactly this order
1667 */
1668 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001669 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1670 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001671 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1672 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001673 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1674 StandardError__doc__},
1675 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1676 /*
1677 * The rest appear in depth-first order of the hierarchy
1678 */
1679 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1680 SystemExit_methods},
1681 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1682 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1683 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1684 EnvironmentError_methods},
1685 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1686 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1687#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001688 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001689 WindowsError__doc__},
1690#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001691#ifdef __VMS
1692 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1693 VMSError__doc__},
1694#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001695 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1696 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1697 {"NotImplementedError", &PyExc_NotImplementedError,
1698 &PyExc_RuntimeError, NotImplementedError__doc__},
1699 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1700 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1701 UnboundLocalError__doc__},
1702 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1703 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1704 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001705 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1706 IndentationError__doc__},
1707 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1708 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001709 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1710 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1711 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1712 IndexError__doc__},
1713 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001714 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001715 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1716 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1717 OverflowError__doc__},
1718 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1719 ZeroDivisionError__doc__},
1720 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1721 FloatingPointError__doc__},
1722 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1723 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001724#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001725 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1726 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1727 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1728 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1729 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1730 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001731#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001732 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001733 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1734 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001735 /* Warning categories */
1736 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1737 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1738 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1739 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001740 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1741 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001742 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001743 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001744 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1745 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001746 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1747 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001748 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1749 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001750 /* Sentinel */
1751 {NULL}
1752};
1753
1754
1755
Mark Hammonda2905272002-07-29 13:42:14 +00001756void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001757_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001758{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001759 char *modulename = "exceptions";
Martin v. Löwis18e16552006-02-15 17:27:45 +00001760 Py_ssize_t modnamesz = strlen(modulename);
Barry Warsaw675ac282000-05-26 19:05:16 +00001761 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001762 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001763
Tim Peters6d6c1a32001-08-02 04:15:00 +00001764 me = Py_InitModule(modulename, functions);
1765 if (me == NULL)
1766 goto err;
1767 mydict = PyModule_GetDict(me);
1768 if (mydict == NULL)
1769 goto err;
1770 bltinmod = PyImport_ImportModule("__builtin__");
1771 if (bltinmod == NULL)
1772 goto err;
1773 bdict = PyModule_GetDict(bltinmod);
1774 if (bdict == NULL)
1775 goto err;
1776 doc = PyString_FromString(module__doc__);
1777 if (doc == NULL)
1778 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001779
Tim Peters6d6c1a32001-08-02 04:15:00 +00001780 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001781 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001782 if (i < 0) {
1783 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001784 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001785 return;
1786 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001787
1788 /* This is the base class of all exceptions, so make it first. */
1789 if (make_Exception(modulename) ||
1790 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1791 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1792 {
1793 Py_FatalError("Base class `Exception' could not be created.");
1794 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001795
Barry Warsaw675ac282000-05-26 19:05:16 +00001796 /* Now we can programmatically create all the remaining exceptions.
1797 * Remember to start the loop at 1 to skip Exceptions.
1798 */
1799 for (i=1; exctable[i].name; i++) {
1800 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001801 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1802 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001803
1804 (void)strcpy(cname, modulename);
1805 (void)strcat(cname, ".");
1806 (void)strcat(cname, exctable[i].name);
1807
1808 if (exctable[i].base == 0)
1809 base = PyExc_StandardError;
1810 else
1811 base = *exctable[i].base;
1812
1813 status = make_class(exctable[i].exc, base, cname,
1814 exctable[i].methods,
1815 exctable[i].docstr);
1816
1817 PyMem_DEL(cname);
1818
1819 if (status)
1820 Py_FatalError("Standard exception classes could not be created.");
1821
1822 if (exctable[i].classinit) {
1823 status = (*exctable[i].classinit)(*exctable[i].exc);
1824 if (status)
1825 Py_FatalError("An exception class could not be initialized.");
1826 }
1827
1828 /* Now insert the class into both this module and the __builtin__
1829 * module.
1830 */
1831 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1832 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1833 {
1834 Py_FatalError("Module dictionary insertion problem.");
1835 }
1836 }
1837
1838 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001839 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00001840 if (!args ||
1841 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1842 {
1843 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1844 }
1845 Py_DECREF(args);
1846
1847 /* We're done with __builtin__ */
1848 Py_DECREF(bltinmod);
1849}
1850
1851
Mark Hammonda2905272002-07-29 13:42:14 +00001852void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001853_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001854{
1855 int i;
1856
1857 Py_XDECREF(PyExc_MemoryErrorInst);
1858 PyExc_MemoryErrorInst = NULL;
1859
1860 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001861 /* clear the class's dictionary, freeing up circular references
1862 * between the class and its methods.
1863 */
1864 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1865 PyDict_Clear(cdict);
1866 Py_DECREF(cdict);
1867
1868 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001869 Py_XDECREF(*exctable[i].exc);
1870 *exctable[i].exc = NULL;
1871 }
1872}