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