summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Objects/exceptions.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/tools/python3/Objects/exceptions.c')
-rw-r--r--contrib/tools/python3/Objects/exceptions.c320
1 files changed, 197 insertions, 123 deletions
diff --git a/contrib/tools/python3/Objects/exceptions.c b/contrib/tools/python3/Objects/exceptions.c
index c579563db75..1eb2a3bbe24 100644
--- a/contrib/tools/python3/Objects/exceptions.c
+++ b/contrib/tools/python3/Objects/exceptions.c
@@ -4,15 +4,16 @@
* Thanks go to Tim Peters and Michael Hudson for debugging.
*/
-#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdbool.h>
+#include "pycore_abstract.h" // _PyObject_RealIsSubclass()
#include "pycore_ceval.h" // _Py_EnterRecursiveCall
-#include "pycore_pyerrors.h" // struct _PyErr_SetRaisedException
#include "pycore_exceptions.h" // struct _Py_exc_state
#include "pycore_initconfig.h"
+#include "pycore_modsupport.h" // _PyArg_NoKeywords()
#include "pycore_object.h"
-#include "structmember.h" // PyMemberDef
+#include "pycore_pyerrors.h" // struct _PyErr_SetRaisedException
+
#include "osdefs.h" // SEP
@@ -208,7 +209,7 @@ BaseException_add_note(PyObject *self, PyObject *note)
}
PyObject *notes;
- if (_PyObject_LookupAttr(self, &_Py_ID(__notes__), &notes) < 0) {
+ if (PyObject_GetOptionalAttr(self, &_Py_ID(__notes__), &notes) < 0) {
return NULL;
}
if (notes == NULL) {
@@ -439,7 +440,7 @@ PyExceptionClass_Name(PyObject *ob)
}
static struct PyMemberDef BaseException_members[] = {
- {"__suppress_context__", T_BOOL,
+ {"__suppress_context__", Py_T_BOOL,
offsetof(PyBaseExceptionObject, suppress_context)},
{NULL}
};
@@ -509,10 +510,10 @@ static PyTypeObject _PyExc_ ## EXCNAME = { \
}; \
PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
-#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
-static PyTypeObject _PyExc_ ## EXCNAME = { \
+#define MiddlingExtendsExceptionEx(EXCBASE, EXCNAME, PYEXCNAME, EXCSTORE, EXCDOC) \
+PyTypeObject _PyExc_ ## EXCNAME = { \
PyVarObject_HEAD_INIT(NULL, 0) \
- # EXCNAME, \
+ # PYEXCNAME, \
sizeof(Py ## EXCSTORE ## Object), \
0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, \
@@ -521,18 +522,22 @@ static PyTypeObject _PyExc_ ## EXCNAME = { \
(inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
(initproc)EXCSTORE ## _init, 0, 0, \
-}; \
-PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
+};
+
+#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
+ static MiddlingExtendsExceptionEx( \
+ EXCBASE, EXCNAME, EXCNAME, EXCSTORE, EXCDOC); \
+ PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCNEW, \
EXCMETHODS, EXCMEMBERS, EXCGETSET, \
- EXCSTR, EXCDOC) \
+ EXCSTR, EXCREPR, EXCDOC) \
static PyTypeObject _PyExc_ ## EXCNAME = { \
PyVarObject_HEAD_INIT(NULL, 0) \
# EXCNAME, \
sizeof(Py ## EXCSTORE ## Object), 0, \
- (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
- (reprfunc)EXCSTR, 0, 0, 0, \
+ (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, (reprfunc)EXCREPR, 0, 0, 0, \
+ 0, 0, (reprfunc)EXCSTR, 0, 0, 0, \
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
(inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
@@ -569,7 +574,7 @@ SimpleExtendsException(PyExc_Exception, StopAsyncIteration,
*/
static PyMemberDef StopIteration_members[] = {
- {"value", T_OBJECT, offsetof(PyStopIterationObject, value), 0,
+ {"value", _Py_T_OBJECT, offsetof(PyStopIterationObject, value), 0,
PyDoc_STR("generator return value")},
{NULL} /* Sentinel */
};
@@ -614,7 +619,7 @@ StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg)
}
ComplexExtendsException(PyExc_Exception, StopIteration, StopIteration,
- 0, 0, StopIteration_members, 0, 0,
+ 0, 0, StopIteration_members, 0, 0, 0,
"Signal the end from iterator.__next__().");
@@ -671,13 +676,13 @@ SystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
}
static PyMemberDef SystemExit_members[] = {
- {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
+ {"code", _Py_T_OBJECT, offsetof(PySystemExitObject, code), 0,
PyDoc_STR("exception code")},
{NULL} /* Sentinel */
};
ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
- 0, 0, SystemExit_members, 0, 0,
+ 0, 0, SystemExit_members, 0, 0, 0,
"Request to exit from the interpreter.");
/*
@@ -702,6 +707,7 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
PyObject *message = NULL;
PyObject *exceptions = NULL;
+ PyObject *exceptions_str = NULL;
if (!PyArg_ParseTuple(args,
"UO:BaseExceptionGroup.__new__",
@@ -717,6 +723,18 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL;
}
+ /* Save initial exceptions sequence as a string in case sequence is mutated */
+ if (!PyList_Check(exceptions) && !PyTuple_Check(exceptions)) {
+ exceptions_str = PyObject_Repr(exceptions);
+ if (exceptions_str == NULL) {
+ /* We don't hold a reference to exceptions, so clear it before
+ * attempting a decref in the cleanup.
+ */
+ exceptions = NULL;
+ goto error;
+ }
+ }
+
exceptions = PySequence_Tuple(exceptions);
if (!exceptions) {
return NULL;
@@ -741,7 +759,7 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (!PyExceptionInstance_Check(exc)) {
PyErr_Format(
PyExc_ValueError,
- "Item %d of second argument (exceptions) is not an exception",
+ "Item %zd of second argument (exceptions) is not an exception",
i);
goto error;
}
@@ -800,9 +818,11 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self->msg = Py_NewRef(message);
self->excs = exceptions;
+ self->excs_str = exceptions_str;
return (PyObject*)self;
error:
- Py_DECREF(exceptions);
+ Py_XDECREF(exceptions);
+ Py_XDECREF(exceptions_str);
return NULL;
}
@@ -841,6 +861,7 @@ BaseExceptionGroup_clear(PyBaseExceptionGroupObject *self)
{
Py_CLEAR(self->msg);
Py_CLEAR(self->excs);
+ Py_CLEAR(self->excs_str);
return BaseException_clear((PyBaseExceptionObject *)self);
}
@@ -858,6 +879,7 @@ BaseExceptionGroup_traverse(PyBaseExceptionGroupObject *self,
{
Py_VISIT(self->msg);
Py_VISIT(self->excs);
+ Py_VISIT(self->excs_str);
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
}
@@ -875,13 +897,56 @@ BaseExceptionGroup_str(PyBaseExceptionGroupObject *self)
}
static PyObject *
-BaseExceptionGroup_derive(PyObject *self_, PyObject *args)
+BaseExceptionGroup_repr(PyBaseExceptionGroupObject *self)
{
- PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_cast(self_);
- PyObject *excs = NULL;
- if (!PyArg_ParseTuple(args, "O", &excs)) {
- return NULL;
+ assert(self->msg);
+
+ PyObject *exceptions_str = NULL;
+
+ /* Use the saved exceptions string for custom sequences. */
+ if (self->excs_str) {
+ exceptions_str = Py_NewRef(self->excs_str);
+ }
+ else {
+ assert(self->excs);
+
+ /* Older versions delegated to BaseException, inserting the current
+ * value of self.args[1]; but this can be mutable and go out-of-sync
+ * with self.exceptions. Instead, use self.exceptions for accuracy,
+ * making it look like self.args[1] for backwards compatibility. */
+ if (PyList_Check(PyTuple_GET_ITEM(self->args, 1))) {
+ PyObject *exceptions_list = PySequence_List(self->excs);
+ if (!exceptions_list) {
+ return NULL;
+ }
+
+ exceptions_str = PyObject_Repr(exceptions_list);
+ Py_DECREF(exceptions_list);
+ }
+ else {
+ exceptions_str = PyObject_Repr(self->excs);
+ }
+
+ if (!exceptions_str) {
+ return NULL;
+ }
}
+
+ assert(exceptions_str != NULL);
+
+ const char *name = _PyType_Name(Py_TYPE(self));
+ PyObject *repr = PyUnicode_FromFormat(
+ "%s(%R, %U)", name,
+ self->msg, exceptions_str);
+
+ Py_DECREF(exceptions_str);
+ return repr;
+}
+
+static PyObject *
+BaseExceptionGroup_derive(PyObject *self_, PyObject *excs)
+{
+ PyBaseExceptionGroupObject *self = _PyBaseExceptionGroupObject_cast(self_);
PyObject *init_args = PyTuple_Pack(2, self->msg, excs);
if (!init_args) {
return NULL;
@@ -941,7 +1006,7 @@ exceptiongroup_subset(
PyException_SetCause(eg, PyException_GetCause(orig));
PyObject *notes;
- if (_PyObject_LookupAttr(orig, &_Py_ID(__notes__), &notes) < 0) {
+ if (PyObject_GetOptionalAttr(orig, &_Py_ID(__notes__), &notes) < 0) {
goto error;
}
if (notes) {
@@ -992,7 +1057,7 @@ get_matcher_type(PyObject *value,
{
assert(value);
- if (PyFunction_Check(value)) {
+ if (PyCallable_Check(value) && !PyType_Check(value)) {
*type = EXCEPTION_GROUP_MATCH_BY_PREDICATE;
return 0;
}
@@ -1016,7 +1081,7 @@ get_matcher_type(PyObject *value,
error:
PyErr_SetString(
PyExc_TypeError,
- "expected a function, exception type or tuple of exception types");
+ "expected an exception type, a tuple of exception types, or a callable (other than a class)");
return -1;
}
@@ -1032,7 +1097,7 @@ exceptiongroup_split_check_match(PyObject *exc,
return PyErr_GivenExceptionMatches(exc, matcher_value);
}
case EXCEPTION_GROUP_MATCH_BY_PREDICATE: {
- assert(PyFunction_Check(matcher_value));
+ assert(PyCallable_Check(matcher_value) && !PyType_Check(matcher_value));
PyObject *exc_matches = PyObject_CallOneArg(matcher_value, exc);
if (exc_matches == NULL) {
return -1;
@@ -1175,13 +1240,8 @@ done:
}
static PyObject *
-BaseExceptionGroup_split(PyObject *self, PyObject *args)
+BaseExceptionGroup_split(PyObject *self, PyObject *matcher_value)
{
- PyObject *matcher_value = NULL;
- if (!PyArg_UnpackTuple(args, "split", 1, 1, &matcher_value)) {
- return NULL;
- }
-
_exceptiongroup_split_matcher_type matcher_type;
if (get_matcher_type(matcher_value, &matcher_type) < 0) {
return NULL;
@@ -1206,13 +1266,8 @@ BaseExceptionGroup_split(PyObject *self, PyObject *args)
}
static PyObject *
-BaseExceptionGroup_subgroup(PyObject *self, PyObject *args)
+BaseExceptionGroup_subgroup(PyObject *self, PyObject *matcher_value)
{
- PyObject *matcher_value = NULL;
- if (!PyArg_UnpackTuple(args, "subgroup", 1, 1, &matcher_value)) {
- return NULL;
- }
-
_exceptiongroup_split_matcher_type matcher_type;
if (get_matcher_type(matcher_value, &matcher_type) < 0) {
return NULL;
@@ -1457,13 +1512,13 @@ PyUnstable_Exc_PrepReraiseStar(PyObject *orig, PyObject *excs)
PyObject *exc = PyList_GET_ITEM(excs, i);
if (exc == NULL || !(PyExceptionInstance_Check(exc) || Py_IsNone(exc))) {
PyErr_Format(PyExc_TypeError,
- "item %d of excs is not an exception", i);
+ "item %zd of excs is not an exception", i);
return NULL;
}
}
/* Make sure that orig has something as traceback, in the interpreter
- * it always does becuase it's a raised exception.
+ * it always does because it's a raised exception.
*/
PyObject *tb = PyException_GetTraceback(orig);
@@ -1477,9 +1532,9 @@ PyUnstable_Exc_PrepReraiseStar(PyObject *orig, PyObject *excs)
}
static PyMemberDef BaseExceptionGroup_members[] = {
- {"message", T_OBJECT, offsetof(PyBaseExceptionGroupObject, msg), READONLY,
+ {"message", _Py_T_OBJECT, offsetof(PyBaseExceptionGroupObject, msg), Py_READONLY,
PyDoc_STR("exception message")},
- {"exceptions", T_OBJECT, offsetof(PyBaseExceptionGroupObject, excs), READONLY,
+ {"exceptions", _Py_T_OBJECT, offsetof(PyBaseExceptionGroupObject, excs), Py_READONLY,
PyDoc_STR("nested exceptions")},
{NULL} /* Sentinel */
};
@@ -1487,16 +1542,16 @@ static PyMemberDef BaseExceptionGroup_members[] = {
static PyMethodDef BaseExceptionGroup_methods[] = {
{"__class_getitem__", (PyCFunction)Py_GenericAlias,
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
- {"derive", (PyCFunction)BaseExceptionGroup_derive, METH_VARARGS},
- {"split", (PyCFunction)BaseExceptionGroup_split, METH_VARARGS},
- {"subgroup", (PyCFunction)BaseExceptionGroup_subgroup, METH_VARARGS},
+ {"derive", (PyCFunction)BaseExceptionGroup_derive, METH_O},
+ {"split", (PyCFunction)BaseExceptionGroup_split, METH_O},
+ {"subgroup", (PyCFunction)BaseExceptionGroup_subgroup, METH_O},
{NULL}
};
ComplexExtendsException(PyExc_BaseException, BaseExceptionGroup,
BaseExceptionGroup, BaseExceptionGroup_new /* new */,
BaseExceptionGroup_methods, BaseExceptionGroup_members,
- 0 /* getset */, BaseExceptionGroup_str,
+ 0 /* getset */, BaseExceptionGroup_str, BaseExceptionGroup_repr,
"A combination of multiple unrelated exceptions.");
/*
@@ -1654,13 +1709,13 @@ ImportError_reduce(PyImportErrorObject *self, PyObject *Py_UNUSED(ignored))
}
static PyMemberDef ImportError_members[] = {
- {"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
+ {"msg", _Py_T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
PyDoc_STR("exception message")},
- {"name", T_OBJECT, offsetof(PyImportErrorObject, name), 0,
+ {"name", _Py_T_OBJECT, offsetof(PyImportErrorObject, name), 0,
PyDoc_STR("module name")},
- {"path", T_OBJECT, offsetof(PyImportErrorObject, path), 0,
+ {"path", _Py_T_OBJECT, offsetof(PyImportErrorObject, path), 0,
PyDoc_STR("module path")},
- {"name_from", T_OBJECT, offsetof(PyImportErrorObject, name_from), 0,
+ {"name_from", _Py_T_OBJECT, offsetof(PyImportErrorObject, name_from), 0,
PyDoc_STR("name imported from module")},
{NULL} /* Sentinel */
};
@@ -1673,7 +1728,7 @@ static PyMethodDef ImportError_methods[] = {
ComplexExtendsException(PyExc_Exception, ImportError,
ImportError, 0 /* new */,
ImportError_methods, ImportError_members,
- 0 /* getset */, ImportError_str,
+ 0 /* getset */, ImportError_str, 0,
"Import can't find module, or can't find name in "
"module.");
@@ -2103,16 +2158,16 @@ OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
}
static PyMemberDef OSError_members[] = {
- {"errno", T_OBJECT, offsetof(PyOSErrorObject, myerrno), 0,
+ {"errno", _Py_T_OBJECT, offsetof(PyOSErrorObject, myerrno), 0,
PyDoc_STR("POSIX exception code")},
- {"strerror", T_OBJECT, offsetof(PyOSErrorObject, strerror), 0,
+ {"strerror", _Py_T_OBJECT, offsetof(PyOSErrorObject, strerror), 0,
PyDoc_STR("exception strerror")},
- {"filename", T_OBJECT, offsetof(PyOSErrorObject, filename), 0,
+ {"filename", _Py_T_OBJECT, offsetof(PyOSErrorObject, filename), 0,
PyDoc_STR("exception filename")},
- {"filename2", T_OBJECT, offsetof(PyOSErrorObject, filename2), 0,
+ {"filename2", _Py_T_OBJECT, offsetof(PyOSErrorObject, filename2), 0,
PyDoc_STR("second exception filename")},
#ifdef MS_WINDOWS
- {"winerror", T_OBJECT, offsetof(PyOSErrorObject, winerror), 0,
+ {"winerror", _Py_T_OBJECT, offsetof(PyOSErrorObject, winerror), 0,
PyDoc_STR("Win32 exception code")},
#endif
{NULL} /* Sentinel */
@@ -2133,7 +2188,7 @@ static PyGetSetDef OSError_getset[] = {
ComplexExtendsException(PyExc_Exception, OSError,
OSError, OSError_new,
OSError_methods, OSError_members, OSError_getset,
- OSError_str,
+ OSError_str, 0,
"Base class for I/O related errors.");
@@ -2190,6 +2245,10 @@ SimpleExtendsException(PyExc_Exception, RuntimeError,
SimpleExtendsException(PyExc_RuntimeError, RecursionError,
"Recursion limit exceeded.");
+// PythonFinalizationError extends RuntimeError
+SimpleExtendsException(PyExc_RuntimeError, PythonFinalizationError,
+ "Operation blocked during Python finalization.");
+
/*
* NotImplementedError extends RuntimeError
*/
@@ -2249,7 +2308,7 @@ NameError_traverse(PyNameErrorObject *self, visitproc visit, void *arg)
}
static PyMemberDef NameError_members[] = {
- {"name", T_OBJECT, offsetof(PyNameErrorObject, name), 0, PyDoc_STR("name")},
+ {"name", _Py_T_OBJECT, offsetof(PyNameErrorObject, name), 0, PyDoc_STR("name")},
{NULL} /* Sentinel */
};
@@ -2260,7 +2319,7 @@ static PyMethodDef NameError_methods[] = {
ComplexExtendsException(PyExc_Exception, NameError,
NameError, 0,
NameError_methods, NameError_members,
- 0, BaseException_str, "Name not found globally.");
+ 0, BaseException_str, 0, "Name not found globally.");
/*
* UnboundLocalError extends NameError
@@ -2368,8 +2427,8 @@ AttributeError_reduce(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignored)
}
static PyMemberDef AttributeError_members[] = {
- {"name", T_OBJECT, offsetof(PyAttributeErrorObject, name), 0, PyDoc_STR("attribute name")},
- {"obj", T_OBJECT, offsetof(PyAttributeErrorObject, obj), 0, PyDoc_STR("object")},
+ {"name", _Py_T_OBJECT, offsetof(PyAttributeErrorObject, name), 0, PyDoc_STR("attribute name")},
+ {"obj", _Py_T_OBJECT, offsetof(PyAttributeErrorObject, obj), 0, PyDoc_STR("object")},
{NULL} /* Sentinel */
};
@@ -2382,7 +2441,7 @@ static PyMethodDef AttributeError_methods[] = {
ComplexExtendsException(PyExc_Exception, AttributeError,
AttributeError, 0,
AttributeError_methods, AttributeError_members,
- 0, BaseException_str, "Attribute not found.");
+ 0, BaseException_str, 0, "Attribute not found.");
/*
* SyntaxError extends Exception
@@ -2407,22 +2466,23 @@ SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
return -1;
}
- self->end_lineno = NULL;
- self->end_offset = NULL;
+ PyObject *filename, *lineno, *offset, *text;
+ PyObject *end_lineno = NULL;
+ PyObject *end_offset = NULL;
if (!PyArg_ParseTuple(info, "OOOO|OO",
- &self->filename, &self->lineno,
- &self->offset, &self->text,
- &self->end_lineno, &self->end_offset)) {
+ &filename, &lineno,
+ &offset, &text,
+ &end_lineno, &end_offset)) {
Py_DECREF(info);
return -1;
}
- Py_INCREF(self->filename);
- Py_INCREF(self->lineno);
- Py_INCREF(self->offset);
- Py_INCREF(self->text);
- Py_XINCREF(self->end_lineno);
- Py_XINCREF(self->end_offset);
+ Py_XSETREF(self->filename, Py_NewRef(filename));
+ Py_XSETREF(self->lineno, Py_NewRef(lineno));
+ Py_XSETREF(self->offset, Py_NewRef(offset));
+ Py_XSETREF(self->text, Py_NewRef(text));
+ Py_XSETREF(self->end_lineno, Py_XNewRef(end_lineno));
+ Py_XSETREF(self->end_offset, Py_XNewRef(end_offset));
Py_DECREF(info);
if (self->end_lineno != NULL && self->end_offset == NULL) {
@@ -2479,8 +2539,6 @@ my_basename(PyObject *name)
int kind;
const void *data;
- if (PyUnicode_READY(name))
- return NULL;
kind = PyUnicode_KIND(name);
data = PyUnicode_DATA(name);
size = PyUnicode_GET_LENGTH(name);
@@ -2543,21 +2601,21 @@ SyntaxError_str(PySyntaxErrorObject *self)
}
static PyMemberDef SyntaxError_members[] = {
- {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
+ {"msg", _Py_T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
PyDoc_STR("exception msg")},
- {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
+ {"filename", _Py_T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
PyDoc_STR("exception filename")},
- {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
+ {"lineno", _Py_T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
PyDoc_STR("exception lineno")},
- {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
+ {"offset", _Py_T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
PyDoc_STR("exception offset")},
- {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
+ {"text", _Py_T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
PyDoc_STR("exception text")},
- {"end_lineno", T_OBJECT, offsetof(PySyntaxErrorObject, end_lineno), 0,
+ {"end_lineno", _Py_T_OBJECT, offsetof(PySyntaxErrorObject, end_lineno), 0,
PyDoc_STR("exception end lineno")},
- {"end_offset", T_OBJECT, offsetof(PySyntaxErrorObject, end_offset), 0,
+ {"end_offset", _Py_T_OBJECT, offsetof(PySyntaxErrorObject, end_offset), 0,
PyDoc_STR("exception end offset")},
- {"print_file_and_line", T_OBJECT,
+ {"print_file_and_line", _Py_T_OBJECT,
offsetof(PySyntaxErrorObject, print_file_and_line), 0,
PyDoc_STR("exception print_file_and_line")},
{NULL} /* Sentinel */
@@ -2565,7 +2623,7 @@ static PyMemberDef SyntaxError_members[] = {
ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
0, 0, SyntaxError_members, 0,
- SyntaxError_str, "Invalid syntax.");
+ SyntaxError_str, 0, "Invalid syntax.");
/*
@@ -2581,6 +2639,11 @@ MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
MiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
"Improper mixture of spaces and tabs.");
+/*
+ * IncompleteInputError extends SyntaxError
+ */
+MiddlingExtendsExceptionEx(PyExc_SyntaxError, IncompleteInputError, _IncompleteInputError,
+ SyntaxError, "incomplete input.");
/*
* LookupError extends Exception
@@ -2618,7 +2681,7 @@ KeyError_str(PyBaseExceptionObject *self)
}
ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
- 0, 0, 0, 0, KeyError_str, "Mapping key not found.");
+ 0, 0, 0, 0, KeyError_str, 0, "Mapping key not found.");
/*
@@ -2912,15 +2975,15 @@ UnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
}
static PyMemberDef UnicodeError_members[] = {
- {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
+ {"encoding", _Py_T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
PyDoc_STR("exception encoding")},
- {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
+ {"object", _Py_T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
PyDoc_STR("exception object")},
- {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
+ {"start", Py_T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
PyDoc_STR("exception start")},
- {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
+ {"end", Py_T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
PyDoc_STR("exception end")},
- {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
+ {"reason", _Py_T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
PyDoc_STR("exception reason")},
{NULL} /* Sentinel */
};
@@ -3315,36 +3378,43 @@ SimpleExtendsException(PyExc_Exception, ReferenceError,
#define MEMERRORS_SAVE 16
+#ifdef Py_GIL_DISABLED
+# define MEMERRORS_LOCK(state) PyMutex_LockFlags(&state->memerrors_lock, _Py_LOCK_DONT_DETACH)
+# define MEMERRORS_UNLOCK(state) PyMutex_Unlock(&state->memerrors_lock)
+#else
+# define MEMERRORS_LOCK(state) ((void)0)
+# define MEMERRORS_UNLOCK(state) ((void)0)
+#endif
+
static PyObject *
get_memory_error(int allow_allocation, PyObject *args, PyObject *kwds)
{
- PyBaseExceptionObject *self;
+ PyBaseExceptionObject *self = NULL;
struct _Py_exc_state *state = get_exc_state();
- if (state->memerrors_freelist == NULL) {
- if (!allow_allocation) {
- PyInterpreterState *interp = _PyInterpreterState_GET();
- return Py_NewRef(
- &_Py_INTERP_SINGLETON(interp, last_resort_memory_error));
- }
- PyObject *result = BaseException_new((PyTypeObject *)PyExc_MemoryError, args, kwds);
- return result;
- }
- /* Fetch object from freelist and revive it */
- self = state->memerrors_freelist;
- self->args = PyTuple_New(0);
- /* This shouldn't happen since the empty tuple is persistent */
+ MEMERRORS_LOCK(state);
+ if (state->memerrors_freelist != NULL) {
+ /* Fetch MemoryError from freelist and initialize it */
+ self = state->memerrors_freelist;
+ state->memerrors_freelist = (PyBaseExceptionObject *) self->dict;
+ state->memerrors_numfree--;
+ self->dict = NULL;
+ self->args = (PyObject *)&_Py_SINGLETON(tuple_empty);
+ _Py_NewReference((PyObject *)self);
+ _PyObject_GC_TRACK(self);
+ }
+ MEMERRORS_UNLOCK(state);
- if (self->args == NULL) {
- return NULL;
+ if (self != NULL) {
+ return (PyObject *)self;
}
- state->memerrors_freelist = (PyBaseExceptionObject *) self->dict;
- state->memerrors_numfree--;
- self->dict = NULL;
- _Py_NewReference((PyObject *)self);
- _PyObject_GC_TRACK(self);
- return (PyObject *)self;
+ if (!allow_allocation) {
+ PyInterpreterState *interp = _PyInterpreterState_GET();
+ return Py_NewRef(
+ &_Py_INTERP_SINGLETON(interp, last_resort_memory_error));
+ }
+ return BaseException_new((PyTypeObject *)PyExc_MemoryError, args, kwds);
}
static PyObject *
@@ -3389,14 +3459,17 @@ MemoryError_dealloc(PyBaseExceptionObject *self)
}
struct _Py_exc_state *state = get_exc_state();
- if (state->memerrors_numfree >= MEMERRORS_SAVE) {
- Py_TYPE(self)->tp_free((PyObject *)self);
- }
- else {
+ MEMERRORS_LOCK(state);
+ if (state->memerrors_numfree < MEMERRORS_SAVE) {
self->dict = (PyObject *) state->memerrors_freelist;
state->memerrors_freelist = self;
state->memerrors_numfree++;
+ MEMERRORS_UNLOCK(state);
+ return;
}
+ MEMERRORS_UNLOCK(state);
+
+ Py_TYPE(self)->tp_free((PyObject *)self);
}
static int
@@ -3570,7 +3643,6 @@ SimpleExtendsException(PyExc_Warning, ResourceWarning,
#undef EOPNOTSUPP
#undef EPROTONOSUPPORT
#undef EPROTOTYPE
-#undef ETIMEDOUT
#undef EWOULDBLOCK
#if defined(WSAEALREADY) && !defined(EALREADY)
@@ -3591,9 +3663,6 @@ SimpleExtendsException(PyExc_Warning, ResourceWarning,
#if defined(WSAESHUTDOWN) && !defined(ESHUTDOWN)
#define ESHUTDOWN WSAESHUTDOWN
#endif
-#if defined(WSAETIMEDOUT) && !defined(ETIMEDOUT)
-#define ETIMEDOUT WSAETIMEDOUT
-#endif
#if defined(WSAEWOULDBLOCK) && !defined(EWOULDBLOCK)
#define EWOULDBLOCK WSAEWOULDBLOCK
#endif
@@ -3671,10 +3740,12 @@ static struct static_exception static_exceptions[] = {
// Level 4: Other subclasses
ITEM(IndentationError), // base: SyntaxError(Exception)
+ {&_PyExc_IncompleteInputError, "_IncompleteInputError"}, // base: SyntaxError(Exception)
ITEM(IndexError), // base: LookupError(Exception)
ITEM(KeyError), // base: LookupError(Exception)
ITEM(ModuleNotFoundError), // base: ImportError(Exception)
ITEM(NotImplementedError), // base: RuntimeError(Exception)
+ ITEM(PythonFinalizationError), // base: RuntimeError(Exception)
ITEM(RecursionError), // base: RuntimeError(Exception)
ITEM(UnboundLocalError), // base: NameError(Exception)
ITEM(UnicodeError), // base: ValueError(Exception)
@@ -3714,7 +3785,7 @@ _PyExc_FiniTypes(PyInterpreterState *interp)
{
for (Py_ssize_t i=Py_ARRAY_LENGTH(static_exceptions) - 1; i >= 0; i--) {
PyTypeObject *exc = static_exceptions[i].exc;
- _PyStaticType_Dealloc(interp, exc);
+ _PyStaticType_FiniBuiltin(interp, exc);
}
}
@@ -3777,6 +3848,9 @@ _PyExc_InitState(PyInterpreterState *interp)
#endif
ADD_ERRNO(ProcessLookupError, ESRCH);
ADD_ERRNO(TimeoutError, ETIMEDOUT);
+#ifdef WSAETIMEDOUT
+ ADD_ERRNO(TimeoutError, WSAETIMEDOUT);
+#endif
return _PyStatus_OK();