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