Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1 | /* This module provides the suite of standard class-based exceptions for |
| 2 | * Python's builtin module. This is a complete C implementation of what, |
| 3 | * in Python 1.5.2, was contained in the exceptions.py module. The problem |
| 4 | * there was that if exceptions.py could not be imported for some reason, |
| 5 | * the entire interpreter would abort. |
| 6 | * |
| 7 | * By moving the exceptions into C and statically linking, we can guarantee |
| 8 | * that the standard exceptions will always be available. |
| 9 | * |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 10 | * |
| 11 | * written by Fredrik Lundh |
| 12 | * modifications, additions, cleanups, and proofreading by Barry Warsaw |
| 13 | * |
| 14 | * Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved. |
| 15 | */ |
| 16 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 17 | #define PY_SSIZE_T_CLEAN |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 18 | #include "Python.h" |
Fred Drake | 185a29b | 2000-08-15 16:20:36 +0000 | [diff] [blame] | 19 | #include "osdefs.h" |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 20 | |
Tim Peters | bf26e07 | 2000-07-12 04:02:10 +0000 | [diff] [blame] | 21 | /* Caution: MS Visual C++ 6 errors if a single string literal exceeds |
| 22 | * 2Kb. So the module docstring has been broken roughly in half, using |
| 23 | * compile-time literal concatenation. |
| 24 | */ |
Skip Montanaro | 995895f | 2002-03-28 20:57:51 +0000 | [diff] [blame] | 25 | |
| 26 | /* NOTE: If the exception class hierarchy changes, don't forget to update |
| 27 | * Doc/lib/libexcs.tex! |
| 28 | */ |
| 29 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 30 | PyDoc_STRVAR(module__doc__, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 31 | "Python's standard exception class hierarchy.\n\ |
| 32 | \n\ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 33 | Exceptions found here are defined both in the exceptions module and the \n\ |
| 34 | built-in namespace. It is recommended that user-defined exceptions inherit \n\ |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame] | 35 | from Exception. See the documentation for the exception inheritance hierarchy.\n\ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 36 | " |
| 37 | |
Tim Peters | bf26e07 | 2000-07-12 04:02:10 +0000 | [diff] [blame] | 38 | /* keep string pieces "small" */ |
Brett Cannon | 54ac294 | 2006-03-01 22:10:49 +0000 | [diff] [blame] | 39 | /* XXX(bcannon): exception hierarchy in Lib/test/exception_hierarchy.txt */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 40 | ); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 41 | |
| 42 | |
| 43 | /* Helper function for populating a dictionary with method wrappers. */ |
| 44 | static int |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 45 | populate_methods(PyObject *klass, PyMethodDef *methods) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 46 | { |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 47 | PyObject *module; |
| 48 | int status = -1; |
| 49 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 50 | if (!methods) |
| 51 | return 0; |
| 52 | |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 53 | module = PyString_FromString("exceptions"); |
| 54 | if (!module) |
| 55 | return 0; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 56 | while (methods->ml_name) { |
| 57 | /* get a wrapper for the built-in function */ |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 58 | PyObject *func = PyCFunction_NewEx(methods, NULL, module); |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 59 | PyObject *meth; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 60 | |
| 61 | if (!func) |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 62 | goto status; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 63 | |
| 64 | /* turn the function into an unbound method */ |
| 65 | if (!(meth = PyMethod_New(func, NULL, klass))) { |
| 66 | Py_DECREF(func); |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 67 | goto status; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 68 | } |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 69 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 70 | /* add method to dictionary */ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 71 | status = PyObject_SetAttrString(klass, methods->ml_name, meth); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 72 | Py_DECREF(meth); |
| 73 | Py_DECREF(func); |
| 74 | |
| 75 | /* stop now if an error occurred, otherwise do the next method */ |
| 76 | if (status) |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 77 | goto status; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 78 | |
| 79 | methods++; |
| 80 | } |
Jeremy Hylton | 4f0dcc9 | 2003-01-31 18:33:18 +0000 | [diff] [blame] | 81 | status = 0; |
| 82 | status: |
| 83 | Py_DECREF(module); |
| 84 | return status; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 87 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 88 | |
| 89 | /* This function is used to create all subsequent exception classes. */ |
| 90 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 91 | make_class(PyObject **klass, PyObject *base, |
| 92 | char *name, PyMethodDef *methods, |
| 93 | char *docstr) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 94 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 95 | PyObject *dict = PyDict_New(); |
| 96 | PyObject *str = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 97 | int status = -1; |
| 98 | |
| 99 | if (!dict) |
| 100 | return -1; |
| 101 | |
| 102 | /* If an error occurs from here on, goto finally instead of explicitly |
| 103 | * returning NULL. |
| 104 | */ |
| 105 | |
| 106 | if (docstr) { |
| 107 | if (!(str = PyString_FromString(docstr))) |
| 108 | goto finally; |
| 109 | if (PyDict_SetItemString(dict, "__doc__", str)) |
| 110 | goto finally; |
| 111 | } |
| 112 | |
| 113 | if (!(*klass = PyErr_NewException(name, base, dict))) |
| 114 | goto finally; |
| 115 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 116 | if (populate_methods(*klass, methods)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 117 | Py_DECREF(*klass); |
| 118 | *klass = NULL; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 119 | goto finally; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | status = 0; |
| 123 | |
| 124 | finally: |
| 125 | Py_XDECREF(dict); |
| 126 | Py_XDECREF(str); |
| 127 | return status; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* Use this for *args signatures, otherwise just use PyArg_ParseTuple() */ |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 132 | static PyObject * |
| 133 | get_self(PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 134 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 135 | PyObject *self = PyTuple_GetItem(args, 0); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 136 | if (!self) { |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 137 | /* Watch out for being called to early in the bootstrapping process */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 138 | if (PyExc_TypeError) { |
| 139 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 140 | "unbound method must be called with instance as first argument"); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 141 | } |
| 142 | return NULL; |
| 143 | } |
| 144 | return self; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | /* Notes on bootstrapping the exception classes. |
| 150 | * |
| 151 | * First thing we create is the base class for all exceptions, called |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 152 | * appropriately BaseException. Creation of this class makes no |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 153 | * assumptions about the existence of any other exception class -- except |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 154 | * for TypeError, which can conditionally exist. |
| 155 | * |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 156 | * Next, Exception is created since it is the common subclass for the rest of |
| 157 | * the needed exceptions for this bootstrapping to work. StandardError is |
| 158 | * created (which is quite simple) followed by |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 159 | * TypeError, because the instantiation of other exceptions can potentially |
| 160 | * throw a TypeError. Once these exceptions are created, all the others |
| 161 | * can be created in any order. See the static exctable below for the |
| 162 | * explicit bootstrap order. |
| 163 | * |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 164 | * All classes after BaseException can be created using PyErr_NewException(). |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 165 | */ |
| 166 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 167 | PyDoc_STRVAR(BaseException__doc__, "Common base class for all exceptions"); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 168 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 169 | /* |
| 170 | Set args and message attributes. |
| 171 | |
| 172 | Assumes self and args have already been set properly with set_self, etc. |
| 173 | */ |
| 174 | static int |
| 175 | set_args_and_message(PyObject *self, PyObject *args) |
| 176 | { |
| 177 | PyObject *message_val; |
| 178 | Py_ssize_t args_len = PySequence_Length(args); |
| 179 | |
| 180 | if (args_len < 0) |
| 181 | return 0; |
| 182 | |
| 183 | /* set args */ |
| 184 | if (PyObject_SetAttrString(self, "args", args) < 0) |
| 185 | return 0; |
| 186 | |
| 187 | /* set message */ |
| 188 | if (args_len == 1) |
| 189 | message_val = PySequence_GetItem(args, 0); |
| 190 | else |
| 191 | message_val = PyString_FromString(""); |
| 192 | if (!message_val) |
| 193 | return 0; |
| 194 | |
| 195 | if (PyObject_SetAttrString(self, "message", message_val) < 0) { |
| 196 | Py_DECREF(message_val); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | Py_DECREF(message_val); |
| 201 | return 1; |
| 202 | } |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 203 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 204 | static PyObject * |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 205 | BaseException__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 206 | { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 207 | if (!(self = get_self(args))) |
| 208 | return NULL; |
| 209 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 210 | /* set args and message attribute */ |
| 211 | args = PySequence_GetSlice(args, 1, PySequence_Length(args)); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 212 | if (!args) |
| 213 | return NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 214 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 215 | if (!set_args_and_message(self, args)) { |
| 216 | Py_DECREF(args); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | Py_DECREF(args); |
| 221 | Py_RETURN_NONE; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 225 | static PyObject * |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 226 | BaseException__str__(PyObject *_self, PyObject *self) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 227 | { |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 228 | PyObject *out, *args; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 229 | |
| 230 | args = PyObject_GetAttrString(self, "args"); |
| 231 | if (!args) |
| 232 | return NULL; |
| 233 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 234 | switch (PySequence_Size(args)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 235 | case 0: |
| 236 | out = PyString_FromString(""); |
| 237 | break; |
| 238 | case 1: |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 239 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 240 | PyObject *tmp = PySequence_GetItem(args, 0); |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 241 | if (tmp) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 242 | out = PyObject_Str(tmp); |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 243 | Py_DECREF(tmp); |
| 244 | } |
| 245 | else |
| 246 | out = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 247 | break; |
Barry Warsaw | b781655 | 2000-07-09 22:27:10 +0000 | [diff] [blame] | 248 | } |
Guido van Rossum | 98b2a42 | 2002-09-18 04:06:32 +0000 | [diff] [blame] | 249 | case -1: |
| 250 | PyErr_Clear(); |
| 251 | /* Fall through */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 252 | default: |
| 253 | out = PyObject_Str(args); |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | Py_DECREF(args); |
| 258 | return out; |
| 259 | } |
| 260 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 261 | #ifdef Py_USING_UNICODE |
| 262 | static PyObject * |
| 263 | BaseException__unicode__(PyObject *self, PyObject *args) |
| 264 | { |
| 265 | Py_ssize_t args_len; |
| 266 | |
| 267 | if (!PyArg_ParseTuple(args, "O:__unicode__", &self)) |
| 268 | return NULL; |
| 269 | |
| 270 | args = PyObject_GetAttrString(self, "args"); |
| 271 | if (!args) |
| 272 | return NULL; |
| 273 | |
| 274 | args_len = PySequence_Size(args); |
| 275 | if (args_len < 0) { |
| 276 | Py_DECREF(args); |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | if (args_len == 0) { |
| 281 | Py_DECREF(args); |
| 282 | return PyUnicode_FromUnicode(NULL, 0); |
| 283 | } |
| 284 | else if (args_len == 1) { |
| 285 | PyObject *temp = PySequence_GetItem(args, 0); |
Brett Cannon | 46872b1 | 2006-03-02 04:31:55 +0000 | [diff] [blame] | 286 | PyObject *unicode_obj; |
| 287 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 288 | if (!temp) { |
| 289 | Py_DECREF(args); |
| 290 | return NULL; |
| 291 | } |
| 292 | Py_DECREF(args); |
Brett Cannon | 46872b1 | 2006-03-02 04:31:55 +0000 | [diff] [blame] | 293 | unicode_obj = PyObject_Unicode(temp); |
| 294 | Py_DECREF(temp); |
| 295 | return unicode_obj; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 296 | } |
| 297 | else { |
Brett Cannon | 46872b1 | 2006-03-02 04:31:55 +0000 | [diff] [blame] | 298 | PyObject *unicode_obj = PyObject_Unicode(args); |
| 299 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 300 | Py_DECREF(args); |
Brett Cannon | 46872b1 | 2006-03-02 04:31:55 +0000 | [diff] [blame] | 301 | return unicode_obj; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | #endif /* Py_USING_UNICODE */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 305 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 306 | static PyObject * |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 307 | BaseException__repr__(PyObject *self, PyObject *args) |
| 308 | { |
| 309 | PyObject *args_attr; |
| 310 | Py_ssize_t args_len; |
| 311 | PyObject *repr_suffix; |
| 312 | PyObject *repr; |
| 313 | |
| 314 | if (!PyArg_ParseTuple(args, "O:__repr__", &self)) |
| 315 | return NULL; |
| 316 | |
| 317 | args_attr = PyObject_GetAttrString(self, "args"); |
| 318 | if (!args_attr) |
| 319 | return NULL; |
| 320 | |
| 321 | args_len = PySequence_Length(args_attr); |
| 322 | if (args_len < 0) { |
| 323 | Py_DECREF(args_attr); |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | if (args_len == 0) { |
| 328 | Py_DECREF(args_attr); |
| 329 | repr_suffix = PyString_FromString("()"); |
| 330 | if (!repr_suffix) |
| 331 | return NULL; |
| 332 | } |
| 333 | else { |
Neal Norwitz | 9742f27 | 2006-03-03 19:13:57 +0000 | [diff] [blame] | 334 | PyObject *args_repr = PyObject_Repr(args_attr); |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 335 | Py_DECREF(args_attr); |
| 336 | if (!args_repr) |
| 337 | return NULL; |
| 338 | |
| 339 | repr_suffix = args_repr; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | repr = PyString_FromString(self->ob_type->tp_name); |
| 343 | if (!repr) { |
| 344 | Py_DECREF(repr_suffix); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | PyString_ConcatAndDel(&repr, repr_suffix); |
| 349 | return repr; |
| 350 | } |
| 351 | |
| 352 | static PyObject * |
| 353 | BaseException__getitem__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 354 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 355 | PyObject *out; |
| 356 | PyObject *index; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 357 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 358 | if (!PyArg_ParseTuple(args, "OO:__getitem__", &self, &index)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 359 | return NULL; |
| 360 | |
| 361 | args = PyObject_GetAttrString(self, "args"); |
| 362 | if (!args) |
| 363 | return NULL; |
| 364 | |
| 365 | out = PyObject_GetItem(args, index); |
| 366 | Py_DECREF(args); |
| 367 | return out; |
| 368 | } |
| 369 | |
| 370 | |
| 371 | static PyMethodDef |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 372 | BaseException_methods[] = { |
| 373 | /* methods for the BaseException class */ |
| 374 | {"__getitem__", BaseException__getitem__, METH_VARARGS}, |
| 375 | {"__repr__", BaseException__repr__, METH_VARARGS}, |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 376 | {"__str__", BaseException__str__, METH_O}, |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 377 | #ifdef Py_USING_UNICODE |
| 378 | {"__unicode__", BaseException__unicode__, METH_VARARGS}, |
| 379 | #endif /* Py_USING_UNICODE */ |
| 380 | {"__init__", BaseException__init__, METH_VARARGS}, |
| 381 | {NULL, NULL } |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | |
| 385 | static int |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 386 | make_BaseException(char *modulename) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 387 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 388 | PyObject *dict = PyDict_New(); |
| 389 | PyObject *str = NULL; |
| 390 | PyObject *name = NULL; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 391 | PyObject *emptytuple = NULL; |
| 392 | PyObject *argstuple = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 393 | int status = -1; |
| 394 | |
| 395 | if (!dict) |
| 396 | return -1; |
| 397 | |
| 398 | /* If an error occurs from here on, goto finally instead of explicitly |
| 399 | * returning NULL. |
| 400 | */ |
| 401 | |
| 402 | if (!(str = PyString_FromString(modulename))) |
| 403 | goto finally; |
| 404 | if (PyDict_SetItemString(dict, "__module__", str)) |
| 405 | goto finally; |
| 406 | Py_DECREF(str); |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 407 | |
| 408 | if (!(str = PyString_FromString(BaseException__doc__))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 409 | goto finally; |
| 410 | if (PyDict_SetItemString(dict, "__doc__", str)) |
| 411 | goto finally; |
| 412 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 413 | if (!(name = PyString_FromString("BaseException"))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 414 | goto finally; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 415 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 416 | if (!(emptytuple = PyTuple_New(0))) |
| 417 | goto finally; |
| 418 | |
| 419 | if (!(argstuple = PyTuple_Pack(3, name, emptytuple, dict))) |
| 420 | goto finally; |
| 421 | |
| 422 | if (!(PyExc_BaseException = PyType_Type.tp_new(&PyType_Type, argstuple, |
| 423 | NULL))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 424 | goto finally; |
| 425 | |
| 426 | /* Now populate the dictionary with the method suite */ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 427 | if (populate_methods(PyExc_BaseException, BaseException_methods)) |
| 428 | /* Don't need to reclaim PyExc_BaseException here because that'll |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 429 | * happen during interpreter shutdown. |
| 430 | */ |
| 431 | goto finally; |
| 432 | |
| 433 | status = 0; |
| 434 | |
| 435 | finally: |
| 436 | Py_XDECREF(dict); |
| 437 | Py_XDECREF(str); |
| 438 | Py_XDECREF(name); |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 439 | Py_XDECREF(emptytuple); |
| 440 | Py_XDECREF(argstuple); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 441 | return status; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 446 | PyDoc_STRVAR(Exception__doc__, "Common base class for all non-exit exceptions."); |
| 447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 448 | PyDoc_STRVAR(StandardError__doc__, |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 449 | "Base class for all standard Python exceptions that do not represent" |
| 450 | "interpreter exiting."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 451 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 452 | PyDoc_STRVAR(TypeError__doc__, "Inappropriate argument type."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 453 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 454 | PyDoc_STRVAR(StopIteration__doc__, "Signal the end from iterator.next()."); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 455 | PyDoc_STRVAR(GeneratorExit__doc__, "Request that a generator exit."); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 456 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 457 | |
| 458 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 459 | PyDoc_STRVAR(SystemExit__doc__, "Request to exit from the interpreter."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 460 | |
| 461 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 462 | static PyObject * |
| 463 | SystemExit__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 464 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 465 | PyObject *code; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 466 | int status; |
| 467 | |
| 468 | if (!(self = get_self(args))) |
| 469 | return NULL; |
| 470 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 471 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 472 | return NULL; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 473 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 474 | if (!set_args_and_message(self, args)) { |
| 475 | Py_DECREF(args); |
| 476 | return NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* set code attribute */ |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 480 | switch (PySequence_Size(args)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 481 | case 0: |
| 482 | Py_INCREF(Py_None); |
| 483 | code = Py_None; |
| 484 | break; |
| 485 | case 1: |
| 486 | code = PySequence_GetItem(args, 0); |
| 487 | break; |
Neal Norwitz | 2c96ab2 | 2002-09-18 22:37:17 +0000 | [diff] [blame] | 488 | case -1: |
| 489 | PyErr_Clear(); |
| 490 | /* Fall through */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 491 | default: |
| 492 | Py_INCREF(args); |
| 493 | code = args; |
| 494 | break; |
| 495 | } |
| 496 | |
| 497 | status = PyObject_SetAttrString(self, "code", code); |
| 498 | Py_DECREF(code); |
| 499 | Py_DECREF(args); |
| 500 | if (status < 0) |
| 501 | return NULL; |
| 502 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 503 | Py_RETURN_NONE; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 507 | static PyMethodDef SystemExit_methods[] = { |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 508 | { "__init__", SystemExit__init__, METH_VARARGS}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 509 | {NULL, NULL} |
| 510 | }; |
| 511 | |
| 512 | |
| 513 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 514 | PyDoc_STRVAR(KeyboardInterrupt__doc__, "Program interrupted by user."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 515 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 516 | PyDoc_STRVAR(ImportError__doc__, |
| 517 | "Import can't find module, or can't find name in module."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 518 | |
| 519 | |
| 520 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 521 | PyDoc_STRVAR(EnvironmentError__doc__, "Base class for I/O related errors."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 522 | |
| 523 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 524 | static PyObject * |
| 525 | EnvironmentError__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 526 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 527 | PyObject *item0 = NULL; |
| 528 | PyObject *item1 = NULL; |
| 529 | PyObject *item2 = NULL; |
| 530 | PyObject *subslice = NULL; |
| 531 | PyObject *rtnval = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 532 | |
| 533 | if (!(self = get_self(args))) |
| 534 | return NULL; |
| 535 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 536 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 537 | return NULL; |
| 538 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 539 | if (!set_args_and_message(self, args)) { |
| 540 | Py_DECREF(args); |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | if (PyObject_SetAttrString(self, "errno", Py_None) || |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 545 | PyObject_SetAttrString(self, "strerror", Py_None) || |
| 546 | PyObject_SetAttrString(self, "filename", Py_None)) |
| 547 | { |
| 548 | goto finally; |
| 549 | } |
| 550 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 551 | switch (PySequence_Size(args)) { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 552 | case 3: |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 553 | /* Where a function has a single filename, such as open() or some |
| 554 | * of the os module functions, PyErr_SetFromErrnoWithFilename() is |
| 555 | * called, giving a third argument which is the filename. But, so |
| 556 | * that old code using in-place unpacking doesn't break, e.g.: |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 557 | * |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 558 | * except IOError, (errno, strerror): |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 559 | * |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 560 | * we hack args so that it only contains two items. This also |
| 561 | * means we need our own __str__() which prints out the filename |
| 562 | * when it was supplied. |
| 563 | */ |
| 564 | item0 = PySequence_GetItem(args, 0); |
| 565 | item1 = PySequence_GetItem(args, 1); |
| 566 | item2 = PySequence_GetItem(args, 2); |
| 567 | if (!item0 || !item1 || !item2) |
| 568 | goto finally; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 569 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 570 | if (PyObject_SetAttrString(self, "errno", item0) || |
| 571 | PyObject_SetAttrString(self, "strerror", item1) || |
| 572 | PyObject_SetAttrString(self, "filename", item2)) |
| 573 | { |
| 574 | goto finally; |
| 575 | } |
| 576 | |
| 577 | subslice = PySequence_GetSlice(args, 0, 2); |
| 578 | if (!subslice || PyObject_SetAttrString(self, "args", subslice)) |
| 579 | goto finally; |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 580 | break; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 581 | |
| 582 | case 2: |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 583 | /* Used when PyErr_SetFromErrno() is called and no filename |
| 584 | * argument is given. |
| 585 | */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 586 | item0 = PySequence_GetItem(args, 0); |
| 587 | item1 = PySequence_GetItem(args, 1); |
| 588 | if (!item0 || !item1) |
| 589 | goto finally; |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 590 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 591 | if (PyObject_SetAttrString(self, "errno", item0) || |
| 592 | PyObject_SetAttrString(self, "strerror", item1)) |
| 593 | { |
| 594 | goto finally; |
| 595 | } |
Barry Warsaw | 7dfeb42 | 2000-07-09 04:56:25 +0000 | [diff] [blame] | 596 | break; |
Neal Norwitz | 2c96ab2 | 2002-09-18 22:37:17 +0000 | [diff] [blame] | 597 | |
| 598 | case -1: |
| 599 | PyErr_Clear(); |
| 600 | break; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | Py_INCREF(Py_None); |
| 604 | rtnval = Py_None; |
| 605 | |
| 606 | finally: |
| 607 | Py_DECREF(args); |
| 608 | Py_XDECREF(item0); |
| 609 | Py_XDECREF(item1); |
| 610 | Py_XDECREF(item2); |
| 611 | Py_XDECREF(subslice); |
| 612 | return rtnval; |
| 613 | } |
| 614 | |
| 615 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 616 | static PyObject * |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 617 | EnvironmentError__str__(PyObject *originalself, PyObject *self) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 618 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 619 | PyObject *filename; |
| 620 | PyObject *serrno; |
| 621 | PyObject *strerror; |
| 622 | PyObject *rtnval = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 623 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 624 | filename = PyObject_GetAttrString(self, "filename"); |
| 625 | serrno = PyObject_GetAttrString(self, "errno"); |
| 626 | strerror = PyObject_GetAttrString(self, "strerror"); |
| 627 | if (!filename || !serrno || !strerror) |
| 628 | goto finally; |
| 629 | |
| 630 | if (filename != Py_None) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 631 | PyObject *fmt = PyString_FromString("[Errno %s] %s: %s"); |
| 632 | PyObject *repr = PyObject_Repr(filename); |
| 633 | PyObject *tuple = PyTuple_New(3); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 634 | |
| 635 | if (!fmt || !repr || !tuple) { |
| 636 | Py_XDECREF(fmt); |
| 637 | Py_XDECREF(repr); |
| 638 | Py_XDECREF(tuple); |
| 639 | goto finally; |
| 640 | } |
| 641 | |
| 642 | PyTuple_SET_ITEM(tuple, 0, serrno); |
| 643 | PyTuple_SET_ITEM(tuple, 1, strerror); |
| 644 | PyTuple_SET_ITEM(tuple, 2, repr); |
| 645 | |
| 646 | rtnval = PyString_Format(fmt, tuple); |
| 647 | |
| 648 | Py_DECREF(fmt); |
| 649 | Py_DECREF(tuple); |
| 650 | /* already freed because tuple owned only reference */ |
| 651 | serrno = NULL; |
| 652 | strerror = NULL; |
| 653 | } |
| 654 | else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 655 | PyObject *fmt = PyString_FromString("[Errno %s] %s"); |
| 656 | PyObject *tuple = PyTuple_New(2); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 657 | |
| 658 | if (!fmt || !tuple) { |
| 659 | Py_XDECREF(fmt); |
| 660 | Py_XDECREF(tuple); |
| 661 | goto finally; |
| 662 | } |
| 663 | |
| 664 | PyTuple_SET_ITEM(tuple, 0, serrno); |
| 665 | PyTuple_SET_ITEM(tuple, 1, strerror); |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 666 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 667 | rtnval = PyString_Format(fmt, tuple); |
| 668 | |
| 669 | Py_DECREF(fmt); |
| 670 | Py_DECREF(tuple); |
| 671 | /* already freed because tuple owned only reference */ |
| 672 | serrno = NULL; |
| 673 | strerror = NULL; |
| 674 | } |
| 675 | else |
| 676 | /* The original Python code said: |
| 677 | * |
| 678 | * return StandardError.__str__(self) |
| 679 | * |
| 680 | * but there is no StandardError__str__() function; we happen to |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 681 | * know that's just a pass through to BaseException__str__(). |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 682 | */ |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 683 | rtnval = BaseException__str__(originalself, self); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 684 | |
| 685 | finally: |
| 686 | Py_XDECREF(filename); |
| 687 | Py_XDECREF(serrno); |
| 688 | Py_XDECREF(strerror); |
| 689 | return rtnval; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | static |
| 694 | PyMethodDef EnvironmentError_methods[] = { |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 695 | {"__init__", EnvironmentError__init__, METH_VARARGS}, |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 696 | {"__str__", EnvironmentError__str__, METH_O}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 697 | {NULL, NULL} |
| 698 | }; |
| 699 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 700 | PyDoc_STRVAR(IOError__doc__, "I/O operation failed."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 701 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 702 | PyDoc_STRVAR(OSError__doc__, "OS system call failed."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 703 | |
| 704 | #ifdef MS_WINDOWS |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 705 | #include "errmap.h" |
| 706 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 707 | PyDoc_STRVAR(WindowsError__doc__, "MS-Windows OS system call failed."); |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 708 | |
| 709 | static PyObject * |
| 710 | WindowsError__init__(PyObject *self, PyObject *args) |
| 711 | { |
| 712 | PyObject *o_errcode, *result; |
| 713 | long errcode, posix_errno; |
| 714 | result = EnvironmentError__init__(self, args); |
| 715 | if (!result) |
| 716 | return NULL; |
| 717 | self = get_self(args); |
| 718 | if (!self) |
| 719 | goto failed; |
| 720 | /* Set errno to the POSIX errno, and winerror to the Win32 |
| 721 | error code. */ |
| 722 | o_errcode = PyObject_GetAttrString(self, "errno"); |
| 723 | if (!o_errcode) |
| 724 | goto failed; |
| 725 | errcode = PyInt_AsLong(o_errcode); |
| 726 | if (!errcode == -1 && PyErr_Occurred()) |
| 727 | goto failed; |
| 728 | posix_errno = winerror_to_errno(errcode); |
| 729 | if (PyObject_SetAttrString(self, "winerror", o_errcode) < 0) |
| 730 | goto failed; |
| 731 | Py_DECREF(o_errcode); |
| 732 | o_errcode = PyInt_FromLong(posix_errno); |
| 733 | if (!o_errcode) |
| 734 | goto failed; |
| 735 | if (PyObject_SetAttrString(self, "errno", o_errcode) < 0) |
| 736 | goto failed; |
| 737 | Py_DECREF(o_errcode); |
| 738 | return result; |
| 739 | failed: |
| 740 | /* Could not set errno. */ |
| 741 | Py_XDECREF(o_errcode); |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 742 | Py_DECREF(result); |
| 743 | return NULL; |
| 744 | } |
| 745 | |
| 746 | static PyObject * |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 747 | WindowsError__str__(PyObject *originalself, PyObject *self) |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 748 | { |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 749 | PyObject *filename; |
| 750 | PyObject *serrno; |
| 751 | PyObject *strerror; |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 752 | PyObject *repr = NULL; |
| 753 | PyObject *fmt = NULL; |
| 754 | PyObject *tuple = NULL; |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 755 | PyObject *rtnval = NULL; |
| 756 | |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 757 | filename = PyObject_GetAttrString(self, "filename"); |
| 758 | serrno = PyObject_GetAttrString(self, "winerror"); |
| 759 | strerror = PyObject_GetAttrString(self, "strerror"); |
| 760 | if (!filename || !serrno || !strerror) |
| 761 | goto finally; |
| 762 | |
| 763 | if (filename != Py_None) { |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 764 | fmt = PyString_FromString("[Error %s] %s: %s"); |
| 765 | repr = PyObject_Repr(filename); |
| 766 | if (!fmt || !repr) |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 767 | goto finally; |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 768 | |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 769 | |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 770 | tuple = PyTuple_Pack(3, serrno, strerror, repr); |
| 771 | if (!tuple) |
| 772 | goto finally; |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 773 | |
| 774 | rtnval = PyString_Format(fmt, tuple); |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 775 | } |
| 776 | else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) { |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 777 | fmt = PyString_FromString("[Error %s] %s"); |
| 778 | if (!fmt) |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 779 | goto finally; |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 780 | |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 781 | tuple = PyTuple_Pack(2, serrno, strerror); |
| 782 | if (!tuple) |
| 783 | goto finally; |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 784 | |
| 785 | rtnval = PyString_Format(fmt, tuple); |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 786 | } |
| 787 | else |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 788 | rtnval = EnvironmentError__str__(originalself, self); |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 789 | |
| 790 | finally: |
| 791 | Py_XDECREF(filename); |
| 792 | Py_XDECREF(serrno); |
| 793 | Py_XDECREF(strerror); |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 794 | Py_XDECREF(repr); |
| 795 | Py_XDECREF(fmt); |
| 796 | Py_XDECREF(tuple); |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 797 | return rtnval; |
| 798 | } |
| 799 | |
| 800 | static |
| 801 | PyMethodDef WindowsError_methods[] = { |
| 802 | {"__init__", WindowsError__init__, METH_VARARGS}, |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 803 | {"__str__", WindowsError__str__, METH_O}, |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 804 | {NULL, NULL} |
| 805 | }; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 806 | #endif /* MS_WINDOWS */ |
| 807 | |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 808 | #ifdef __VMS |
| 809 | static char |
| 810 | VMSError__doc__[] = "OpenVMS OS system call failed."; |
| 811 | #endif |
| 812 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 813 | PyDoc_STRVAR(EOFError__doc__, "Read beyond end of file."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 814 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 815 | PyDoc_STRVAR(RuntimeError__doc__, "Unspecified run-time error."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 816 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 817 | PyDoc_STRVAR(NotImplementedError__doc__, |
| 818 | "Method or function hasn't been implemented yet."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 820 | PyDoc_STRVAR(NameError__doc__, "Name not found globally."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 821 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 822 | PyDoc_STRVAR(UnboundLocalError__doc__, |
| 823 | "Local name referenced but not bound to a value."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 824 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 825 | PyDoc_STRVAR(AttributeError__doc__, "Attribute not found."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 826 | |
| 827 | |
| 828 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 829 | PyDoc_STRVAR(SyntaxError__doc__, "Invalid syntax."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 830 | |
| 831 | |
| 832 | static int |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 833 | SyntaxError__classinit__(PyObject *klass) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 834 | { |
Barry Warsaw | 87bec35 | 2000-08-18 05:05:37 +0000 | [diff] [blame] | 835 | int retval = 0; |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 836 | PyObject *emptystring = PyString_FromString(""); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 837 | |
| 838 | /* Additional class-creation time initializations */ |
| 839 | if (!emptystring || |
| 840 | PyObject_SetAttrString(klass, "msg", emptystring) || |
| 841 | PyObject_SetAttrString(klass, "filename", Py_None) || |
| 842 | PyObject_SetAttrString(klass, "lineno", Py_None) || |
| 843 | PyObject_SetAttrString(klass, "offset", Py_None) || |
Martin v. Löwis | cfeb3b6 | 2002-03-03 21:30:27 +0000 | [diff] [blame] | 844 | PyObject_SetAttrString(klass, "text", Py_None) || |
| 845 | PyObject_SetAttrString(klass, "print_file_and_line", Py_None)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 846 | { |
Barry Warsaw | 87bec35 | 2000-08-18 05:05:37 +0000 | [diff] [blame] | 847 | retval = -1; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 848 | } |
Barry Warsaw | 87bec35 | 2000-08-18 05:05:37 +0000 | [diff] [blame] | 849 | Py_XDECREF(emptystring); |
| 850 | return retval; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 854 | static PyObject * |
| 855 | SyntaxError__init__(PyObject *self, PyObject *args) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 856 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 857 | PyObject *rtnval = NULL; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 858 | Py_ssize_t lenargs; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 859 | |
| 860 | if (!(self = get_self(args))) |
| 861 | return NULL; |
| 862 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 863 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 864 | return NULL; |
| 865 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 866 | if (!set_args_and_message(self, args)) { |
| 867 | Py_DECREF(args); |
| 868 | return NULL; |
| 869 | } |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 870 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 871 | lenargs = PySequence_Size(args); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 872 | if (lenargs >= 1) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 873 | PyObject *item0 = PySequence_GetItem(args, 0); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 874 | int status; |
| 875 | |
| 876 | if (!item0) |
| 877 | goto finally; |
| 878 | status = PyObject_SetAttrString(self, "msg", item0); |
| 879 | Py_DECREF(item0); |
| 880 | if (status) |
| 881 | goto finally; |
| 882 | } |
| 883 | if (lenargs == 2) { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 884 | PyObject *info = PySequence_GetItem(args, 1); |
Fred Drake | 9da7f3b | 2001-02-28 21:52:10 +0000 | [diff] [blame] | 885 | PyObject *filename = NULL, *lineno = NULL; |
| 886 | PyObject *offset = NULL, *text = NULL; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 887 | int status = 1; |
| 888 | |
| 889 | if (!info) |
| 890 | goto finally; |
| 891 | |
| 892 | filename = PySequence_GetItem(info, 0); |
Fred Drake | 9da7f3b | 2001-02-28 21:52:10 +0000 | [diff] [blame] | 893 | if (filename != NULL) { |
| 894 | lineno = PySequence_GetItem(info, 1); |
| 895 | if (lineno != NULL) { |
| 896 | offset = PySequence_GetItem(info, 2); |
| 897 | if (offset != NULL) { |
| 898 | text = PySequence_GetItem(info, 3); |
| 899 | if (text != NULL) { |
| 900 | status = |
| 901 | PyObject_SetAttrString(self, "filename", filename) |
| 902 | || PyObject_SetAttrString(self, "lineno", lineno) |
| 903 | || PyObject_SetAttrString(self, "offset", offset) |
| 904 | || PyObject_SetAttrString(self, "text", text); |
| 905 | Py_DECREF(text); |
| 906 | } |
| 907 | Py_DECREF(offset); |
| 908 | } |
| 909 | Py_DECREF(lineno); |
| 910 | } |
| 911 | Py_DECREF(filename); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 912 | } |
Fred Drake | 9da7f3b | 2001-02-28 21:52:10 +0000 | [diff] [blame] | 913 | Py_DECREF(info); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 914 | |
| 915 | if (status) |
| 916 | goto finally; |
| 917 | } |
| 918 | Py_INCREF(Py_None); |
| 919 | rtnval = Py_None; |
| 920 | |
| 921 | finally: |
| 922 | Py_DECREF(args); |
| 923 | return rtnval; |
| 924 | } |
| 925 | |
| 926 | |
Fred Drake | 185a29b | 2000-08-15 16:20:36 +0000 | [diff] [blame] | 927 | /* This is called "my_basename" instead of just "basename" to avoid name |
| 928 | conflicts with glibc; basename is already prototyped if _GNU_SOURCE is |
| 929 | defined, and Python does define that. */ |
| 930 | static char * |
| 931 | my_basename(char *name) |
| 932 | { |
| 933 | char *cp = name; |
| 934 | char *result = name; |
| 935 | |
| 936 | if (name == NULL) |
| 937 | return "???"; |
| 938 | while (*cp != '\0') { |
| 939 | if (*cp == SEP) |
| 940 | result = cp + 1; |
| 941 | ++cp; |
| 942 | } |
| 943 | return result; |
| 944 | } |
| 945 | |
| 946 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 947 | static PyObject * |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 948 | SyntaxError__str__(PyObject *_self, PyObject *self) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 949 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 950 | PyObject *msg; |
| 951 | PyObject *str; |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 952 | PyObject *filename, *lineno, *result; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 953 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 954 | if (!(msg = PyObject_GetAttrString(self, "msg"))) |
| 955 | return NULL; |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 956 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 957 | str = PyObject_Str(msg); |
| 958 | Py_DECREF(msg); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 959 | result = str; |
| 960 | |
| 961 | /* XXX -- do all the additional formatting with filename and |
| 962 | lineno here */ |
| 963 | |
Guido van Rossum | 602d451 | 2002-09-03 20:24:09 +0000 | [diff] [blame] | 964 | if (str != NULL && PyString_Check(str)) { |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 965 | int have_filename = 0; |
| 966 | int have_lineno = 0; |
| 967 | char *buffer = NULL; |
| 968 | |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 969 | if ((filename = PyObject_GetAttrString(self, "filename")) != NULL) |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 970 | have_filename = PyString_Check(filename); |
| 971 | else |
| 972 | PyErr_Clear(); |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 973 | |
| 974 | if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL) |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 975 | have_lineno = PyInt_Check(lineno); |
| 976 | else |
| 977 | PyErr_Clear(); |
| 978 | |
| 979 | if (have_filename || have_lineno) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 980 | Py_ssize_t bufsize = PyString_GET_SIZE(str) + 64; |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 981 | if (have_filename) |
| 982 | bufsize += PyString_GET_SIZE(filename); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 983 | |
Anthony Baxter | a863d33 | 2006-04-11 07:43:46 +0000 | [diff] [blame] | 984 | buffer = (char *)PyMem_MALLOC(bufsize); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 985 | if (buffer != NULL) { |
| 986 | if (have_filename && have_lineno) |
Jeremy Hylton | 05bd787 | 2001-11-28 20:24:33 +0000 | [diff] [blame] | 987 | PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)", |
| 988 | PyString_AS_STRING(str), |
| 989 | my_basename(PyString_AS_STRING(filename)), |
| 990 | PyInt_AsLong(lineno)); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 991 | else if (have_filename) |
Jeremy Hylton | 05bd787 | 2001-11-28 20:24:33 +0000 | [diff] [blame] | 992 | PyOS_snprintf(buffer, bufsize, "%s (%s)", |
| 993 | PyString_AS_STRING(str), |
| 994 | my_basename(PyString_AS_STRING(filename))); |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 995 | else if (have_lineno) |
Jeremy Hylton | 05bd787 | 2001-11-28 20:24:33 +0000 | [diff] [blame] | 996 | PyOS_snprintf(buffer, bufsize, "%s (line %ld)", |
| 997 | PyString_AS_STRING(str), |
| 998 | PyInt_AsLong(lineno)); |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 999 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 1000 | result = PyString_FromString(buffer); |
Barry Warsaw | 77c9f50 | 2000-08-16 19:43:17 +0000 | [diff] [blame] | 1001 | PyMem_FREE(buffer); |
| 1002 | |
Fred Drake | 1aba577 | 2000-08-15 15:46:16 +0000 | [diff] [blame] | 1003 | if (result == NULL) |
| 1004 | result = str; |
| 1005 | else |
| 1006 | Py_DECREF(str); |
| 1007 | } |
| 1008 | } |
| 1009 | Py_XDECREF(filename); |
| 1010 | Py_XDECREF(lineno); |
| 1011 | } |
| 1012 | return result; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 1016 | static PyMethodDef SyntaxError_methods[] = { |
Jeremy Hylton | 4e542a3 | 2000-06-30 04:59:59 +0000 | [diff] [blame] | 1017 | {"__init__", SyntaxError__init__, METH_VARARGS}, |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 1018 | {"__str__", SyntaxError__str__, METH_O}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1019 | {NULL, NULL} |
| 1020 | }; |
| 1021 | |
| 1022 | |
Guido van Rossum | 602d451 | 2002-09-03 20:24:09 +0000 | [diff] [blame] | 1023 | static PyObject * |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 1024 | KeyError__str__(PyObject *_self, PyObject *self) |
Guido van Rossum | 602d451 | 2002-09-03 20:24:09 +0000 | [diff] [blame] | 1025 | { |
| 1026 | PyObject *argsattr; |
| 1027 | PyObject *result; |
| 1028 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1029 | argsattr = PyObject_GetAttrString(self, "args"); |
| 1030 | if (!argsattr) |
| 1031 | return NULL; |
Guido van Rossum | 602d451 | 2002-09-03 20:24:09 +0000 | [diff] [blame] | 1032 | |
| 1033 | /* If args is a tuple of exactly one item, apply repr to args[0]. |
| 1034 | This is done so that e.g. the exception raised by {}[''] prints |
| 1035 | KeyError: '' |
| 1036 | rather than the confusing |
| 1037 | KeyError |
| 1038 | alone. The downside is that if KeyError is raised with an explanatory |
| 1039 | string, that string will be displayed in quotes. Too bad. |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1040 | If args is anything else, use the default BaseException__str__(). |
Guido van Rossum | 602d451 | 2002-09-03 20:24:09 +0000 | [diff] [blame] | 1041 | */ |
| 1042 | if (PyTuple_Check(argsattr) && PyTuple_GET_SIZE(argsattr) == 1) { |
| 1043 | PyObject *key = PyTuple_GET_ITEM(argsattr, 0); |
| 1044 | result = PyObject_Repr(key); |
| 1045 | } |
| 1046 | else |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 1047 | result = BaseException__str__(_self, self); |
Guido van Rossum | 602d451 | 2002-09-03 20:24:09 +0000 | [diff] [blame] | 1048 | |
| 1049 | Py_DECREF(argsattr); |
| 1050 | return result; |
| 1051 | } |
| 1052 | |
| 1053 | static PyMethodDef KeyError_methods[] = { |
Martin v. Löwis | 2a0ad4d | 2006-05-15 09:22:27 +0000 | [diff] [blame^] | 1054 | {"__str__", KeyError__str__, METH_O}, |
Guido van Rossum | 602d451 | 2002-09-03 20:24:09 +0000 | [diff] [blame] | 1055 | {NULL, NULL} |
| 1056 | }; |
| 1057 | |
| 1058 | |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 1059 | #ifdef Py_USING_UNICODE |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1060 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1061 | int get_int(PyObject *exc, const char *name, Py_ssize_t *value) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1062 | { |
| 1063 | PyObject *attr = PyObject_GetAttrString(exc, (char *)name); |
| 1064 | |
| 1065 | if (!attr) |
| 1066 | return -1; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1067 | if (PyInt_Check(attr)) { |
| 1068 | *value = PyInt_AS_LONG(attr); |
| 1069 | } else if (PyLong_Check(attr)) { |
| 1070 | *value = (size_t)PyLong_AsLongLong(attr); |
| 1071 | if (*value == -1) { |
| 1072 | Py_DECREF(attr); |
| 1073 | return -1; |
| 1074 | } |
| 1075 | } else { |
Walter Dörwald | fd08e4c | 2002-09-02 16:10:06 +0000 | [diff] [blame] | 1076 | PyErr_Format(PyExc_TypeError, "%.200s attribute must be int", name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1077 | Py_DECREF(attr); |
| 1078 | return -1; |
| 1079 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1080 | Py_DECREF(attr); |
| 1081 | return 0; |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | static |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1086 | int set_ssize_t(PyObject *exc, const char *name, Py_ssize_t value) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1087 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1088 | PyObject *obj = PyInt_FromSsize_t(value); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1089 | int result; |
| 1090 | |
| 1091 | if (!obj) |
| 1092 | return -1; |
| 1093 | result = PyObject_SetAttrString(exc, (char *)name, obj); |
| 1094 | Py_DECREF(obj); |
| 1095 | return result; |
| 1096 | } |
| 1097 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1098 | static |
| 1099 | PyObject *get_string(PyObject *exc, const char *name) |
| 1100 | { |
| 1101 | PyObject *attr = PyObject_GetAttrString(exc, (char *)name); |
| 1102 | |
| 1103 | if (!attr) |
| 1104 | return NULL; |
| 1105 | if (!PyString_Check(attr)) { |
Walter Dörwald | fd08e4c | 2002-09-02 16:10:06 +0000 | [diff] [blame] | 1106 | PyErr_Format(PyExc_TypeError, "%.200s attribute must be str", name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1107 | Py_DECREF(attr); |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | return attr; |
| 1111 | } |
| 1112 | |
| 1113 | |
| 1114 | static |
| 1115 | int set_string(PyObject *exc, const char *name, const char *value) |
| 1116 | { |
| 1117 | PyObject *obj = PyString_FromString(value); |
| 1118 | int result; |
| 1119 | |
| 1120 | if (!obj) |
| 1121 | return -1; |
| 1122 | result = PyObject_SetAttrString(exc, (char *)name, obj); |
| 1123 | Py_DECREF(obj); |
| 1124 | return result; |
| 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | static |
| 1129 | PyObject *get_unicode(PyObject *exc, const char *name) |
| 1130 | { |
| 1131 | PyObject *attr = PyObject_GetAttrString(exc, (char *)name); |
| 1132 | |
| 1133 | if (!attr) |
| 1134 | return NULL; |
| 1135 | if (!PyUnicode_Check(attr)) { |
Walter Dörwald | fd08e4c | 2002-09-02 16:10:06 +0000 | [diff] [blame] | 1136 | PyErr_Format(PyExc_TypeError, "%.200s attribute must be unicode", name); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1137 | Py_DECREF(attr); |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | return attr; |
| 1141 | } |
| 1142 | |
| 1143 | PyObject * PyUnicodeEncodeError_GetEncoding(PyObject *exc) |
| 1144 | { |
| 1145 | return get_string(exc, "encoding"); |
| 1146 | } |
| 1147 | |
| 1148 | PyObject * PyUnicodeDecodeError_GetEncoding(PyObject *exc) |
| 1149 | { |
| 1150 | return get_string(exc, "encoding"); |
| 1151 | } |
| 1152 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1153 | PyObject *PyUnicodeEncodeError_GetObject(PyObject *exc) |
| 1154 | { |
| 1155 | return get_unicode(exc, "object"); |
| 1156 | } |
| 1157 | |
| 1158 | PyObject *PyUnicodeDecodeError_GetObject(PyObject *exc) |
| 1159 | { |
| 1160 | return get_string(exc, "object"); |
| 1161 | } |
| 1162 | |
| 1163 | PyObject *PyUnicodeTranslateError_GetObject(PyObject *exc) |
| 1164 | { |
| 1165 | return get_unicode(exc, "object"); |
| 1166 | } |
| 1167 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1168 | int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1169 | { |
| 1170 | if (!get_int(exc, "start", start)) { |
| 1171 | PyObject *object = PyUnicodeEncodeError_GetObject(exc); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1172 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1173 | if (!object) |
| 1174 | return -1; |
| 1175 | size = PyUnicode_GET_SIZE(object); |
| 1176 | if (*start<0) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1177 | *start = 0; /*XXX check for values <0*/ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1178 | if (*start>=size) |
| 1179 | *start = size-1; |
| 1180 | Py_DECREF(object); |
| 1181 | return 0; |
| 1182 | } |
| 1183 | return -1; |
| 1184 | } |
| 1185 | |
| 1186 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1187 | int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1188 | { |
| 1189 | if (!get_int(exc, "start", start)) { |
| 1190 | PyObject *object = PyUnicodeDecodeError_GetObject(exc); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1191 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1192 | if (!object) |
| 1193 | return -1; |
| 1194 | size = PyString_GET_SIZE(object); |
| 1195 | if (*start<0) |
| 1196 | *start = 0; |
| 1197 | if (*start>=size) |
| 1198 | *start = size-1; |
| 1199 | Py_DECREF(object); |
| 1200 | return 0; |
| 1201 | } |
| 1202 | return -1; |
| 1203 | } |
| 1204 | |
| 1205 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1206 | int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1207 | { |
| 1208 | return PyUnicodeEncodeError_GetStart(exc, start); |
| 1209 | } |
| 1210 | |
| 1211 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1212 | int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1213 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1214 | return set_ssize_t(exc, "start", start); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1218 | int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1219 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1220 | return set_ssize_t(exc, "start", start); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1224 | int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1225 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1226 | return set_ssize_t(exc, "start", start); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1230 | int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1231 | { |
| 1232 | if (!get_int(exc, "end", end)) { |
| 1233 | PyObject *object = PyUnicodeEncodeError_GetObject(exc); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1234 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1235 | if (!object) |
| 1236 | return -1; |
| 1237 | size = PyUnicode_GET_SIZE(object); |
| 1238 | if (*end<1) |
| 1239 | *end = 1; |
| 1240 | if (*end>size) |
| 1241 | *end = size; |
| 1242 | Py_DECREF(object); |
| 1243 | return 0; |
| 1244 | } |
| 1245 | return -1; |
| 1246 | } |
| 1247 | |
| 1248 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1249 | int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1250 | { |
| 1251 | if (!get_int(exc, "end", end)) { |
| 1252 | PyObject *object = PyUnicodeDecodeError_GetObject(exc); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1253 | Py_ssize_t size; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1254 | if (!object) |
| 1255 | return -1; |
| 1256 | size = PyString_GET_SIZE(object); |
| 1257 | if (*end<1) |
| 1258 | *end = 1; |
| 1259 | if (*end>size) |
| 1260 | *end = size; |
| 1261 | Py_DECREF(object); |
| 1262 | return 0; |
| 1263 | } |
| 1264 | return -1; |
| 1265 | } |
| 1266 | |
| 1267 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1268 | int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1269 | { |
| 1270 | return PyUnicodeEncodeError_GetEnd(exc, start); |
| 1271 | } |
| 1272 | |
| 1273 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1274 | int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1275 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1276 | return set_ssize_t(exc, "end", end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1280 | int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1281 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1282 | return set_ssize_t(exc, "end", end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1286 | int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1287 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1288 | return set_ssize_t(exc, "end", end); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | |
| 1292 | PyObject *PyUnicodeEncodeError_GetReason(PyObject *exc) |
| 1293 | { |
| 1294 | return get_string(exc, "reason"); |
| 1295 | } |
| 1296 | |
| 1297 | |
| 1298 | PyObject *PyUnicodeDecodeError_GetReason(PyObject *exc) |
| 1299 | { |
| 1300 | return get_string(exc, "reason"); |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | PyObject *PyUnicodeTranslateError_GetReason(PyObject *exc) |
| 1305 | { |
| 1306 | return get_string(exc, "reason"); |
| 1307 | } |
| 1308 | |
| 1309 | |
| 1310 | int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason) |
| 1311 | { |
| 1312 | return set_string(exc, "reason", reason); |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason) |
| 1317 | { |
| 1318 | return set_string(exc, "reason", reason); |
| 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason) |
| 1323 | { |
| 1324 | return set_string(exc, "reason", reason); |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | static PyObject * |
| 1329 | UnicodeError__init__(PyObject *self, PyObject *args, PyTypeObject *objecttype) |
| 1330 | { |
| 1331 | PyObject *rtnval = NULL; |
| 1332 | PyObject *encoding; |
| 1333 | PyObject *object; |
| 1334 | PyObject *start; |
| 1335 | PyObject *end; |
| 1336 | PyObject *reason; |
| 1337 | |
| 1338 | if (!(self = get_self(args))) |
| 1339 | return NULL; |
| 1340 | |
| 1341 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
| 1342 | return NULL; |
| 1343 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1344 | if (!set_args_and_message(self, args)) { |
| 1345 | Py_DECREF(args); |
| 1346 | return NULL; |
| 1347 | } |
| 1348 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1349 | if (!PyArg_ParseTuple(args, "O!O!O!O!O!", |
| 1350 | &PyString_Type, &encoding, |
| 1351 | objecttype, &object, |
| 1352 | &PyInt_Type, &start, |
| 1353 | &PyInt_Type, &end, |
| 1354 | &PyString_Type, &reason)) |
Walter Dörwald | e98147a | 2003-08-14 20:59:07 +0000 | [diff] [blame] | 1355 | goto finally; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1356 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1357 | if (PyObject_SetAttrString(self, "encoding", encoding)) |
| 1358 | goto finally; |
| 1359 | if (PyObject_SetAttrString(self, "object", object)) |
| 1360 | goto finally; |
| 1361 | if (PyObject_SetAttrString(self, "start", start)) |
| 1362 | goto finally; |
| 1363 | if (PyObject_SetAttrString(self, "end", end)) |
| 1364 | goto finally; |
| 1365 | if (PyObject_SetAttrString(self, "reason", reason)) |
| 1366 | goto finally; |
| 1367 | |
| 1368 | Py_INCREF(Py_None); |
| 1369 | rtnval = Py_None; |
| 1370 | |
| 1371 | finally: |
| 1372 | Py_DECREF(args); |
| 1373 | return rtnval; |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | static PyObject * |
| 1378 | UnicodeEncodeError__init__(PyObject *self, PyObject *args) |
| 1379 | { |
| 1380 | return UnicodeError__init__(self, args, &PyUnicode_Type); |
| 1381 | } |
| 1382 | |
| 1383 | static PyObject * |
| 1384 | UnicodeEncodeError__str__(PyObject *self, PyObject *arg) |
| 1385 | { |
| 1386 | PyObject *encodingObj = NULL; |
| 1387 | PyObject *objectObj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1388 | Py_ssize_t start; |
| 1389 | Py_ssize_t end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1390 | PyObject *reasonObj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1391 | PyObject *result = NULL; |
| 1392 | |
| 1393 | self = arg; |
| 1394 | |
| 1395 | if (!(encodingObj = PyUnicodeEncodeError_GetEncoding(self))) |
| 1396 | goto error; |
| 1397 | |
| 1398 | if (!(objectObj = PyUnicodeEncodeError_GetObject(self))) |
| 1399 | goto error; |
| 1400 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1401 | if (PyUnicodeEncodeError_GetStart(self, &start)) |
| 1402 | goto error; |
| 1403 | |
| 1404 | if (PyUnicodeEncodeError_GetEnd(self, &end)) |
| 1405 | goto error; |
| 1406 | |
| 1407 | if (!(reasonObj = PyUnicodeEncodeError_GetReason(self))) |
| 1408 | goto error; |
| 1409 | |
| 1410 | if (end==start+1) { |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 1411 | int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start]; |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1412 | char badchar_str[20]; |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 1413 | if (badchar <= 0xff) |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1414 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar); |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 1415 | else if (badchar <= 0xffff) |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1416 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar); |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 1417 | else |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1418 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar); |
| 1419 | result = PyString_FromFormat( |
| 1420 | "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1421 | PyString_AS_STRING(encodingObj), |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1422 | badchar_str, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1423 | start, |
| 1424 | PyString_AS_STRING(reasonObj) |
| 1425 | ); |
| 1426 | } |
| 1427 | else { |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1428 | result = PyString_FromFormat( |
| 1429 | "'%.400s' codec can't encode characters in position %zd-%zd: %.400s", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1430 | PyString_AS_STRING(encodingObj), |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1431 | start, |
| 1432 | (end-1), |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1433 | PyString_AS_STRING(reasonObj) |
| 1434 | ); |
| 1435 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1436 | |
| 1437 | error: |
| 1438 | Py_XDECREF(reasonObj); |
| 1439 | Py_XDECREF(objectObj); |
| 1440 | Py_XDECREF(encodingObj); |
| 1441 | return result; |
| 1442 | } |
| 1443 | |
| 1444 | static PyMethodDef UnicodeEncodeError_methods[] = { |
| 1445 | {"__init__", UnicodeEncodeError__init__, METH_VARARGS}, |
| 1446 | {"__str__", UnicodeEncodeError__str__, METH_O}, |
| 1447 | {NULL, NULL} |
| 1448 | }; |
| 1449 | |
| 1450 | |
| 1451 | PyObject * PyUnicodeEncodeError_Create( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1452 | const char *encoding, const Py_UNICODE *object, Py_ssize_t length, |
| 1453 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1454 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1455 | return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1456 | encoding, object, length, start, end, reason); |
| 1457 | } |
| 1458 | |
| 1459 | |
| 1460 | static PyObject * |
| 1461 | UnicodeDecodeError__init__(PyObject *self, PyObject *args) |
| 1462 | { |
| 1463 | return UnicodeError__init__(self, args, &PyString_Type); |
| 1464 | } |
| 1465 | |
| 1466 | static PyObject * |
| 1467 | UnicodeDecodeError__str__(PyObject *self, PyObject *arg) |
| 1468 | { |
| 1469 | PyObject *encodingObj = NULL; |
| 1470 | PyObject *objectObj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1471 | Py_ssize_t start; |
| 1472 | Py_ssize_t end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1473 | PyObject *reasonObj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1474 | PyObject *result = NULL; |
| 1475 | |
| 1476 | self = arg; |
| 1477 | |
| 1478 | if (!(encodingObj = PyUnicodeDecodeError_GetEncoding(self))) |
| 1479 | goto error; |
| 1480 | |
| 1481 | if (!(objectObj = PyUnicodeDecodeError_GetObject(self))) |
| 1482 | goto error; |
| 1483 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1484 | if (PyUnicodeDecodeError_GetStart(self, &start)) |
| 1485 | goto error; |
| 1486 | |
| 1487 | if (PyUnicodeDecodeError_GetEnd(self, &end)) |
| 1488 | goto error; |
| 1489 | |
| 1490 | if (!(reasonObj = PyUnicodeDecodeError_GetReason(self))) |
| 1491 | goto error; |
| 1492 | |
| 1493 | if (end==start+1) { |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1494 | /* FromFormat does not support %02x, so format that separately */ |
| 1495 | char byte[4]; |
| 1496 | PyOS_snprintf(byte, sizeof(byte), "%02x", |
| 1497 | ((int)PyString_AS_STRING(objectObj)[start])&0xff); |
| 1498 | result = PyString_FromFormat( |
| 1499 | "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1500 | PyString_AS_STRING(encodingObj), |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1501 | byte, |
| 1502 | start, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1503 | PyString_AS_STRING(reasonObj) |
| 1504 | ); |
| 1505 | } |
| 1506 | else { |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1507 | result = PyString_FromFormat( |
| 1508 | "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1509 | PyString_AS_STRING(encodingObj), |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1510 | start, |
| 1511 | (end-1), |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1512 | PyString_AS_STRING(reasonObj) |
| 1513 | ); |
| 1514 | } |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1515 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1516 | |
| 1517 | error: |
| 1518 | Py_XDECREF(reasonObj); |
| 1519 | Py_XDECREF(objectObj); |
| 1520 | Py_XDECREF(encodingObj); |
| 1521 | return result; |
| 1522 | } |
| 1523 | |
| 1524 | static PyMethodDef UnicodeDecodeError_methods[] = { |
| 1525 | {"__init__", UnicodeDecodeError__init__, METH_VARARGS}, |
| 1526 | {"__str__", UnicodeDecodeError__str__, METH_O}, |
| 1527 | {NULL, NULL} |
| 1528 | }; |
| 1529 | |
| 1530 | |
| 1531 | PyObject * PyUnicodeDecodeError_Create( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1532 | const char *encoding, const char *object, Py_ssize_t length, |
| 1533 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1534 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1535 | assert(length < INT_MAX); |
| 1536 | assert(start < INT_MAX); |
| 1537 | assert(end < INT_MAX); |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 1538 | return PyObject_CallFunction(PyExc_UnicodeDecodeError, "ss#nns", |
| 1539 | encoding, object, length, start, end, reason); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | static PyObject * |
| 1544 | UnicodeTranslateError__init__(PyObject *self, PyObject *args) |
| 1545 | { |
| 1546 | PyObject *rtnval = NULL; |
| 1547 | PyObject *object; |
| 1548 | PyObject *start; |
| 1549 | PyObject *end; |
| 1550 | PyObject *reason; |
| 1551 | |
| 1552 | if (!(self = get_self(args))) |
| 1553 | return NULL; |
| 1554 | |
| 1555 | if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args)))) |
| 1556 | return NULL; |
| 1557 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1558 | if (!set_args_and_message(self, args)) { |
| 1559 | Py_DECREF(args); |
| 1560 | return NULL; |
| 1561 | } |
| 1562 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1563 | if (!PyArg_ParseTuple(args, "O!O!O!O!", |
| 1564 | &PyUnicode_Type, &object, |
| 1565 | &PyInt_Type, &start, |
| 1566 | &PyInt_Type, &end, |
| 1567 | &PyString_Type, &reason)) |
| 1568 | goto finally; |
| 1569 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1570 | if (PyObject_SetAttrString(self, "object", object)) |
| 1571 | goto finally; |
| 1572 | if (PyObject_SetAttrString(self, "start", start)) |
| 1573 | goto finally; |
| 1574 | if (PyObject_SetAttrString(self, "end", end)) |
| 1575 | goto finally; |
| 1576 | if (PyObject_SetAttrString(self, "reason", reason)) |
| 1577 | goto finally; |
| 1578 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1579 | rtnval = Py_None; |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1580 | Py_INCREF(rtnval); |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1581 | |
| 1582 | finally: |
| 1583 | Py_DECREF(args); |
| 1584 | return rtnval; |
| 1585 | } |
| 1586 | |
| 1587 | |
| 1588 | static PyObject * |
| 1589 | UnicodeTranslateError__str__(PyObject *self, PyObject *arg) |
| 1590 | { |
| 1591 | PyObject *objectObj = NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1592 | Py_ssize_t start; |
| 1593 | Py_ssize_t end; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1594 | PyObject *reasonObj = NULL; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1595 | PyObject *result = NULL; |
| 1596 | |
| 1597 | self = arg; |
| 1598 | |
| 1599 | if (!(objectObj = PyUnicodeTranslateError_GetObject(self))) |
| 1600 | goto error; |
| 1601 | |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1602 | if (PyUnicodeTranslateError_GetStart(self, &start)) |
| 1603 | goto error; |
| 1604 | |
| 1605 | if (PyUnicodeTranslateError_GetEnd(self, &end)) |
| 1606 | goto error; |
| 1607 | |
| 1608 | if (!(reasonObj = PyUnicodeTranslateError_GetReason(self))) |
| 1609 | goto error; |
| 1610 | |
| 1611 | if (end==start+1) { |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 1612 | int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start]; |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1613 | char badchar_str[20]; |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 1614 | if (badchar <= 0xff) |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1615 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar); |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 1616 | else if (badchar <= 0xffff) |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1617 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar); |
Walter Dörwald | fd196bd | 2003-08-12 17:32:43 +0000 | [diff] [blame] | 1618 | else |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1619 | PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar); |
| 1620 | result = PyString_FromFormat( |
| 1621 | "can't translate character u'\\%s' in position %zd: %.400s", |
| 1622 | badchar_str, |
| 1623 | start, |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1624 | PyString_AS_STRING(reasonObj) |
| 1625 | ); |
| 1626 | } |
| 1627 | else { |
Martin v. Löwis | 720ddb6 | 2006-02-16 07:11:33 +0000 | [diff] [blame] | 1628 | result = PyString_FromFormat( |
| 1629 | "can't translate characters in position %zd-%zd: %.400s", |
| 1630 | start, |
| 1631 | (end-1), |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1632 | PyString_AS_STRING(reasonObj) |
| 1633 | ); |
| 1634 | } |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1635 | |
| 1636 | error: |
| 1637 | Py_XDECREF(reasonObj); |
| 1638 | Py_XDECREF(objectObj); |
| 1639 | return result; |
| 1640 | } |
| 1641 | |
| 1642 | static PyMethodDef UnicodeTranslateError_methods[] = { |
| 1643 | {"__init__", UnicodeTranslateError__init__, METH_VARARGS}, |
| 1644 | {"__str__", UnicodeTranslateError__str__, METH_O}, |
| 1645 | {NULL, NULL} |
| 1646 | }; |
| 1647 | |
| 1648 | |
| 1649 | PyObject * PyUnicodeTranslateError_Create( |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1650 | const Py_UNICODE *object, Py_ssize_t length, |
| 1651 | Py_ssize_t start, Py_ssize_t end, const char *reason) |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1652 | { |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 1653 | return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns", |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1654 | object, length, start, end, reason); |
| 1655 | } |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 1656 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1657 | |
| 1658 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1659 | |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1660 | /* Exception doc strings */ |
| 1661 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1662 | PyDoc_STRVAR(AssertionError__doc__, "Assertion failed."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1663 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1664 | PyDoc_STRVAR(LookupError__doc__, "Base class for lookup errors."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1665 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1666 | PyDoc_STRVAR(IndexError__doc__, "Sequence index out of range."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1667 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1668 | PyDoc_STRVAR(KeyError__doc__, "Mapping key not found."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1669 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1670 | PyDoc_STRVAR(ArithmeticError__doc__, "Base class for arithmetic errors."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1671 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1672 | PyDoc_STRVAR(OverflowError__doc__, "Result too large to be represented."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1673 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1674 | PyDoc_STRVAR(ZeroDivisionError__doc__, |
| 1675 | "Second argument to a division or modulo operation was zero."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1676 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1677 | PyDoc_STRVAR(FloatingPointError__doc__, "Floating point operation failed."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1678 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1679 | PyDoc_STRVAR(ValueError__doc__, |
| 1680 | "Inappropriate argument value (of correct type)."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1681 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1682 | PyDoc_STRVAR(UnicodeError__doc__, "Unicode related error."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1683 | |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 1684 | #ifdef Py_USING_UNICODE |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1685 | PyDoc_STRVAR(UnicodeEncodeError__doc__, "Unicode encoding error."); |
| 1686 | |
| 1687 | PyDoc_STRVAR(UnicodeDecodeError__doc__, "Unicode decoding error."); |
| 1688 | |
| 1689 | PyDoc_STRVAR(UnicodeTranslateError__doc__, "Unicode translation error."); |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 1690 | #endif |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1691 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1692 | PyDoc_STRVAR(SystemError__doc__, |
| 1693 | "Internal error in the Python interpreter.\n\ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1694 | \n\ |
| 1695 | Please report this to the Python maintainer, along with the traceback,\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1696 | the Python version, and the hardware/OS platform and version."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1697 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1698 | PyDoc_STRVAR(ReferenceError__doc__, |
| 1699 | "Weak ref proxy used after referent went away."); |
Fred Drake | bb9fa21 | 2001-10-05 21:50:08 +0000 | [diff] [blame] | 1700 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1701 | PyDoc_STRVAR(MemoryError__doc__, "Out of memory."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1702 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1703 | PyDoc_STRVAR(IndentationError__doc__, "Improper indentation."); |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1705 | PyDoc_STRVAR(TabError__doc__, "Improper mixture of spaces and tabs."); |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1706 | |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1707 | /* Warning category docstrings */ |
| 1708 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1709 | PyDoc_STRVAR(Warning__doc__, "Base class for warning categories."); |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1710 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1711 | PyDoc_STRVAR(UserWarning__doc__, |
| 1712 | "Base class for warnings generated by user code."); |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1713 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1714 | PyDoc_STRVAR(DeprecationWarning__doc__, |
| 1715 | "Base class for warnings about deprecated features."); |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1717 | PyDoc_STRVAR(PendingDeprecationWarning__doc__, |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 1718 | "Base class for warnings about features which will be deprecated " |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1719 | "in the future."); |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 1720 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1721 | PyDoc_STRVAR(SyntaxWarning__doc__, |
| 1722 | "Base class for warnings about dubious syntax."); |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1723 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1724 | PyDoc_STRVAR(OverflowWarning__doc__, |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 1725 | "Base class for warnings about numeric overflow. Won't exist in Python 2.5."); |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame] | 1726 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1727 | PyDoc_STRVAR(RuntimeWarning__doc__, |
| 1728 | "Base class for warnings about dubious runtime behavior."); |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1729 | |
Barry Warsaw | 9f00739 | 2002-08-14 15:51:29 +0000 | [diff] [blame] | 1730 | PyDoc_STRVAR(FutureWarning__doc__, |
| 1731 | "Base class for warnings about constructs that will change semantically " |
| 1732 | "in the future."); |
| 1733 | |
Thomas Wouters | 9df4e6f | 2006-04-27 23:13:20 +0000 | [diff] [blame] | 1734 | PyDoc_STRVAR(ImportWarning__doc__, |
| 1735 | "Base class for warnings about probable mistakes in module imports"); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1736 | |
| 1737 | |
| 1738 | /* module global functions */ |
| 1739 | static PyMethodDef functions[] = { |
| 1740 | /* Sentinel */ |
| 1741 | {NULL, NULL} |
| 1742 | }; |
| 1743 | |
| 1744 | |
| 1745 | |
| 1746 | /* Global C API defined exceptions */ |
| 1747 | |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1748 | PyObject *PyExc_BaseException; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1749 | PyObject *PyExc_Exception; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1750 | PyObject *PyExc_StopIteration; |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 1751 | PyObject *PyExc_GeneratorExit; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1752 | PyObject *PyExc_StandardError; |
| 1753 | PyObject *PyExc_ArithmeticError; |
| 1754 | PyObject *PyExc_LookupError; |
| 1755 | |
| 1756 | PyObject *PyExc_AssertionError; |
| 1757 | PyObject *PyExc_AttributeError; |
| 1758 | PyObject *PyExc_EOFError; |
| 1759 | PyObject *PyExc_FloatingPointError; |
| 1760 | PyObject *PyExc_EnvironmentError; |
| 1761 | PyObject *PyExc_IOError; |
| 1762 | PyObject *PyExc_OSError; |
| 1763 | PyObject *PyExc_ImportError; |
| 1764 | PyObject *PyExc_IndexError; |
| 1765 | PyObject *PyExc_KeyError; |
| 1766 | PyObject *PyExc_KeyboardInterrupt; |
| 1767 | PyObject *PyExc_MemoryError; |
| 1768 | PyObject *PyExc_NameError; |
| 1769 | PyObject *PyExc_OverflowError; |
| 1770 | PyObject *PyExc_RuntimeError; |
| 1771 | PyObject *PyExc_NotImplementedError; |
| 1772 | PyObject *PyExc_SyntaxError; |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1773 | PyObject *PyExc_IndentationError; |
| 1774 | PyObject *PyExc_TabError; |
Fred Drake | bb9fa21 | 2001-10-05 21:50:08 +0000 | [diff] [blame] | 1775 | PyObject *PyExc_ReferenceError; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1776 | PyObject *PyExc_SystemError; |
| 1777 | PyObject *PyExc_SystemExit; |
| 1778 | PyObject *PyExc_UnboundLocalError; |
| 1779 | PyObject *PyExc_UnicodeError; |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1780 | PyObject *PyExc_UnicodeEncodeError; |
| 1781 | PyObject *PyExc_UnicodeDecodeError; |
| 1782 | PyObject *PyExc_UnicodeTranslateError; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1783 | PyObject *PyExc_TypeError; |
| 1784 | PyObject *PyExc_ValueError; |
| 1785 | PyObject *PyExc_ZeroDivisionError; |
| 1786 | #ifdef MS_WINDOWS |
| 1787 | PyObject *PyExc_WindowsError; |
| 1788 | #endif |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1789 | #ifdef __VMS |
| 1790 | PyObject *PyExc_VMSError; |
| 1791 | #endif |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1792 | |
| 1793 | /* Pre-computed MemoryError instance. Best to create this as early as |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1794 | * possible and not wait until a MemoryError is actually raised! |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1795 | */ |
| 1796 | PyObject *PyExc_MemoryErrorInst; |
| 1797 | |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1798 | /* Predefined warning categories */ |
| 1799 | PyObject *PyExc_Warning; |
| 1800 | PyObject *PyExc_UserWarning; |
| 1801 | PyObject *PyExc_DeprecationWarning; |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 1802 | PyObject *PyExc_PendingDeprecationWarning; |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1803 | PyObject *PyExc_SyntaxWarning; |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 1804 | /* PyExc_OverflowWarning should be removed for Python 2.5 */ |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame] | 1805 | PyObject *PyExc_OverflowWarning; |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1806 | PyObject *PyExc_RuntimeWarning; |
Barry Warsaw | 9f00739 | 2002-08-14 15:51:29 +0000 | [diff] [blame] | 1807 | PyObject *PyExc_FutureWarning; |
Thomas Wouters | 9df4e6f | 2006-04-27 23:13:20 +0000 | [diff] [blame] | 1808 | PyObject *PyExc_ImportWarning; |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1809 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1810 | |
| 1811 | |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 1812 | /* mapping between exception names and their PyObject ** */ |
| 1813 | static struct { |
| 1814 | char *name; |
| 1815 | PyObject **exc; |
| 1816 | PyObject **base; /* NULL == PyExc_StandardError */ |
| 1817 | char *docstr; |
| 1818 | PyMethodDef *methods; |
| 1819 | int (*classinit)(PyObject *); |
| 1820 | } exctable[] = { |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1821 | /* |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1822 | * The first four classes MUST appear in exactly this order |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1823 | */ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1824 | {"BaseException", &PyExc_BaseException}, |
| 1825 | {"Exception", &PyExc_Exception, &PyExc_BaseException, Exception__doc__}, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 1826 | {"StopIteration", &PyExc_StopIteration, &PyExc_Exception, |
| 1827 | StopIteration__doc__}, |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 1828 | {"GeneratorExit", &PyExc_GeneratorExit, &PyExc_Exception, |
| 1829 | GeneratorExit__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1830 | {"StandardError", &PyExc_StandardError, &PyExc_Exception, |
| 1831 | StandardError__doc__}, |
| 1832 | {"TypeError", &PyExc_TypeError, 0, TypeError__doc__}, |
| 1833 | /* |
| 1834 | * The rest appear in depth-first order of the hierarchy |
| 1835 | */ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1836 | {"SystemExit", &PyExc_SystemExit, &PyExc_BaseException, SystemExit__doc__, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1837 | SystemExit_methods}, |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1838 | {"KeyboardInterrupt", &PyExc_KeyboardInterrupt, &PyExc_BaseException, |
| 1839 | KeyboardInterrupt__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1840 | {"ImportError", &PyExc_ImportError, 0, ImportError__doc__}, |
| 1841 | {"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__, |
| 1842 | EnvironmentError_methods}, |
| 1843 | {"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__}, |
| 1844 | {"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__}, |
| 1845 | #ifdef MS_WINDOWS |
Mark Hammond | 557a044 | 2000-08-15 00:37:32 +0000 | [diff] [blame] | 1846 | {"WindowsError", &PyExc_WindowsError, &PyExc_OSError, |
Martin v. Löwis | 879768d | 2006-05-11 13:28:43 +0000 | [diff] [blame] | 1847 | WindowsError__doc__, WindowsError_methods}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1848 | #endif /* MS_WINDOWS */ |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 1849 | #ifdef __VMS |
| 1850 | {"VMSError", &PyExc_VMSError, &PyExc_OSError, |
| 1851 | VMSError__doc__}, |
| 1852 | #endif |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1853 | {"EOFError", &PyExc_EOFError, 0, EOFError__doc__}, |
| 1854 | {"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__}, |
| 1855 | {"NotImplementedError", &PyExc_NotImplementedError, |
| 1856 | &PyExc_RuntimeError, NotImplementedError__doc__}, |
| 1857 | {"NameError", &PyExc_NameError, 0, NameError__doc__}, |
| 1858 | {"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError, |
| 1859 | UnboundLocalError__doc__}, |
| 1860 | {"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__}, |
| 1861 | {"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__, |
| 1862 | SyntaxError_methods, SyntaxError__classinit__}, |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1863 | {"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError, |
| 1864 | IndentationError__doc__}, |
| 1865 | {"TabError", &PyExc_TabError, &PyExc_IndentationError, |
| 1866 | TabError__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1867 | {"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__}, |
| 1868 | {"LookupError", &PyExc_LookupError, 0, LookupError__doc__}, |
| 1869 | {"IndexError", &PyExc_IndexError, &PyExc_LookupError, |
| 1870 | IndexError__doc__}, |
| 1871 | {"KeyError", &PyExc_KeyError, &PyExc_LookupError, |
Guido van Rossum | 602d451 | 2002-09-03 20:24:09 +0000 | [diff] [blame] | 1872 | KeyError__doc__, KeyError_methods}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1873 | {"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__}, |
| 1874 | {"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError, |
| 1875 | OverflowError__doc__}, |
| 1876 | {"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError, |
| 1877 | ZeroDivisionError__doc__}, |
| 1878 | {"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError, |
| 1879 | FloatingPointError__doc__}, |
| 1880 | {"ValueError", &PyExc_ValueError, 0, ValueError__doc__}, |
| 1881 | {"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__}, |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 1882 | #ifdef Py_USING_UNICODE |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 1883 | {"UnicodeEncodeError", &PyExc_UnicodeEncodeError, &PyExc_UnicodeError, |
| 1884 | UnicodeEncodeError__doc__, UnicodeEncodeError_methods}, |
| 1885 | {"UnicodeDecodeError", &PyExc_UnicodeDecodeError, &PyExc_UnicodeError, |
| 1886 | UnicodeDecodeError__doc__, UnicodeDecodeError_methods}, |
| 1887 | {"UnicodeTranslateError", &PyExc_UnicodeTranslateError, &PyExc_UnicodeError, |
| 1888 | UnicodeTranslateError__doc__, UnicodeTranslateError_methods}, |
Walter Dörwald | bf73db8 | 2002-11-21 20:08:33 +0000 | [diff] [blame] | 1889 | #endif |
Fred Drake | bb9fa21 | 2001-10-05 21:50:08 +0000 | [diff] [blame] | 1890 | {"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1891 | {"SystemError", &PyExc_SystemError, 0, SystemError__doc__}, |
| 1892 | {"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__}, |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1893 | /* Warning categories */ |
| 1894 | {"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__}, |
| 1895 | {"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__}, |
| 1896 | {"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning, |
| 1897 | DeprecationWarning__doc__}, |
Neal Norwitz | d68f517 | 2002-05-29 15:54:55 +0000 | [diff] [blame] | 1898 | {"PendingDeprecationWarning", &PyExc_PendingDeprecationWarning, &PyExc_Warning, |
| 1899 | PendingDeprecationWarning__doc__}, |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1900 | {"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__}, |
Tim Peters | c885443 | 2004-08-25 02:14:08 +0000 | [diff] [blame] | 1901 | /* OverflowWarning should be removed for Python 2.5 */ |
Guido van Rossum | ae347b3 | 2001-08-23 02:56:07 +0000 | [diff] [blame] | 1902 | {"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning, |
| 1903 | OverflowWarning__doc__}, |
Guido van Rossum | d0977cd | 2000-12-15 21:58:29 +0000 | [diff] [blame] | 1904 | {"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning, |
| 1905 | RuntimeWarning__doc__}, |
Barry Warsaw | 9f00739 | 2002-08-14 15:51:29 +0000 | [diff] [blame] | 1906 | {"FutureWarning", &PyExc_FutureWarning, &PyExc_Warning, |
| 1907 | FutureWarning__doc__}, |
Thomas Wouters | 9df4e6f | 2006-04-27 23:13:20 +0000 | [diff] [blame] | 1908 | {"ImportWarning", &PyExc_ImportWarning, &PyExc_Warning, |
| 1909 | ImportWarning__doc__}, |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1910 | /* Sentinel */ |
| 1911 | {NULL} |
| 1912 | }; |
| 1913 | |
| 1914 | |
| 1915 | |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 1916 | void |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1917 | _PyExc_Init(void) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1918 | { |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 1919 | char *modulename = "exceptions"; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1920 | Py_ssize_t modnamesz = strlen(modulename); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1921 | int i; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1922 | PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1923 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1924 | me = Py_InitModule(modulename, functions); |
| 1925 | if (me == NULL) |
| 1926 | goto err; |
| 1927 | mydict = PyModule_GetDict(me); |
| 1928 | if (mydict == NULL) |
| 1929 | goto err; |
| 1930 | bltinmod = PyImport_ImportModule("__builtin__"); |
| 1931 | if (bltinmod == NULL) |
| 1932 | goto err; |
| 1933 | bdict = PyModule_GetDict(bltinmod); |
| 1934 | if (bdict == NULL) |
| 1935 | goto err; |
| 1936 | doc = PyString_FromString(module__doc__); |
| 1937 | if (doc == NULL) |
| 1938 | goto err; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1939 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1940 | i = PyDict_SetItemString(mydict, "__doc__", doc); |
Barry Warsaw | 8fcaa92 | 2000-07-01 04:45:52 +0000 | [diff] [blame] | 1941 | Py_DECREF(doc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1942 | if (i < 0) { |
| 1943 | err: |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1944 | Py_FatalError("exceptions bootstrapping error."); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1945 | return; |
| 1946 | } |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1947 | |
| 1948 | /* This is the base class of all exceptions, so make it first. */ |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1949 | if (make_BaseException(modulename) || |
| 1950 | PyDict_SetItemString(mydict, "BaseException", PyExc_BaseException) || |
| 1951 | PyDict_SetItemString(bdict, "BaseException", PyExc_BaseException)) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1952 | { |
Brett Cannon | bf36409 | 2006-03-01 04:25:17 +0000 | [diff] [blame] | 1953 | Py_FatalError("Base class `BaseException' could not be created."); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1954 | } |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 1955 | |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1956 | /* Now we can programmatically create all the remaining exceptions. |
| 1957 | * Remember to start the loop at 1 to skip Exceptions. |
| 1958 | */ |
| 1959 | for (i=1; exctable[i].name; i++) { |
| 1960 | int status; |
Thomas Wouters | 0452d1f | 2000-07-22 18:45:06 +0000 | [diff] [blame] | 1961 | char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2); |
| 1962 | PyObject *base; |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 1963 | |
| 1964 | (void)strcpy(cname, modulename); |
| 1965 | (void)strcat(cname, "."); |
| 1966 | (void)strcat(cname, exctable[i].name); |
| 1967 | |
| 1968 | if (exctable[i].base == 0) |
| 1969 | base = PyExc_StandardError; |
| 1970 | else |
| 1971 | base = *exctable[i].base; |
| 1972 | |
| 1973 | status = make_class(exctable[i].exc, base, cname, |
| 1974 | exctable[i].methods, |
| 1975 | exctable[i].docstr); |
| 1976 | |
| 1977 | PyMem_DEL(cname); |
| 1978 | |
| 1979 | if (status) |
| 1980 | Py_FatalError("Standard exception classes could not be created."); |
| 1981 | |
| 1982 | if (exctable[i].classinit) { |
| 1983 | status = (*exctable[i].classinit)(*exctable[i].exc); |
| 1984 | if (status) |
| 1985 | Py_FatalError("An exception class could not be initialized."); |
| 1986 | } |
| 1987 | |
| 1988 | /* Now insert the class into both this module and the __builtin__ |
| 1989 | * module. |
| 1990 | */ |
| 1991 | if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) || |
| 1992 | PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc)) |
| 1993 | { |
| 1994 | Py_FatalError("Module dictionary insertion problem."); |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | /* Now we need to pre-allocate a MemoryError instance */ |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1999 | args = PyTuple_New(0); |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 2000 | if (!args || |
| 2001 | !(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args))) |
| 2002 | { |
| 2003 | Py_FatalError("Cannot pre-allocate MemoryError instance\n"); |
| 2004 | } |
| 2005 | Py_DECREF(args); |
| 2006 | |
| 2007 | /* We're done with __builtin__ */ |
| 2008 | Py_DECREF(bltinmod); |
| 2009 | } |
| 2010 | |
| 2011 | |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 2012 | void |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2013 | _PyExc_Fini(void) |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 2014 | { |
| 2015 | int i; |
| 2016 | |
| 2017 | Py_XDECREF(PyExc_MemoryErrorInst); |
| 2018 | PyExc_MemoryErrorInst = NULL; |
| 2019 | |
| 2020 | for (i=0; exctable[i].name; i++) { |
Barry Warsaw | 9667ed2 | 2001-01-23 16:08:34 +0000 | [diff] [blame] | 2021 | /* clear the class's dictionary, freeing up circular references |
| 2022 | * between the class and its methods. |
| 2023 | */ |
| 2024 | PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__"); |
| 2025 | PyDict_Clear(cdict); |
| 2026 | Py_DECREF(cdict); |
| 2027 | |
| 2028 | /* Now decref the exception class */ |
Barry Warsaw | 675ac28 | 2000-05-26 19:05:16 +0000 | [diff] [blame] | 2029 | Py_XDECREF(*exctable[i].exc); |
| 2030 | *exctable[i].exc = NULL; |
| 2031 | } |
| 2032 | } |