blob: 58e7c94b9ef280b42f57bc64b1ab7e8349b292a0 [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;
1241 char buffer[1000];
1242 PyObject *result = NULL;
1243
1244 self = arg;
1245
1246 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1247 goto error;
1248
1249 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1250 goto error;
1251
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001252 if (PyUnicodeEncodeError_GetStart(self, &start))
1253 goto error;
1254
1255 if (PyUnicodeEncodeError_GetEnd(self, &end))
1256 goto error;
1257
1258 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1259 goto error;
1260
1261 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001262 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
1263 char *format;
1264 if (badchar <= 0xff)
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001265 format = "'%.400s' codec can't encode character u'\\x%02x' in position %d: %.400s";
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001266 else if (badchar <= 0xffff)
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001267 format = "'%.400s' codec can't encode character u'\\u%04x' in position %d: %.400s";
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001268 else
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001269 format = "'%.400s' codec can't encode character u'\\U%08x' in position %d: %.400s";
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001270 PyOS_snprintf(buffer, sizeof(buffer),
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001271 format,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001272 PyString_AS_STRING(encodingObj),
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001273 badchar,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001274 start,
1275 PyString_AS_STRING(reasonObj)
1276 );
1277 }
1278 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001279 /* XXX %zd? */
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001280 PyOS_snprintf(buffer, sizeof(buffer),
1281 "'%.400s' codec can't encode characters in position %d-%d: %.400s",
1282 PyString_AS_STRING(encodingObj),
Martin v. Löwis18e16552006-02-15 17:27:45 +00001283 (int)start,
1284 (int)(end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001285 PyString_AS_STRING(reasonObj)
1286 );
1287 }
1288 result = PyString_FromString(buffer);
1289
1290error:
1291 Py_XDECREF(reasonObj);
1292 Py_XDECREF(objectObj);
1293 Py_XDECREF(encodingObj);
1294 return result;
1295}
1296
1297static PyMethodDef UnicodeEncodeError_methods[] = {
1298 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1299 {"__str__", UnicodeEncodeError__str__, METH_O},
1300 {NULL, NULL}
1301};
1302
1303
1304PyObject * PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001305 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1306 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001307{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001308 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001309 encoding, object, length, start, end, reason);
1310}
1311
1312
1313static PyObject *
1314UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1315{
1316 return UnicodeError__init__(self, args, &PyString_Type);
1317}
1318
1319static PyObject *
1320UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1321{
1322 PyObject *encodingObj = NULL;
1323 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001324 Py_ssize_t start;
1325 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001326 PyObject *reasonObj = NULL;
1327 char buffer[1000];
1328 PyObject *result = NULL;
1329
1330 self = arg;
1331
1332 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1333 goto error;
1334
1335 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1336 goto error;
1337
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001338 if (PyUnicodeDecodeError_GetStart(self, &start))
1339 goto error;
1340
1341 if (PyUnicodeDecodeError_GetEnd(self, &end))
1342 goto error;
1343
1344 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1345 goto error;
1346
1347 if (end==start+1) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001348 /* XXX %zd? */
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001349 PyOS_snprintf(buffer, sizeof(buffer),
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001350 "'%.400s' codec can't decode byte 0x%02x in position %d: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001351 PyString_AS_STRING(encodingObj),
1352 ((int)PyString_AS_STRING(objectObj)[start])&0xff,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001353 (int)start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001354 PyString_AS_STRING(reasonObj)
1355 );
1356 }
1357 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001358 /* XXX %zd? */
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001359 PyOS_snprintf(buffer, sizeof(buffer),
1360 "'%.400s' codec can't decode bytes in position %d-%d: %.400s",
1361 PyString_AS_STRING(encodingObj),
Martin v. Löwis18e16552006-02-15 17:27:45 +00001362 (int)start,
1363 (int)(end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001364 PyString_AS_STRING(reasonObj)
1365 );
1366 }
1367 result = PyString_FromString(buffer);
1368
1369error:
1370 Py_XDECREF(reasonObj);
1371 Py_XDECREF(objectObj);
1372 Py_XDECREF(encodingObj);
1373 return result;
1374}
1375
1376static PyMethodDef UnicodeDecodeError_methods[] = {
1377 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1378 {"__str__", UnicodeDecodeError__str__, METH_O},
1379 {NULL, NULL}
1380};
1381
1382
1383PyObject * PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001384 const char *encoding, const char *object, Py_ssize_t length,
1385 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001386{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001387 assert(length < INT_MAX);
1388 assert(start < INT_MAX);
1389 assert(end < INT_MAX);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001390 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
Martin v. Löwis18e16552006-02-15 17:27:45 +00001391 encoding, object, (int)length, (int)start, (int)end, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001392}
1393
1394
1395static PyObject *
1396UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1397{
1398 PyObject *rtnval = NULL;
1399 PyObject *object;
1400 PyObject *start;
1401 PyObject *end;
1402 PyObject *reason;
1403
1404 if (!(self = get_self(args)))
1405 return NULL;
1406
1407 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1408 return NULL;
1409
1410 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1411 &PyUnicode_Type, &object,
1412 &PyInt_Type, &start,
1413 &PyInt_Type, &end,
1414 &PyString_Type, &reason))
1415 goto finally;
1416
1417 if (PyObject_SetAttrString(self, "args", args))
1418 goto finally;
1419
1420 if (PyObject_SetAttrString(self, "object", object))
1421 goto finally;
1422 if (PyObject_SetAttrString(self, "start", start))
1423 goto finally;
1424 if (PyObject_SetAttrString(self, "end", end))
1425 goto finally;
1426 if (PyObject_SetAttrString(self, "reason", reason))
1427 goto finally;
1428
1429 Py_INCREF(Py_None);
1430 rtnval = Py_None;
1431
1432 finally:
1433 Py_DECREF(args);
1434 return rtnval;
1435}
1436
1437
1438static PyObject *
1439UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1440{
1441 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001442 Py_ssize_t start;
1443 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001444 PyObject *reasonObj = NULL;
1445 char buffer[1000];
1446 PyObject *result = NULL;
1447
1448 self = arg;
1449
1450 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1451 goto error;
1452
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001453 if (PyUnicodeTranslateError_GetStart(self, &start))
1454 goto error;
1455
1456 if (PyUnicodeTranslateError_GetEnd(self, &end))
1457 goto error;
1458
1459 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1460 goto error;
1461
1462 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001463 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
1464 char *format;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001465 /* XXX %zd? */
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001466 if (badchar <= 0xff)
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001467 format = "can't translate character u'\\x%02x' in position %d: %.400s";
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001468 else if (badchar <= 0xffff)
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001469 format = "can't translate character u'\\u%04x' in position %d: %.400s";
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001470 else
Walter Dörwalda54b92b2003-08-12 17:34:49 +00001471 format = "can't translate character u'\\U%08x' in position %d: %.400s";
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001472 PyOS_snprintf(buffer, sizeof(buffer),
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001473 format,
1474 badchar,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001475 (int)start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001476 PyString_AS_STRING(reasonObj)
1477 );
1478 }
1479 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001480 /* XXX %zd? */
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001481 PyOS_snprintf(buffer, sizeof(buffer),
1482 "can't translate characters in position %d-%d: %.400s",
Martin v. Löwis18e16552006-02-15 17:27:45 +00001483 (int)start,
1484 (int)(end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001485 PyString_AS_STRING(reasonObj)
1486 );
1487 }
1488 result = PyString_FromString(buffer);
1489
1490error:
1491 Py_XDECREF(reasonObj);
1492 Py_XDECREF(objectObj);
1493 return result;
1494}
1495
1496static PyMethodDef UnicodeTranslateError_methods[] = {
1497 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1498 {"__str__", UnicodeTranslateError__str__, METH_O},
1499 {NULL, NULL}
1500};
1501
1502
1503PyObject * PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001504 const Py_UNICODE *object, Py_ssize_t length,
1505 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001506{
1507 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1508 object, length, start, end, reason);
1509}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001510#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001511
1512
Barry Warsaw675ac282000-05-26 19:05:16 +00001513
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001514/* Exception doc strings */
1515
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001516PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001517
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001518PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001519
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001520PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001521
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001522PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001523
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001524PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001525
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001526PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001527
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001528PyDoc_STRVAR(ZeroDivisionError__doc__,
1529"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001530
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001531PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001533PyDoc_STRVAR(ValueError__doc__,
1534"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001536PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001537
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001538#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001539PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1540
1541PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1542
1543PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001544#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001546PyDoc_STRVAR(SystemError__doc__,
1547"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001548\n\
1549Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001550the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001551
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001552PyDoc_STRVAR(ReferenceError__doc__,
1553"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001554
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001555PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001556
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001557PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001558
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001559PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001560
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001561/* Warning category docstrings */
1562
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001563PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001564
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001565PyDoc_STRVAR(UserWarning__doc__,
1566"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001567
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001568PyDoc_STRVAR(DeprecationWarning__doc__,
1569"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001570
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001571PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001572"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001573"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001574
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001575PyDoc_STRVAR(SyntaxWarning__doc__,
1576"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001577
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001578PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001579"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001580
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001581PyDoc_STRVAR(RuntimeWarning__doc__,
1582"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001583
Barry Warsaw9f007392002-08-14 15:51:29 +00001584PyDoc_STRVAR(FutureWarning__doc__,
1585"Base class for warnings about constructs that will change semantically "
1586"in the future.");
1587
Barry Warsaw675ac282000-05-26 19:05:16 +00001588
1589
1590/* module global functions */
1591static PyMethodDef functions[] = {
1592 /* Sentinel */
1593 {NULL, NULL}
1594};
1595
1596
1597
1598/* Global C API defined exceptions */
1599
1600PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001601PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001602PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001603PyObject *PyExc_StandardError;
1604PyObject *PyExc_ArithmeticError;
1605PyObject *PyExc_LookupError;
1606
1607PyObject *PyExc_AssertionError;
1608PyObject *PyExc_AttributeError;
1609PyObject *PyExc_EOFError;
1610PyObject *PyExc_FloatingPointError;
1611PyObject *PyExc_EnvironmentError;
1612PyObject *PyExc_IOError;
1613PyObject *PyExc_OSError;
1614PyObject *PyExc_ImportError;
1615PyObject *PyExc_IndexError;
1616PyObject *PyExc_KeyError;
1617PyObject *PyExc_KeyboardInterrupt;
1618PyObject *PyExc_MemoryError;
1619PyObject *PyExc_NameError;
1620PyObject *PyExc_OverflowError;
1621PyObject *PyExc_RuntimeError;
1622PyObject *PyExc_NotImplementedError;
1623PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001624PyObject *PyExc_IndentationError;
1625PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001626PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001627PyObject *PyExc_SystemError;
1628PyObject *PyExc_SystemExit;
1629PyObject *PyExc_UnboundLocalError;
1630PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001631PyObject *PyExc_UnicodeEncodeError;
1632PyObject *PyExc_UnicodeDecodeError;
1633PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001634PyObject *PyExc_TypeError;
1635PyObject *PyExc_ValueError;
1636PyObject *PyExc_ZeroDivisionError;
1637#ifdef MS_WINDOWS
1638PyObject *PyExc_WindowsError;
1639#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001640#ifdef __VMS
1641PyObject *PyExc_VMSError;
1642#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001643
1644/* Pre-computed MemoryError instance. Best to create this as early as
1645 * possibly and not wait until a MemoryError is actually raised!
1646 */
1647PyObject *PyExc_MemoryErrorInst;
1648
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001649/* Predefined warning categories */
1650PyObject *PyExc_Warning;
1651PyObject *PyExc_UserWarning;
1652PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001653PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001654PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001655/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001656PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001657PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001658PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001659
Barry Warsaw675ac282000-05-26 19:05:16 +00001660
1661
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001662/* mapping between exception names and their PyObject ** */
1663static struct {
1664 char *name;
1665 PyObject **exc;
1666 PyObject **base; /* NULL == PyExc_StandardError */
1667 char *docstr;
1668 PyMethodDef *methods;
1669 int (*classinit)(PyObject *);
1670} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001671 /*
1672 * The first three classes MUST appear in exactly this order
1673 */
1674 {"Exception", &PyExc_Exception},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001675 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1676 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001677 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1678 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001679 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1680 StandardError__doc__},
1681 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1682 /*
1683 * The rest appear in depth-first order of the hierarchy
1684 */
1685 {"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
1686 SystemExit_methods},
1687 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
1688 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1689 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1690 EnvironmentError_methods},
1691 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1692 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1693#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001694 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001695 WindowsError__doc__},
1696#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001697#ifdef __VMS
1698 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1699 VMSError__doc__},
1700#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001701 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1702 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1703 {"NotImplementedError", &PyExc_NotImplementedError,
1704 &PyExc_RuntimeError, NotImplementedError__doc__},
1705 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1706 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1707 UnboundLocalError__doc__},
1708 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1709 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1710 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001711 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1712 IndentationError__doc__},
1713 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1714 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001715 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1716 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1717 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1718 IndexError__doc__},
1719 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001720 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001721 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1722 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1723 OverflowError__doc__},
1724 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1725 ZeroDivisionError__doc__},
1726 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1727 FloatingPointError__doc__},
1728 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1729 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001730#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001731 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1732 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1733 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1734 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1735 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1736 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001737#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001738 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001739 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1740 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001741 /* Warning categories */
1742 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1743 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1744 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1745 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001746 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1747 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001748 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001749 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001750 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1751 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001752 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1753 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001754 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1755 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001756 /* Sentinel */
1757 {NULL}
1758};
1759
1760
1761
Mark Hammonda2905272002-07-29 13:42:14 +00001762void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001763_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001764{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001765 char *modulename = "exceptions";
Martin v. Löwis18e16552006-02-15 17:27:45 +00001766 Py_ssize_t modnamesz = strlen(modulename);
Barry Warsaw675ac282000-05-26 19:05:16 +00001767 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001768 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001769
Tim Peters6d6c1a32001-08-02 04:15:00 +00001770 me = Py_InitModule(modulename, functions);
1771 if (me == NULL)
1772 goto err;
1773 mydict = PyModule_GetDict(me);
1774 if (mydict == NULL)
1775 goto err;
1776 bltinmod = PyImport_ImportModule("__builtin__");
1777 if (bltinmod == NULL)
1778 goto err;
1779 bdict = PyModule_GetDict(bltinmod);
1780 if (bdict == NULL)
1781 goto err;
1782 doc = PyString_FromString(module__doc__);
1783 if (doc == NULL)
1784 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001785
Tim Peters6d6c1a32001-08-02 04:15:00 +00001786 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001787 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001788 if (i < 0) {
1789 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001790 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001791 return;
1792 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001793
1794 /* This is the base class of all exceptions, so make it first. */
1795 if (make_Exception(modulename) ||
1796 PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
1797 PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
1798 {
1799 Py_FatalError("Base class `Exception' could not be created.");
1800 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001801
Barry Warsaw675ac282000-05-26 19:05:16 +00001802 /* Now we can programmatically create all the remaining exceptions.
1803 * Remember to start the loop at 1 to skip Exceptions.
1804 */
1805 for (i=1; exctable[i].name; i++) {
1806 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001807 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1808 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001809
1810 (void)strcpy(cname, modulename);
1811 (void)strcat(cname, ".");
1812 (void)strcat(cname, exctable[i].name);
1813
1814 if (exctable[i].base == 0)
1815 base = PyExc_StandardError;
1816 else
1817 base = *exctable[i].base;
1818
1819 status = make_class(exctable[i].exc, base, cname,
1820 exctable[i].methods,
1821 exctable[i].docstr);
1822
1823 PyMem_DEL(cname);
1824
1825 if (status)
1826 Py_FatalError("Standard exception classes could not be created.");
1827
1828 if (exctable[i].classinit) {
1829 status = (*exctable[i].classinit)(*exctable[i].exc);
1830 if (status)
1831 Py_FatalError("An exception class could not be initialized.");
1832 }
1833
1834 /* Now insert the class into both this module and the __builtin__
1835 * module.
1836 */
1837 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1838 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1839 {
1840 Py_FatalError("Module dictionary insertion problem.");
1841 }
1842 }
1843
1844 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001845 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00001846 if (!args ||
1847 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1848 {
1849 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1850 }
1851 Py_DECREF(args);
1852
1853 /* We're done with __builtin__ */
1854 Py_DECREF(bltinmod);
1855}
1856
1857
Mark Hammonda2905272002-07-29 13:42:14 +00001858void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001859_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001860{
1861 int i;
1862
1863 Py_XDECREF(PyExc_MemoryErrorInst);
1864 PyExc_MemoryErrorInst = NULL;
1865
1866 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001867 /* clear the class's dictionary, freeing up circular references
1868 * between the class and its methods.
1869 */
1870 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1871 PyDict_Clear(cdict);
1872 Py_DECREF(cdict);
1873
1874 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001875 Py_XDECREF(*exctable[i].exc);
1876 *exctable[i].exc = NULL;
1877 }
1878}