]>
Commit | Line | Data |
---|---|---|
53b2ba57 DM |
1 | /*\r |
2 | * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.\r | |
3 | *\r | |
4 | * Thanks go to Tim Peters and Michael Hudson for debugging.\r | |
5 | */\r | |
6 | \r | |
7 | #define PY_SSIZE_T_CLEAN\r | |
8 | #include <Python.h>\r | |
9 | #include "structmember.h"\r | |
10 | #include "osdefs.h"\r | |
11 | \r | |
12 | #define EXC_MODULE_NAME "exceptions."\r | |
13 | \r | |
14 | /* NOTE: If the exception class hierarchy changes, don't forget to update\r | |
15 | * Lib/test/exception_hierarchy.txt\r | |
16 | */\r | |
17 | \r | |
18 | PyDoc_STRVAR(exceptions_doc, "Python's standard exception class hierarchy.\n\\r | |
19 | \n\\r | |
20 | Exceptions found here are defined both in the exceptions module and the\n\\r | |
21 | built-in namespace. It is recommended that user-defined exceptions\n\\r | |
22 | inherit from Exception. See the documentation for the exception\n\\r | |
23 | inheritance hierarchy.\n\\r | |
24 | ");\r | |
25 | \r | |
26 | /*\r | |
27 | * BaseException\r | |
28 | */\r | |
29 | static PyObject *\r | |
30 | BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r | |
31 | {\r | |
32 | PyBaseExceptionObject *self;\r | |
33 | \r | |
34 | self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);\r | |
35 | if (!self)\r | |
36 | return NULL;\r | |
37 | /* the dict is created on the fly in PyObject_GenericSetAttr */\r | |
38 | self->message = self->dict = NULL;\r | |
39 | \r | |
40 | self->args = PyTuple_New(0);\r | |
41 | if (!self->args) {\r | |
42 | Py_DECREF(self);\r | |
43 | return NULL;\r | |
44 | }\r | |
45 | \r | |
46 | self->message = PyString_FromString("");\r | |
47 | if (!self->message) {\r | |
48 | Py_DECREF(self);\r | |
49 | return NULL;\r | |
50 | }\r | |
51 | \r | |
52 | return (PyObject *)self;\r | |
53 | }\r | |
54 | \r | |
55 | static int\r | |
56 | BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)\r | |
57 | {\r | |
58 | if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))\r | |
59 | return -1;\r | |
60 | \r | |
61 | Py_DECREF(self->args);\r | |
62 | self->args = args;\r | |
63 | Py_INCREF(self->args);\r | |
64 | \r | |
65 | if (PyTuple_GET_SIZE(self->args) == 1) {\r | |
66 | Py_CLEAR(self->message);\r | |
67 | self->message = PyTuple_GET_ITEM(self->args, 0);\r | |
68 | Py_INCREF(self->message);\r | |
69 | }\r | |
70 | return 0;\r | |
71 | }\r | |
72 | \r | |
73 | static int\r | |
74 | BaseException_clear(PyBaseExceptionObject *self)\r | |
75 | {\r | |
76 | Py_CLEAR(self->dict);\r | |
77 | Py_CLEAR(self->args);\r | |
78 | Py_CLEAR(self->message);\r | |
79 | return 0;\r | |
80 | }\r | |
81 | \r | |
82 | static void\r | |
83 | BaseException_dealloc(PyBaseExceptionObject *self)\r | |
84 | {\r | |
85 | _PyObject_GC_UNTRACK(self);\r | |
86 | BaseException_clear(self);\r | |
87 | Py_TYPE(self)->tp_free((PyObject *)self);\r | |
88 | }\r | |
89 | \r | |
90 | static int\r | |
91 | BaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)\r | |
92 | {\r | |
93 | Py_VISIT(self->dict);\r | |
94 | Py_VISIT(self->args);\r | |
95 | Py_VISIT(self->message);\r | |
96 | return 0;\r | |
97 | }\r | |
98 | \r | |
99 | static PyObject *\r | |
100 | BaseException_str(PyBaseExceptionObject *self)\r | |
101 | {\r | |
102 | PyObject *out;\r | |
103 | \r | |
104 | switch (PyTuple_GET_SIZE(self->args)) {\r | |
105 | case 0:\r | |
106 | out = PyString_FromString("");\r | |
107 | break;\r | |
108 | case 1:\r | |
109 | out = PyObject_Str(PyTuple_GET_ITEM(self->args, 0));\r | |
110 | break;\r | |
111 | default:\r | |
112 | out = PyObject_Str(self->args);\r | |
113 | break;\r | |
114 | }\r | |
115 | \r | |
116 | return out;\r | |
117 | }\r | |
118 | \r | |
119 | #ifdef Py_USING_UNICODE\r | |
120 | static PyObject *\r | |
121 | BaseException_unicode(PyBaseExceptionObject *self)\r | |
122 | {\r | |
123 | PyObject *out;\r | |
124 | \r | |
125 | /* issue6108: if __str__ has been overridden in the subclass, unicode()\r | |
126 | should return the message returned by __str__ as used to happen\r | |
127 | before this method was implemented. */\r | |
128 | if (Py_TYPE(self)->tp_str != (reprfunc)BaseException_str) {\r | |
129 | PyObject *str;\r | |
130 | /* Unlike PyObject_Str, tp_str can return unicode (i.e. return the\r | |
131 | equivalent of unicode(e.__str__()) instead of unicode(str(e))). */\r | |
132 | str = Py_TYPE(self)->tp_str((PyObject*)self);\r | |
133 | if (str == NULL)\r | |
134 | return NULL;\r | |
135 | out = PyObject_Unicode(str);\r | |
136 | Py_DECREF(str);\r | |
137 | return out;\r | |
138 | }\r | |
139 | \r | |
140 | switch (PyTuple_GET_SIZE(self->args)) {\r | |
141 | case 0:\r | |
142 | out = PyUnicode_FromString("");\r | |
143 | break;\r | |
144 | case 1:\r | |
145 | out = PyObject_Unicode(PyTuple_GET_ITEM(self->args, 0));\r | |
146 | break;\r | |
147 | default:\r | |
148 | out = PyObject_Unicode(self->args);\r | |
149 | break;\r | |
150 | }\r | |
151 | \r | |
152 | return out;\r | |
153 | }\r | |
154 | #endif\r | |
155 | \r | |
156 | static PyObject *\r | |
157 | BaseException_repr(PyBaseExceptionObject *self)\r | |
158 | {\r | |
159 | PyObject *repr_suffix;\r | |
160 | PyObject *repr;\r | |
161 | char *name;\r | |
162 | char *dot;\r | |
163 | \r | |
164 | repr_suffix = PyObject_Repr(self->args);\r | |
165 | if (!repr_suffix)\r | |
166 | return NULL;\r | |
167 | \r | |
168 | name = (char *)Py_TYPE(self)->tp_name;\r | |
169 | dot = strrchr(name, '.');\r | |
170 | if (dot != NULL) name = dot+1;\r | |
171 | \r | |
172 | repr = PyString_FromString(name);\r | |
173 | if (!repr) {\r | |
174 | Py_DECREF(repr_suffix);\r | |
175 | return NULL;\r | |
176 | }\r | |
177 | \r | |
178 | PyString_ConcatAndDel(&repr, repr_suffix);\r | |
179 | return repr;\r | |
180 | }\r | |
181 | \r | |
182 | /* Pickling support */\r | |
183 | static PyObject *\r | |
184 | BaseException_reduce(PyBaseExceptionObject *self)\r | |
185 | {\r | |
186 | if (self->args && self->dict)\r | |
187 | return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);\r | |
188 | else\r | |
189 | return PyTuple_Pack(2, Py_TYPE(self), self->args);\r | |
190 | }\r | |
191 | \r | |
192 | /*\r | |
193 | * Needed for backward compatibility, since exceptions used to store\r | |
194 | * all their attributes in the __dict__. Code is taken from cPickle's\r | |
195 | * load_build function.\r | |
196 | */\r | |
197 | static PyObject *\r | |
198 | BaseException_setstate(PyObject *self, PyObject *state)\r | |
199 | {\r | |
200 | PyObject *d_key, *d_value;\r | |
201 | Py_ssize_t i = 0;\r | |
202 | \r | |
203 | if (state != Py_None) {\r | |
204 | if (!PyDict_Check(state)) {\r | |
205 | PyErr_SetString(PyExc_TypeError, "state is not a dictionary");\r | |
206 | return NULL;\r | |
207 | }\r | |
208 | while (PyDict_Next(state, &i, &d_key, &d_value)) {\r | |
209 | if (PyObject_SetAttr(self, d_key, d_value) < 0)\r | |
210 | return NULL;\r | |
211 | }\r | |
212 | }\r | |
213 | Py_RETURN_NONE;\r | |
214 | }\r | |
215 | \r | |
216 | \r | |
217 | static PyMethodDef BaseException_methods[] = {\r | |
218 | {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },\r | |
219 | {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },\r | |
220 | #ifdef Py_USING_UNICODE\r | |
221 | {"__unicode__", (PyCFunction)BaseException_unicode, METH_NOARGS },\r | |
222 | #endif\r | |
223 | {NULL, NULL, 0, NULL},\r | |
224 | };\r | |
225 | \r | |
226 | \r | |
227 | \r | |
228 | static PyObject *\r | |
229 | BaseException_getitem(PyBaseExceptionObject *self, Py_ssize_t index)\r | |
230 | {\r | |
231 | if (PyErr_WarnPy3k("__getitem__ not supported for exception "\r | |
232 | "classes in 3.x; use args attribute", 1) < 0)\r | |
233 | return NULL;\r | |
234 | return PySequence_GetItem(self->args, index);\r | |
235 | }\r | |
236 | \r | |
237 | static PyObject *\r | |
238 | BaseException_getslice(PyBaseExceptionObject *self,\r | |
239 | Py_ssize_t start, Py_ssize_t stop)\r | |
240 | {\r | |
241 | if (PyErr_WarnPy3k("__getslice__ not supported for exception "\r | |
242 | "classes in 3.x; use args attribute", 1) < 0)\r | |
243 | return NULL;\r | |
244 | return PySequence_GetSlice(self->args, start, stop);\r | |
245 | }\r | |
246 | \r | |
247 | static PySequenceMethods BaseException_as_sequence = {\r | |
248 | 0, /* sq_length; */\r | |
249 | 0, /* sq_concat; */\r | |
250 | 0, /* sq_repeat; */\r | |
251 | (ssizeargfunc)BaseException_getitem, /* sq_item; */\r | |
252 | (ssizessizeargfunc)BaseException_getslice, /* sq_slice; */\r | |
253 | 0, /* sq_ass_item; */\r | |
254 | 0, /* sq_ass_slice; */\r | |
255 | 0, /* sq_contains; */\r | |
256 | 0, /* sq_inplace_concat; */\r | |
257 | 0 /* sq_inplace_repeat; */\r | |
258 | };\r | |
259 | \r | |
260 | static PyObject *\r | |
261 | BaseException_get_dict(PyBaseExceptionObject *self)\r | |
262 | {\r | |
263 | if (self->dict == NULL) {\r | |
264 | self->dict = PyDict_New();\r | |
265 | if (!self->dict)\r | |
266 | return NULL;\r | |
267 | }\r | |
268 | Py_INCREF(self->dict);\r | |
269 | return self->dict;\r | |
270 | }\r | |
271 | \r | |
272 | static int\r | |
273 | BaseException_set_dict(PyBaseExceptionObject *self, PyObject *val)\r | |
274 | {\r | |
275 | if (val == NULL) {\r | |
276 | PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");\r | |
277 | return -1;\r | |
278 | }\r | |
279 | if (!PyDict_Check(val)) {\r | |
280 | PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");\r | |
281 | return -1;\r | |
282 | }\r | |
283 | Py_CLEAR(self->dict);\r | |
284 | Py_INCREF(val);\r | |
285 | self->dict = val;\r | |
286 | return 0;\r | |
287 | }\r | |
288 | \r | |
289 | static PyObject *\r | |
290 | BaseException_get_args(PyBaseExceptionObject *self)\r | |
291 | {\r | |
292 | if (self->args == NULL) {\r | |
293 | Py_INCREF(Py_None);\r | |
294 | return Py_None;\r | |
295 | }\r | |
296 | Py_INCREF(self->args);\r | |
297 | return self->args;\r | |
298 | }\r | |
299 | \r | |
300 | static int\r | |
301 | BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)\r | |
302 | {\r | |
303 | PyObject *seq;\r | |
304 | if (val == NULL) {\r | |
305 | PyErr_SetString(PyExc_TypeError, "args may not be deleted");\r | |
306 | return -1;\r | |
307 | }\r | |
308 | seq = PySequence_Tuple(val);\r | |
309 | if (!seq)\r | |
310 | return -1;\r | |
311 | Py_CLEAR(self->args);\r | |
312 | self->args = seq;\r | |
313 | return 0;\r | |
314 | }\r | |
315 | \r | |
316 | static PyObject *\r | |
317 | BaseException_get_message(PyBaseExceptionObject *self)\r | |
318 | {\r | |
319 | PyObject *msg;\r | |
320 | \r | |
321 | /* if "message" is in self->dict, accessing a user-set message attribute */\r | |
322 | if (self->dict &&\r | |
323 | (msg = PyDict_GetItemString(self->dict, "message"))) {\r | |
324 | Py_INCREF(msg);\r | |
325 | return msg;\r | |
326 | }\r | |
327 | \r | |
328 | if (self->message == NULL) {\r | |
329 | PyErr_SetString(PyExc_AttributeError, "message attribute was deleted");\r | |
330 | return NULL;\r | |
331 | }\r | |
332 | \r | |
333 | /* accessing the deprecated "builtin" message attribute of Exception */\r | |
334 | if (PyErr_WarnEx(PyExc_DeprecationWarning,\r | |
335 | "BaseException.message has been deprecated as "\r | |
336 | "of Python 2.6", 1) < 0)\r | |
337 | return NULL;\r | |
338 | \r | |
339 | Py_INCREF(self->message);\r | |
340 | return self->message;\r | |
341 | }\r | |
342 | \r | |
343 | static int\r | |
344 | BaseException_set_message(PyBaseExceptionObject *self, PyObject *val)\r | |
345 | {\r | |
346 | /* if val is NULL, delete the message attribute */\r | |
347 | if (val == NULL) {\r | |
348 | if (self->dict && PyDict_GetItemString(self->dict, "message")) {\r | |
349 | if (PyDict_DelItemString(self->dict, "message") < 0)\r | |
350 | return -1;\r | |
351 | }\r | |
352 | Py_CLEAR(self->message);\r | |
353 | return 0;\r | |
354 | }\r | |
355 | \r | |
356 | /* else set it in __dict__, but may need to create the dict first */\r | |
357 | if (self->dict == NULL) {\r | |
358 | self->dict = PyDict_New();\r | |
359 | if (!self->dict)\r | |
360 | return -1;\r | |
361 | }\r | |
362 | return PyDict_SetItemString(self->dict, "message", val);\r | |
363 | }\r | |
364 | \r | |
365 | static PyGetSetDef BaseException_getset[] = {\r | |
366 | {"__dict__", (getter)BaseException_get_dict, (setter)BaseException_set_dict},\r | |
367 | {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},\r | |
368 | {"message", (getter)BaseException_get_message,\r | |
369 | (setter)BaseException_set_message},\r | |
370 | {NULL},\r | |
371 | };\r | |
372 | \r | |
373 | \r | |
374 | static PyTypeObject _PyExc_BaseException = {\r | |
375 | PyObject_HEAD_INIT(NULL)\r | |
376 | 0, /*ob_size*/\r | |
377 | EXC_MODULE_NAME "BaseException", /*tp_name*/\r | |
378 | sizeof(PyBaseExceptionObject), /*tp_basicsize*/\r | |
379 | 0, /*tp_itemsize*/\r | |
380 | (destructor)BaseException_dealloc, /*tp_dealloc*/\r | |
381 | 0, /*tp_print*/\r | |
382 | 0, /*tp_getattr*/\r | |
383 | 0, /*tp_setattr*/\r | |
384 | 0, /* tp_compare; */\r | |
385 | (reprfunc)BaseException_repr, /*tp_repr*/\r | |
386 | 0, /*tp_as_number*/\r | |
387 | &BaseException_as_sequence, /*tp_as_sequence*/\r | |
388 | 0, /*tp_as_mapping*/\r | |
389 | 0, /*tp_hash */\r | |
390 | 0, /*tp_call*/\r | |
391 | (reprfunc)BaseException_str, /*tp_str*/\r | |
392 | PyObject_GenericGetAttr, /*tp_getattro*/\r | |
393 | PyObject_GenericSetAttr, /*tp_setattro*/\r | |
394 | 0, /*tp_as_buffer*/\r | |
395 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |\r | |
396 | Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/\r | |
397 | PyDoc_STR("Common base class for all exceptions"), /* tp_doc */\r | |
398 | (traverseproc)BaseException_traverse, /* tp_traverse */\r | |
399 | (inquiry)BaseException_clear, /* tp_clear */\r | |
400 | 0, /* tp_richcompare */\r | |
401 | 0, /* tp_weaklistoffset */\r | |
402 | 0, /* tp_iter */\r | |
403 | 0, /* tp_iternext */\r | |
404 | BaseException_methods, /* tp_methods */\r | |
405 | 0, /* tp_members */\r | |
406 | BaseException_getset, /* tp_getset */\r | |
407 | 0, /* tp_base */\r | |
408 | 0, /* tp_dict */\r | |
409 | 0, /* tp_descr_get */\r | |
410 | 0, /* tp_descr_set */\r | |
411 | offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */\r | |
412 | (initproc)BaseException_init, /* tp_init */\r | |
413 | 0, /* tp_alloc */\r | |
414 | BaseException_new, /* tp_new */\r | |
415 | };\r | |
416 | /* the CPython API expects exceptions to be (PyObject *) - both a hold-over\r | |
417 | from the previous implmentation and also allowing Python objects to be used\r | |
418 | in the API */\r | |
419 | PyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;\r | |
420 | \r | |
421 | /* note these macros omit the last semicolon so the macro invocation may\r | |
422 | * include it and not look strange.\r | |
423 | */\r | |
424 | #define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \\r | |
425 | static PyTypeObject _PyExc_ ## EXCNAME = { \\r | |
426 | PyObject_HEAD_INIT(NULL) \\r | |
427 | 0, \\r | |
428 | EXC_MODULE_NAME # EXCNAME, \\r | |
429 | sizeof(PyBaseExceptionObject), \\r | |
430 | 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \\r | |
431 | 0, 0, 0, 0, 0, 0, 0, \\r | |
432 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \\r | |
433 | PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \\r | |
434 | (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \\r | |
435 | 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \\r | |
436 | (initproc)BaseException_init, 0, BaseException_new,\\r | |
437 | }; \\r | |
438 | PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME\r | |
439 | \r | |
440 | #define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \\r | |
441 | static PyTypeObject _PyExc_ ## EXCNAME = { \\r | |
442 | PyObject_HEAD_INIT(NULL) \\r | |
443 | 0, \\r | |
444 | EXC_MODULE_NAME # EXCNAME, \\r | |
445 | sizeof(Py ## EXCSTORE ## Object), \\r | |
446 | 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \\r | |
447 | 0, 0, 0, 0, 0, \\r | |
448 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \\r | |
449 | PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \\r | |
450 | (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \\r | |
451 | 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \\r | |
452 | (initproc)EXCSTORE ## _init, 0, BaseException_new,\\r | |
453 | }; \\r | |
454 | PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME\r | |
455 | \r | |
456 | #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHODS, EXCMEMBERS, EXCSTR, EXCDOC) \\r | |
457 | static PyTypeObject _PyExc_ ## EXCNAME = { \\r | |
458 | PyObject_HEAD_INIT(NULL) \\r | |
459 | 0, \\r | |
460 | EXC_MODULE_NAME # EXCNAME, \\r | |
461 | sizeof(Py ## EXCSTORE ## Object), 0, \\r | |
462 | (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \\r | |
463 | (reprfunc)EXCSTR, 0, 0, 0, \\r | |
464 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \\r | |
465 | PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \\r | |
466 | (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \\r | |
467 | EXCMEMBERS, 0, &_ ## EXCBASE, \\r | |
468 | 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \\r | |
469 | (initproc)EXCSTORE ## _init, 0, BaseException_new,\\r | |
470 | }; \\r | |
471 | PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME\r | |
472 | \r | |
473 | \r | |
474 | /*\r | |
475 | * Exception extends BaseException\r | |
476 | */\r | |
477 | SimpleExtendsException(PyExc_BaseException, Exception,\r | |
478 | "Common base class for all non-exit exceptions.");\r | |
479 | \r | |
480 | \r | |
481 | /*\r | |
482 | * StandardError extends Exception\r | |
483 | */\r | |
484 | SimpleExtendsException(PyExc_Exception, StandardError,\r | |
485 | "Base class for all standard Python exceptions that do not represent\n"\r | |
486 | "interpreter exiting.");\r | |
487 | \r | |
488 | \r | |
489 | /*\r | |
490 | * TypeError extends StandardError\r | |
491 | */\r | |
492 | SimpleExtendsException(PyExc_StandardError, TypeError,\r | |
493 | "Inappropriate argument type.");\r | |
494 | \r | |
495 | \r | |
496 | /*\r | |
497 | * StopIteration extends Exception\r | |
498 | */\r | |
499 | SimpleExtendsException(PyExc_Exception, StopIteration,\r | |
500 | "Signal the end from iterator.next().");\r | |
501 | \r | |
502 | \r | |
503 | /*\r | |
504 | * GeneratorExit extends BaseException\r | |
505 | */\r | |
506 | SimpleExtendsException(PyExc_BaseException, GeneratorExit,\r | |
507 | "Request that a generator exit.");\r | |
508 | \r | |
509 | \r | |
510 | /*\r | |
511 | * SystemExit extends BaseException\r | |
512 | */\r | |
513 | \r | |
514 | static int\r | |
515 | SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)\r | |
516 | {\r | |
517 | Py_ssize_t size = PyTuple_GET_SIZE(args);\r | |
518 | \r | |
519 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)\r | |
520 | return -1;\r | |
521 | \r | |
522 | if (size == 0)\r | |
523 | return 0;\r | |
524 | Py_CLEAR(self->code);\r | |
525 | if (size == 1)\r | |
526 | self->code = PyTuple_GET_ITEM(args, 0);\r | |
527 | else if (size > 1)\r | |
528 | self->code = args;\r | |
529 | Py_INCREF(self->code);\r | |
530 | return 0;\r | |
531 | }\r | |
532 | \r | |
533 | static int\r | |
534 | SystemExit_clear(PySystemExitObject *self)\r | |
535 | {\r | |
536 | Py_CLEAR(self->code);\r | |
537 | return BaseException_clear((PyBaseExceptionObject *)self);\r | |
538 | }\r | |
539 | \r | |
540 | static void\r | |
541 | SystemExit_dealloc(PySystemExitObject *self)\r | |
542 | {\r | |
543 | _PyObject_GC_UNTRACK(self);\r | |
544 | SystemExit_clear(self);\r | |
545 | Py_TYPE(self)->tp_free((PyObject *)self);\r | |
546 | }\r | |
547 | \r | |
548 | static int\r | |
549 | SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)\r | |
550 | {\r | |
551 | Py_VISIT(self->code);\r | |
552 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);\r | |
553 | }\r | |
554 | \r | |
555 | static PyMemberDef SystemExit_members[] = {\r | |
556 | {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,\r | |
557 | PyDoc_STR("exception code")},\r | |
558 | {NULL} /* Sentinel */\r | |
559 | };\r | |
560 | \r | |
561 | ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,\r | |
562 | SystemExit_dealloc, 0, SystemExit_members, 0,\r | |
563 | "Request to exit from the interpreter.");\r | |
564 | \r | |
565 | /*\r | |
566 | * KeyboardInterrupt extends BaseException\r | |
567 | */\r | |
568 | SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,\r | |
569 | "Program interrupted by user.");\r | |
570 | \r | |
571 | \r | |
572 | /*\r | |
573 | * ImportError extends StandardError\r | |
574 | */\r | |
575 | SimpleExtendsException(PyExc_StandardError, ImportError,\r | |
576 | "Import can't find module, or can't find name in module.");\r | |
577 | \r | |
578 | \r | |
579 | /*\r | |
580 | * EnvironmentError extends StandardError\r | |
581 | */\r | |
582 | \r | |
583 | /* Where a function has a single filename, such as open() or some\r | |
584 | * of the os module functions, PyErr_SetFromErrnoWithFilename() is\r | |
585 | * called, giving a third argument which is the filename. But, so\r | |
586 | * that old code using in-place unpacking doesn't break, e.g.:\r | |
587 | *\r | |
588 | * except IOError, (errno, strerror):\r | |
589 | *\r | |
590 | * we hack args so that it only contains two items. This also\r | |
591 | * means we need our own __str__() which prints out the filename\r | |
592 | * when it was supplied.\r | |
593 | */\r | |
594 | static int\r | |
595 | EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args,\r | |
596 | PyObject *kwds)\r | |
597 | {\r | |
598 | PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;\r | |
599 | PyObject *subslice = NULL;\r | |
600 | \r | |
601 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)\r | |
602 | return -1;\r | |
603 | \r | |
604 | if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) {\r | |
605 | return 0;\r | |
606 | }\r | |
607 | \r | |
608 | if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3,\r | |
609 | &myerrno, &strerror, &filename)) {\r | |
610 | return -1;\r | |
611 | }\r | |
612 | Py_CLEAR(self->myerrno); /* replacing */\r | |
613 | self->myerrno = myerrno;\r | |
614 | Py_INCREF(self->myerrno);\r | |
615 | \r | |
616 | Py_CLEAR(self->strerror); /* replacing */\r | |
617 | self->strerror = strerror;\r | |
618 | Py_INCREF(self->strerror);\r | |
619 | \r | |
620 | /* self->filename will remain Py_None otherwise */\r | |
621 | if (filename != NULL) {\r | |
622 | Py_CLEAR(self->filename); /* replacing */\r | |
623 | self->filename = filename;\r | |
624 | Py_INCREF(self->filename);\r | |
625 | \r | |
626 | subslice = PyTuple_GetSlice(args, 0, 2);\r | |
627 | if (!subslice)\r | |
628 | return -1;\r | |
629 | \r | |
630 | Py_DECREF(self->args); /* replacing args */\r | |
631 | self->args = subslice;\r | |
632 | }\r | |
633 | return 0;\r | |
634 | }\r | |
635 | \r | |
636 | static int\r | |
637 | EnvironmentError_clear(PyEnvironmentErrorObject *self)\r | |
638 | {\r | |
639 | Py_CLEAR(self->myerrno);\r | |
640 | Py_CLEAR(self->strerror);\r | |
641 | Py_CLEAR(self->filename);\r | |
642 | return BaseException_clear((PyBaseExceptionObject *)self);\r | |
643 | }\r | |
644 | \r | |
645 | static void\r | |
646 | EnvironmentError_dealloc(PyEnvironmentErrorObject *self)\r | |
647 | {\r | |
648 | _PyObject_GC_UNTRACK(self);\r | |
649 | EnvironmentError_clear(self);\r | |
650 | Py_TYPE(self)->tp_free((PyObject *)self);\r | |
651 | }\r | |
652 | \r | |
653 | static int\r | |
654 | EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit,\r | |
655 | void *arg)\r | |
656 | {\r | |
657 | Py_VISIT(self->myerrno);\r | |
658 | Py_VISIT(self->strerror);\r | |
659 | Py_VISIT(self->filename);\r | |
660 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);\r | |
661 | }\r | |
662 | \r | |
663 | static PyObject *\r | |
664 | EnvironmentError_str(PyEnvironmentErrorObject *self)\r | |
665 | {\r | |
666 | PyObject *rtnval = NULL;\r | |
667 | \r | |
668 | if (self->filename) {\r | |
669 | PyObject *fmt;\r | |
670 | PyObject *repr;\r | |
671 | PyObject *tuple;\r | |
672 | \r | |
673 | fmt = PyString_FromString("[Errno %s] %s: %s");\r | |
674 | if (!fmt)\r | |
675 | return NULL;\r | |
676 | \r | |
677 | repr = PyObject_Repr(self->filename);\r | |
678 | if (!repr) {\r | |
679 | Py_DECREF(fmt);\r | |
680 | return NULL;\r | |
681 | }\r | |
682 | tuple = PyTuple_New(3);\r | |
683 | if (!tuple) {\r | |
684 | Py_DECREF(repr);\r | |
685 | Py_DECREF(fmt);\r | |
686 | return NULL;\r | |
687 | }\r | |
688 | \r | |
689 | if (self->myerrno) {\r | |
690 | Py_INCREF(self->myerrno);\r | |
691 | PyTuple_SET_ITEM(tuple, 0, self->myerrno);\r | |
692 | }\r | |
693 | else {\r | |
694 | Py_INCREF(Py_None);\r | |
695 | PyTuple_SET_ITEM(tuple, 0, Py_None);\r | |
696 | }\r | |
697 | if (self->strerror) {\r | |
698 | Py_INCREF(self->strerror);\r | |
699 | PyTuple_SET_ITEM(tuple, 1, self->strerror);\r | |
700 | }\r | |
701 | else {\r | |
702 | Py_INCREF(Py_None);\r | |
703 | PyTuple_SET_ITEM(tuple, 1, Py_None);\r | |
704 | }\r | |
705 | \r | |
706 | PyTuple_SET_ITEM(tuple, 2, repr);\r | |
707 | \r | |
708 | rtnval = PyString_Format(fmt, tuple);\r | |
709 | \r | |
710 | Py_DECREF(fmt);\r | |
711 | Py_DECREF(tuple);\r | |
712 | }\r | |
713 | else if (self->myerrno && self->strerror) {\r | |
714 | PyObject *fmt;\r | |
715 | PyObject *tuple;\r | |
716 | \r | |
717 | fmt = PyString_FromString("[Errno %s] %s");\r | |
718 | if (!fmt)\r | |
719 | return NULL;\r | |
720 | \r | |
721 | tuple = PyTuple_New(2);\r | |
722 | if (!tuple) {\r | |
723 | Py_DECREF(fmt);\r | |
724 | return NULL;\r | |
725 | }\r | |
726 | \r | |
727 | if (self->myerrno) {\r | |
728 | Py_INCREF(self->myerrno);\r | |
729 | PyTuple_SET_ITEM(tuple, 0, self->myerrno);\r | |
730 | }\r | |
731 | else {\r | |
732 | Py_INCREF(Py_None);\r | |
733 | PyTuple_SET_ITEM(tuple, 0, Py_None);\r | |
734 | }\r | |
735 | if (self->strerror) {\r | |
736 | Py_INCREF(self->strerror);\r | |
737 | PyTuple_SET_ITEM(tuple, 1, self->strerror);\r | |
738 | }\r | |
739 | else {\r | |
740 | Py_INCREF(Py_None);\r | |
741 | PyTuple_SET_ITEM(tuple, 1, Py_None);\r | |
742 | }\r | |
743 | \r | |
744 | rtnval = PyString_Format(fmt, tuple);\r | |
745 | \r | |
746 | Py_DECREF(fmt);\r | |
747 | Py_DECREF(tuple);\r | |
748 | }\r | |
749 | else\r | |
750 | rtnval = BaseException_str((PyBaseExceptionObject *)self);\r | |
751 | \r | |
752 | return rtnval;\r | |
753 | }\r | |
754 | \r | |
755 | static PyMemberDef EnvironmentError_members[] = {\r | |
756 | {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,\r | |
757 | PyDoc_STR("exception errno")},\r | |
758 | {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,\r | |
759 | PyDoc_STR("exception strerror")},\r | |
760 | {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,\r | |
761 | PyDoc_STR("exception filename")},\r | |
762 | {NULL} /* Sentinel */\r | |
763 | };\r | |
764 | \r | |
765 | \r | |
766 | static PyObject *\r | |
767 | EnvironmentError_reduce(PyEnvironmentErrorObject *self)\r | |
768 | {\r | |
769 | PyObject *args = self->args;\r | |
770 | PyObject *res = NULL, *tmp;\r | |
771 | \r | |
772 | /* self->args is only the first two real arguments if there was a\r | |
773 | * file name given to EnvironmentError. */\r | |
774 | if (PyTuple_GET_SIZE(args) == 2 && self->filename) {\r | |
775 | args = PyTuple_New(3);\r | |
776 | if (!args)\r | |
777 | return NULL;\r | |
778 | \r | |
779 | tmp = PyTuple_GET_ITEM(self->args, 0);\r | |
780 | Py_INCREF(tmp);\r | |
781 | PyTuple_SET_ITEM(args, 0, tmp);\r | |
782 | \r | |
783 | tmp = PyTuple_GET_ITEM(self->args, 1);\r | |
784 | Py_INCREF(tmp);\r | |
785 | PyTuple_SET_ITEM(args, 1, tmp);\r | |
786 | \r | |
787 | Py_INCREF(self->filename);\r | |
788 | PyTuple_SET_ITEM(args, 2, self->filename);\r | |
789 | } else\r | |
790 | Py_INCREF(args);\r | |
791 | \r | |
792 | if (self->dict)\r | |
793 | res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);\r | |
794 | else\r | |
795 | res = PyTuple_Pack(2, Py_TYPE(self), args);\r | |
796 | Py_DECREF(args);\r | |
797 | return res;\r | |
798 | }\r | |
799 | \r | |
800 | \r | |
801 | static PyMethodDef EnvironmentError_methods[] = {\r | |
802 | {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS},\r | |
803 | {NULL}\r | |
804 | };\r | |
805 | \r | |
806 | ComplexExtendsException(PyExc_StandardError, EnvironmentError,\r | |
807 | EnvironmentError, EnvironmentError_dealloc,\r | |
808 | EnvironmentError_methods, EnvironmentError_members,\r | |
809 | EnvironmentError_str,\r | |
810 | "Base class for I/O related errors.");\r | |
811 | \r | |
812 | \r | |
813 | /*\r | |
814 | * IOError extends EnvironmentError\r | |
815 | */\r | |
816 | MiddlingExtendsException(PyExc_EnvironmentError, IOError,\r | |
817 | EnvironmentError, "I/O operation failed.");\r | |
818 | \r | |
819 | \r | |
820 | /*\r | |
821 | * OSError extends EnvironmentError\r | |
822 | */\r | |
823 | MiddlingExtendsException(PyExc_EnvironmentError, OSError,\r | |
824 | EnvironmentError, "OS system call failed.");\r | |
825 | \r | |
826 | \r | |
827 | /*\r | |
828 | * WindowsError extends OSError\r | |
829 | */\r | |
830 | #ifdef MS_WINDOWS\r | |
831 | #include "errmap.h"\r | |
832 | \r | |
833 | static int\r | |
834 | WindowsError_clear(PyWindowsErrorObject *self)\r | |
835 | {\r | |
836 | Py_CLEAR(self->myerrno);\r | |
837 | Py_CLEAR(self->strerror);\r | |
838 | Py_CLEAR(self->filename);\r | |
839 | Py_CLEAR(self->winerror);\r | |
840 | return BaseException_clear((PyBaseExceptionObject *)self);\r | |
841 | }\r | |
842 | \r | |
843 | static void\r | |
844 | WindowsError_dealloc(PyWindowsErrorObject *self)\r | |
845 | {\r | |
846 | _PyObject_GC_UNTRACK(self);\r | |
847 | WindowsError_clear(self);\r | |
848 | Py_TYPE(self)->tp_free((PyObject *)self);\r | |
849 | }\r | |
850 | \r | |
851 | static int\r | |
852 | WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)\r | |
853 | {\r | |
854 | Py_VISIT(self->myerrno);\r | |
855 | Py_VISIT(self->strerror);\r | |
856 | Py_VISIT(self->filename);\r | |
857 | Py_VISIT(self->winerror);\r | |
858 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);\r | |
859 | }\r | |
860 | \r | |
861 | static int\r | |
862 | WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)\r | |
863 | {\r | |
864 | PyObject *o_errcode = NULL;\r | |
865 | long errcode;\r | |
866 | long posix_errno;\r | |
867 | \r | |
868 | if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)\r | |
869 | == -1)\r | |
870 | return -1;\r | |
871 | \r | |
872 | if (self->myerrno == NULL)\r | |
873 | return 0;\r | |
874 | \r | |
875 | /* Set errno to the POSIX errno, and winerror to the Win32\r | |
876 | error code. */\r | |
877 | errcode = PyInt_AsLong(self->myerrno);\r | |
878 | if (errcode == -1 && PyErr_Occurred())\r | |
879 | return -1;\r | |
880 | posix_errno = winerror_to_errno(errcode);\r | |
881 | \r | |
882 | Py_CLEAR(self->winerror);\r | |
883 | self->winerror = self->myerrno;\r | |
884 | \r | |
885 | o_errcode = PyInt_FromLong(posix_errno);\r | |
886 | if (!o_errcode)\r | |
887 | return -1;\r | |
888 | \r | |
889 | self->myerrno = o_errcode;\r | |
890 | \r | |
891 | return 0;\r | |
892 | }\r | |
893 | \r | |
894 | \r | |
895 | static PyObject *\r | |
896 | WindowsError_str(PyWindowsErrorObject *self)\r | |
897 | {\r | |
898 | PyObject *rtnval = NULL;\r | |
899 | \r | |
900 | if (self->filename) {\r | |
901 | PyObject *fmt;\r | |
902 | PyObject *repr;\r | |
903 | PyObject *tuple;\r | |
904 | \r | |
905 | fmt = PyString_FromString("[Error %s] %s: %s");\r | |
906 | if (!fmt)\r | |
907 | return NULL;\r | |
908 | \r | |
909 | repr = PyObject_Repr(self->filename);\r | |
910 | if (!repr) {\r | |
911 | Py_DECREF(fmt);\r | |
912 | return NULL;\r | |
913 | }\r | |
914 | tuple = PyTuple_New(3);\r | |
915 | if (!tuple) {\r | |
916 | Py_DECREF(repr);\r | |
917 | Py_DECREF(fmt);\r | |
918 | return NULL;\r | |
919 | }\r | |
920 | \r | |
921 | if (self->winerror) {\r | |
922 | Py_INCREF(self->winerror);\r | |
923 | PyTuple_SET_ITEM(tuple, 0, self->winerror);\r | |
924 | }\r | |
925 | else {\r | |
926 | Py_INCREF(Py_None);\r | |
927 | PyTuple_SET_ITEM(tuple, 0, Py_None);\r | |
928 | }\r | |
929 | if (self->strerror) {\r | |
930 | Py_INCREF(self->strerror);\r | |
931 | PyTuple_SET_ITEM(tuple, 1, self->strerror);\r | |
932 | }\r | |
933 | else {\r | |
934 | Py_INCREF(Py_None);\r | |
935 | PyTuple_SET_ITEM(tuple, 1, Py_None);\r | |
936 | }\r | |
937 | \r | |
938 | PyTuple_SET_ITEM(tuple, 2, repr);\r | |
939 | \r | |
940 | rtnval = PyString_Format(fmt, tuple);\r | |
941 | \r | |
942 | Py_DECREF(fmt);\r | |
943 | Py_DECREF(tuple);\r | |
944 | }\r | |
945 | else if (self->winerror && self->strerror) {\r | |
946 | PyObject *fmt;\r | |
947 | PyObject *tuple;\r | |
948 | \r | |
949 | fmt = PyString_FromString("[Error %s] %s");\r | |
950 | if (!fmt)\r | |
951 | return NULL;\r | |
952 | \r | |
953 | tuple = PyTuple_New(2);\r | |
954 | if (!tuple) {\r | |
955 | Py_DECREF(fmt);\r | |
956 | return NULL;\r | |
957 | }\r | |
958 | \r | |
959 | if (self->winerror) {\r | |
960 | Py_INCREF(self->winerror);\r | |
961 | PyTuple_SET_ITEM(tuple, 0, self->winerror);\r | |
962 | }\r | |
963 | else {\r | |
964 | Py_INCREF(Py_None);\r | |
965 | PyTuple_SET_ITEM(tuple, 0, Py_None);\r | |
966 | }\r | |
967 | if (self->strerror) {\r | |
968 | Py_INCREF(self->strerror);\r | |
969 | PyTuple_SET_ITEM(tuple, 1, self->strerror);\r | |
970 | }\r | |
971 | else {\r | |
972 | Py_INCREF(Py_None);\r | |
973 | PyTuple_SET_ITEM(tuple, 1, Py_None);\r | |
974 | }\r | |
975 | \r | |
976 | rtnval = PyString_Format(fmt, tuple);\r | |
977 | \r | |
978 | Py_DECREF(fmt);\r | |
979 | Py_DECREF(tuple);\r | |
980 | }\r | |
981 | else\r | |
982 | rtnval = EnvironmentError_str((PyEnvironmentErrorObject *)self);\r | |
983 | \r | |
984 | return rtnval;\r | |
985 | }\r | |
986 | \r | |
987 | static PyMemberDef WindowsError_members[] = {\r | |
988 | {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,\r | |
989 | PyDoc_STR("POSIX exception code")},\r | |
990 | {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,\r | |
991 | PyDoc_STR("exception strerror")},\r | |
992 | {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,\r | |
993 | PyDoc_STR("exception filename")},\r | |
994 | {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,\r | |
995 | PyDoc_STR("Win32 exception code")},\r | |
996 | {NULL} /* Sentinel */\r | |
997 | };\r | |
998 | \r | |
999 | ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,\r | |
1000 | WindowsError_dealloc, 0, WindowsError_members,\r | |
1001 | WindowsError_str, "MS-Windows OS system call failed.");\r | |
1002 | \r | |
1003 | #endif /* MS_WINDOWS */\r | |
1004 | \r | |
1005 | \r | |
1006 | /*\r | |
1007 | * VMSError extends OSError (I think)\r | |
1008 | */\r | |
1009 | #ifdef __VMS\r | |
1010 | MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,\r | |
1011 | "OpenVMS OS system call failed.");\r | |
1012 | #endif\r | |
1013 | \r | |
1014 | \r | |
1015 | /*\r | |
1016 | * EOFError extends StandardError\r | |
1017 | */\r | |
1018 | SimpleExtendsException(PyExc_StandardError, EOFError,\r | |
1019 | "Read beyond end of file.");\r | |
1020 | \r | |
1021 | \r | |
1022 | /*\r | |
1023 | * RuntimeError extends StandardError\r | |
1024 | */\r | |
1025 | SimpleExtendsException(PyExc_StandardError, RuntimeError,\r | |
1026 | "Unspecified run-time error.");\r | |
1027 | \r | |
1028 | \r | |
1029 | /*\r | |
1030 | * NotImplementedError extends RuntimeError\r | |
1031 | */\r | |
1032 | SimpleExtendsException(PyExc_RuntimeError, NotImplementedError,\r | |
1033 | "Method or function hasn't been implemented yet.");\r | |
1034 | \r | |
1035 | /*\r | |
1036 | * NameError extends StandardError\r | |
1037 | */\r | |
1038 | SimpleExtendsException(PyExc_StandardError, NameError,\r | |
1039 | "Name not found globally.");\r | |
1040 | \r | |
1041 | /*\r | |
1042 | * UnboundLocalError extends NameError\r | |
1043 | */\r | |
1044 | SimpleExtendsException(PyExc_NameError, UnboundLocalError,\r | |
1045 | "Local name referenced but not bound to a value.");\r | |
1046 | \r | |
1047 | /*\r | |
1048 | * AttributeError extends StandardError\r | |
1049 | */\r | |
1050 | SimpleExtendsException(PyExc_StandardError, AttributeError,\r | |
1051 | "Attribute not found.");\r | |
1052 | \r | |
1053 | \r | |
1054 | /*\r | |
1055 | * SyntaxError extends StandardError\r | |
1056 | */\r | |
1057 | \r | |
1058 | static int\r | |
1059 | SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)\r | |
1060 | {\r | |
1061 | PyObject *info = NULL;\r | |
1062 | Py_ssize_t lenargs = PyTuple_GET_SIZE(args);\r | |
1063 | \r | |
1064 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)\r | |
1065 | return -1;\r | |
1066 | \r | |
1067 | if (lenargs >= 1) {\r | |
1068 | Py_CLEAR(self->msg);\r | |
1069 | self->msg = PyTuple_GET_ITEM(args, 0);\r | |
1070 | Py_INCREF(self->msg);\r | |
1071 | }\r | |
1072 | if (lenargs == 2) {\r | |
1073 | info = PyTuple_GET_ITEM(args, 1);\r | |
1074 | info = PySequence_Tuple(info);\r | |
1075 | if (!info)\r | |
1076 | return -1;\r | |
1077 | \r | |
1078 | if (PyTuple_GET_SIZE(info) != 4) {\r | |
1079 | /* not a very good error message, but it's what Python 2.4 gives */\r | |
1080 | PyErr_SetString(PyExc_IndexError, "tuple index out of range");\r | |
1081 | Py_DECREF(info);\r | |
1082 | return -1;\r | |
1083 | }\r | |
1084 | \r | |
1085 | Py_CLEAR(self->filename);\r | |
1086 | self->filename = PyTuple_GET_ITEM(info, 0);\r | |
1087 | Py_INCREF(self->filename);\r | |
1088 | \r | |
1089 | Py_CLEAR(self->lineno);\r | |
1090 | self->lineno = PyTuple_GET_ITEM(info, 1);\r | |
1091 | Py_INCREF(self->lineno);\r | |
1092 | \r | |
1093 | Py_CLEAR(self->offset);\r | |
1094 | self->offset = PyTuple_GET_ITEM(info, 2);\r | |
1095 | Py_INCREF(self->offset);\r | |
1096 | \r | |
1097 | Py_CLEAR(self->text);\r | |
1098 | self->text = PyTuple_GET_ITEM(info, 3);\r | |
1099 | Py_INCREF(self->text);\r | |
1100 | \r | |
1101 | Py_DECREF(info);\r | |
1102 | }\r | |
1103 | return 0;\r | |
1104 | }\r | |
1105 | \r | |
1106 | static int\r | |
1107 | SyntaxError_clear(PySyntaxErrorObject *self)\r | |
1108 | {\r | |
1109 | Py_CLEAR(self->msg);\r | |
1110 | Py_CLEAR(self->filename);\r | |
1111 | Py_CLEAR(self->lineno);\r | |
1112 | Py_CLEAR(self->offset);\r | |
1113 | Py_CLEAR(self->text);\r | |
1114 | Py_CLEAR(self->print_file_and_line);\r | |
1115 | return BaseException_clear((PyBaseExceptionObject *)self);\r | |
1116 | }\r | |
1117 | \r | |
1118 | static void\r | |
1119 | SyntaxError_dealloc(PySyntaxErrorObject *self)\r | |
1120 | {\r | |
1121 | _PyObject_GC_UNTRACK(self);\r | |
1122 | SyntaxError_clear(self);\r | |
1123 | Py_TYPE(self)->tp_free((PyObject *)self);\r | |
1124 | }\r | |
1125 | \r | |
1126 | static int\r | |
1127 | SyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)\r | |
1128 | {\r | |
1129 | Py_VISIT(self->msg);\r | |
1130 | Py_VISIT(self->filename);\r | |
1131 | Py_VISIT(self->lineno);\r | |
1132 | Py_VISIT(self->offset);\r | |
1133 | Py_VISIT(self->text);\r | |
1134 | Py_VISIT(self->print_file_and_line);\r | |
1135 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);\r | |
1136 | }\r | |
1137 | \r | |
1138 | /* This is called "my_basename" instead of just "basename" to avoid name\r | |
1139 | conflicts with glibc; basename is already prototyped if _GNU_SOURCE is\r | |
1140 | defined, and Python does define that. */\r | |
1141 | static char *\r | |
1142 | my_basename(char *name)\r | |
1143 | {\r | |
1144 | char *cp = name;\r | |
1145 | char *result = name;\r | |
1146 | \r | |
1147 | if (name == NULL)\r | |
1148 | return "???";\r | |
1149 | while (*cp != '\0') {\r | |
1150 | if (*cp == SEP)\r | |
1151 | result = cp + 1;\r | |
1152 | ++cp;\r | |
1153 | }\r | |
1154 | return result;\r | |
1155 | }\r | |
1156 | \r | |
1157 | \r | |
1158 | static PyObject *\r | |
1159 | SyntaxError_str(PySyntaxErrorObject *self)\r | |
1160 | {\r | |
1161 | PyObject *str;\r | |
1162 | PyObject *result;\r | |
1163 | int have_filename = 0;\r | |
1164 | int have_lineno = 0;\r | |
1165 | char *buffer = NULL;\r | |
1166 | Py_ssize_t bufsize;\r | |
1167 | \r | |
1168 | if (self->msg)\r | |
1169 | str = PyObject_Str(self->msg);\r | |
1170 | else\r | |
1171 | str = PyObject_Str(Py_None);\r | |
1172 | if (!str)\r | |
1173 | return NULL;\r | |
1174 | /* Don't fiddle with non-string return (shouldn't happen anyway) */\r | |
1175 | if (!PyString_Check(str))\r | |
1176 | return str;\r | |
1177 | \r | |
1178 | /* XXX -- do all the additional formatting with filename and\r | |
1179 | lineno here */\r | |
1180 | \r | |
1181 | have_filename = (self->filename != NULL) &&\r | |
1182 | PyString_Check(self->filename);\r | |
1183 | have_lineno = (self->lineno != NULL) && PyInt_Check(self->lineno);\r | |
1184 | \r | |
1185 | if (!have_filename && !have_lineno)\r | |
1186 | return str;\r | |
1187 | \r | |
1188 | bufsize = PyString_GET_SIZE(str) + 64;\r | |
1189 | if (have_filename)\r | |
1190 | bufsize += PyString_GET_SIZE(self->filename);\r | |
1191 | \r | |
1192 | buffer = PyMem_MALLOC(bufsize);\r | |
1193 | if (buffer == NULL)\r | |
1194 | return str;\r | |
1195 | \r | |
1196 | if (have_filename && have_lineno)\r | |
1197 | PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",\r | |
1198 | PyString_AS_STRING(str),\r | |
1199 | my_basename(PyString_AS_STRING(self->filename)),\r | |
1200 | PyInt_AsLong(self->lineno));\r | |
1201 | else if (have_filename)\r | |
1202 | PyOS_snprintf(buffer, bufsize, "%s (%s)",\r | |
1203 | PyString_AS_STRING(str),\r | |
1204 | my_basename(PyString_AS_STRING(self->filename)));\r | |
1205 | else /* only have_lineno */\r | |
1206 | PyOS_snprintf(buffer, bufsize, "%s (line %ld)",\r | |
1207 | PyString_AS_STRING(str),\r | |
1208 | PyInt_AsLong(self->lineno));\r | |
1209 | \r | |
1210 | result = PyString_FromString(buffer);\r | |
1211 | PyMem_FREE(buffer);\r | |
1212 | \r | |
1213 | if (result == NULL)\r | |
1214 | result = str;\r | |
1215 | else\r | |
1216 | Py_DECREF(str);\r | |
1217 | return result;\r | |
1218 | }\r | |
1219 | \r | |
1220 | static PyMemberDef SyntaxError_members[] = {\r | |
1221 | {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,\r | |
1222 | PyDoc_STR("exception msg")},\r | |
1223 | {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,\r | |
1224 | PyDoc_STR("exception filename")},\r | |
1225 | {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,\r | |
1226 | PyDoc_STR("exception lineno")},\r | |
1227 | {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,\r | |
1228 | PyDoc_STR("exception offset")},\r | |
1229 | {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,\r | |
1230 | PyDoc_STR("exception text")},\r | |
1231 | {"print_file_and_line", T_OBJECT,\r | |
1232 | offsetof(PySyntaxErrorObject, print_file_and_line), 0,\r | |
1233 | PyDoc_STR("exception print_file_and_line")},\r | |
1234 | {NULL} /* Sentinel */\r | |
1235 | };\r | |
1236 | \r | |
1237 | ComplexExtendsException(PyExc_StandardError, SyntaxError, SyntaxError,\r | |
1238 | SyntaxError_dealloc, 0, SyntaxError_members,\r | |
1239 | SyntaxError_str, "Invalid syntax.");\r | |
1240 | \r | |
1241 | \r | |
1242 | /*\r | |
1243 | * IndentationError extends SyntaxError\r | |
1244 | */\r | |
1245 | MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,\r | |
1246 | "Improper indentation.");\r | |
1247 | \r | |
1248 | \r | |
1249 | /*\r | |
1250 | * TabError extends IndentationError\r | |
1251 | */\r | |
1252 | MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,\r | |
1253 | "Improper mixture of spaces and tabs.");\r | |
1254 | \r | |
1255 | \r | |
1256 | /*\r | |
1257 | * LookupError extends StandardError\r | |
1258 | */\r | |
1259 | SimpleExtendsException(PyExc_StandardError, LookupError,\r | |
1260 | "Base class for lookup errors.");\r | |
1261 | \r | |
1262 | \r | |
1263 | /*\r | |
1264 | * IndexError extends LookupError\r | |
1265 | */\r | |
1266 | SimpleExtendsException(PyExc_LookupError, IndexError,\r | |
1267 | "Sequence index out of range.");\r | |
1268 | \r | |
1269 | \r | |
1270 | /*\r | |
1271 | * KeyError extends LookupError\r | |
1272 | */\r | |
1273 | static PyObject *\r | |
1274 | KeyError_str(PyBaseExceptionObject *self)\r | |
1275 | {\r | |
1276 | /* If args is a tuple of exactly one item, apply repr to args[0].\r | |
1277 | This is done so that e.g. the exception raised by {}[''] prints\r | |
1278 | KeyError: ''\r | |
1279 | rather than the confusing\r | |
1280 | KeyError\r | |
1281 | alone. The downside is that if KeyError is raised with an explanatory\r | |
1282 | string, that string will be displayed in quotes. Too bad.\r | |
1283 | If args is anything else, use the default BaseException__str__().\r | |
1284 | */\r | |
1285 | if (PyTuple_GET_SIZE(self->args) == 1) {\r | |
1286 | return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));\r | |
1287 | }\r | |
1288 | return BaseException_str(self);\r | |
1289 | }\r | |
1290 | \r | |
1291 | ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,\r | |
1292 | 0, 0, 0, KeyError_str, "Mapping key not found.");\r | |
1293 | \r | |
1294 | \r | |
1295 | /*\r | |
1296 | * ValueError extends StandardError\r | |
1297 | */\r | |
1298 | SimpleExtendsException(PyExc_StandardError, ValueError,\r | |
1299 | "Inappropriate argument value (of correct type).");\r | |
1300 | \r | |
1301 | /*\r | |
1302 | * UnicodeError extends ValueError\r | |
1303 | */\r | |
1304 | \r | |
1305 | SimpleExtendsException(PyExc_ValueError, UnicodeError,\r | |
1306 | "Unicode related error.");\r | |
1307 | \r | |
1308 | #ifdef Py_USING_UNICODE\r | |
1309 | static PyObject *\r | |
1310 | get_string(PyObject *attr, const char *name)\r | |
1311 | {\r | |
1312 | if (!attr) {\r | |
1313 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);\r | |
1314 | return NULL;\r | |
1315 | }\r | |
1316 | \r | |
1317 | if (!PyString_Check(attr)) {\r | |
1318 | PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name);\r | |
1319 | return NULL;\r | |
1320 | }\r | |
1321 | Py_INCREF(attr);\r | |
1322 | return attr;\r | |
1323 | }\r | |
1324 | \r | |
1325 | \r | |
1326 | static int\r | |
1327 | set_string(PyObject **attr, const char *value)\r | |
1328 | {\r | |
1329 | PyObject *obj = PyString_FromString(value);\r | |
1330 | if (!obj)\r | |
1331 | return -1;\r | |
1332 | Py_CLEAR(*attr);\r | |
1333 | *attr = obj;\r | |
1334 | return 0;\r | |
1335 | }\r | |
1336 | \r | |
1337 | \r | |
1338 | static PyObject *\r | |
1339 | get_unicode(PyObject *attr, const char *name)\r | |
1340 | {\r | |
1341 | if (!attr) {\r | |
1342 | PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);\r | |
1343 | return NULL;\r | |
1344 | }\r | |
1345 | \r | |
1346 | if (!PyUnicode_Check(attr)) {\r | |
1347 | PyErr_Format(PyExc_TypeError,\r | |
1348 | "%.200s attribute must be unicode", name);\r | |
1349 | return NULL;\r | |
1350 | }\r | |
1351 | Py_INCREF(attr);\r | |
1352 | return attr;\r | |
1353 | }\r | |
1354 | \r | |
1355 | PyObject *\r | |
1356 | PyUnicodeEncodeError_GetEncoding(PyObject *exc)\r | |
1357 | {\r | |
1358 | return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");\r | |
1359 | }\r | |
1360 | \r | |
1361 | PyObject *\r | |
1362 | PyUnicodeDecodeError_GetEncoding(PyObject *exc)\r | |
1363 | {\r | |
1364 | return get_string(((PyUnicodeErrorObject *)exc)->encoding, "encoding");\r | |
1365 | }\r | |
1366 | \r | |
1367 | PyObject *\r | |
1368 | PyUnicodeEncodeError_GetObject(PyObject *exc)\r | |
1369 | {\r | |
1370 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");\r | |
1371 | }\r | |
1372 | \r | |
1373 | PyObject *\r | |
1374 | PyUnicodeDecodeError_GetObject(PyObject *exc)\r | |
1375 | {\r | |
1376 | return get_string(((PyUnicodeErrorObject *)exc)->object, "object");\r | |
1377 | }\r | |
1378 | \r | |
1379 | PyObject *\r | |
1380 | PyUnicodeTranslateError_GetObject(PyObject *exc)\r | |
1381 | {\r | |
1382 | return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");\r | |
1383 | }\r | |
1384 | \r | |
1385 | int\r | |
1386 | PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)\r | |
1387 | {\r | |
1388 | Py_ssize_t size;\r | |
1389 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,\r | |
1390 | "object");\r | |
1391 | if (!obj)\r | |
1392 | return -1;\r | |
1393 | *start = ((PyUnicodeErrorObject *)exc)->start;\r | |
1394 | size = PyUnicode_GET_SIZE(obj);\r | |
1395 | if (*start<0)\r | |
1396 | *start = 0; /*XXX check for values <0*/\r | |
1397 | if (*start>=size)\r | |
1398 | *start = size-1;\r | |
1399 | Py_DECREF(obj);\r | |
1400 | return 0;\r | |
1401 | }\r | |
1402 | \r | |
1403 | \r | |
1404 | int\r | |
1405 | PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)\r | |
1406 | {\r | |
1407 | Py_ssize_t size;\r | |
1408 | PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,\r | |
1409 | "object");\r | |
1410 | if (!obj)\r | |
1411 | return -1;\r | |
1412 | size = PyString_GET_SIZE(obj);\r | |
1413 | *start = ((PyUnicodeErrorObject *)exc)->start;\r | |
1414 | if (*start<0)\r | |
1415 | *start = 0;\r | |
1416 | if (*start>=size)\r | |
1417 | *start = size-1;\r | |
1418 | Py_DECREF(obj);\r | |
1419 | return 0;\r | |
1420 | }\r | |
1421 | \r | |
1422 | \r | |
1423 | int\r | |
1424 | PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)\r | |
1425 | {\r | |
1426 | return PyUnicodeEncodeError_GetStart(exc, start);\r | |
1427 | }\r | |
1428 | \r | |
1429 | \r | |
1430 | int\r | |
1431 | PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)\r | |
1432 | {\r | |
1433 | ((PyUnicodeErrorObject *)exc)->start = start;\r | |
1434 | return 0;\r | |
1435 | }\r | |
1436 | \r | |
1437 | \r | |
1438 | int\r | |
1439 | PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)\r | |
1440 | {\r | |
1441 | ((PyUnicodeErrorObject *)exc)->start = start;\r | |
1442 | return 0;\r | |
1443 | }\r | |
1444 | \r | |
1445 | \r | |
1446 | int\r | |
1447 | PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)\r | |
1448 | {\r | |
1449 | ((PyUnicodeErrorObject *)exc)->start = start;\r | |
1450 | return 0;\r | |
1451 | }\r | |
1452 | \r | |
1453 | \r | |
1454 | int\r | |
1455 | PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)\r | |
1456 | {\r | |
1457 | Py_ssize_t size;\r | |
1458 | PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,\r | |
1459 | "object");\r | |
1460 | if (!obj)\r | |
1461 | return -1;\r | |
1462 | *end = ((PyUnicodeErrorObject *)exc)->end;\r | |
1463 | size = PyUnicode_GET_SIZE(obj);\r | |
1464 | if (*end<1)\r | |
1465 | *end = 1;\r | |
1466 | if (*end>size)\r | |
1467 | *end = size;\r | |
1468 | Py_DECREF(obj);\r | |
1469 | return 0;\r | |
1470 | }\r | |
1471 | \r | |
1472 | \r | |
1473 | int\r | |
1474 | PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)\r | |
1475 | {\r | |
1476 | Py_ssize_t size;\r | |
1477 | PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object,\r | |
1478 | "object");\r | |
1479 | if (!obj)\r | |
1480 | return -1;\r | |
1481 | *end = ((PyUnicodeErrorObject *)exc)->end;\r | |
1482 | size = PyString_GET_SIZE(obj);\r | |
1483 | if (*end<1)\r | |
1484 | *end = 1;\r | |
1485 | if (*end>size)\r | |
1486 | *end = size;\r | |
1487 | Py_DECREF(obj);\r | |
1488 | return 0;\r | |
1489 | }\r | |
1490 | \r | |
1491 | \r | |
1492 | int\r | |
1493 | PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)\r | |
1494 | {\r | |
1495 | return PyUnicodeEncodeError_GetEnd(exc, start);\r | |
1496 | }\r | |
1497 | \r | |
1498 | \r | |
1499 | int\r | |
1500 | PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)\r | |
1501 | {\r | |
1502 | ((PyUnicodeErrorObject *)exc)->end = end;\r | |
1503 | return 0;\r | |
1504 | }\r | |
1505 | \r | |
1506 | \r | |
1507 | int\r | |
1508 | PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)\r | |
1509 | {\r | |
1510 | ((PyUnicodeErrorObject *)exc)->end = end;\r | |
1511 | return 0;\r | |
1512 | }\r | |
1513 | \r | |
1514 | \r | |
1515 | int\r | |
1516 | PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)\r | |
1517 | {\r | |
1518 | ((PyUnicodeErrorObject *)exc)->end = end;\r | |
1519 | return 0;\r | |
1520 | }\r | |
1521 | \r | |
1522 | PyObject *\r | |
1523 | PyUnicodeEncodeError_GetReason(PyObject *exc)\r | |
1524 | {\r | |
1525 | return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");\r | |
1526 | }\r | |
1527 | \r | |
1528 | \r | |
1529 | PyObject *\r | |
1530 | PyUnicodeDecodeError_GetReason(PyObject *exc)\r | |
1531 | {\r | |
1532 | return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");\r | |
1533 | }\r | |
1534 | \r | |
1535 | \r | |
1536 | PyObject *\r | |
1537 | PyUnicodeTranslateError_GetReason(PyObject *exc)\r | |
1538 | {\r | |
1539 | return get_string(((PyUnicodeErrorObject *)exc)->reason, "reason");\r | |
1540 | }\r | |
1541 | \r | |
1542 | \r | |
1543 | int\r | |
1544 | PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)\r | |
1545 | {\r | |
1546 | return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);\r | |
1547 | }\r | |
1548 | \r | |
1549 | \r | |
1550 | int\r | |
1551 | PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)\r | |
1552 | {\r | |
1553 | return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);\r | |
1554 | }\r | |
1555 | \r | |
1556 | \r | |
1557 | int\r | |
1558 | PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)\r | |
1559 | {\r | |
1560 | return set_string(&((PyUnicodeErrorObject *)exc)->reason, reason);\r | |
1561 | }\r | |
1562 | \r | |
1563 | \r | |
1564 | static int\r | |
1565 | UnicodeError_init(PyUnicodeErrorObject *self, PyObject *args, PyObject *kwds,\r | |
1566 | PyTypeObject *objecttype)\r | |
1567 | {\r | |
1568 | Py_CLEAR(self->encoding);\r | |
1569 | Py_CLEAR(self->object);\r | |
1570 | Py_CLEAR(self->reason);\r | |
1571 | \r | |
1572 | if (!PyArg_ParseTuple(args, "O!O!nnO!",\r | |
1573 | &PyString_Type, &self->encoding,\r | |
1574 | objecttype, &self->object,\r | |
1575 | &self->start,\r | |
1576 | &self->end,\r | |
1577 | &PyString_Type, &self->reason)) {\r | |
1578 | self->encoding = self->object = self->reason = NULL;\r | |
1579 | return -1;\r | |
1580 | }\r | |
1581 | \r | |
1582 | Py_INCREF(self->encoding);\r | |
1583 | Py_INCREF(self->object);\r | |
1584 | Py_INCREF(self->reason);\r | |
1585 | \r | |
1586 | return 0;\r | |
1587 | }\r | |
1588 | \r | |
1589 | static int\r | |
1590 | UnicodeError_clear(PyUnicodeErrorObject *self)\r | |
1591 | {\r | |
1592 | Py_CLEAR(self->encoding);\r | |
1593 | Py_CLEAR(self->object);\r | |
1594 | Py_CLEAR(self->reason);\r | |
1595 | return BaseException_clear((PyBaseExceptionObject *)self);\r | |
1596 | }\r | |
1597 | \r | |
1598 | static void\r | |
1599 | UnicodeError_dealloc(PyUnicodeErrorObject *self)\r | |
1600 | {\r | |
1601 | _PyObject_GC_UNTRACK(self);\r | |
1602 | UnicodeError_clear(self);\r | |
1603 | Py_TYPE(self)->tp_free((PyObject *)self);\r | |
1604 | }\r | |
1605 | \r | |
1606 | static int\r | |
1607 | UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)\r | |
1608 | {\r | |
1609 | Py_VISIT(self->encoding);\r | |
1610 | Py_VISIT(self->object);\r | |
1611 | Py_VISIT(self->reason);\r | |
1612 | return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);\r | |
1613 | }\r | |
1614 | \r | |
1615 | static PyMemberDef UnicodeError_members[] = {\r | |
1616 | {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,\r | |
1617 | PyDoc_STR("exception encoding")},\r | |
1618 | {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,\r | |
1619 | PyDoc_STR("exception object")},\r | |
1620 | {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,\r | |
1621 | PyDoc_STR("exception start")},\r | |
1622 | {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,\r | |
1623 | PyDoc_STR("exception end")},\r | |
1624 | {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,\r | |
1625 | PyDoc_STR("exception reason")},\r | |
1626 | {NULL} /* Sentinel */\r | |
1627 | };\r | |
1628 | \r | |
1629 | \r | |
1630 | /*\r | |
1631 | * UnicodeEncodeError extends UnicodeError\r | |
1632 | */\r | |
1633 | \r | |
1634 | static int\r | |
1635 | UnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)\r | |
1636 | {\r | |
1637 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)\r | |
1638 | return -1;\r | |
1639 | return UnicodeError_init((PyUnicodeErrorObject *)self, args,\r | |
1640 | kwds, &PyUnicode_Type);\r | |
1641 | }\r | |
1642 | \r | |
1643 | static PyObject *\r | |
1644 | UnicodeEncodeError_str(PyObject *self)\r | |
1645 | {\r | |
1646 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;\r | |
1647 | PyObject *result = NULL;\r | |
1648 | PyObject *reason_str = NULL;\r | |
1649 | PyObject *encoding_str = NULL;\r | |
1650 | \r | |
1651 | if (!uself->object)\r | |
1652 | /* Not properly initialized. */\r | |
1653 | return PyUnicode_FromString("");\r | |
1654 | \r | |
1655 | /* Get reason and encoding as strings, which they might not be if\r | |
1656 | they've been modified after we were contructed. */\r | |
1657 | reason_str = PyObject_Str(uself->reason);\r | |
1658 | if (reason_str == NULL)\r | |
1659 | goto done;\r | |
1660 | encoding_str = PyObject_Str(uself->encoding);\r | |
1661 | if (encoding_str == NULL)\r | |
1662 | goto done;\r | |
1663 | \r | |
1664 | if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {\r | |
1665 | int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];\r | |
1666 | char badchar_str[20];\r | |
1667 | if (badchar <= 0xff)\r | |
1668 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);\r | |
1669 | else if (badchar <= 0xffff)\r | |
1670 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);\r | |
1671 | else\r | |
1672 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);\r | |
1673 | result = PyString_FromFormat(\r | |
1674 | "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",\r | |
1675 | PyString_AS_STRING(encoding_str),\r | |
1676 | badchar_str,\r | |
1677 | uself->start,\r | |
1678 | PyString_AS_STRING(reason_str));\r | |
1679 | }\r | |
1680 | else {\r | |
1681 | result = PyString_FromFormat(\r | |
1682 | "'%.400s' codec can't encode characters in position %zd-%zd: %.400s",\r | |
1683 | PyString_AS_STRING(encoding_str),\r | |
1684 | uself->start,\r | |
1685 | uself->end-1,\r | |
1686 | PyString_AS_STRING(reason_str));\r | |
1687 | }\r | |
1688 | done:\r | |
1689 | Py_XDECREF(reason_str);\r | |
1690 | Py_XDECREF(encoding_str);\r | |
1691 | return result;\r | |
1692 | }\r | |
1693 | \r | |
1694 | static PyTypeObject _PyExc_UnicodeEncodeError = {\r | |
1695 | PyObject_HEAD_INIT(NULL)\r | |
1696 | 0,\r | |
1697 | EXC_MODULE_NAME "UnicodeEncodeError",\r | |
1698 | sizeof(PyUnicodeErrorObject), 0,\r | |
1699 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r | |
1700 | (reprfunc)UnicodeEncodeError_str, 0, 0, 0,\r | |
1701 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,\r | |
1702 | PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,\r | |
1703 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,\r | |
1704 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),\r | |
1705 | (initproc)UnicodeEncodeError_init, 0, BaseException_new,\r | |
1706 | };\r | |
1707 | PyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;\r | |
1708 | \r | |
1709 | PyObject *\r | |
1710 | PyUnicodeEncodeError_Create(\r | |
1711 | const char *encoding, const Py_UNICODE *object, Py_ssize_t length,\r | |
1712 | Py_ssize_t start, Py_ssize_t end, const char *reason)\r | |
1713 | {\r | |
1714 | return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",\r | |
1715 | encoding, object, length, start, end, reason);\r | |
1716 | }\r | |
1717 | \r | |
1718 | \r | |
1719 | /*\r | |
1720 | * UnicodeDecodeError extends UnicodeError\r | |
1721 | */\r | |
1722 | \r | |
1723 | static int\r | |
1724 | UnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)\r | |
1725 | {\r | |
1726 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)\r | |
1727 | return -1;\r | |
1728 | return UnicodeError_init((PyUnicodeErrorObject *)self, args,\r | |
1729 | kwds, &PyString_Type);\r | |
1730 | }\r | |
1731 | \r | |
1732 | static PyObject *\r | |
1733 | UnicodeDecodeError_str(PyObject *self)\r | |
1734 | {\r | |
1735 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;\r | |
1736 | PyObject *result = NULL;\r | |
1737 | PyObject *reason_str = NULL;\r | |
1738 | PyObject *encoding_str = NULL;\r | |
1739 | \r | |
1740 | if (!uself->object)\r | |
1741 | /* Not properly initialized. */\r | |
1742 | return PyUnicode_FromString("");\r | |
1743 | \r | |
1744 | /* Get reason and encoding as strings, which they might not be if\r | |
1745 | they've been modified after we were contructed. */\r | |
1746 | reason_str = PyObject_Str(uself->reason);\r | |
1747 | if (reason_str == NULL)\r | |
1748 | goto done;\r | |
1749 | encoding_str = PyObject_Str(uself->encoding);\r | |
1750 | if (encoding_str == NULL)\r | |
1751 | goto done;\r | |
1752 | \r | |
1753 | if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {\r | |
1754 | /* FromFormat does not support %02x, so format that separately */\r | |
1755 | char byte[4];\r | |
1756 | PyOS_snprintf(byte, sizeof(byte), "%02x",\r | |
1757 | ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);\r | |
1758 | result = PyString_FromFormat(\r | |
1759 | "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",\r | |
1760 | PyString_AS_STRING(encoding_str),\r | |
1761 | byte,\r | |
1762 | uself->start,\r | |
1763 | PyString_AS_STRING(reason_str));\r | |
1764 | }\r | |
1765 | else {\r | |
1766 | result = PyString_FromFormat(\r | |
1767 | "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",\r | |
1768 | PyString_AS_STRING(encoding_str),\r | |
1769 | uself->start,\r | |
1770 | uself->end-1,\r | |
1771 | PyString_AS_STRING(reason_str));\r | |
1772 | }\r | |
1773 | done:\r | |
1774 | Py_XDECREF(reason_str);\r | |
1775 | Py_XDECREF(encoding_str);\r | |
1776 | return result;\r | |
1777 | }\r | |
1778 | \r | |
1779 | static PyTypeObject _PyExc_UnicodeDecodeError = {\r | |
1780 | PyObject_HEAD_INIT(NULL)\r | |
1781 | 0,\r | |
1782 | EXC_MODULE_NAME "UnicodeDecodeError",\r | |
1783 | sizeof(PyUnicodeErrorObject), 0,\r | |
1784 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r | |
1785 | (reprfunc)UnicodeDecodeError_str, 0, 0, 0,\r | |
1786 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,\r | |
1787 | PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,\r | |
1788 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,\r | |
1789 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),\r | |
1790 | (initproc)UnicodeDecodeError_init, 0, BaseException_new,\r | |
1791 | };\r | |
1792 | PyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;\r | |
1793 | \r | |
1794 | PyObject *\r | |
1795 | PyUnicodeDecodeError_Create(\r | |
1796 | const char *encoding, const char *object, Py_ssize_t length,\r | |
1797 | Py_ssize_t start, Py_ssize_t end, const char *reason)\r | |
1798 | {\r | |
1799 | return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns",\r | |
1800 | encoding, object, length, start, end, reason);\r | |
1801 | }\r | |
1802 | \r | |
1803 | \r | |
1804 | /*\r | |
1805 | * UnicodeTranslateError extends UnicodeError\r | |
1806 | */\r | |
1807 | \r | |
1808 | static int\r | |
1809 | UnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,\r | |
1810 | PyObject *kwds)\r | |
1811 | {\r | |
1812 | if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)\r | |
1813 | return -1;\r | |
1814 | \r | |
1815 | Py_CLEAR(self->object);\r | |
1816 | Py_CLEAR(self->reason);\r | |
1817 | \r | |
1818 | if (!PyArg_ParseTuple(args, "O!nnO!",\r | |
1819 | &PyUnicode_Type, &self->object,\r | |
1820 | &self->start,\r | |
1821 | &self->end,\r | |
1822 | &PyString_Type, &self->reason)) {\r | |
1823 | self->object = self->reason = NULL;\r | |
1824 | return -1;\r | |
1825 | }\r | |
1826 | \r | |
1827 | Py_INCREF(self->object);\r | |
1828 | Py_INCREF(self->reason);\r | |
1829 | \r | |
1830 | return 0;\r | |
1831 | }\r | |
1832 | \r | |
1833 | \r | |
1834 | static PyObject *\r | |
1835 | UnicodeTranslateError_str(PyObject *self)\r | |
1836 | {\r | |
1837 | PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;\r | |
1838 | PyObject *result = NULL;\r | |
1839 | PyObject *reason_str = NULL;\r | |
1840 | \r | |
1841 | if (!uself->object)\r | |
1842 | /* Not properly initialized. */\r | |
1843 | return PyUnicode_FromString("");\r | |
1844 | \r | |
1845 | /* Get reason as a string, which it might not be if it's been\r | |
1846 | modified after we were contructed. */\r | |
1847 | reason_str = PyObject_Str(uself->reason);\r | |
1848 | if (reason_str == NULL)\r | |
1849 | goto done;\r | |
1850 | \r | |
1851 | if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {\r | |
1852 | int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];\r | |
1853 | char badchar_str[20];\r | |
1854 | if (badchar <= 0xff)\r | |
1855 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);\r | |
1856 | else if (badchar <= 0xffff)\r | |
1857 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);\r | |
1858 | else\r | |
1859 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);\r | |
1860 | result = PyString_FromFormat(\r | |
1861 | "can't translate character u'\\%s' in position %zd: %.400s",\r | |
1862 | badchar_str,\r | |
1863 | uself->start,\r | |
1864 | PyString_AS_STRING(reason_str));\r | |
1865 | } else {\r | |
1866 | result = PyString_FromFormat(\r | |
1867 | "can't translate characters in position %zd-%zd: %.400s",\r | |
1868 | uself->start,\r | |
1869 | uself->end-1,\r | |
1870 | PyString_AS_STRING(reason_str));\r | |
1871 | }\r | |
1872 | done:\r | |
1873 | Py_XDECREF(reason_str);\r | |
1874 | return result;\r | |
1875 | }\r | |
1876 | \r | |
1877 | static PyTypeObject _PyExc_UnicodeTranslateError = {\r | |
1878 | PyObject_HEAD_INIT(NULL)\r | |
1879 | 0,\r | |
1880 | EXC_MODULE_NAME "UnicodeTranslateError",\r | |
1881 | sizeof(PyUnicodeErrorObject), 0,\r | |
1882 | (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\r | |
1883 | (reprfunc)UnicodeTranslateError_str, 0, 0, 0,\r | |
1884 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,\r | |
1885 | PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,\r | |
1886 | (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,\r | |
1887 | 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),\r | |
1888 | (initproc)UnicodeTranslateError_init, 0, BaseException_new,\r | |
1889 | };\r | |
1890 | PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;\r | |
1891 | \r | |
1892 | PyObject *\r | |
1893 | PyUnicodeTranslateError_Create(\r | |
1894 | const Py_UNICODE *object, Py_ssize_t length,\r | |
1895 | Py_ssize_t start, Py_ssize_t end, const char *reason)\r | |
1896 | {\r | |
1897 | return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",\r | |
1898 | object, length, start, end, reason);\r | |
1899 | }\r | |
1900 | #endif\r | |
1901 | \r | |
1902 | \r | |
1903 | /*\r | |
1904 | * AssertionError extends StandardError\r | |
1905 | */\r | |
1906 | SimpleExtendsException(PyExc_StandardError, AssertionError,\r | |
1907 | "Assertion failed.");\r | |
1908 | \r | |
1909 | \r | |
1910 | /*\r | |
1911 | * ArithmeticError extends StandardError\r | |
1912 | */\r | |
1913 | SimpleExtendsException(PyExc_StandardError, ArithmeticError,\r | |
1914 | "Base class for arithmetic errors.");\r | |
1915 | \r | |
1916 | \r | |
1917 | /*\r | |
1918 | * FloatingPointError extends ArithmeticError\r | |
1919 | */\r | |
1920 | SimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,\r | |
1921 | "Floating point operation failed.");\r | |
1922 | \r | |
1923 | \r | |
1924 | /*\r | |
1925 | * OverflowError extends ArithmeticError\r | |
1926 | */\r | |
1927 | SimpleExtendsException(PyExc_ArithmeticError, OverflowError,\r | |
1928 | "Result too large to be represented.");\r | |
1929 | \r | |
1930 | \r | |
1931 | /*\r | |
1932 | * ZeroDivisionError extends ArithmeticError\r | |
1933 | */\r | |
1934 | SimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,\r | |
1935 | "Second argument to a division or modulo operation was zero.");\r | |
1936 | \r | |
1937 | \r | |
1938 | /*\r | |
1939 | * SystemError extends StandardError\r | |
1940 | */\r | |
1941 | SimpleExtendsException(PyExc_StandardError, SystemError,\r | |
1942 | "Internal error in the Python interpreter.\n"\r | |
1943 | "\n"\r | |
1944 | "Please report this to the Python maintainer, along with the traceback,\n"\r | |
1945 | "the Python version, and the hardware/OS platform and version.");\r | |
1946 | \r | |
1947 | \r | |
1948 | /*\r | |
1949 | * ReferenceError extends StandardError\r | |
1950 | */\r | |
1951 | SimpleExtendsException(PyExc_StandardError, ReferenceError,\r | |
1952 | "Weak ref proxy used after referent went away.");\r | |
1953 | \r | |
1954 | \r | |
1955 | /*\r | |
1956 | * MemoryError extends StandardError\r | |
1957 | */\r | |
1958 | SimpleExtendsException(PyExc_StandardError, MemoryError, "Out of memory.");\r | |
1959 | \r | |
1960 | /*\r | |
1961 | * BufferError extends StandardError\r | |
1962 | */\r | |
1963 | SimpleExtendsException(PyExc_StandardError, BufferError, "Buffer error.");\r | |
1964 | \r | |
1965 | \r | |
1966 | /* Warning category docstrings */\r | |
1967 | \r | |
1968 | /*\r | |
1969 | * Warning extends Exception\r | |
1970 | */\r | |
1971 | SimpleExtendsException(PyExc_Exception, Warning,\r | |
1972 | "Base class for warning categories.");\r | |
1973 | \r | |
1974 | \r | |
1975 | /*\r | |
1976 | * UserWarning extends Warning\r | |
1977 | */\r | |
1978 | SimpleExtendsException(PyExc_Warning, UserWarning,\r | |
1979 | "Base class for warnings generated by user code.");\r | |
1980 | \r | |
1981 | \r | |
1982 | /*\r | |
1983 | * DeprecationWarning extends Warning\r | |
1984 | */\r | |
1985 | SimpleExtendsException(PyExc_Warning, DeprecationWarning,\r | |
1986 | "Base class for warnings about deprecated features.");\r | |
1987 | \r | |
1988 | \r | |
1989 | /*\r | |
1990 | * PendingDeprecationWarning extends Warning\r | |
1991 | */\r | |
1992 | SimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,\r | |
1993 | "Base class for warnings about features which will be deprecated\n"\r | |
1994 | "in the future.");\r | |
1995 | \r | |
1996 | \r | |
1997 | /*\r | |
1998 | * SyntaxWarning extends Warning\r | |
1999 | */\r | |
2000 | SimpleExtendsException(PyExc_Warning, SyntaxWarning,\r | |
2001 | "Base class for warnings about dubious syntax.");\r | |
2002 | \r | |
2003 | \r | |
2004 | /*\r | |
2005 | * RuntimeWarning extends Warning\r | |
2006 | */\r | |
2007 | SimpleExtendsException(PyExc_Warning, RuntimeWarning,\r | |
2008 | "Base class for warnings about dubious runtime behavior.");\r | |
2009 | \r | |
2010 | \r | |
2011 | /*\r | |
2012 | * FutureWarning extends Warning\r | |
2013 | */\r | |
2014 | SimpleExtendsException(PyExc_Warning, FutureWarning,\r | |
2015 | "Base class for warnings about constructs that will change semantically\n"\r | |
2016 | "in the future.");\r | |
2017 | \r | |
2018 | \r | |
2019 | /*\r | |
2020 | * ImportWarning extends Warning\r | |
2021 | */\r | |
2022 | SimpleExtendsException(PyExc_Warning, ImportWarning,\r | |
2023 | "Base class for warnings about probable mistakes in module imports");\r | |
2024 | \r | |
2025 | \r | |
2026 | /*\r | |
2027 | * UnicodeWarning extends Warning\r | |
2028 | */\r | |
2029 | SimpleExtendsException(PyExc_Warning, UnicodeWarning,\r | |
2030 | "Base class for warnings about Unicode related problems, mostly\n"\r | |
2031 | "related to conversion problems.");\r | |
2032 | \r | |
2033 | /*\r | |
2034 | * BytesWarning extends Warning\r | |
2035 | */\r | |
2036 | SimpleExtendsException(PyExc_Warning, BytesWarning,\r | |
2037 | "Base class for warnings about bytes and buffer related problems, mostly\n"\r | |
2038 | "related to conversion from str or comparing to str.");\r | |
2039 | \r | |
2040 | /* Pre-computed MemoryError instance. Best to create this as early as\r | |
2041 | * possible and not wait until a MemoryError is actually raised!\r | |
2042 | */\r | |
2043 | PyObject *PyExc_MemoryErrorInst=NULL;\r | |
2044 | \r | |
2045 | /* Pre-computed RuntimeError instance for when recursion depth is reached.\r | |
2046 | Meant to be used when normalizing the exception for exceeding the recursion\r | |
2047 | depth will cause its own infinite recursion.\r | |
2048 | */\r | |
2049 | PyObject *PyExc_RecursionErrorInst = NULL;\r | |
2050 | \r | |
2051 | /* module global functions */\r | |
2052 | static PyMethodDef functions[] = {\r | |
2053 | /* Sentinel */\r | |
2054 | {NULL, NULL}\r | |
2055 | };\r | |
2056 | \r | |
2057 | #define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \\r | |
2058 | Py_FatalError("exceptions bootstrapping error.");\r | |
2059 | \r | |
2060 | #define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \\r | |
2061 | PyModule_AddObject(m, # TYPE, PyExc_ ## TYPE); \\r | |
2062 | if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \\r | |
2063 | Py_FatalError("Module dictionary insertion problem.");\r | |
2064 | \r | |
2065 | \r | |
2066 | PyMODINIT_FUNC\r | |
2067 | _PyExc_Init(void)\r | |
2068 | {\r | |
2069 | PyObject *m, *bltinmod, *bdict;\r | |
2070 | \r | |
2071 | PRE_INIT(BaseException)\r | |
2072 | PRE_INIT(Exception)\r | |
2073 | PRE_INIT(StandardError)\r | |
2074 | PRE_INIT(TypeError)\r | |
2075 | PRE_INIT(StopIteration)\r | |
2076 | PRE_INIT(GeneratorExit)\r | |
2077 | PRE_INIT(SystemExit)\r | |
2078 | PRE_INIT(KeyboardInterrupt)\r | |
2079 | PRE_INIT(ImportError)\r | |
2080 | PRE_INIT(EnvironmentError)\r | |
2081 | PRE_INIT(IOError)\r | |
2082 | PRE_INIT(OSError)\r | |
2083 | #ifdef MS_WINDOWS\r | |
2084 | PRE_INIT(WindowsError)\r | |
2085 | #endif\r | |
2086 | #ifdef __VMS\r | |
2087 | PRE_INIT(VMSError)\r | |
2088 | #endif\r | |
2089 | PRE_INIT(EOFError)\r | |
2090 | PRE_INIT(RuntimeError)\r | |
2091 | PRE_INIT(NotImplementedError)\r | |
2092 | PRE_INIT(NameError)\r | |
2093 | PRE_INIT(UnboundLocalError)\r | |
2094 | PRE_INIT(AttributeError)\r | |
2095 | PRE_INIT(SyntaxError)\r | |
2096 | PRE_INIT(IndentationError)\r | |
2097 | PRE_INIT(TabError)\r | |
2098 | PRE_INIT(LookupError)\r | |
2099 | PRE_INIT(IndexError)\r | |
2100 | PRE_INIT(KeyError)\r | |
2101 | PRE_INIT(ValueError)\r | |
2102 | PRE_INIT(UnicodeError)\r | |
2103 | #ifdef Py_USING_UNICODE\r | |
2104 | PRE_INIT(UnicodeEncodeError)\r | |
2105 | PRE_INIT(UnicodeDecodeError)\r | |
2106 | PRE_INIT(UnicodeTranslateError)\r | |
2107 | #endif\r | |
2108 | PRE_INIT(AssertionError)\r | |
2109 | PRE_INIT(ArithmeticError)\r | |
2110 | PRE_INIT(FloatingPointError)\r | |
2111 | PRE_INIT(OverflowError)\r | |
2112 | PRE_INIT(ZeroDivisionError)\r | |
2113 | PRE_INIT(SystemError)\r | |
2114 | PRE_INIT(ReferenceError)\r | |
2115 | PRE_INIT(MemoryError)\r | |
2116 | PRE_INIT(BufferError)\r | |
2117 | PRE_INIT(Warning)\r | |
2118 | PRE_INIT(UserWarning)\r | |
2119 | PRE_INIT(DeprecationWarning)\r | |
2120 | PRE_INIT(PendingDeprecationWarning)\r | |
2121 | PRE_INIT(SyntaxWarning)\r | |
2122 | PRE_INIT(RuntimeWarning)\r | |
2123 | PRE_INIT(FutureWarning)\r | |
2124 | PRE_INIT(ImportWarning)\r | |
2125 | PRE_INIT(UnicodeWarning)\r | |
2126 | PRE_INIT(BytesWarning)\r | |
2127 | \r | |
2128 | m = Py_InitModule4("exceptions", functions, exceptions_doc,\r | |
2129 | (PyObject *)NULL, PYTHON_API_VERSION);\r | |
2130 | if (m == NULL)\r | |
2131 | return;\r | |
2132 | \r | |
2133 | bltinmod = PyImport_ImportModule("__builtin__");\r | |
2134 | if (bltinmod == NULL)\r | |
2135 | Py_FatalError("exceptions bootstrapping error.");\r | |
2136 | bdict = PyModule_GetDict(bltinmod);\r | |
2137 | if (bdict == NULL)\r | |
2138 | Py_FatalError("exceptions bootstrapping error.");\r | |
2139 | \r | |
2140 | POST_INIT(BaseException)\r | |
2141 | POST_INIT(Exception)\r | |
2142 | POST_INIT(StandardError)\r | |
2143 | POST_INIT(TypeError)\r | |
2144 | POST_INIT(StopIteration)\r | |
2145 | POST_INIT(GeneratorExit)\r | |
2146 | POST_INIT(SystemExit)\r | |
2147 | POST_INIT(KeyboardInterrupt)\r | |
2148 | POST_INIT(ImportError)\r | |
2149 | POST_INIT(EnvironmentError)\r | |
2150 | POST_INIT(IOError)\r | |
2151 | POST_INIT(OSError)\r | |
2152 | #ifdef MS_WINDOWS\r | |
2153 | POST_INIT(WindowsError)\r | |
2154 | #endif\r | |
2155 | #ifdef __VMS\r | |
2156 | POST_INIT(VMSError)\r | |
2157 | #endif\r | |
2158 | POST_INIT(EOFError)\r | |
2159 | POST_INIT(RuntimeError)\r | |
2160 | POST_INIT(NotImplementedError)\r | |
2161 | POST_INIT(NameError)\r | |
2162 | POST_INIT(UnboundLocalError)\r | |
2163 | POST_INIT(AttributeError)\r | |
2164 | POST_INIT(SyntaxError)\r | |
2165 | POST_INIT(IndentationError)\r | |
2166 | POST_INIT(TabError)\r | |
2167 | POST_INIT(LookupError)\r | |
2168 | POST_INIT(IndexError)\r | |
2169 | POST_INIT(KeyError)\r | |
2170 | POST_INIT(ValueError)\r | |
2171 | POST_INIT(UnicodeError)\r | |
2172 | #ifdef Py_USING_UNICODE\r | |
2173 | POST_INIT(UnicodeEncodeError)\r | |
2174 | POST_INIT(UnicodeDecodeError)\r | |
2175 | POST_INIT(UnicodeTranslateError)\r | |
2176 | #endif\r | |
2177 | POST_INIT(AssertionError)\r | |
2178 | POST_INIT(ArithmeticError)\r | |
2179 | POST_INIT(FloatingPointError)\r | |
2180 | POST_INIT(OverflowError)\r | |
2181 | POST_INIT(ZeroDivisionError)\r | |
2182 | POST_INIT(SystemError)\r | |
2183 | POST_INIT(ReferenceError)\r | |
2184 | POST_INIT(MemoryError)\r | |
2185 | POST_INIT(BufferError)\r | |
2186 | POST_INIT(Warning)\r | |
2187 | POST_INIT(UserWarning)\r | |
2188 | POST_INIT(DeprecationWarning)\r | |
2189 | POST_INIT(PendingDeprecationWarning)\r | |
2190 | POST_INIT(SyntaxWarning)\r | |
2191 | POST_INIT(RuntimeWarning)\r | |
2192 | POST_INIT(FutureWarning)\r | |
2193 | POST_INIT(ImportWarning)\r | |
2194 | POST_INIT(UnicodeWarning)\r | |
2195 | POST_INIT(BytesWarning)\r | |
2196 | \r | |
2197 | PyExc_MemoryErrorInst = BaseException_new(&_PyExc_MemoryError, NULL, NULL);\r | |
2198 | if (!PyExc_MemoryErrorInst)\r | |
2199 | Py_FatalError("Cannot pre-allocate MemoryError instance");\r | |
2200 | \r | |
2201 | PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);\r | |
2202 | if (!PyExc_RecursionErrorInst)\r | |
2203 | Py_FatalError("Cannot pre-allocate RuntimeError instance for "\r | |
2204 | "recursion errors");\r | |
2205 | else {\r | |
2206 | PyBaseExceptionObject *err_inst =\r | |
2207 | (PyBaseExceptionObject *)PyExc_RecursionErrorInst;\r | |
2208 | PyObject *args_tuple;\r | |
2209 | PyObject *exc_message;\r | |
2210 | exc_message = PyString_FromString("maximum recursion depth exceeded");\r | |
2211 | if (!exc_message)\r | |
2212 | Py_FatalError("cannot allocate argument for RuntimeError "\r | |
2213 | "pre-allocation");\r | |
2214 | args_tuple = PyTuple_Pack(1, exc_message);\r | |
2215 | if (!args_tuple)\r | |
2216 | Py_FatalError("cannot allocate tuple for RuntimeError "\r | |
2217 | "pre-allocation");\r | |
2218 | Py_DECREF(exc_message);\r | |
2219 | if (BaseException_init(err_inst, args_tuple, NULL))\r | |
2220 | Py_FatalError("init of pre-allocated RuntimeError failed");\r | |
2221 | Py_DECREF(args_tuple);\r | |
2222 | }\r | |
2223 | Py_DECREF(bltinmod);\r | |
2224 | }\r | |
2225 | \r | |
2226 | void\r | |
2227 | _PyExc_Fini(void)\r | |
2228 | {\r | |
2229 | Py_CLEAR(PyExc_MemoryErrorInst);\r | |
2230 | Py_CLEAR(PyExc_RecursionErrorInst);\r | |
2231 | }\r |