mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
New version of PyErr_NewException() that is compatible with -X option.
This commit is contained in:
parent
999e5e921e
commit
2ac650f385
1 changed files with 36 additions and 15 deletions
|
@ -330,30 +330,51 @@ PyErr_Format(exception, format, va_alist)
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
PyErr_NewException(name, base, dict)
|
PyErr_NewException(name, base, dict)
|
||||||
char *name;
|
char *name; /* modulename.classname */
|
||||||
PyObject *base;
|
PyObject *base;
|
||||||
PyObject *dict;
|
PyObject *dict;
|
||||||
{
|
{
|
||||||
PyObject *nname = PyString_InternFromString(name);
|
char *dot;
|
||||||
PyObject *ndict = NULL;
|
PyObject *modulename = NULL;
|
||||||
PyObject *nbases = NULL;
|
PyObject *classname = NULL;
|
||||||
|
PyObject *mydict = NULL;
|
||||||
|
PyObject *bases = NULL;
|
||||||
PyObject *result = NULL;
|
PyObject *result = NULL;
|
||||||
if (nname == NULL)
|
dot = strrchr(name, '.');
|
||||||
|
if (dot == NULL) {
|
||||||
|
PyErr_SetString(PyExc_SystemError,
|
||||||
|
"PyErr_NewException: name must be module.class");
|
||||||
return NULL;
|
return NULL;
|
||||||
if (dict == NULL) {
|
|
||||||
dict = ndict = PyDict_New();
|
|
||||||
if (dict == NULL)
|
|
||||||
goto failure;
|
|
||||||
}
|
}
|
||||||
if (base == NULL)
|
if (base == NULL)
|
||||||
base = PyExc_Exception;
|
base = PyExc_Exception;
|
||||||
nbases = Py_BuildValue("(O)", base);
|
if (!PyClass_Check(base)) {
|
||||||
if (nbases == NULL)
|
/* Must be using string-based standard exceptions (-X) */
|
||||||
|
return PyString_FromString(name);
|
||||||
|
}
|
||||||
|
if (dict == NULL) {
|
||||||
|
dict = mydict = PyDict_New();
|
||||||
|
if (dict == NULL)
|
||||||
goto failure;
|
goto failure;
|
||||||
result = PyClass_New(nbases, dict, nname);
|
}
|
||||||
|
if (PyDict_GetItemString(dict, "__module__") == NULL) {
|
||||||
|
modulename = PyString_FromStringAndSize(name, (int)(dot-name));
|
||||||
|
if (modulename == NULL)
|
||||||
|
goto failure;
|
||||||
|
if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
|
||||||
|
goto failure;
|
||||||
|
}
|
||||||
|
classname = PyString_FromString(dot+1);
|
||||||
|
if (classname == NULL)
|
||||||
|
goto failure;
|
||||||
|
bases = Py_BuildValue("(O)", base);
|
||||||
|
if (bases == NULL)
|
||||||
|
goto failure;
|
||||||
|
result = PyClass_New(bases, dict, classname);
|
||||||
failure:
|
failure:
|
||||||
Py_XDECREF(nbases);
|
Py_XDECREF(bases);
|
||||||
Py_XDECREF(ndict);
|
Py_XDECREF(mydict);
|
||||||
Py_XDECREF(nname);
|
Py_XDECREF(classname);
|
||||||
|
Py_XDECREF(modulename);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue