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