blob: 560aeb86e0509bc1dbed63043199f4ec782fe3a1 [file] [log] [blame]
Barry Warsaw675ac282000-05-26 19:05:16 +00001/* This module provides the suite of standard class-based exceptions for
2 * Python's builtin module. This is a complete C implementation of what,
3 * in Python 1.5.2, was contained in the exceptions.py module. The problem
4 * there was that if exceptions.py could not be imported for some reason,
5 * the entire interpreter would abort.
6 *
7 * By moving the exceptions into C and statically linking, we can guarantee
8 * that the standard exceptions will always be available.
9 *
Barry Warsaw675ac282000-05-26 19:05:16 +000010 *
11 * written by Fredrik Lundh
12 * modifications, additions, cleanups, and proofreading by Barry Warsaw
13 *
14 * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
15 */
16
17#include "Python.h"
Fred Drake185a29b2000-08-15 16:20:36 +000018#include "osdefs.h"
Barry Warsaw675ac282000-05-26 19:05:16 +000019
Tim Petersbf26e072000-07-12 04:02:10 +000020/* Caution: MS Visual C++ 6 errors if a single string literal exceeds
21 * 2Kb. So the module docstring has been broken roughly in half, using
22 * compile-time literal concatenation.
23 */
Skip Montanaro995895f2002-03-28 20:57:51 +000024
25/* NOTE: If the exception class hierarchy changes, don't forget to update
26 * Doc/lib/libexcs.tex!
27 */
28
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029PyDoc_STRVAR(module__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +000030"Python's standard exception class hierarchy.\n\
31\n\
Brett Cannonbf364092006-03-01 04:25:17 +000032Exceptions found here are defined both in the exceptions module and the \n\
33built-in namespace. It is recommended that user-defined exceptions inherit \n\
Brett Cannon54ac2942006-03-01 22:10:49 +000034from Exception. See the documentation for the exception inheritance hierarchy.\n\
Brett Cannonbf364092006-03-01 04:25:17 +000035"
36
Tim Petersbf26e072000-07-12 04:02:10 +000037 /* keep string pieces "small" */
Brett Cannon54ac2942006-03-01 22:10:49 +000038/* XXX(bcannon): exception hierarchy in Lib/test/exception_hierarchy.txt */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039);
Barry Warsaw675ac282000-05-26 19:05:16 +000040
41
42/* Helper function for populating a dictionary with method wrappers. */
43static int
Brett Cannonbf364092006-03-01 04:25:17 +000044populate_methods(PyObject *klass, PyMethodDef *methods)
Barry Warsaw675ac282000-05-26 19:05:16 +000045{
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000046 PyObject *module;
47 int status = -1;
48
Barry Warsaw675ac282000-05-26 19:05:16 +000049 if (!methods)
50 return 0;
51
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000052 module = PyString_FromString("exceptions");
53 if (!module)
54 return 0;
Barry Warsaw675ac282000-05-26 19:05:16 +000055 while (methods->ml_name) {
56 /* get a wrapper for the built-in function */
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000057 PyObject *func = PyCFunction_NewEx(methods, NULL, module);
Thomas Wouters0452d1f2000-07-22 18:45:06 +000058 PyObject *meth;
Barry Warsaw675ac282000-05-26 19:05:16 +000059
60 if (!func)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000061 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000062
63 /* turn the function into an unbound method */
64 if (!(meth = PyMethod_New(func, NULL, klass))) {
65 Py_DECREF(func);
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000066 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000067 }
Barry Warsaw9667ed22001-01-23 16:08:34 +000068
Barry Warsaw675ac282000-05-26 19:05:16 +000069 /* add method to dictionary */
Brett Cannonbf364092006-03-01 04:25:17 +000070 status = PyObject_SetAttrString(klass, methods->ml_name, meth);
Barry Warsaw675ac282000-05-26 19:05:16 +000071 Py_DECREF(meth);
72 Py_DECREF(func);
73
74 /* stop now if an error occurred, otherwise do the next method */
75 if (status)
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000076 goto status;
Barry Warsaw675ac282000-05-26 19:05:16 +000077
78 methods++;
79 }
Jeremy Hylton4f0dcc92003-01-31 18:33:18 +000080 status = 0;
81 status:
82 Py_DECREF(module);
83 return status;
Barry Warsaw675ac282000-05-26 19:05:16 +000084}
85
Barry Warsaw9667ed22001-01-23 16:08:34 +000086
Barry Warsaw675ac282000-05-26 19:05:16 +000087
88/* This function is used to create all subsequent exception classes. */
89static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +000090make_class(PyObject **klass, PyObject *base,
91 char *name, PyMethodDef *methods,
92 char *docstr)
Barry Warsaw675ac282000-05-26 19:05:16 +000093{
Thomas Wouters0452d1f2000-07-22 18:45:06 +000094 PyObject *dict = PyDict_New();
95 PyObject *str = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +000096 int status = -1;
97
98 if (!dict)
99 return -1;
100
101 /* If an error occurs from here on, goto finally instead of explicitly
102 * returning NULL.
103 */
104
105 if (docstr) {
106 if (!(str = PyString_FromString(docstr)))
107 goto finally;
108 if (PyDict_SetItemString(dict, "__doc__", str))
109 goto finally;
110 }
111
112 if (!(*klass = PyErr_NewException(name, base, dict)))
113 goto finally;
114
Brett Cannonbf364092006-03-01 04:25:17 +0000115 if (populate_methods(*klass, methods)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000116 Py_DECREF(*klass);
117 *klass = NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000118 goto finally;
Barry Warsaw675ac282000-05-26 19:05:16 +0000119 }
120
121 status = 0;
122
123 finally:
124 Py_XDECREF(dict);
125 Py_XDECREF(str);
126 return status;
127}
128
129
130/* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000131static PyObject *
132get_self(PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000133{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000134 PyObject *self = PyTuple_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000135 if (!self) {
Thomas Wouters7e474022000-07-16 12:04:32 +0000136 /* Watch out for being called to early in the bootstrapping process */
Barry Warsaw675ac282000-05-26 19:05:16 +0000137 if (PyExc_TypeError) {
138 PyErr_SetString(PyExc_TypeError,
Fred Drake661ea262000-10-24 19:57:45 +0000139 "unbound method must be called with instance as first argument");
Barry Warsaw675ac282000-05-26 19:05:16 +0000140 }
141 return NULL;
142 }
143 return self;
144}
145
146
147
148/* Notes on bootstrapping the exception classes.
149 *
150 * First thing we create is the base class for all exceptions, called
Brett Cannonbf364092006-03-01 04:25:17 +0000151 * appropriately BaseException. Creation of this class makes no
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000152 * assumptions about the existence of any other exception class -- except
Barry Warsaw675ac282000-05-26 19:05:16 +0000153 * for TypeError, which can conditionally exist.
154 *
Brett Cannonbf364092006-03-01 04:25:17 +0000155 * Next, Exception is created since it is the common subclass for the rest of
156 * the needed exceptions for this bootstrapping to work. StandardError is
157 * created (which is quite simple) followed by
Barry Warsaw675ac282000-05-26 19:05:16 +0000158 * TypeError, because the instantiation of other exceptions can potentially
159 * throw a TypeError. Once these exceptions are created, all the others
160 * can be created in any order. See the static exctable below for the
161 * explicit bootstrap order.
162 *
Brett Cannonbf364092006-03-01 04:25:17 +0000163 * All classes after BaseException can be created using PyErr_NewException().
Barry Warsaw675ac282000-05-26 19:05:16 +0000164 */
165
Brett Cannonbf364092006-03-01 04:25:17 +0000166PyDoc_STRVAR(BaseException__doc__, "Common base class for all exceptions");
Barry Warsaw675ac282000-05-26 19:05:16 +0000167
Brett Cannonbf364092006-03-01 04:25:17 +0000168/*
169 Set args and message attributes.
170
171 Assumes self and args have already been set properly with set_self, etc.
172*/
173static int
174set_args_and_message(PyObject *self, PyObject *args)
175{
176 PyObject *message_val;
177 Py_ssize_t args_len = PySequence_Length(args);
178
179 if (args_len < 0)
180 return 0;
181
182 /* set args */
183 if (PyObject_SetAttrString(self, "args", args) < 0)
184 return 0;
185
186 /* set message */
187 if (args_len == 1)
188 message_val = PySequence_GetItem(args, 0);
189 else
190 message_val = PyString_FromString("");
191 if (!message_val)
192 return 0;
193
194 if (PyObject_SetAttrString(self, "message", message_val) < 0) {
195 Py_DECREF(message_val);
196 return 0;
197 }
198
199 Py_DECREF(message_val);
200 return 1;
201}
Barry Warsaw675ac282000-05-26 19:05:16 +0000202
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000203static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000204BaseException__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000205{
Barry Warsaw675ac282000-05-26 19:05:16 +0000206 if (!(self = get_self(args)))
207 return NULL;
208
Brett Cannonbf364092006-03-01 04:25:17 +0000209 /* set args and message attribute */
210 args = PySequence_GetSlice(args, 1, PySequence_Length(args));
Barry Warsaw675ac282000-05-26 19:05:16 +0000211 if (!args)
212 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000213
Brett Cannonbf364092006-03-01 04:25:17 +0000214 if (!set_args_and_message(self, args)) {
215 Py_DECREF(args);
216 return NULL;
217 }
218
219 Py_DECREF(args);
220 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000221}
222
223
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000224static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000225BaseException__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000226{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000227 PyObject *out;
Barry Warsaw675ac282000-05-26 19:05:16 +0000228
Fred Drake1aba5772000-08-15 15:46:16 +0000229 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000230 return NULL;
231
232 args = PyObject_GetAttrString(self, "args");
233 if (!args)
234 return NULL;
235
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000236 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000237 case 0:
238 out = PyString_FromString("");
239 break;
240 case 1:
Barry Warsawb7816552000-07-09 22:27:10 +0000241 {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000242 PyObject *tmp = PySequence_GetItem(args, 0);
Barry Warsawb7816552000-07-09 22:27:10 +0000243 if (tmp) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000244 out = PyObject_Str(tmp);
Barry Warsawb7816552000-07-09 22:27:10 +0000245 Py_DECREF(tmp);
246 }
247 else
248 out = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000249 break;
Barry Warsawb7816552000-07-09 22:27:10 +0000250 }
Guido van Rossum98b2a422002-09-18 04:06:32 +0000251 case -1:
252 PyErr_Clear();
253 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000254 default:
255 out = PyObject_Str(args);
256 break;
257 }
258
259 Py_DECREF(args);
260 return out;
261}
262
Brett Cannonbf364092006-03-01 04:25:17 +0000263#ifdef Py_USING_UNICODE
264static PyObject *
265BaseException__unicode__(PyObject *self, PyObject *args)
266{
267 Py_ssize_t args_len;
268
269 if (!PyArg_ParseTuple(args, "O:__unicode__", &self))
270 return NULL;
271
272 args = PyObject_GetAttrString(self, "args");
273 if (!args)
274 return NULL;
275
276 args_len = PySequence_Size(args);
277 if (args_len < 0) {
278 Py_DECREF(args);
279 return NULL;
280 }
281
282 if (args_len == 0) {
283 Py_DECREF(args);
284 return PyUnicode_FromUnicode(NULL, 0);
285 }
286 else if (args_len == 1) {
287 PyObject *temp = PySequence_GetItem(args, 0);
Brett Cannon46872b12006-03-02 04:31:55 +0000288 PyObject *unicode_obj;
289
Brett Cannonbf364092006-03-01 04:25:17 +0000290 if (!temp) {
291 Py_DECREF(args);
292 return NULL;
293 }
294 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000295 unicode_obj = PyObject_Unicode(temp);
296 Py_DECREF(temp);
297 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000298 }
299 else {
Brett Cannon46872b12006-03-02 04:31:55 +0000300 PyObject *unicode_obj = PyObject_Unicode(args);
301
Brett Cannonbf364092006-03-01 04:25:17 +0000302 Py_DECREF(args);
Brett Cannon46872b12006-03-02 04:31:55 +0000303 return unicode_obj;
Brett Cannonbf364092006-03-01 04:25:17 +0000304 }
305}
306#endif /* Py_USING_UNICODE */
Barry Warsaw675ac282000-05-26 19:05:16 +0000307
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000308static PyObject *
Brett Cannonbf364092006-03-01 04:25:17 +0000309BaseException__repr__(PyObject *self, PyObject *args)
310{
311 PyObject *args_attr;
312 Py_ssize_t args_len;
313 PyObject *repr_suffix;
314 PyObject *repr;
315
316 if (!PyArg_ParseTuple(args, "O:__repr__", &self))
317 return NULL;
318
319 args_attr = PyObject_GetAttrString(self, "args");
320 if (!args_attr)
321 return NULL;
322
323 args_len = PySequence_Length(args_attr);
324 if (args_len < 0) {
325 Py_DECREF(args_attr);
326 return NULL;
327 }
328
329 if (args_len == 0) {
330 Py_DECREF(args_attr);
331 repr_suffix = PyString_FromString("()");
332 if (!repr_suffix)
333 return NULL;
334 }
335 else {
336 PyObject *args_repr;
337 /*PyObject *right_paren;
338
339 repr_suffix = PyString_FromString("(*");
340 if (!repr_suffix) {
341 Py_DECREF(args_attr);
342 return NULL;
343 }*/
344
345 args_repr = PyObject_Repr(args_attr);
346 Py_DECREF(args_attr);
347 if (!args_repr)
348 return NULL;
349
350 repr_suffix = args_repr;
351
352 /*PyString_ConcatAndDel(&repr_suffix, args_repr);
353 if (!repr_suffix)
354 return NULL;
355
356 right_paren = PyString_FromString(")");
357 if (!right_paren) {
358 Py_DECREF(repr_suffix);
359 return NULL;
360 }
361
362 PyString_ConcatAndDel(&repr_suffix, right_paren);
363 if (!repr_suffix)
364 return NULL;*/
365 }
366
367 repr = PyString_FromString(self->ob_type->tp_name);
368 if (!repr) {
369 Py_DECREF(repr_suffix);
370 return NULL;
371 }
372
373 PyString_ConcatAndDel(&repr, repr_suffix);
374 return repr;
375}
376
377static PyObject *
378BaseException__getitem__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000379{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000380 PyObject *out;
381 PyObject *index;
Barry Warsaw675ac282000-05-26 19:05:16 +0000382
Fred Drake1aba5772000-08-15 15:46:16 +0000383 if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index))
Barry Warsaw675ac282000-05-26 19:05:16 +0000384 return NULL;
385
386 args = PyObject_GetAttrString(self, "args");
387 if (!args)
388 return NULL;
389
390 out = PyObject_GetItem(args, index);
391 Py_DECREF(args);
392 return out;
393}
394
395
396static PyMethodDef
Brett Cannonbf364092006-03-01 04:25:17 +0000397BaseException_methods[] = {
398 /* methods for the BaseException class */
399 {"__getitem__", BaseException__getitem__, METH_VARARGS},
400 {"__repr__", BaseException__repr__, METH_VARARGS},
401 {"__str__", BaseException__str__, METH_VARARGS},
402#ifdef Py_USING_UNICODE
403 {"__unicode__", BaseException__unicode__, METH_VARARGS},
404#endif /* Py_USING_UNICODE */
405 {"__init__", BaseException__init__, METH_VARARGS},
406 {NULL, NULL }
Barry Warsaw675ac282000-05-26 19:05:16 +0000407};
408
409
410static int
Brett Cannonbf364092006-03-01 04:25:17 +0000411make_BaseException(char *modulename)
Barry Warsaw675ac282000-05-26 19:05:16 +0000412{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000413 PyObject *dict = PyDict_New();
414 PyObject *str = NULL;
415 PyObject *name = NULL;
Brett Cannonbf364092006-03-01 04:25:17 +0000416 PyObject *emptytuple = NULL;
417 PyObject *argstuple = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000418 int status = -1;
419
420 if (!dict)
421 return -1;
422
423 /* If an error occurs from here on, goto finally instead of explicitly
424 * returning NULL.
425 */
426
427 if (!(str = PyString_FromString(modulename)))
428 goto finally;
429 if (PyDict_SetItemString(dict, "__module__", str))
430 goto finally;
431 Py_DECREF(str);
Brett Cannonbf364092006-03-01 04:25:17 +0000432
433 if (!(str = PyString_FromString(BaseException__doc__)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000434 goto finally;
435 if (PyDict_SetItemString(dict, "__doc__", str))
436 goto finally;
437
Brett Cannonbf364092006-03-01 04:25:17 +0000438 if (!(name = PyString_FromString("BaseException")))
Barry Warsaw675ac282000-05-26 19:05:16 +0000439 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000440
Brett Cannonbf364092006-03-01 04:25:17 +0000441 if (!(emptytuple = PyTuple_New(0)))
442 goto finally;
443
444 if (!(argstuple = PyTuple_Pack(3, name, emptytuple, dict)))
445 goto finally;
446
447 if (!(PyExc_BaseException = PyType_Type.tp_new(&PyType_Type, argstuple,
448 NULL)))
Barry Warsaw675ac282000-05-26 19:05:16 +0000449 goto finally;
450
451 /* Now populate the dictionary with the method suite */
Brett Cannonbf364092006-03-01 04:25:17 +0000452 if (populate_methods(PyExc_BaseException, BaseException_methods))
453 /* Don't need to reclaim PyExc_BaseException here because that'll
Barry Warsaw675ac282000-05-26 19:05:16 +0000454 * happen during interpreter shutdown.
455 */
456 goto finally;
457
458 status = 0;
459
460 finally:
461 Py_XDECREF(dict);
462 Py_XDECREF(str);
463 Py_XDECREF(name);
Brett Cannonbf364092006-03-01 04:25:17 +0000464 Py_XDECREF(emptytuple);
465 Py_XDECREF(argstuple);
Barry Warsaw675ac282000-05-26 19:05:16 +0000466 return status;
467}
468
469
470
Brett Cannonbf364092006-03-01 04:25:17 +0000471PyDoc_STRVAR(Exception__doc__, "Common base class for all non-exit exceptions.");
472
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000473PyDoc_STRVAR(StandardError__doc__,
Brett Cannonbf364092006-03-01 04:25:17 +0000474"Base class for all standard Python exceptions that do not represent"
475"interpreter exiting.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000476
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000477PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000478
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000479PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next().");
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000480PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit.");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000481
Barry Warsaw675ac282000-05-26 19:05:16 +0000482
483
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000484PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000485
486
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000487static PyObject *
488SystemExit__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000489{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000490 PyObject *code;
Barry Warsaw675ac282000-05-26 19:05:16 +0000491 int status;
492
493 if (!(self = get_self(args)))
494 return NULL;
495
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000496 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000497 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000498
Brett Cannonbf364092006-03-01 04:25:17 +0000499 if (!set_args_and_message(self, args)) {
500 Py_DECREF(args);
501 return NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000502 }
503
504 /* set code attribute */
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000505 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000506 case 0:
507 Py_INCREF(Py_None);
508 code = Py_None;
509 break;
510 case 1:
511 code = PySequence_GetItem(args, 0);
512 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000513 case -1:
514 PyErr_Clear();
515 /* Fall through */
Barry Warsaw675ac282000-05-26 19:05:16 +0000516 default:
517 Py_INCREF(args);
518 code = args;
519 break;
520 }
521
522 status = PyObject_SetAttrString(self, "code", code);
523 Py_DECREF(code);
524 Py_DECREF(args);
525 if (status < 0)
526 return NULL;
527
Brett Cannonbf364092006-03-01 04:25:17 +0000528 Py_RETURN_NONE;
Barry Warsaw675ac282000-05-26 19:05:16 +0000529}
530
531
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000532static PyMethodDef SystemExit_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000533 { "__init__", SystemExit__init__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000534 {NULL, NULL}
535};
536
537
538
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000539PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000540
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000541PyDoc_STRVAR(ImportError__doc__,
542"Import can't find module, or can't find name in module.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000543
544
545
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000546PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000547
548
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000549static PyObject *
550EnvironmentError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000551{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000552 PyObject *item0 = NULL;
553 PyObject *item1 = NULL;
554 PyObject *item2 = NULL;
555 PyObject *subslice = NULL;
556 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000557
558 if (!(self = get_self(args)))
559 return NULL;
560
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000561 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000562 return NULL;
563
Brett Cannonbf364092006-03-01 04:25:17 +0000564 if (!set_args_and_message(self, args)) {
565 Py_DECREF(args);
566 return NULL;
567 }
568
569 if (PyObject_SetAttrString(self, "errno", Py_None) ||
Barry Warsaw675ac282000-05-26 19:05:16 +0000570 PyObject_SetAttrString(self, "strerror", Py_None) ||
571 PyObject_SetAttrString(self, "filename", Py_None))
572 {
573 goto finally;
574 }
575
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000576 switch (PySequence_Size(args)) {
Barry Warsaw675ac282000-05-26 19:05:16 +0000577 case 3:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000578 /* Where a function has a single filename, such as open() or some
579 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
580 * called, giving a third argument which is the filename. But, so
581 * that old code using in-place unpacking doesn't break, e.g.:
Barry Warsaw9667ed22001-01-23 16:08:34 +0000582 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000583 * except IOError, (errno, strerror):
Barry Warsaw9667ed22001-01-23 16:08:34 +0000584 *
Barry Warsaw675ac282000-05-26 19:05:16 +0000585 * we hack args so that it only contains two items. This also
586 * means we need our own __str__() which prints out the filename
587 * when it was supplied.
588 */
589 item0 = PySequence_GetItem(args, 0);
590 item1 = PySequence_GetItem(args, 1);
591 item2 = PySequence_GetItem(args, 2);
592 if (!item0 || !item1 || !item2)
593 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000594
Barry Warsaw675ac282000-05-26 19:05:16 +0000595 if (PyObject_SetAttrString(self, "errno", item0) ||
596 PyObject_SetAttrString(self, "strerror", item1) ||
597 PyObject_SetAttrString(self, "filename", item2))
598 {
599 goto finally;
600 }
601
602 subslice = PySequence_GetSlice(args, 0, 2);
603 if (!subslice || PyObject_SetAttrString(self, "args", subslice))
604 goto finally;
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000605 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000606
607 case 2:
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000608 /* Used when PyErr_SetFromErrno() is called and no filename
609 * argument is given.
610 */
Barry Warsaw675ac282000-05-26 19:05:16 +0000611 item0 = PySequence_GetItem(args, 0);
612 item1 = PySequence_GetItem(args, 1);
613 if (!item0 || !item1)
614 goto finally;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000615
Barry Warsaw675ac282000-05-26 19:05:16 +0000616 if (PyObject_SetAttrString(self, "errno", item0) ||
617 PyObject_SetAttrString(self, "strerror", item1))
618 {
619 goto finally;
620 }
Barry Warsaw7dfeb422000-07-09 04:56:25 +0000621 break;
Neal Norwitz2c96ab22002-09-18 22:37:17 +0000622
623 case -1:
624 PyErr_Clear();
625 break;
Barry Warsaw675ac282000-05-26 19:05:16 +0000626 }
627
628 Py_INCREF(Py_None);
629 rtnval = Py_None;
630
631 finally:
632 Py_DECREF(args);
633 Py_XDECREF(item0);
634 Py_XDECREF(item1);
635 Py_XDECREF(item2);
636 Py_XDECREF(subslice);
637 return rtnval;
638}
639
640
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000641static PyObject *
642EnvironmentError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000643{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000644 PyObject *originalself = self;
645 PyObject *filename;
646 PyObject *serrno;
647 PyObject *strerror;
648 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000649
Fred Drake1aba5772000-08-15 15:46:16 +0000650 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000651 return NULL;
Barry Warsaw9667ed22001-01-23 16:08:34 +0000652
Barry Warsaw675ac282000-05-26 19:05:16 +0000653 filename = PyObject_GetAttrString(self, "filename");
654 serrno = PyObject_GetAttrString(self, "errno");
655 strerror = PyObject_GetAttrString(self, "strerror");
656 if (!filename || !serrno || !strerror)
657 goto finally;
658
659 if (filename != Py_None) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000660 PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
661 PyObject *repr = PyObject_Repr(filename);
662 PyObject *tuple = PyTuple_New(3);
Barry Warsaw675ac282000-05-26 19:05:16 +0000663
664 if (!fmt || !repr || !tuple) {
665 Py_XDECREF(fmt);
666 Py_XDECREF(repr);
667 Py_XDECREF(tuple);
668 goto finally;
669 }
670
671 PyTuple_SET_ITEM(tuple, 0, serrno);
672 PyTuple_SET_ITEM(tuple, 1, strerror);
673 PyTuple_SET_ITEM(tuple, 2, repr);
674
675 rtnval = PyString_Format(fmt, tuple);
676
677 Py_DECREF(fmt);
678 Py_DECREF(tuple);
679 /* already freed because tuple owned only reference */
680 serrno = NULL;
681 strerror = NULL;
682 }
683 else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000684 PyObject *fmt = PyString_FromString("[Errno %s] %s");
685 PyObject *tuple = PyTuple_New(2);
Barry Warsaw675ac282000-05-26 19:05:16 +0000686
687 if (!fmt || !tuple) {
688 Py_XDECREF(fmt);
689 Py_XDECREF(tuple);
690 goto finally;
691 }
692
693 PyTuple_SET_ITEM(tuple, 0, serrno);
694 PyTuple_SET_ITEM(tuple, 1, strerror);
Barry Warsaw9667ed22001-01-23 16:08:34 +0000695
Barry Warsaw675ac282000-05-26 19:05:16 +0000696 rtnval = PyString_Format(fmt, tuple);
697
698 Py_DECREF(fmt);
699 Py_DECREF(tuple);
700 /* already freed because tuple owned only reference */
701 serrno = NULL;
702 strerror = NULL;
703 }
704 else
705 /* The original Python code said:
706 *
707 * return StandardError.__str__(self)
708 *
709 * but there is no StandardError__str__() function; we happen to
Brett Cannonbf364092006-03-01 04:25:17 +0000710 * know that's just a pass through to BaseException__str__().
Barry Warsaw675ac282000-05-26 19:05:16 +0000711 */
Brett Cannonbf364092006-03-01 04:25:17 +0000712 rtnval = BaseException__str__(originalself, args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000713
714 finally:
715 Py_XDECREF(filename);
716 Py_XDECREF(serrno);
717 Py_XDECREF(strerror);
718 return rtnval;
719}
720
721
722static
723PyMethodDef EnvironmentError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000724 {"__init__", EnvironmentError__init__, METH_VARARGS},
725 {"__str__", EnvironmentError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000726 {NULL, NULL}
727};
728
729
730
731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000732PyDoc_STRVAR(IOError__doc__, "I/O operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000733
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000734PyDoc_STRVAR(OSError__doc__, "OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000735
736#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000737PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000738#endif /* MS_WINDOWS */
739
Martin v. Löwis79acb9e2002-12-06 12:48:53 +0000740#ifdef __VMS
741static char
742VMSError__doc__[] = "OpenVMS OS system call failed.";
743#endif
744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000745PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000746
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000747PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000748
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000749PyDoc_STRVAR(NotImplementedError__doc__,
750"Method or function hasn't been implemented yet.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000751
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000752PyDoc_STRVAR(NameError__doc__, "Name not found globally.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000753
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000754PyDoc_STRVAR(UnboundLocalError__doc__,
755"Local name referenced but not bound to a value.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000757PyDoc_STRVAR(AttributeError__doc__, "Attribute not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000758
759
760
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000761PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax.");
Barry Warsaw675ac282000-05-26 19:05:16 +0000762
763
764static int
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000765SyntaxError__classinit__(PyObject *klass)
Barry Warsaw675ac282000-05-26 19:05:16 +0000766{
Barry Warsaw87bec352000-08-18 05:05:37 +0000767 int retval = 0;
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000768 PyObject *emptystring = PyString_FromString("");
Barry Warsaw675ac282000-05-26 19:05:16 +0000769
770 /* Additional class-creation time initializations */
771 if (!emptystring ||
772 PyObject_SetAttrString(klass, "msg", emptystring) ||
773 PyObject_SetAttrString(klass, "filename", Py_None) ||
774 PyObject_SetAttrString(klass, "lineno", Py_None) ||
775 PyObject_SetAttrString(klass, "offset", Py_None) ||
Martin v. Löwiscfeb3b62002-03-03 21:30:27 +0000776 PyObject_SetAttrString(klass, "text", Py_None) ||
777 PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
Barry Warsaw675ac282000-05-26 19:05:16 +0000778 {
Barry Warsaw87bec352000-08-18 05:05:37 +0000779 retval = -1;
Barry Warsaw675ac282000-05-26 19:05:16 +0000780 }
Barry Warsaw87bec352000-08-18 05:05:37 +0000781 Py_XDECREF(emptystring);
782 return retval;
Barry Warsaw675ac282000-05-26 19:05:16 +0000783}
784
785
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000786static PyObject *
787SyntaxError__init__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000788{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000789 PyObject *rtnval = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000790 int lenargs;
791
792 if (!(self = get_self(args)))
793 return NULL;
794
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000795 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
Barry Warsaw675ac282000-05-26 19:05:16 +0000796 return NULL;
797
Brett Cannonbf364092006-03-01 04:25:17 +0000798 if (!set_args_and_message(self, args)) {
799 Py_DECREF(args);
800 return NULL;
801 }
Barry Warsaw675ac282000-05-26 19:05:16 +0000802
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000803 lenargs = PySequence_Size(args);
Barry Warsaw675ac282000-05-26 19:05:16 +0000804 if (lenargs >= 1) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000805 PyObject *item0 = PySequence_GetItem(args, 0);
Barry Warsaw675ac282000-05-26 19:05:16 +0000806 int status;
807
808 if (!item0)
809 goto finally;
810 status = PyObject_SetAttrString(self, "msg", item0);
811 Py_DECREF(item0);
812 if (status)
813 goto finally;
814 }
815 if (lenargs == 2) {
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000816 PyObject *info = PySequence_GetItem(args, 1);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000817 PyObject *filename = NULL, *lineno = NULL;
818 PyObject *offset = NULL, *text = NULL;
Barry Warsaw675ac282000-05-26 19:05:16 +0000819 int status = 1;
820
821 if (!info)
822 goto finally;
823
824 filename = PySequence_GetItem(info, 0);
Fred Drake9da7f3b2001-02-28 21:52:10 +0000825 if (filename != NULL) {
826 lineno = PySequence_GetItem(info, 1);
827 if (lineno != NULL) {
828 offset = PySequence_GetItem(info, 2);
829 if (offset != NULL) {
830 text = PySequence_GetItem(info, 3);
831 if (text != NULL) {
832 status =
833 PyObject_SetAttrString(self, "filename", filename)
834 || PyObject_SetAttrString(self, "lineno", lineno)
835 || PyObject_SetAttrString(self, "offset", offset)
836 || PyObject_SetAttrString(self, "text", text);
837 Py_DECREF(text);
838 }
839 Py_DECREF(offset);
840 }
841 Py_DECREF(lineno);
842 }
843 Py_DECREF(filename);
Barry Warsaw675ac282000-05-26 19:05:16 +0000844 }
Fred Drake9da7f3b2001-02-28 21:52:10 +0000845 Py_DECREF(info);
Barry Warsaw675ac282000-05-26 19:05:16 +0000846
847 if (status)
848 goto finally;
849 }
850 Py_INCREF(Py_None);
851 rtnval = Py_None;
852
853 finally:
854 Py_DECREF(args);
855 return rtnval;
856}
857
858
Fred Drake185a29b2000-08-15 16:20:36 +0000859/* This is called "my_basename" instead of just "basename" to avoid name
860 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
861 defined, and Python does define that. */
862static char *
863my_basename(char *name)
864{
865 char *cp = name;
866 char *result = name;
867
868 if (name == NULL)
869 return "???";
870 while (*cp != '\0') {
871 if (*cp == SEP)
872 result = cp + 1;
873 ++cp;
874 }
875 return result;
876}
877
878
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000879static PyObject *
880SyntaxError__str__(PyObject *self, PyObject *args)
Barry Warsaw675ac282000-05-26 19:05:16 +0000881{
Thomas Wouters0452d1f2000-07-22 18:45:06 +0000882 PyObject *msg;
883 PyObject *str;
Fred Drake1aba5772000-08-15 15:46:16 +0000884 PyObject *filename, *lineno, *result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000885
Fred Drake1aba5772000-08-15 15:46:16 +0000886 if (!PyArg_ParseTuple(args, "O:__str__", &self))
Barry Warsaw675ac282000-05-26 19:05:16 +0000887 return NULL;
888
889 if (!(msg = PyObject_GetAttrString(self, "msg")))
890 return NULL;
Fred Drake1aba5772000-08-15 15:46:16 +0000891
Barry Warsaw675ac282000-05-26 19:05:16 +0000892 str = PyObject_Str(msg);
893 Py_DECREF(msg);
Fred Drake1aba5772000-08-15 15:46:16 +0000894 result = str;
895
896 /* XXX -- do all the additional formatting with filename and
897 lineno here */
898
Guido van Rossum602d4512002-09-03 20:24:09 +0000899 if (str != NULL && PyString_Check(str)) {
Fred Drake1aba5772000-08-15 15:46:16 +0000900 int have_filename = 0;
901 int have_lineno = 0;
902 char *buffer = NULL;
903
Barry Warsaw77c9f502000-08-16 19:43:17 +0000904 if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000905 have_filename = PyString_Check(filename);
906 else
907 PyErr_Clear();
Barry Warsaw77c9f502000-08-16 19:43:17 +0000908
909 if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
Fred Drake1aba5772000-08-15 15:46:16 +0000910 have_lineno = PyInt_Check(lineno);
911 else
912 PyErr_Clear();
913
914 if (have_filename || have_lineno) {
Barry Warsaw77c9f502000-08-16 19:43:17 +0000915 int bufsize = PyString_GET_SIZE(str) + 64;
916 if (have_filename)
917 bufsize += PyString_GET_SIZE(filename);
Fred Drake1aba5772000-08-15 15:46:16 +0000918
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000919 buffer = PyMem_MALLOC(bufsize);
Fred Drake1aba5772000-08-15 15:46:16 +0000920 if (buffer != NULL) {
921 if (have_filename && have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000922 PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
923 PyString_AS_STRING(str),
924 my_basename(PyString_AS_STRING(filename)),
925 PyInt_AsLong(lineno));
Fred Drake1aba5772000-08-15 15:46:16 +0000926 else if (have_filename)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000927 PyOS_snprintf(buffer, bufsize, "%s (%s)",
928 PyString_AS_STRING(str),
929 my_basename(PyString_AS_STRING(filename)));
Fred Drake1aba5772000-08-15 15:46:16 +0000930 else if (have_lineno)
Jeremy Hylton05bd7872001-11-28 20:24:33 +0000931 PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
932 PyString_AS_STRING(str),
933 PyInt_AsLong(lineno));
Barry Warsaw77c9f502000-08-16 19:43:17 +0000934
Fred Drake1aba5772000-08-15 15:46:16 +0000935 result = PyString_FromString(buffer);
Barry Warsaw77c9f502000-08-16 19:43:17 +0000936 PyMem_FREE(buffer);
937
Fred Drake1aba5772000-08-15 15:46:16 +0000938 if (result == NULL)
939 result = str;
940 else
941 Py_DECREF(str);
942 }
943 }
944 Py_XDECREF(filename);
945 Py_XDECREF(lineno);
946 }
947 return result;
Barry Warsaw675ac282000-05-26 19:05:16 +0000948}
949
950
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000951static PyMethodDef SyntaxError_methods[] = {
Jeremy Hylton4e542a32000-06-30 04:59:59 +0000952 {"__init__", SyntaxError__init__, METH_VARARGS},
953 {"__str__", SyntaxError__str__, METH_VARARGS},
Barry Warsaw675ac282000-05-26 19:05:16 +0000954 {NULL, NULL}
955};
956
957
Guido van Rossum602d4512002-09-03 20:24:09 +0000958static PyObject *
959KeyError__str__(PyObject *self, PyObject *args)
960{
961 PyObject *argsattr;
962 PyObject *result;
963
964 if (!PyArg_ParseTuple(args, "O:__str__", &self))
965 return NULL;
966
Brett Cannonbf364092006-03-01 04:25:17 +0000967 argsattr = PyObject_GetAttrString(self, "args");
968 if (!argsattr)
969 return NULL;
Guido van Rossum602d4512002-09-03 20:24:09 +0000970
971 /* If args is a tuple of exactly one item, apply repr to args[0].
972 This is done so that e.g. the exception raised by {}[''] prints
973 KeyError: ''
974 rather than the confusing
975 KeyError
976 alone. The downside is that if KeyError is raised with an explanatory
977 string, that string will be displayed in quotes. Too bad.
Brett Cannonbf364092006-03-01 04:25:17 +0000978 If args is anything else, use the default BaseException__str__().
Guido van Rossum602d4512002-09-03 20:24:09 +0000979 */
980 if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) {
981 PyObject *key = PyTuple_GET_ITEM(argsattr, 0);
982 result = PyObject_Repr(key);
983 }
984 else
Brett Cannonbf364092006-03-01 04:25:17 +0000985 result = BaseException__str__(self, args);
Guido van Rossum602d4512002-09-03 20:24:09 +0000986
987 Py_DECREF(argsattr);
988 return result;
989}
990
991static PyMethodDef KeyError_methods[] = {
992 {"__str__", KeyError__str__, METH_VARARGS},
993 {NULL, NULL}
994};
995
996
Walter Dörwaldbf73db82002-11-21 20:08:33 +0000997#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000998static
Martin v. Löwis18e16552006-02-15 17:27:45 +0000999int get_int(PyObject *exc, const char *name, Py_ssize_t *value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001000{
1001 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1002
1003 if (!attr)
1004 return -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001005 if (PyInt_Check(attr)) {
1006 *value = PyInt_AS_LONG(attr);
1007 } else if (PyLong_Check(attr)) {
1008 *value = (size_t)PyLong_AsLongLong(attr);
1009 if (*value == -1) {
1010 Py_DECREF(attr);
1011 return -1;
1012 }
1013 } else {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001014 PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001015 Py_DECREF(attr);
1016 return -1;
1017 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001018 Py_DECREF(attr);
1019 return 0;
1020}
1021
1022
1023static
Martin v. Löwis18e16552006-02-15 17:27:45 +00001024int set_ssize_t(PyObject *exc, const char *name, Py_ssize_t value)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001025{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001026 PyObject *obj = PyInt_FromSsize_t(value);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001027 int result;
1028
1029 if (!obj)
1030 return -1;
1031 result = PyObject_SetAttrString(exc, (char *)name, obj);
1032 Py_DECREF(obj);
1033 return result;
1034}
1035
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001036static
1037PyObject *get_string(PyObject *exc, const char *name)
1038{
1039 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1040
1041 if (!attr)
1042 return NULL;
1043 if (!PyString_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001044 PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001045 Py_DECREF(attr);
1046 return NULL;
1047 }
1048 return attr;
1049}
1050
1051
1052static
1053int set_string(PyObject *exc, const char *name, const char *value)
1054{
1055 PyObject *obj = PyString_FromString(value);
1056 int result;
1057
1058 if (!obj)
1059 return -1;
1060 result = PyObject_SetAttrString(exc, (char *)name, obj);
1061 Py_DECREF(obj);
1062 return result;
1063}
1064
1065
1066static
1067PyObject *get_unicode(PyObject *exc, const char *name)
1068{
1069 PyObject *attr = PyObject_GetAttrString(exc, (char *)name);
1070
1071 if (!attr)
1072 return NULL;
1073 if (!PyUnicode_Check(attr)) {
Walter Dörwaldfd08e4c2002-09-02 16:10:06 +00001074 PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001075 Py_DECREF(attr);
1076 return NULL;
1077 }
1078 return attr;
1079}
1080
1081PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc)
1082{
1083 return get_string(exc, "encoding");
1084}
1085
1086PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc)
1087{
1088 return get_string(exc, "encoding");
1089}
1090
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001091PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc)
1092{
1093 return get_unicode(exc, "object");
1094}
1095
1096PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc)
1097{
1098 return get_string(exc, "object");
1099}
1100
1101PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc)
1102{
1103 return get_unicode(exc, "object");
1104}
1105
Martin v. Löwis18e16552006-02-15 17:27:45 +00001106int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001107{
1108 if (!get_int(exc, "start", start)) {
1109 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001110 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001111 if (!object)
1112 return -1;
1113 size = PyUnicode_GET_SIZE(object);
1114 if (*start<0)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001115 *start = 0; /*XXX check for values <0*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001116 if (*start>=size)
1117 *start = size-1;
1118 Py_DECREF(object);
1119 return 0;
1120 }
1121 return -1;
1122}
1123
1124
Martin v. Löwis18e16552006-02-15 17:27:45 +00001125int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001126{
1127 if (!get_int(exc, "start", start)) {
1128 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001129 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001130 if (!object)
1131 return -1;
1132 size = PyString_GET_SIZE(object);
1133 if (*start<0)
1134 *start = 0;
1135 if (*start>=size)
1136 *start = size-1;
1137 Py_DECREF(object);
1138 return 0;
1139 }
1140 return -1;
1141}
1142
1143
Martin v. Löwis18e16552006-02-15 17:27:45 +00001144int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001145{
1146 return PyUnicodeEncodeError_GetStart(exc, start);
1147}
1148
1149
Martin v. Löwis18e16552006-02-15 17:27:45 +00001150int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001151{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001152 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001153}
1154
1155
Martin v. Löwis18e16552006-02-15 17:27:45 +00001156int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001157{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001158 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001159}
1160
1161
Martin v. Löwis18e16552006-02-15 17:27:45 +00001162int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001163{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001164 return set_ssize_t(exc, "start", start);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001165}
1166
1167
Martin v. Löwis18e16552006-02-15 17:27:45 +00001168int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001169{
1170 if (!get_int(exc, "end", end)) {
1171 PyObject *object = PyUnicodeEncodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001172 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001173 if (!object)
1174 return -1;
1175 size = PyUnicode_GET_SIZE(object);
1176 if (*end<1)
1177 *end = 1;
1178 if (*end>size)
1179 *end = size;
1180 Py_DECREF(object);
1181 return 0;
1182 }
1183 return -1;
1184}
1185
1186
Martin v. Löwis18e16552006-02-15 17:27:45 +00001187int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001188{
1189 if (!get_int(exc, "end", end)) {
1190 PyObject *object = PyUnicodeDecodeError_GetObject(exc);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001191 Py_ssize_t size;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001192 if (!object)
1193 return -1;
1194 size = PyString_GET_SIZE(object);
1195 if (*end<1)
1196 *end = 1;
1197 if (*end>size)
1198 *end = size;
1199 Py_DECREF(object);
1200 return 0;
1201 }
1202 return -1;
1203}
1204
1205
Martin v. Löwis18e16552006-02-15 17:27:45 +00001206int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001207{
1208 return PyUnicodeEncodeError_GetEnd(exc, start);
1209}
1210
1211
Martin v. Löwis18e16552006-02-15 17:27:45 +00001212int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001213{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001214 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001215}
1216
1217
Martin v. Löwis18e16552006-02-15 17:27:45 +00001218int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001219{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001220 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001221}
1222
1223
Martin v. Löwis18e16552006-02-15 17:27:45 +00001224int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001225{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001226 return set_ssize_t(exc, "end", end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001227}
1228
1229
1230PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc)
1231{
1232 return get_string(exc, "reason");
1233}
1234
1235
1236PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc)
1237{
1238 return get_string(exc, "reason");
1239}
1240
1241
1242PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc)
1243{
1244 return get_string(exc, "reason");
1245}
1246
1247
1248int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1249{
1250 return set_string(exc, "reason", reason);
1251}
1252
1253
1254int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1255{
1256 return set_string(exc, "reason", reason);
1257}
1258
1259
1260int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1261{
1262 return set_string(exc, "reason", reason);
1263}
1264
1265
1266static PyObject *
1267UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype)
1268{
1269 PyObject *rtnval = NULL;
1270 PyObject *encoding;
1271 PyObject *object;
1272 PyObject *start;
1273 PyObject *end;
1274 PyObject *reason;
1275
1276 if (!(self = get_self(args)))
1277 return NULL;
1278
1279 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1280 return NULL;
1281
Brett Cannonbf364092006-03-01 04:25:17 +00001282 if (!set_args_and_message(self, args)) {
1283 Py_DECREF(args);
1284 return NULL;
1285 }
1286
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001287 if (!PyArg_ParseTuple(args, "O!O!O!O!O!",
1288 &PyString_Type, &encoding,
1289 objecttype, &object,
1290 &PyInt_Type, &start,
1291 &PyInt_Type, &end,
1292 &PyString_Type, &reason))
Walter Dörwalde98147a2003-08-14 20:59:07 +00001293 goto finally;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001294
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001295 if (PyObject_SetAttrString(self, "encoding", encoding))
1296 goto finally;
1297 if (PyObject_SetAttrString(self, "object", object))
1298 goto finally;
1299 if (PyObject_SetAttrString(self, "start", start))
1300 goto finally;
1301 if (PyObject_SetAttrString(self, "end", end))
1302 goto finally;
1303 if (PyObject_SetAttrString(self, "reason", reason))
1304 goto finally;
1305
1306 Py_INCREF(Py_None);
1307 rtnval = Py_None;
1308
1309 finally:
1310 Py_DECREF(args);
1311 return rtnval;
1312}
1313
1314
1315static PyObject *
1316UnicodeEncodeError__init__(PyObject *self, PyObject *args)
1317{
1318 return UnicodeError__init__(self, args, &PyUnicode_Type);
1319}
1320
1321static PyObject *
1322UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
1323{
1324 PyObject *encodingObj = NULL;
1325 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001326 Py_ssize_t start;
1327 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001328 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001329 PyObject *result = NULL;
1330
1331 self = arg;
1332
1333 if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self)))
1334 goto error;
1335
1336 if (!(objectObj = PyUnicodeEncodeError_GetObject(self)))
1337 goto error;
1338
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001339 if (PyUnicodeEncodeError_GetStart(self, &start))
1340 goto error;
1341
1342 if (PyUnicodeEncodeError_GetEnd(self, &end))
1343 goto error;
1344
1345 if (!(reasonObj = PyUnicodeEncodeError_GetReason(self)))
1346 goto error;
1347
1348 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001349 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001350 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001351 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001352 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001353 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001354 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001355 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001356 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1357 result = PyString_FromFormat(
1358 "'%.400s' codec can't encode character u'\\%s' in position %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 badchar_str,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001361 start,
1362 PyString_AS_STRING(reasonObj)
1363 );
1364 }
1365 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001366 result = PyString_FromFormat(
1367 "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001368 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001369 start,
1370 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001371 PyString_AS_STRING(reasonObj)
1372 );
1373 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001374
1375error:
1376 Py_XDECREF(reasonObj);
1377 Py_XDECREF(objectObj);
1378 Py_XDECREF(encodingObj);
1379 return result;
1380}
1381
1382static PyMethodDef UnicodeEncodeError_methods[] = {
1383 {"__init__", UnicodeEncodeError__init__, METH_VARARGS},
1384 {"__str__", UnicodeEncodeError__str__, METH_O},
1385 {NULL, NULL}
1386};
1387
1388
1389PyObject * PyUnicodeEncodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001390 const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1391 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001392{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001393 return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001394 encoding, object, length, start, end, reason);
1395}
1396
1397
1398static PyObject *
1399UnicodeDecodeError__init__(PyObject *self, PyObject *args)
1400{
1401 return UnicodeError__init__(self, args, &PyString_Type);
1402}
1403
1404static PyObject *
1405UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
1406{
1407 PyObject *encodingObj = NULL;
1408 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001409 Py_ssize_t start;
1410 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001411 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001412 PyObject *result = NULL;
1413
1414 self = arg;
1415
1416 if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self)))
1417 goto error;
1418
1419 if (!(objectObj = PyUnicodeDecodeError_GetObject(self)))
1420 goto error;
1421
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001422 if (PyUnicodeDecodeError_GetStart(self, &start))
1423 goto error;
1424
1425 if (PyUnicodeDecodeError_GetEnd(self, &end))
1426 goto error;
1427
1428 if (!(reasonObj = PyUnicodeDecodeError_GetReason(self)))
1429 goto error;
1430
1431 if (end==start+1) {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001432 /* FromFormat does not support %02x, so format that separately */
1433 char byte[4];
1434 PyOS_snprintf(byte, sizeof(byte), "%02x",
1435 ((int)PyString_AS_STRING(objectObj)[start])&0xff);
1436 result = PyString_FromFormat(
1437 "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001438 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001439 byte,
1440 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001441 PyString_AS_STRING(reasonObj)
1442 );
1443 }
1444 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001445 result = PyString_FromFormat(
1446 "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001447 PyString_AS_STRING(encodingObj),
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001448 start,
1449 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001450 PyString_AS_STRING(reasonObj)
1451 );
1452 }
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001453
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001454
1455error:
1456 Py_XDECREF(reasonObj);
1457 Py_XDECREF(objectObj);
1458 Py_XDECREF(encodingObj);
1459 return result;
1460}
1461
1462static PyMethodDef UnicodeDecodeError_methods[] = {
1463 {"__init__", UnicodeDecodeError__init__, METH_VARARGS},
1464 {"__str__", UnicodeDecodeError__str__, METH_O},
1465 {NULL, NULL}
1466};
1467
1468
1469PyObject * PyUnicodeDecodeError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001470 const char *encoding, const char *object, Py_ssize_t length,
1471 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001472{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001473 assert(length < INT_MAX);
1474 assert(start < INT_MAX);
1475 assert(end < INT_MAX);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001476 return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#iis",
Martin v. Löwis18e16552006-02-15 17:27:45 +00001477 encoding, object, (int)length, (int)start, (int)end, reason);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001478}
1479
1480
1481static PyObject *
1482UnicodeTranslateError__init__(PyObject *self, PyObject *args)
1483{
1484 PyObject *rtnval = NULL;
1485 PyObject *object;
1486 PyObject *start;
1487 PyObject *end;
1488 PyObject *reason;
1489
1490 if (!(self = get_self(args)))
1491 return NULL;
1492
1493 if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
1494 return NULL;
1495
Brett Cannonbf364092006-03-01 04:25:17 +00001496 if (!set_args_and_message(self, args)) {
1497 Py_DECREF(args);
1498 return NULL;
1499 }
1500
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001501 if (!PyArg_ParseTuple(args, "O!O!O!O!",
1502 &PyUnicode_Type, &object,
1503 &PyInt_Type, &start,
1504 &PyInt_Type, &end,
1505 &PyString_Type, &reason))
1506 goto finally;
1507
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001508 if (PyObject_SetAttrString(self, "object", object))
1509 goto finally;
1510 if (PyObject_SetAttrString(self, "start", start))
1511 goto finally;
1512 if (PyObject_SetAttrString(self, "end", end))
1513 goto finally;
1514 if (PyObject_SetAttrString(self, "reason", reason))
1515 goto finally;
1516
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001517 rtnval = Py_None;
Brett Cannonbf364092006-03-01 04:25:17 +00001518 Py_INCREF(rtnval);
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001519
1520 finally:
1521 Py_DECREF(args);
1522 return rtnval;
1523}
1524
1525
1526static PyObject *
1527UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
1528{
1529 PyObject *objectObj = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001530 Py_ssize_t start;
1531 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001532 PyObject *reasonObj = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001533 PyObject *result = NULL;
1534
1535 self = arg;
1536
1537 if (!(objectObj = PyUnicodeTranslateError_GetObject(self)))
1538 goto error;
1539
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001540 if (PyUnicodeTranslateError_GetStart(self, &start))
1541 goto error;
1542
1543 if (PyUnicodeTranslateError_GetEnd(self, &end))
1544 goto error;
1545
1546 if (!(reasonObj = PyUnicodeTranslateError_GetReason(self)))
1547 goto error;
1548
1549 if (end==start+1) {
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001550 int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001551 char badchar_str[20];
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001552 if (badchar <= 0xff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001553 PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001554 else if (badchar <= 0xffff)
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001555 PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
Walter Dörwaldfd196bd2003-08-12 17:32:43 +00001556 else
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001557 PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1558 result = PyString_FromFormat(
1559 "can't translate character u'\\%s' in position %zd: %.400s",
1560 badchar_str,
1561 start,
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001562 PyString_AS_STRING(reasonObj)
1563 );
1564 }
1565 else {
Martin v. Löwis720ddb62006-02-16 07:11:33 +00001566 result = PyString_FromFormat(
1567 "can't translate characters in position %zd-%zd: %.400s",
1568 start,
1569 (end-1),
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001570 PyString_AS_STRING(reasonObj)
1571 );
1572 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001573
1574error:
1575 Py_XDECREF(reasonObj);
1576 Py_XDECREF(objectObj);
1577 return result;
1578}
1579
1580static PyMethodDef UnicodeTranslateError_methods[] = {
1581 {"__init__", UnicodeTranslateError__init__, METH_VARARGS},
1582 {"__str__", UnicodeTranslateError__str__, METH_O},
1583 {NULL, NULL}
1584};
1585
1586
1587PyObject * PyUnicodeTranslateError_Create(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001588 const Py_UNICODE *object, Py_ssize_t length,
1589 Py_ssize_t start, Py_ssize_t end, const char *reason)
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001590{
1591 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#iis",
1592 object, length, start, end, reason);
1593}
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001594#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001595
1596
Barry Warsaw675ac282000-05-26 19:05:16 +00001597
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001598/* Exception doc strings */
1599
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001600PyDoc_STRVAR(AssertionError__doc__, "Assertion failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001601
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001602PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001604PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001605
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001606PyDoc_STRVAR(KeyError__doc__, "Mapping key not found.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001607
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001608PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001609
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001610PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001611
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001612PyDoc_STRVAR(ZeroDivisionError__doc__,
1613"Second argument to a division or modulo operation was zero.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001615PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001616
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001617PyDoc_STRVAR(ValueError__doc__,
1618"Inappropriate argument value (of correct type).");
Barry Warsaw675ac282000-05-26 19:05:16 +00001619
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001620PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001621
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001622#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001623PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error.");
1624
1625PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error.");
1626
1627PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error.");
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001628#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001630PyDoc_STRVAR(SystemError__doc__,
1631"Internal error in the Python interpreter.\n\
Barry Warsaw675ac282000-05-26 19:05:16 +00001632\n\
1633Please report this to the Python maintainer, along with the traceback,\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001634the Python version, and the hardware/OS platform and version.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001635
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001636PyDoc_STRVAR(ReferenceError__doc__,
1637"Weak ref proxy used after referent went away.");
Fred Drakebb9fa212001-10-05 21:50:08 +00001638
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001639PyDoc_STRVAR(MemoryError__doc__, "Out of memory.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001640
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001641PyDoc_STRVAR(IndentationError__doc__, "Improper indentation.");
Fred Drake85f36392000-07-11 17:53:00 +00001642
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001643PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs.");
Fred Drake85f36392000-07-11 17:53:00 +00001644
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001645/* Warning category docstrings */
1646
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001647PyDoc_STRVAR(Warning__doc__, "Base class for warning categories.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001648
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001649PyDoc_STRVAR(UserWarning__doc__,
1650"Base class for warnings generated by user code.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001652PyDoc_STRVAR(DeprecationWarning__doc__,
1653"Base class for warnings about deprecated features.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001655PyDoc_STRVAR(PendingDeprecationWarning__doc__,
Neal Norwitzd68f5172002-05-29 15:54:55 +00001656"Base class for warnings about features which will be deprecated "
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001657"in the future.");
Neal Norwitzd68f5172002-05-29 15:54:55 +00001658
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001659PyDoc_STRVAR(SyntaxWarning__doc__,
1660"Base class for warnings about dubious syntax.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001662PyDoc_STRVAR(OverflowWarning__doc__,
Tim Petersc8854432004-08-25 02:14:08 +00001663"Base class for warnings about numeric overflow. Won't exist in Python 2.5.");
Guido van Rossumae347b32001-08-23 02:56:07 +00001664
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001665PyDoc_STRVAR(RuntimeWarning__doc__,
1666"Base class for warnings about dubious runtime behavior.");
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001667
Barry Warsaw9f007392002-08-14 15:51:29 +00001668PyDoc_STRVAR(FutureWarning__doc__,
1669"Base class for warnings about constructs that will change semantically "
1670"in the future.");
1671
Barry Warsaw675ac282000-05-26 19:05:16 +00001672
1673
1674/* module global functions */
1675static PyMethodDef functions[] = {
1676 /* Sentinel */
1677 {NULL, NULL}
1678};
1679
1680
1681
1682/* Global C API defined exceptions */
1683
Brett Cannonbf364092006-03-01 04:25:17 +00001684PyObject *PyExc_BaseException;
Barry Warsaw675ac282000-05-26 19:05:16 +00001685PyObject *PyExc_Exception;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001686PyObject *PyExc_StopIteration;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001687PyObject *PyExc_GeneratorExit;
Barry Warsaw675ac282000-05-26 19:05:16 +00001688PyObject *PyExc_StandardError;
1689PyObject *PyExc_ArithmeticError;
1690PyObject *PyExc_LookupError;
1691
1692PyObject *PyExc_AssertionError;
1693PyObject *PyExc_AttributeError;
1694PyObject *PyExc_EOFError;
1695PyObject *PyExc_FloatingPointError;
1696PyObject *PyExc_EnvironmentError;
1697PyObject *PyExc_IOError;
1698PyObject *PyExc_OSError;
1699PyObject *PyExc_ImportError;
1700PyObject *PyExc_IndexError;
1701PyObject *PyExc_KeyError;
1702PyObject *PyExc_KeyboardInterrupt;
1703PyObject *PyExc_MemoryError;
1704PyObject *PyExc_NameError;
1705PyObject *PyExc_OverflowError;
1706PyObject *PyExc_RuntimeError;
1707PyObject *PyExc_NotImplementedError;
1708PyObject *PyExc_SyntaxError;
Fred Drake85f36392000-07-11 17:53:00 +00001709PyObject *PyExc_IndentationError;
1710PyObject *PyExc_TabError;
Fred Drakebb9fa212001-10-05 21:50:08 +00001711PyObject *PyExc_ReferenceError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001712PyObject *PyExc_SystemError;
1713PyObject *PyExc_SystemExit;
1714PyObject *PyExc_UnboundLocalError;
1715PyObject *PyExc_UnicodeError;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001716PyObject *PyExc_UnicodeEncodeError;
1717PyObject *PyExc_UnicodeDecodeError;
1718PyObject *PyExc_UnicodeTranslateError;
Barry Warsaw675ac282000-05-26 19:05:16 +00001719PyObject *PyExc_TypeError;
1720PyObject *PyExc_ValueError;
1721PyObject *PyExc_ZeroDivisionError;
1722#ifdef MS_WINDOWS
1723PyObject *PyExc_WindowsError;
1724#endif
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001725#ifdef __VMS
1726PyObject *PyExc_VMSError;
1727#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001728
1729/* Pre-computed MemoryError instance. Best to create this as early as
Brett Cannonbf364092006-03-01 04:25:17 +00001730 * possible and not wait until a MemoryError is actually raised!
Barry Warsaw675ac282000-05-26 19:05:16 +00001731 */
1732PyObject *PyExc_MemoryErrorInst;
1733
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001734/* Predefined warning categories */
1735PyObject *PyExc_Warning;
1736PyObject *PyExc_UserWarning;
1737PyObject *PyExc_DeprecationWarning;
Neal Norwitzd68f5172002-05-29 15:54:55 +00001738PyObject *PyExc_PendingDeprecationWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001739PyObject *PyExc_SyntaxWarning;
Tim Petersc8854432004-08-25 02:14:08 +00001740/* PyExc_OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001741PyObject *PyExc_OverflowWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001742PyObject *PyExc_RuntimeWarning;
Barry Warsaw9f007392002-08-14 15:51:29 +00001743PyObject *PyExc_FutureWarning;
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001744
Barry Warsaw675ac282000-05-26 19:05:16 +00001745
1746
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001747/* mapping between exception names and their PyObject ** */
1748static struct {
1749 char *name;
1750 PyObject **exc;
1751 PyObject **base; /* NULL == PyExc_StandardError */
1752 char *docstr;
1753 PyMethodDef *methods;
1754 int (*classinit)(PyObject *);
1755} exctable[] = {
Barry Warsaw675ac282000-05-26 19:05:16 +00001756 /*
Brett Cannonbf364092006-03-01 04:25:17 +00001757 * The first four classes MUST appear in exactly this order
Barry Warsaw675ac282000-05-26 19:05:16 +00001758 */
Brett Cannonbf364092006-03-01 04:25:17 +00001759 {"BaseException", &PyExc_BaseException},
1760 {"Exception", &PyExc_Exception, &PyExc_BaseException, Exception__doc__},
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001761 {"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
1762 StopIteration__doc__},
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001763 {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception,
1764 GeneratorExit__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001765 {"StandardError", &PyExc_StandardError, &PyExc_Exception,
1766 StandardError__doc__},
1767 {"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
1768 /*
1769 * The rest appear in depth-first order of the hierarchy
1770 */
Brett Cannonbf364092006-03-01 04:25:17 +00001771 {"SystemExit", &PyExc_SystemExit, &PyExc_BaseException, SystemExit__doc__,
Barry Warsaw675ac282000-05-26 19:05:16 +00001772 SystemExit_methods},
Brett Cannonbf364092006-03-01 04:25:17 +00001773 {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, &PyExc_BaseException,
1774 KeyboardInterrupt__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001775 {"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
1776 {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
1777 EnvironmentError_methods},
1778 {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
1779 {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
1780#ifdef MS_WINDOWS
Mark Hammond557a0442000-08-15 00:37:32 +00001781 {"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
Barry Warsaw675ac282000-05-26 19:05:16 +00001782 WindowsError__doc__},
1783#endif /* MS_WINDOWS */
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00001784#ifdef __VMS
1785 {"VMSError", &PyExc_VMSError, &PyExc_OSError,
1786 VMSError__doc__},
1787#endif
Barry Warsaw675ac282000-05-26 19:05:16 +00001788 {"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
1789 {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
1790 {"NotImplementedError", &PyExc_NotImplementedError,
1791 &PyExc_RuntimeError, NotImplementedError__doc__},
1792 {"NameError", &PyExc_NameError, 0, NameError__doc__},
1793 {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
1794 UnboundLocalError__doc__},
1795 {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
1796 {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
1797 SyntaxError_methods, SyntaxError__classinit__},
Fred Drake85f36392000-07-11 17:53:00 +00001798 {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
1799 IndentationError__doc__},
1800 {"TabError", &PyExc_TabError, &PyExc_IndentationError,
1801 TabError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001802 {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
1803 {"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
1804 {"IndexError", &PyExc_IndexError, &PyExc_LookupError,
1805 IndexError__doc__},
1806 {"KeyError", &PyExc_KeyError, &PyExc_LookupError,
Guido van Rossum602d4512002-09-03 20:24:09 +00001807 KeyError__doc__, KeyError_methods},
Barry Warsaw675ac282000-05-26 19:05:16 +00001808 {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
1809 {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
1810 OverflowError__doc__},
1811 {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
1812 ZeroDivisionError__doc__},
1813 {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
1814 FloatingPointError__doc__},
1815 {"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
1816 {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001817#ifdef Py_USING_UNICODE
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001818 {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError,
1819 UnicodeEncodeError__doc__, UnicodeEncodeError_methods},
1820 {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError,
1821 UnicodeDecodeError__doc__, UnicodeDecodeError_methods},
1822 {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError,
1823 UnicodeTranslateError__doc__, UnicodeTranslateError_methods},
Walter Dörwaldbf73db82002-11-21 20:08:33 +00001824#endif
Fred Drakebb9fa212001-10-05 21:50:08 +00001825 {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001826 {"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
1827 {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001828 /* Warning categories */
1829 {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
1830 {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
1831 {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
1832 DeprecationWarning__doc__},
Neal Norwitzd68f5172002-05-29 15:54:55 +00001833 {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning,
1834 PendingDeprecationWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001835 {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
Tim Petersc8854432004-08-25 02:14:08 +00001836 /* OverflowWarning should be removed for Python 2.5 */
Guido van Rossumae347b32001-08-23 02:56:07 +00001837 {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
1838 OverflowWarning__doc__},
Guido van Rossumd0977cd2000-12-15 21:58:29 +00001839 {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
1840 RuntimeWarning__doc__},
Barry Warsaw9f007392002-08-14 15:51:29 +00001841 {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning,
1842 FutureWarning__doc__},
Barry Warsaw675ac282000-05-26 19:05:16 +00001843 /* Sentinel */
1844 {NULL}
1845};
1846
1847
1848
Mark Hammonda2905272002-07-29 13:42:14 +00001849void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001850_PyExc_Init(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001851{
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001852 char *modulename = "exceptions";
Martin v. Löwis18e16552006-02-15 17:27:45 +00001853 Py_ssize_t modnamesz = strlen(modulename);
Barry Warsaw675ac282000-05-26 19:05:16 +00001854 int i;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001855 PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
Barry Warsaw675ac282000-05-26 19:05:16 +00001856
Tim Peters6d6c1a32001-08-02 04:15:00 +00001857 me = Py_InitModule(modulename, functions);
1858 if (me == NULL)
1859 goto err;
1860 mydict = PyModule_GetDict(me);
1861 if (mydict == NULL)
1862 goto err;
1863 bltinmod = PyImport_ImportModule("__builtin__");
1864 if (bltinmod == NULL)
1865 goto err;
1866 bdict = PyModule_GetDict(bltinmod);
1867 if (bdict == NULL)
1868 goto err;
1869 doc = PyString_FromString(module__doc__);
1870 if (doc == NULL)
1871 goto err;
Barry Warsaw675ac282000-05-26 19:05:16 +00001872
Tim Peters6d6c1a32001-08-02 04:15:00 +00001873 i = PyDict_SetItemString(mydict, "__doc__", doc);
Barry Warsaw8fcaa922000-07-01 04:45:52 +00001874 Py_DECREF(doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001875 if (i < 0) {
1876 err:
Barry Warsaw675ac282000-05-26 19:05:16 +00001877 Py_FatalError("exceptions bootstrapping error.");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001878 return;
1879 }
Barry Warsaw675ac282000-05-26 19:05:16 +00001880
1881 /* This is the base class of all exceptions, so make it first. */
Brett Cannonbf364092006-03-01 04:25:17 +00001882 if (make_BaseException(modulename) ||
1883 PyDict_SetItemString(mydict, "BaseException", PyExc_BaseException) ||
1884 PyDict_SetItemString(bdict, "BaseException", PyExc_BaseException))
Barry Warsaw675ac282000-05-26 19:05:16 +00001885 {
Brett Cannonbf364092006-03-01 04:25:17 +00001886 Py_FatalError("Base class `BaseException' could not be created.");
Barry Warsaw675ac282000-05-26 19:05:16 +00001887 }
Barry Warsaw9667ed22001-01-23 16:08:34 +00001888
Barry Warsaw675ac282000-05-26 19:05:16 +00001889 /* Now we can programmatically create all the remaining exceptions.
1890 * Remember to start the loop at 1 to skip Exceptions.
1891 */
1892 for (i=1; exctable[i].name; i++) {
1893 int status;
Thomas Wouters0452d1f2000-07-22 18:45:06 +00001894 char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
1895 PyObject *base;
Barry Warsaw675ac282000-05-26 19:05:16 +00001896
1897 (void)strcpy(cname, modulename);
1898 (void)strcat(cname, ".");
1899 (void)strcat(cname, exctable[i].name);
1900
1901 if (exctable[i].base == 0)
1902 base = PyExc_StandardError;
1903 else
1904 base = *exctable[i].base;
1905
1906 status = make_class(exctable[i].exc, base, cname,
1907 exctable[i].methods,
1908 exctable[i].docstr);
1909
1910 PyMem_DEL(cname);
1911
1912 if (status)
1913 Py_FatalError("Standard exception classes could not be created.");
1914
1915 if (exctable[i].classinit) {
1916 status = (*exctable[i].classinit)(*exctable[i].exc);
1917 if (status)
1918 Py_FatalError("An exception class could not be initialized.");
1919 }
1920
1921 /* Now insert the class into both this module and the __builtin__
1922 * module.
1923 */
1924 if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
1925 PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
1926 {
1927 Py_FatalError("Module dictionary insertion problem.");
1928 }
1929 }
1930
1931 /* Now we need to pre-allocate a MemoryError instance */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001932 args = PyTuple_New(0);
Barry Warsaw675ac282000-05-26 19:05:16 +00001933 if (!args ||
1934 !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
1935 {
1936 Py_FatalError("Cannot pre-allocate MemoryError instance\n");
1937 }
1938 Py_DECREF(args);
1939
1940 /* We're done with __builtin__ */
1941 Py_DECREF(bltinmod);
1942}
1943
1944
Mark Hammonda2905272002-07-29 13:42:14 +00001945void
Tim Peters6d6c1a32001-08-02 04:15:00 +00001946_PyExc_Fini(void)
Barry Warsaw675ac282000-05-26 19:05:16 +00001947{
1948 int i;
1949
1950 Py_XDECREF(PyExc_MemoryErrorInst);
1951 PyExc_MemoryErrorInst = NULL;
1952
1953 for (i=0; exctable[i].name; i++) {
Barry Warsaw9667ed22001-01-23 16:08:34 +00001954 /* clear the class's dictionary, freeing up circular references
1955 * between the class and its methods.
1956 */
1957 PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
1958 PyDict_Clear(cdict);
1959 Py_DECREF(cdict);
1960
1961 /* Now decref the exception class */
Barry Warsaw675ac282000-05-26 19:05:16 +00001962 Py_XDECREF(*exctable[i].exc);
1963 *exctable[i].exc = NULL;
1964 }
1965}