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