PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Objects/exceptions.c

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