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