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