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