📄 exceptions.c
字号:
DeprecationWarning__doc__[] =
"Base class for warnings about deprecated features.";
const static char
SyntaxWarning__doc__[] = "Base class for warnings about dubious syntax.";
const static char
OverflowWarning__doc__[] = "Base class for warnings about numeric overflow.";
const static char
RuntimeWarning__doc__[] =
"Base class for warnings about dubious runtime behavior.";
/* module global functions */
static const PyMethodDef functions[] = {
/* Sentinel */
{NULL, NULL}
};
/* Global C API defined exceptions */
/*
PyObject *PyExc_Exception;
PyObject *PyExc_StopIteration;
PyObject *PyExc_StandardError;
PyObject *PyExc_ArithmeticError;
PyObject *PyExc_LookupError;
PyObject *PyExc_AssertionError;
PyObject *PyExc_AttributeError;
PyObject *PyExc_EOFError;
PyObject *PyExc_FloatingPointError;
PyObject *PyExc_EnvironmentError;
PyObject *PyExc_IOError;
PyObject *PyExc_OSError;
PyObject *PyExc_ImportError;
PyObject *PyExc_IndexError;
PyObject *PyExc_KeyError;
PyObject *PyExc_KeyboardInterrupt;
PyObject *PyExc_MemoryError;
PyObject *PyExc_NameError;
PyObject *PyExc_OverflowError;
PyObject *PyExc_RuntimeError;
PyObject *PyExc_NotImplementedError;
PyObject *PyExc_SyntaxError;
PyObject *PyExc_IndentationError;
PyObject *PyExc_TabError;
PyObject *PyExc_ReferenceError;
PyObject *PyExc_SystemError;
PyObject *PyExc_SystemExit;
PyObject *PyExc_UnboundLocalError;
PyObject *PyExc_UnicodeError;
PyObject *PyExc_TypeError;
PyObject *PyExc_ValueError;
PyObject *PyExc_ZeroDivisionError;
#ifdef MS_WINDOWS
PyObject *PyExc_WindowsError;
#endif
*/
/* Pre-computed MemoryError instance. Best to create this as early as
* possibly and not wait until a MemoryError is actually raised!
*/
#ifndef SYMBIAN
PyObject *PyExc_MemoryErrorInst;
/* Predefined warning categories */
PyObject *PyExc_Warning;
PyObject *PyExc_UserWarning;
PyObject *PyExc_DeprecationWarning;
PyObject *PyExc_SyntaxWarning;
PyObject *PyExc_OverflowWarning;
PyObject *PyExc_RuntimeWarning;
#endif /* SYMBIAN */
/* mapping between exception names and their PyObject ** */
#ifndef SYMBIAN
static struct {
char *name;
PyObject **exc;
PyObject **base; /* NULL == PyExc_StandardError */
char *docstr;
PyMethodDef *methods;
int (*classinit)(PyObject *);
} exctable[] = {
/*
* The first three classes MUST appear in exactly this order
*/
{"Exception", &PyExc_Exception},
{"StopIteration", &PyExc_StopIteration, &PyExc_Exception,
StopIteration__doc__},
{"StandardError", &PyExc_StandardError, &PyExc_Exception,
StandardError__doc__},
{"TypeError", &PyExc_TypeError, 0, TypeError__doc__},
/*
* The rest appear in depth-first order of the hierarchy
*/
{"SystemExit", &PyExc_SystemExit, &PyExc_Exception, SystemExit__doc__,
SystemExit_methods},
{"KeyboardInterrupt", &PyExc_KeyboardInterrupt, 0, KeyboardInterrupt__doc__},
{"ImportError", &PyExc_ImportError, 0, ImportError__doc__},
{"EnvironmentError", &PyExc_EnvironmentError, 0, EnvironmentError__doc__,
EnvironmentError_methods},
{"IOError", &PyExc_IOError, &PyExc_EnvironmentError, IOError__doc__},
{"OSError", &PyExc_OSError, &PyExc_EnvironmentError, OSError__doc__},
#ifdef MS_WINDOWS
{"WindowsError", &PyExc_WindowsError, &PyExc_OSError,
WindowsError__doc__},
#endif /* MS_WINDOWS */
{"EOFError", &PyExc_EOFError, 0, EOFError__doc__},
{"RuntimeError", &PyExc_RuntimeError, 0, RuntimeError__doc__},
{"NotImplementedError", &PyExc_NotImplementedError,
&PyExc_RuntimeError, NotImplementedError__doc__},
{"NameError", &PyExc_NameError, 0, NameError__doc__},
{"UnboundLocalError", &PyExc_UnboundLocalError, &PyExc_NameError,
UnboundLocalError__doc__},
{"AttributeError", &PyExc_AttributeError, 0, AttributeError__doc__},
{"SyntaxError", &PyExc_SyntaxError, 0, SyntaxError__doc__,
SyntaxError_methods, SyntaxError__classinit__},
{"IndentationError", &PyExc_IndentationError, &PyExc_SyntaxError,
IndentationError__doc__},
{"TabError", &PyExc_TabError, &PyExc_IndentationError,
TabError__doc__},
{"AssertionError", &PyExc_AssertionError, 0, AssertionError__doc__},
{"LookupError", &PyExc_LookupError, 0, LookupError__doc__},
{"IndexError", &PyExc_IndexError, &PyExc_LookupError,
IndexError__doc__},
{"KeyError", &PyExc_KeyError, &PyExc_LookupError,
KeyError__doc__},
{"ArithmeticError", &PyExc_ArithmeticError, 0, ArithmeticError__doc__},
{"OverflowError", &PyExc_OverflowError, &PyExc_ArithmeticError,
OverflowError__doc__},
{"ZeroDivisionError", &PyExc_ZeroDivisionError, &PyExc_ArithmeticError,
ZeroDivisionError__doc__},
{"FloatingPointError", &PyExc_FloatingPointError, &PyExc_ArithmeticError,
FloatingPointError__doc__},
{"ValueError", &PyExc_ValueError, 0, ValueError__doc__},
{"UnicodeError", &PyExc_UnicodeError, &PyExc_ValueError, UnicodeError__doc__},
{"ReferenceError", &PyExc_ReferenceError, 0, ReferenceError__doc__},
{"SystemError", &PyExc_SystemError, 0, SystemError__doc__},
{"MemoryError", &PyExc_MemoryError, 0, MemoryError__doc__},
/* Warning categories */
{"Warning", &PyExc_Warning, &PyExc_Exception, Warning__doc__},
{"UserWarning", &PyExc_UserWarning, &PyExc_Warning, UserWarning__doc__},
{"DeprecationWarning", &PyExc_DeprecationWarning, &PyExc_Warning,
DeprecationWarning__doc__},
{"SyntaxWarning", &PyExc_SyntaxWarning, &PyExc_Warning, SyntaxWarning__doc__},
{"OverflowWarning", &PyExc_OverflowWarning, &PyExc_Warning,
OverflowWarning__doc__},
{"RuntimeWarning", &PyExc_RuntimeWarning, &PyExc_Warning,
RuntimeWarning__doc__},
/* Sentinel */
{NULL}
};
#else /* SYMBIAN */
// XXX:CW32 Following struct, changes in casts (char* and PyMethodDef*)
static const struct {
char *name;
PyObject **exc;
PyObject **base; /* NULL == PyExc_StandardError */
char *docstr;
PyMethodDef *methods;
int (*classinit)(PyObject *);
} c_exctable[] = {
/*
* The first three classes MUST appear in exactly this order
*/
{"Exception", NULL},
{"StopIteration", NULL, NULL,
(char *)StopIteration__doc__},
{"StandardError", NULL, NULL,
(char *)StandardError__doc__},
{"TypeError", NULL, 0, (char *)TypeError__doc__},
/*
* The rest appear in depth-first order of the hierarchy
*/
{"SystemExit", NULL, NULL, (char *)SystemExit__doc__,
(PyMethodDef *)SystemExit_methods},
{"KeyboardInterrupt", NULL, 0, (char *)KeyboardInterrupt__doc__},
{"ImportError", NULL, 0, (char *)ImportError__doc__},
{"EnvironmentError", NULL, 0, (char *)EnvironmentError__doc__,
(PyMethodDef *)EnvironmentError_methods},
{"IOError", NULL, NULL, (char *)IOError__doc__},
{"OSError", NULL, NULL, (char *)OSError__doc__},
#ifdef MS_WINDOWS
{"WindowsError", NULL, NULL,
WindowsError__doc__},
#endif /* MS_WINDOWS */
{"SymbianError", NULL, NULL, NULL},
{"EOFError", NULL, 0, (char *)EOFError__doc__},
{"RuntimeError", NULL, 0, (char *)RuntimeError__doc__},
{"NotImplementedError", NULL, NULL, (char *)NotImplementedError__doc__},
{"NameError", NULL, 0, (char *)NameError__doc__},
{"UnboundLocalError", NULL, NULL,
(char *)UnboundLocalError__doc__},
{"AttributeError", NULL, 0, (char *)AttributeError__doc__},
{"SyntaxError", NULL, 0, (char *)SyntaxError__doc__,
(PyMethodDef *)SyntaxError_methods, SyntaxError__classinit__},
{"IndentationError", NULL, NULL,
(char *)IndentationError__doc__},
{"TabError", NULL, NULL,
(char *)TabError__doc__},
{"AssertionError", NULL, 0, (char *)AssertionError__doc__},
{"LookupError", NULL, 0, (char *)LookupError__doc__},
{"IndexError", NULL, NULL,
(char *)IndexError__doc__},
{"KeyError", NULL, NULL,
(char *)KeyError__doc__},
{"ArithmeticError", NULL, 0, (char *)ArithmeticError__doc__},
{"OverflowError", NULL, NULL,
(char *)OverflowError__doc__},
{"ZeroDivisionError", NULL, NULL,
(char *)ZeroDivisionError__doc__},
{"FloatingPointError", NULL, NULL,
(char *)FloatingPointError__doc__},
{"ValueError", NULL, 0, (char *)ValueError__doc__},
{"UnicodeError", NULL, NULL, (char *)UnicodeError__doc__},
{"ReferenceError", NULL, 0, (char *)ReferenceError__doc__},
{"SystemError", NULL, 0, (char *)SystemError__doc__},
{"MemoryError", NULL, 0, (char *)MemoryError__doc__},
/* Warning categories */
{"Warning", NULL, NULL, (char *)Warning__doc__},
{"UserWarning", NULL, NULL, (char *)UserWarning__doc__},
{"DeprecationWarning", NULL, NULL,
(char *)DeprecationWarning__doc__},
{"SyntaxWarning", NULL, NULL, (char *)SyntaxWarning__doc__},
{"OverflowWarning", NULL, NULL,
(char *)OverflowWarning__doc__},
{"RuntimeWarning", NULL, NULL,
(char *)RuntimeWarning__doc__},
/* Sentinel */
{NULL}
};
/* initialize exception defs in RAM (shallow copy) */
extern int exceptions_globals_init()
{
int i;
int sz = 0;
t_exctable* pglobal_exctable;
t_exctable* temp;
SPy_Python_globals* pyglobals = PYTHON_GLOBALS;
while(c_exctable[sz].name != NULL)
sz++;
pyglobals->exctable = (t_exctable*) PyMem_Malloc((sz+1) * sizeof(t_exctable));
if (!(pyglobals->exctable))
return (-1);
for (i=0; i < (sz+1); i++) {
temp = &((pyglobals->exctable)[i]);
temp->name = c_exctable[i].name;
temp->exc = c_exctable[i].exc;
temp->base = c_exctable[i].base;
temp->docstr = NULL; /* leave out the doc strings */
temp->methods = c_exctable[i].methods;
temp->classinit = c_exctable[i].classinit;
}
pglobal_exctable = pyglobals->exctable;
pglobal_exctable[0].exc = &PyExc_Exception;
pglobal_exctable[1].exc = &PyExc_StopIteration;
pglobal_exctable[1].base = &PyExc_Exception;
pglobal_exctable[2].exc = &PyExc_StandardError;
pglobal_exctable[2].base = &PyExc_Exception;
pglobal_exctable[3].exc = &PyExc_TypeError;
pglobal_exctable[4].exc = &PyExc_SystemExit;
pglobal_exctable[4].base = &PyExc_Exception;
pglobal_exctable[5].exc = &PyExc_KeyboardInterrupt;
pglobal_exctable[6].exc = &PyExc_ImportError;
pglobal_exctable[7].exc = &PyExc_EnvironmentError;
pglobal_exctable[8].exc = &PyExc_IOError;
pglobal_exctable[8].base = &PyExc_EnvironmentError;
pglobal_exctable[9].exc = &PyExc_OSError;
pglobal_exctable[9].base = &PyExc_EnvironmentError;
pglobal_exctable[10].exc = &PyExc_SymbianError;
pglobal_exctable[10].base = &PyExc_OSError;
pglobal_exctable[11].exc = &PyExc_EOFError;
pglobal_exctable[12].exc = &PyExc_RuntimeError;
pglobal_exctable[13].exc = &PyExc_NotImplementedError;
pglobal_exctable[13].base = &PyExc_RuntimeError;
pglobal_exctable[14].exc = &PyExc_NameError;
pglobal_exctable[15].exc = &PyExc_UnboundLocalError;
pglobal_exctable[15].base = &PyExc_NameError;
pglobal_exctable[16].exc = &PyExc_AttributeError;
pglobal_exctable[17].exc = &PyExc_SyntaxError;
pglobal_exctable[18].exc = &PyExc_IndentationError;
pglobal_exctable[18].base = &PyExc_SyntaxError;
pglobal_exctable[19].exc = &PyExc_TabError;
pglobal_exctable[19].base = &PyExc_IndentationError;
pglobal_exctable[20].exc = &PyExc_AssertionError;
pglobal_exctable[21].exc = &PyExc_LookupError;
pglobal_exctable[22].exc = &PyExc_IndexError;
pglobal_exctable[22].base = &PyExc_LookupError;
pglobal_exctable[23].exc = &PyExc_KeyError;
pglobal_exctable[23].base = &PyExc_LookupError;
pglobal_exctable[24].exc = &PyExc_ArithmeticError;
pglobal_exctable[25].exc = &PyExc_OverflowError;
pglobal_exctable[25].base = &PyExc_ArithmeticError;
pglobal_exctable[26].exc = &PyExc_ZeroDivisionError;
pglobal_exctable[26].base = &PyExc_ArithmeticError;
pglobal_exctable[27].exc = &PyExc_FloatingPointError;
pglobal_exctable[27].base = &PyExc_ArithmeticError;
pglobal_exctable[28].exc = &PyExc_ValueError;
pglobal_exctable[29].exc = &PyExc_UnicodeError;
pglobal_exctable[29].base = &PyExc_ValueError;
pglobal_exctable[30].exc = &PyExc_ReferenceError;
pglobal_exctable[31].exc = &PyExc_SystemError;
pglobal_exctable[32].exc = &PyExc_MemoryError;
/* Warning categories */
pglobal_exctable[33].exc = &PyExc_Warning;
pglobal_exctable[33].base = &PyExc_Exception;
pglobal_exctable[34].exc = &PyExc_UserWarning;
pglobal_exctable[34].base = &PyExc_Warning;
pglobal_exctable[35].exc = &PyExc_DeprecationWarning;
pglobal_exctable[35].base = &PyExc_Warning;
pglobal_exctable[36].exc = &PyExc_SyntaxWarning;
pglobal_exctable[36].base = &PyExc_Warning;
pglobal_exctable[37].exc = &PyExc_OverflowWarning;
pglobal_exctable[37].base = &PyExc_Warning;
pglobal_exctable[38].exc = &PyExc_RuntimeWarning;
pglobal_exctable[38].base = &PyExc_Warning;
return 0;
}
extern void exceptions_globals_fini()
{
PyMem_Free(PYTHON_GLOBALS->exctable);
}
#define exctable (PYTHON_GLOBALS->exctable)
#endif /* SYMBIAN */
DL_EXPORT(void)
_PyExc_Init(void)
{
char *modulename = "exceptions";
int modnamesz = strlen(modulename);
int i;
PyObject *me, *mydict, *bltinmod, *bdict, *doc, *args;
me = Py_InitModule(modulename, functions);
if (me == NULL)
goto err;
mydict = PyModule_GetDict(me);
if (mydict == NULL)
goto err;
bltinmod = PyImport_ImportModule("__builtin__");
if (bltinmod == NULL)
goto err;
bdict = PyModule_GetDict(bltinmod);
if (bdict == NULL)
goto err;
doc = PyString_FromString(module__doc__);
if (doc == NULL)
goto err;
i = PyDict_SetItemString(mydict, "__doc__", doc);
Py_DECREF(doc);
if (i < 0) {
err:
Py_FatalError("exceptions bootstrapping error.");
return;
}
/* This is the base class of all exceptions, so make it first. */
if (make_Exception(modulename) ||
PyDict_SetItemString(mydict, "Exception", PyExc_Exception) ||
PyDict_SetItemString(bdict, "Exception", PyExc_Exception))
{
Py_FatalError("Base class `Exception' could not be created.");
}
/* Now we can programmatically create all the remaining exceptions.
* Remember to start the loop at 1 to skip Exceptions.
*/
for (i=1; exctable[i].name; i++) {
int status;
char *cname = PyMem_NEW(char, modnamesz+strlen(exctable[i].name)+2);
PyObject *base;
(void)strcpy(cname, modulename);
(void)strcat(cname, ".");
(void)strcat(cname, exctable[i].name);
if (exctable[i].base == 0)
base = PyExc_StandardError;
else
base = *exctable[i].base;
status = make_class(exctable[i].exc, base, cname,
exctable[i].methods,
exctable[i].docstr);
PyMem_DEL(cname);
if (status)
Py_FatalError("Standard exception classes could not be created.");
if (exctable[i].classinit) {
status = (*exctable[i].classinit)(*exctable[i].exc);
if (status)
Py_FatalError("An exception class could not be initialized.");
}
/* Now insert the class into both this module and the __builtin__
* module.
*/
if (PyDict_SetItemString(mydict, exctable[i].name, *exctable[i].exc) ||
PyDict_SetItemString(bdict, exctable[i].name, *exctable[i].exc))
{
Py_FatalError("Module dictionary insertion problem.");
}
}
/* Now we need to pre-allocate a MemoryError instance */
args = Py_BuildValue("()");
if (!args ||
!(PyExc_MemoryErrorInst = PyEval_CallObject(PyExc_MemoryError, args)))
{
Py_FatalError("Cannot pre-allocate MemoryError instance\n");
}
Py_DECREF(args);
/* We're done with __builtin__ */
Py_DECREF(bltinmod);
}
DL_EXPORT(void)
_PyExc_Fini(void)
{
int i;
Py_XDECREF(PyExc_MemoryErrorInst);
PyExc_MemoryErrorInst = NULL;
for (i=0; exctable[i].name; i++) {
/* clear the class's dictionary, freeing up circular references
* between the class and its methods.
*/
PyObject* cdict = PyObject_GetAttrString(*exctable[i].exc, "__dict__");
PyDict_Clear(cdict);
Py_DECREF(cdict);
/* Now decref the exception class */
Py_XDECREF(*exctable[i].exc);
*exctable[i].exc = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -