Mercurial > cpython-lru_cache-weakref
comparison Python/errors.c @ 6377:4cb223f29337 legacy-trunk
New version of PyErr_NewException() that is compatible with -X option.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 03 Oct 1997 19:50:55 +0000 |
| parents | 9e82dd76288e |
| children | bb440a2975ab |
comparison
equal
deleted
inserted
replaced
| 6376:1afcac7658e5 | 6377:4cb223f29337 |
|---|---|
| 328 } | 328 } |
| 329 | 329 |
| 330 | 330 |
| 331 PyObject * | 331 PyObject * |
| 332 PyErr_NewException(name, base, dict) | 332 PyErr_NewException(name, base, dict) |
| 333 char *name; | 333 char *name; /* modulename.classname */ |
| 334 PyObject *base; | 334 PyObject *base; |
| 335 PyObject *dict; | 335 PyObject *dict; |
| 336 { | 336 { |
| 337 PyObject *nname = PyString_InternFromString(name); | 337 char *dot; |
| 338 PyObject *ndict = NULL; | 338 PyObject *modulename = NULL; |
| 339 PyObject *nbases = NULL; | 339 PyObject *classname = NULL; |
| 340 PyObject *mydict = NULL; | |
| 341 PyObject *bases = NULL; | |
| 340 PyObject *result = NULL; | 342 PyObject *result = NULL; |
| 341 if (nname == NULL) | 343 dot = strrchr(name, '.'); |
| 344 if (dot == NULL) { | |
| 345 PyErr_SetString(PyExc_SystemError, | |
| 346 "PyErr_NewException: name must be module.class"); | |
| 342 return NULL; | 347 return NULL; |
| 348 } | |
| 349 if (base == NULL) | |
| 350 base = PyExc_Exception; | |
| 351 if (!PyClass_Check(base)) { | |
| 352 /* Must be using string-based standard exceptions (-X) */ | |
| 353 return PyString_FromString(name); | |
| 354 } | |
| 343 if (dict == NULL) { | 355 if (dict == NULL) { |
| 344 dict = ndict = PyDict_New(); | 356 dict = mydict = PyDict_New(); |
| 345 if (dict == NULL) | 357 if (dict == NULL) |
| 346 goto failure; | 358 goto failure; |
| 347 } | 359 } |
| 348 if (base == NULL) | 360 if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 349 base = PyExc_Exception; | 361 modulename = PyString_FromStringAndSize(name, (int)(dot-name)); |
| 350 nbases = Py_BuildValue("(O)", base); | 362 if (modulename == NULL) |
| 351 if (nbases == NULL) | 363 goto failure; |
| 364 if (PyDict_SetItemString(dict, "__module__", modulename) != 0) | |
| 365 goto failure; | |
| 366 } | |
| 367 classname = PyString_FromString(dot+1); | |
| 368 if (classname == NULL) | |
| 352 goto failure; | 369 goto failure; |
| 353 result = PyClass_New(nbases, dict, nname); | 370 bases = Py_BuildValue("(O)", base); |
| 371 if (bases == NULL) | |
| 372 goto failure; | |
| 373 result = PyClass_New(bases, dict, classname); | |
| 354 failure: | 374 failure: |
| 355 Py_XDECREF(nbases); | 375 Py_XDECREF(bases); |
| 356 Py_XDECREF(ndict); | 376 Py_XDECREF(mydict); |
| 357 Py_XDECREF(nname); | 377 Py_XDECREF(classname); |
| 378 Py_XDECREF(modulename); | |
| 358 return result; | 379 return result; |
| 359 } | 380 } |
