📄 exceptions.c
字号:
PyObject *item2 = NULL;
PyObject *subslice = NULL;
PyObject *rtnval = NULL;
if (!(self = get_self(args)))
return NULL;
if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
return NULL;
if (PyObject_SetAttrString(self, "args", args) ||
PyObject_SetAttrString(self, "errno", Py_None) ||
PyObject_SetAttrString(self, "strerror", Py_None) ||
PyObject_SetAttrString(self, "filename", Py_None))
{
goto finally;
}
switch (PySequence_Size(args)) {
case 3:
/* Where a function has a single filename, such as open() or some
* of the os module functions, PyErr_SetFromErrnoWithFilename() is
* called, giving a third argument which is the filename. But, so
* that old code using in-place unpacking doesn't break, e.g.:
*
* except IOError, (errno, strerror):
*
* we hack args so that it only contains two items. This also
* means we need our own __str__() which prints out the filename
* when it was supplied.
*/
item0 = PySequence_GetItem(args, 0);
item1 = PySequence_GetItem(args, 1);
item2 = PySequence_GetItem(args, 2);
if (!item0 || !item1 || !item2)
goto finally;
if (PyObject_SetAttrString(self, "errno", item0) ||
PyObject_SetAttrString(self, "strerror", item1) ||
PyObject_SetAttrString(self, "filename", item2))
{
goto finally;
}
subslice = PySequence_GetSlice(args, 0, 2);
if (!subslice || PyObject_SetAttrString(self, "args", subslice))
goto finally;
break;
case 2:
/* Used when PyErr_SetFromErrno() is called and no filename
* argument is given.
*/
item0 = PySequence_GetItem(args, 0);
item1 = PySequence_GetItem(args, 1);
if (!item0 || !item1)
goto finally;
if (PyObject_SetAttrString(self, "errno", item0) ||
PyObject_SetAttrString(self, "strerror", item1))
{
goto finally;
}
break;
case -1:
PyErr_Clear();
break;
}
Py_INCREF(Py_None);
rtnval = Py_None;
finally:
Py_DECREF(args);
Py_XDECREF(item0);
Py_XDECREF(item1);
Py_XDECREF(item2);
Py_XDECREF(subslice);
return rtnval;
}
static PyObject *
EnvironmentError__str__(PyObject *self, PyObject *args)
{
PyObject *originalself = self;
PyObject *filename;
PyObject *serrno;
PyObject *strerror;
PyObject *rtnval = NULL;
if (!PyArg_ParseTuple(args, "O:__str__", &self))
return NULL;
filename = PyObject_GetAttrString(self, "filename");
serrno = PyObject_GetAttrString(self, "errno");
strerror = PyObject_GetAttrString(self, "strerror");
if (!filename || !serrno || !strerror)
goto finally;
if (filename != Py_None) {
PyObject *fmt = PyString_FromString("[Errno %s] %s: %s");
PyObject *repr = PyObject_Repr(filename);
PyObject *tuple = PyTuple_New(3);
if (!fmt || !repr || !tuple) {
Py_XDECREF(fmt);
Py_XDECREF(repr);
Py_XDECREF(tuple);
goto finally;
}
PyTuple_SET_ITEM(tuple, 0, serrno);
PyTuple_SET_ITEM(tuple, 1, strerror);
PyTuple_SET_ITEM(tuple, 2, repr);
rtnval = PyString_Format(fmt, tuple);
Py_DECREF(fmt);
Py_DECREF(tuple);
/* already freed because tuple owned only reference */
serrno = NULL;
strerror = NULL;
}
else if (PyObject_IsTrue(serrno) && PyObject_IsTrue(strerror)) {
PyObject *fmt = PyString_FromString("[Errno %s] %s");
PyObject *tuple = PyTuple_New(2);
if (!fmt || !tuple) {
Py_XDECREF(fmt);
Py_XDECREF(tuple);
goto finally;
}
PyTuple_SET_ITEM(tuple, 0, serrno);
PyTuple_SET_ITEM(tuple, 1, strerror);
rtnval = PyString_Format(fmt, tuple);
Py_DECREF(fmt);
Py_DECREF(tuple);
/* already freed because tuple owned only reference */
serrno = NULL;
strerror = NULL;
}
else
/* The original Python code said:
*
* return StandardError.__str__(self)
*
* but there is no StandardError__str__() function; we happen to
* know that's just a pass through to Exception__str__().
*/
rtnval = Exception__str__(originalself, args);
finally:
Py_XDECREF(filename);
Py_XDECREF(serrno);
Py_XDECREF(strerror);
return rtnval;
}
const static
PyMethodDef EnvironmentError_methods[] = {
{"__init__", EnvironmentError__init__, METH_VARARGS},
{"__str__", EnvironmentError__str__, METH_VARARGS},
{NULL, NULL}
};
const static char
IOError__doc__[] = "I/O operation failed.";
const static char
OSError__doc__[] = "OS system call failed.";
#ifdef MS_WINDOWS
const static char
WindowsError__doc__[] = "MS-Windows OS system call failed.";
#endif /* MS_WINDOWS */
const static char
EOFError__doc__[] = "Read beyond end of file.";
const static char
RuntimeError__doc__[] = "Unspecified run-time error.";
const static char
NotImplementedError__doc__[] =
"Method or function hasn't been implemented yet.";
const static char
NameError__doc__[] = "Name not found globally.";
const static char
UnboundLocalError__doc__[] =
"Local name referenced but not bound to a value.";
const static char
AttributeError__doc__[] = "Attribute not found.";
const static char
SyntaxError__doc__[] = "Invalid syntax.";
static int
SyntaxError__classinit__(PyObject *klass)
{
int retval = 0;
PyObject *emptystring = PyString_FromString("");
/* Additional class-creation time initializations */
if (!emptystring ||
PyObject_SetAttrString(klass, "msg", emptystring) ||
PyObject_SetAttrString(klass, "filename", Py_None) ||
PyObject_SetAttrString(klass, "lineno", Py_None) ||
PyObject_SetAttrString(klass, "offset", Py_None) ||
PyObject_SetAttrString(klass, "text", Py_None) ||
PyObject_SetAttrString(klass, "print_file_and_line", Py_None))
{
retval = -1;
}
Py_XDECREF(emptystring);
return retval;
}
static PyObject *
SyntaxError__init__(PyObject *self, PyObject *args)
{
PyObject *rtnval = NULL;
int lenargs;
if (!(self = get_self(args)))
return NULL;
if (!(args = PySequence_GetSlice(args, 1, PySequence_Size(args))))
return NULL;
if (PyObject_SetAttrString(self, "args", args))
goto finally;
lenargs = PySequence_Size(args);
if (lenargs >= 1) {
PyObject *item0 = PySequence_GetItem(args, 0);
int status;
if (!item0)
goto finally;
status = PyObject_SetAttrString(self, "msg", item0);
Py_DECREF(item0);
if (status)
goto finally;
}
if (lenargs == 2) {
PyObject *info = PySequence_GetItem(args, 1);
PyObject *filename = NULL, *lineno = NULL;
PyObject *offset = NULL, *text = NULL;
int status = 1;
if (!info)
goto finally;
filename = PySequence_GetItem(info, 0);
if (filename != NULL) {
lineno = PySequence_GetItem(info, 1);
if (lineno != NULL) {
offset = PySequence_GetItem(info, 2);
if (offset != NULL) {
text = PySequence_GetItem(info, 3);
if (text != NULL) {
status =
PyObject_SetAttrString(self, "filename", filename)
|| PyObject_SetAttrString(self, "lineno", lineno)
|| PyObject_SetAttrString(self, "offset", offset)
|| PyObject_SetAttrString(self, "text", text);
Py_DECREF(text);
}
Py_DECREF(offset);
}
Py_DECREF(lineno);
}
Py_DECREF(filename);
}
Py_DECREF(info);
if (status)
goto finally;
}
Py_INCREF(Py_None);
rtnval = Py_None;
finally:
Py_DECREF(args);
return rtnval;
}
/* This is called "my_basename" instead of just "basename" to avoid name
conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
defined, and Python does define that. */
static char *
my_basename(char *name)
{
char *cp = name;
char *result = name;
if (name == NULL)
return "???";
while (*cp != '\0') {
if (*cp == SEP)
result = cp + 1;
++cp;
}
return result;
}
static PyObject *
SyntaxError__str__(PyObject *self, PyObject *args)
{
PyObject *msg;
PyObject *str;
PyObject *filename, *lineno, *result;
if (!PyArg_ParseTuple(args, "O:__str__", &self))
return NULL;
if (!(msg = PyObject_GetAttrString(self, "msg")))
return NULL;
str = PyObject_Str(msg);
Py_DECREF(msg);
result = str;
/* XXX -- do all the additional formatting with filename and
lineno here */
if (PyString_Check(str)) {
int have_filename = 0;
int have_lineno = 0;
char *buffer = NULL;
if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
have_filename = PyString_Check(filename);
else
PyErr_Clear();
if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
have_lineno = PyInt_Check(lineno);
else
PyErr_Clear();
if (have_filename || have_lineno) {
int bufsize = PyString_GET_SIZE(str) + 64;
if (have_filename)
bufsize += PyString_GET_SIZE(filename);
buffer = PyMem_MALLOC(bufsize);
if (buffer != NULL) {
if (have_filename && have_lineno)
PyOS_snprintf(buffer, bufsize, "%s (%s, line %ld)",
PyString_AS_STRING(str),
my_basename(PyString_AS_STRING(filename)),
PyInt_AsLong(lineno));
else if (have_filename)
PyOS_snprintf(buffer, bufsize, "%s (%s)",
PyString_AS_STRING(str),
my_basename(PyString_AS_STRING(filename)));
else if (have_lineno)
PyOS_snprintf(buffer, bufsize, "%s (line %ld)",
PyString_AS_STRING(str),
PyInt_AsLong(lineno));
result = PyString_FromString(buffer);
PyMem_FREE(buffer);
if (result == NULL)
result = str;
else
Py_DECREF(str);
}
}
Py_XDECREF(filename);
Py_XDECREF(lineno);
}
return result;
}
const static PyMethodDef SyntaxError_methods[] = {
{"__init__", SyntaxError__init__, METH_VARARGS},
{"__str__", SyntaxError__str__, METH_VARARGS},
{NULL, NULL}
};
/* Exception doc strings */
const static char
AssertionError__doc__[] = "Assertion failed.";
const static char
LookupError__doc__[] = "Base class for lookup errors.";
const static char
IndexError__doc__[] = "Sequence index out of range.";
const static char
KeyError__doc__[] = "Mapping key not found.";
const static char
ArithmeticError__doc__[] = "Base class for arithmetic errors.";
const static char
OverflowError__doc__[] = "Result too large to be represented.";
const static char
ZeroDivisionError__doc__[] =
"Second argument to a division or modulo operation was zero.";
const static char
FloatingPointError__doc__[] = "Floating point operation failed.";
const static char
ValueError__doc__[] = "Inappropriate argument value (of correct type).";
const static char
UnicodeError__doc__[] = "Unicode related error.";
const static char
SystemError__doc__[] = "Internal error in the Python interpreter.\n\
\n\
Please report this to the Python maintainer, along with the traceback,\n\
the Python version, and the hardware/OS platform and version.";
const static char
ReferenceError__doc__[] = "Weak ref proxy used after referent went away.";
const static char
MemoryError__doc__[] = "Out of memory.";
const static char
IndentationError__doc__[] = "Improper indentation.";
const static char
TabError__doc__[] = "Improper mixture of spaces and tabs.";
/* Warning category docstrings */
const static char
Warning__doc__[] = "Base class for warning categories.";
const static char
UserWarning__doc__[] = "Base class for warnings generated by user code.";
const static char
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -