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