ยปCore Development>Code coverage>Objects/exceptions.c

Python code coverage for Objects/exceptions.c

#countcontent
1n/a/*
2n/a * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3n/a *
4n/a * Thanks go to Tim Peters and Michael Hudson for debugging.
5n/a */
6n/a
7n/a#define PY_SSIZE_T_CLEAN
8n/a#include <Python.h>
9n/a#include "structmember.h"
10n/a#include "osdefs.h"
11n/a
12n/a
13n/a/* Compatibility aliases */
14n/aPyObject *PyExc_EnvironmentError = NULL;
15n/aPyObject *PyExc_IOError = NULL;
16n/a#ifdef MS_WINDOWS
17n/aPyObject *PyExc_WindowsError = NULL;
18n/a#endif
19n/a
20n/a/* The dict map from errno codes to OSError subclasses */
21n/astatic PyObject *errnomap = NULL;
22n/a
23n/a
24n/a/* NOTE: If the exception class hierarchy changes, don't forget to update
25n/a * Lib/test/exception_hierarchy.txt
26n/a */
27n/a
28n/a/*
29n/a * BaseException
30n/a */
31n/astatic PyObject *
32n/aBaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
33n/a{
34n/a PyBaseExceptionObject *self;
35n/a
36n/a self = (PyBaseExceptionObject *)type->tp_alloc(type, 0);
37n/a if (!self)
38n/a return NULL;
39n/a /* the dict is created on the fly in PyObject_GenericSetAttr */
40n/a self->dict = NULL;
41n/a self->traceback = self->cause = self->context = NULL;
42n/a self->suppress_context = 0;
43n/a
44n/a if (args) {
45n/a self->args = args;
46n/a Py_INCREF(args);
47n/a return (PyObject *)self;
48n/a }
49n/a
50n/a self->args = PyTuple_New(0);
51n/a if (!self->args) {
52n/a Py_DECREF(self);
53n/a return NULL;
54n/a }
55n/a
56n/a return (PyObject *)self;
57n/a}
58n/a
59n/astatic int
60n/aBaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
61n/a{
62n/a if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
63n/a return -1;
64n/a
65n/a Py_INCREF(args);
66n/a Py_XSETREF(self->args, args);
67n/a
68n/a return 0;
69n/a}
70n/a
71n/astatic int
72n/aBaseException_clear(PyBaseExceptionObject *self)
73n/a{
74n/a Py_CLEAR(self->dict);
75n/a Py_CLEAR(self->args);
76n/a Py_CLEAR(self->traceback);
77n/a Py_CLEAR(self->cause);
78n/a Py_CLEAR(self->context);
79n/a return 0;
80n/a}
81n/a
82n/astatic void
83n/aBaseException_dealloc(PyBaseExceptionObject *self)
84n/a{
85n/a _PyObject_GC_UNTRACK(self);
86n/a BaseException_clear(self);
87n/a Py_TYPE(self)->tp_free((PyObject *)self);
88n/a}
89n/a
90n/astatic int
91n/aBaseException_traverse(PyBaseExceptionObject *self, visitproc visit, void *arg)
92n/a{
93n/a Py_VISIT(self->dict);
94n/a Py_VISIT(self->args);
95n/a Py_VISIT(self->traceback);
96n/a Py_VISIT(self->cause);
97n/a Py_VISIT(self->context);
98n/a return 0;
99n/a}
100n/a
101n/astatic PyObject *
102n/aBaseException_str(PyBaseExceptionObject *self)
103n/a{
104n/a switch (PyTuple_GET_SIZE(self->args)) {
105n/a case 0:
106n/a return PyUnicode_FromString("");
107n/a case 1:
108n/a return PyObject_Str(PyTuple_GET_ITEM(self->args, 0));
109n/a default:
110n/a return PyObject_Str(self->args);
111n/a }
112n/a}
113n/a
114n/astatic PyObject *
115n/aBaseException_repr(PyBaseExceptionObject *self)
116n/a{
117n/a const char *name;
118n/a const char *dot;
119n/a
120n/a name = Py_TYPE(self)->tp_name;
121n/a dot = (const char *) strrchr(name, '.');
122n/a if (dot != NULL) name = dot+1;
123n/a
124n/a return PyUnicode_FromFormat("%s%R", name, self->args);
125n/a}
126n/a
127n/a/* Pickling support */
128n/astatic PyObject *
129n/aBaseException_reduce(PyBaseExceptionObject *self)
130n/a{
131n/a if (self->args && self->dict)
132n/a return PyTuple_Pack(3, Py_TYPE(self), self->args, self->dict);
133n/a else
134n/a return PyTuple_Pack(2, Py_TYPE(self), self->args);
135n/a}
136n/a
137n/a/*
138n/a * Needed for backward compatibility, since exceptions used to store
139n/a * all their attributes in the __dict__. Code is taken from cPickle's
140n/a * load_build function.
141n/a */
142n/astatic PyObject *
143n/aBaseException_setstate(PyObject *self, PyObject *state)
144n/a{
145n/a PyObject *d_key, *d_value;
146n/a Py_ssize_t i = 0;
147n/a
148n/a if (state != Py_None) {
149n/a if (!PyDict_Check(state)) {
150n/a PyErr_SetString(PyExc_TypeError, "state is not a dictionary");
151n/a return NULL;
152n/a }
153n/a while (PyDict_Next(state, &i, &d_key, &d_value)) {
154n/a if (PyObject_SetAttr(self, d_key, d_value) < 0)
155n/a return NULL;
156n/a }
157n/a }
158n/a Py_RETURN_NONE;
159n/a}
160n/a
161n/astatic PyObject *
162n/aBaseException_with_traceback(PyObject *self, PyObject *tb) {
163n/a if (PyException_SetTraceback(self, tb))
164n/a return NULL;
165n/a
166n/a Py_INCREF(self);
167n/a return self;
168n/a}
169n/a
170n/aPyDoc_STRVAR(with_traceback_doc,
171n/a"Exception.with_traceback(tb) --\n\
172n/a set self.__traceback__ to tb and return self.");
173n/a
174n/a
175n/astatic PyMethodDef BaseException_methods[] = {
176n/a {"__reduce__", (PyCFunction)BaseException_reduce, METH_NOARGS },
177n/a {"__setstate__", (PyCFunction)BaseException_setstate, METH_O },
178n/a {"with_traceback", (PyCFunction)BaseException_with_traceback, METH_O,
179n/a with_traceback_doc},
180n/a {NULL, NULL, 0, NULL},
181n/a};
182n/a
183n/astatic PyObject *
184n/aBaseException_get_args(PyBaseExceptionObject *self)
185n/a{
186n/a if (self->args == NULL) {
187n/a Py_RETURN_NONE;
188n/a }
189n/a Py_INCREF(self->args);
190n/a return self->args;
191n/a}
192n/a
193n/astatic int
194n/aBaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
195n/a{
196n/a PyObject *seq;
197n/a if (val == NULL) {
198n/a PyErr_SetString(PyExc_TypeError, "args may not be deleted");
199n/a return -1;
200n/a }
201n/a seq = PySequence_Tuple(val);
202n/a if (!seq)
203n/a return -1;
204n/a Py_XSETREF(self->args, seq);
205n/a return 0;
206n/a}
207n/a
208n/astatic PyObject *
209n/aBaseException_get_tb(PyBaseExceptionObject *self)
210n/a{
211n/a if (self->traceback == NULL) {
212n/a Py_RETURN_NONE;
213n/a }
214n/a Py_INCREF(self->traceback);
215n/a return self->traceback;
216n/a}
217n/a
218n/astatic int
219n/aBaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb)
220n/a{
221n/a if (tb == NULL) {
222n/a PyErr_SetString(PyExc_TypeError, "__traceback__ may not be deleted");
223n/a return -1;
224n/a }
225n/a else if (!(tb == Py_None || PyTraceBack_Check(tb))) {
226n/a PyErr_SetString(PyExc_TypeError,
227n/a "__traceback__ must be a traceback or None");
228n/a return -1;
229n/a }
230n/a
231n/a Py_INCREF(tb);
232n/a Py_XSETREF(self->traceback, tb);
233n/a return 0;
234n/a}
235n/a
236n/astatic PyObject *
237n/aBaseException_get_context(PyObject *self) {
238n/a PyObject *res = PyException_GetContext(self);
239n/a if (res)
240n/a return res; /* new reference already returned above */
241n/a Py_RETURN_NONE;
242n/a}
243n/a
244n/astatic int
245n/aBaseException_set_context(PyObject *self, PyObject *arg) {
246n/a if (arg == NULL) {
247n/a PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted");
248n/a return -1;
249n/a } else if (arg == Py_None) {
250n/a arg = NULL;
251n/a } else if (!PyExceptionInstance_Check(arg)) {
252n/a PyErr_SetString(PyExc_TypeError, "exception context must be None "
253n/a "or derive from BaseException");
254n/a return -1;
255n/a } else {
256n/a /* PyException_SetContext steals this reference */
257n/a Py_INCREF(arg);
258n/a }
259n/a PyException_SetContext(self, arg);
260n/a return 0;
261n/a}
262n/a
263n/astatic PyObject *
264n/aBaseException_get_cause(PyObject *self) {
265n/a PyObject *res = PyException_GetCause(self);
266n/a if (res)
267n/a return res; /* new reference already returned above */
268n/a Py_RETURN_NONE;
269n/a}
270n/a
271n/astatic int
272n/aBaseException_set_cause(PyObject *self, PyObject *arg) {
273n/a if (arg == NULL) {
274n/a PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
275n/a return -1;
276n/a } else if (arg == Py_None) {
277n/a arg = NULL;
278n/a } else if (!PyExceptionInstance_Check(arg)) {
279n/a PyErr_SetString(PyExc_TypeError, "exception cause must be None "
280n/a "or derive from BaseException");
281n/a return -1;
282n/a } else {
283n/a /* PyException_SetCause steals this reference */
284n/a Py_INCREF(arg);
285n/a }
286n/a PyException_SetCause(self, arg);
287n/a return 0;
288n/a}
289n/a
290n/a
291n/astatic PyGetSetDef BaseException_getset[] = {
292n/a {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
293n/a {"args", (getter)BaseException_get_args, (setter)BaseException_set_args},
294n/a {"__traceback__", (getter)BaseException_get_tb, (setter)BaseException_set_tb},
295n/a {"__context__", (getter)BaseException_get_context,
296n/a (setter)BaseException_set_context, PyDoc_STR("exception context")},
297n/a {"__cause__", (getter)BaseException_get_cause,
298n/a (setter)BaseException_set_cause, PyDoc_STR("exception cause")},
299n/a {NULL},
300n/a};
301n/a
302n/a
303n/aPyObject *
304n/aPyException_GetTraceback(PyObject *self) {
305n/a PyBaseExceptionObject *base_self = (PyBaseExceptionObject *)self;
306n/a Py_XINCREF(base_self->traceback);
307n/a return base_self->traceback;
308n/a}
309n/a
310n/a
311n/aint
312n/aPyException_SetTraceback(PyObject *self, PyObject *tb) {
313n/a return BaseException_set_tb((PyBaseExceptionObject *)self, tb);
314n/a}
315n/a
316n/aPyObject *
317n/aPyException_GetCause(PyObject *self) {
318n/a PyObject *cause = ((PyBaseExceptionObject *)self)->cause;
319n/a Py_XINCREF(cause);
320n/a return cause;
321n/a}
322n/a
323n/a/* Steals a reference to cause */
324n/avoid
325n/aPyException_SetCause(PyObject *self, PyObject *cause)
326n/a{
327n/a ((PyBaseExceptionObject *)self)->suppress_context = 1;
328n/a Py_XSETREF(((PyBaseExceptionObject *)self)->cause, cause);
329n/a}
330n/a
331n/aPyObject *
332n/aPyException_GetContext(PyObject *self) {
333n/a PyObject *context = ((PyBaseExceptionObject *)self)->context;
334n/a Py_XINCREF(context);
335n/a return context;
336n/a}
337n/a
338n/a/* Steals a reference to context */
339n/avoid
340n/aPyException_SetContext(PyObject *self, PyObject *context)
341n/a{
342n/a Py_XSETREF(((PyBaseExceptionObject *)self)->context, context);
343n/a}
344n/a
345n/a
346n/astatic struct PyMemberDef BaseException_members[] = {
347n/a {"__suppress_context__", T_BOOL,
348n/a offsetof(PyBaseExceptionObject, suppress_context)},
349n/a {NULL}
350n/a};
351n/a
352n/a
353n/astatic PyTypeObject _PyExc_BaseException = {
354n/a PyVarObject_HEAD_INIT(NULL, 0)
355n/a "BaseException", /*tp_name*/
356n/a sizeof(PyBaseExceptionObject), /*tp_basicsize*/
357n/a 0, /*tp_itemsize*/
358n/a (destructor)BaseException_dealloc, /*tp_dealloc*/
359n/a 0, /*tp_print*/
360n/a 0, /*tp_getattr*/
361n/a 0, /*tp_setattr*/
362n/a 0, /* tp_reserved; */
363n/a (reprfunc)BaseException_repr, /*tp_repr*/
364n/a 0, /*tp_as_number*/
365n/a 0, /*tp_as_sequence*/
366n/a 0, /*tp_as_mapping*/
367n/a 0, /*tp_hash */
368n/a 0, /*tp_call*/
369n/a (reprfunc)BaseException_str, /*tp_str*/
370n/a PyObject_GenericGetAttr, /*tp_getattro*/
371n/a PyObject_GenericSetAttr, /*tp_setattro*/
372n/a 0, /*tp_as_buffer*/
373n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
374n/a Py_TPFLAGS_BASE_EXC_SUBCLASS, /*tp_flags*/
375n/a PyDoc_STR("Common base class for all exceptions"), /* tp_doc */
376n/a (traverseproc)BaseException_traverse, /* tp_traverse */
377n/a (inquiry)BaseException_clear, /* tp_clear */
378n/a 0, /* tp_richcompare */
379n/a 0, /* tp_weaklistoffset */
380n/a 0, /* tp_iter */
381n/a 0, /* tp_iternext */
382n/a BaseException_methods, /* tp_methods */
383n/a BaseException_members, /* tp_members */
384n/a BaseException_getset, /* tp_getset */
385n/a 0, /* tp_base */
386n/a 0, /* tp_dict */
387n/a 0, /* tp_descr_get */
388n/a 0, /* tp_descr_set */
389n/a offsetof(PyBaseExceptionObject, dict), /* tp_dictoffset */
390n/a (initproc)BaseException_init, /* tp_init */
391n/a 0, /* tp_alloc */
392n/a BaseException_new, /* tp_new */
393n/a};
394n/a/* the CPython API expects exceptions to be (PyObject *) - both a hold-over
395n/afrom the previous implmentation and also allowing Python objects to be used
396n/ain the API */
397n/aPyObject *PyExc_BaseException = (PyObject *)&_PyExc_BaseException;
398n/a
399n/a/* note these macros omit the last semicolon so the macro invocation may
400n/a * include it and not look strange.
401n/a */
402n/a#define SimpleExtendsException(EXCBASE, EXCNAME, EXCDOC) \
403n/astatic PyTypeObject _PyExc_ ## EXCNAME = { \
404n/a PyVarObject_HEAD_INIT(NULL, 0) \
405n/a # EXCNAME, \
406n/a sizeof(PyBaseExceptionObject), \
407n/a 0, (destructor)BaseException_dealloc, 0, 0, 0, 0, 0, 0, 0, \
408n/a 0, 0, 0, 0, 0, 0, 0, \
409n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
410n/a PyDoc_STR(EXCDOC), (traverseproc)BaseException_traverse, \
411n/a (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
412n/a 0, 0, 0, offsetof(PyBaseExceptionObject, dict), \
413n/a (initproc)BaseException_init, 0, BaseException_new,\
414n/a}; \
415n/aPyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
416n/a
417n/a#define MiddlingExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDOC) \
418n/astatic PyTypeObject _PyExc_ ## EXCNAME = { \
419n/a PyVarObject_HEAD_INIT(NULL, 0) \
420n/a # EXCNAME, \
421n/a sizeof(Py ## EXCSTORE ## Object), \
422n/a 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
423n/a 0, 0, 0, 0, 0, \
424n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
425n/a PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
426n/a (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
427n/a 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
428n/a (initproc)EXCSTORE ## _init, 0, 0, \
429n/a}; \
430n/aPyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
431n/a
432n/a#define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCNEW, \
433n/a EXCMETHODS, EXCMEMBERS, EXCGETSET, \
434n/a EXCSTR, EXCDOC) \
435n/astatic PyTypeObject _PyExc_ ## EXCNAME = { \
436n/a PyVarObject_HEAD_INIT(NULL, 0) \
437n/a # EXCNAME, \
438n/a sizeof(Py ## EXCSTORE ## Object), 0, \
439n/a (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
440n/a (reprfunc)EXCSTR, 0, 0, 0, \
441n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
442n/a PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
443n/a (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
444n/a EXCMEMBERS, EXCGETSET, &_ ## EXCBASE, \
445n/a 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
446n/a (initproc)EXCSTORE ## _init, 0, EXCNEW,\
447n/a}; \
448n/aPyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
449n/a
450n/a
451n/a/*
452n/a * Exception extends BaseException
453n/a */
454n/aSimpleExtendsException(PyExc_BaseException, Exception,
455n/a "Common base class for all non-exit exceptions.");
456n/a
457n/a
458n/a/*
459n/a * TypeError extends Exception
460n/a */
461n/aSimpleExtendsException(PyExc_Exception, TypeError,
462n/a "Inappropriate argument type.");
463n/a
464n/a
465n/a/*
466n/a * StopAsyncIteration extends Exception
467n/a */
468n/aSimpleExtendsException(PyExc_Exception, StopAsyncIteration,
469n/a "Signal the end from iterator.__anext__().");
470n/a
471n/a
472n/a/*
473n/a * StopIteration extends Exception
474n/a */
475n/a
476n/astatic PyMemberDef StopIteration_members[] = {
477n/a {"value", T_OBJECT, offsetof(PyStopIterationObject, value), 0,
478n/a PyDoc_STR("generator return value")},
479n/a {NULL} /* Sentinel */
480n/a};
481n/a
482n/astatic int
483n/aStopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
484n/a{
485n/a Py_ssize_t size = PyTuple_GET_SIZE(args);
486n/a PyObject *value;
487n/a
488n/a if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
489n/a return -1;
490n/a Py_CLEAR(self->value);
491n/a if (size > 0)
492n/a value = PyTuple_GET_ITEM(args, 0);
493n/a else
494n/a value = Py_None;
495n/a Py_INCREF(value);
496n/a self->value = value;
497n/a return 0;
498n/a}
499n/a
500n/astatic int
501n/aStopIteration_clear(PyStopIterationObject *self)
502n/a{
503n/a Py_CLEAR(self->value);
504n/a return BaseException_clear((PyBaseExceptionObject *)self);
505n/a}
506n/a
507n/astatic void
508n/aStopIteration_dealloc(PyStopIterationObject *self)
509n/a{
510n/a _PyObject_GC_UNTRACK(self);
511n/a StopIteration_clear(self);
512n/a Py_TYPE(self)->tp_free((PyObject *)self);
513n/a}
514n/a
515n/astatic int
516n/aStopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg)
517n/a{
518n/a Py_VISIT(self->value);
519n/a return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
520n/a}
521n/a
522n/aComplexExtendsException(
523n/a PyExc_Exception, /* base */
524n/a StopIteration, /* name */
525n/a StopIteration, /* prefix for *_init, etc */
526n/a 0, /* new */
527n/a 0, /* methods */
528n/a StopIteration_members, /* members */
529n/a 0, /* getset */
530n/a 0, /* str */
531n/a "Signal the end from iterator.__next__()."
532n/a);
533n/a
534n/a
535n/a/*
536n/a * GeneratorExit extends BaseException
537n/a */
538n/aSimpleExtendsException(PyExc_BaseException, GeneratorExit,
539n/a "Request that a generator exit.");
540n/a
541n/a
542n/a/*
543n/a * SystemExit extends BaseException
544n/a */
545n/a
546n/astatic int
547n/aSystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
548n/a{
549n/a Py_ssize_t size = PyTuple_GET_SIZE(args);
550n/a
551n/a if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
552n/a return -1;
553n/a
554n/a if (size == 0)
555n/a return 0;
556n/a if (size == 1) {
557n/a Py_INCREF(PyTuple_GET_ITEM(args, 0));
558n/a Py_XSETREF(self->code, PyTuple_GET_ITEM(args, 0));
559n/a }
560n/a else { /* size > 1 */
561n/a Py_INCREF(args);
562n/a Py_XSETREF(self->code, args);
563n/a }
564n/a return 0;
565n/a}
566n/a
567n/astatic int
568n/aSystemExit_clear(PySystemExitObject *self)
569n/a{
570n/a Py_CLEAR(self->code);
571n/a return BaseException_clear((PyBaseExceptionObject *)self);
572n/a}
573n/a
574n/astatic void
575n/aSystemExit_dealloc(PySystemExitObject *self)
576n/a{
577n/a _PyObject_GC_UNTRACK(self);
578n/a SystemExit_clear(self);
579n/a Py_TYPE(self)->tp_free((PyObject *)self);
580n/a}
581n/a
582n/astatic int
583n/aSystemExit_traverse(PySystemExitObject *self, visitproc visit, void *arg)
584n/a{
585n/a Py_VISIT(self->code);
586n/a return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
587n/a}
588n/a
589n/astatic PyMemberDef SystemExit_members[] = {
590n/a {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
591n/a PyDoc_STR("exception code")},
592n/a {NULL} /* Sentinel */
593n/a};
594n/a
595n/aComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
596n/a 0, 0, SystemExit_members, 0, 0,
597n/a "Request to exit from the interpreter.");
598n/a
599n/a/*
600n/a * KeyboardInterrupt extends BaseException
601n/a */
602n/aSimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
603n/a "Program interrupted by user.");
604n/a
605n/a
606n/a/*
607n/a * ImportError extends Exception
608n/a */
609n/a
610n/astatic int
611n/aImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
612n/a{
613n/a static char *kwlist[] = {"name", "path", 0};
614n/a PyObject *empty_tuple;
615n/a PyObject *msg = NULL;
616n/a PyObject *name = NULL;
617n/a PyObject *path = NULL;
618n/a
619n/a if (BaseException_init((PyBaseExceptionObject *)self, args, NULL) == -1)
620n/a return -1;
621n/a
622n/a empty_tuple = PyTuple_New(0);
623n/a if (!empty_tuple)
624n/a return -1;
625n/a if (!PyArg_ParseTupleAndKeywords(empty_tuple, kwds, "|$OO:ImportError", kwlist,
626n/a &name, &path)) {
627n/a Py_DECREF(empty_tuple);
628n/a return -1;
629n/a }
630n/a Py_DECREF(empty_tuple);
631n/a
632n/a Py_XINCREF(name);
633n/a Py_XSETREF(self->name, name);
634n/a
635n/a Py_XINCREF(path);
636n/a Py_XSETREF(self->path, path);
637n/a
638n/a if (PyTuple_GET_SIZE(args) == 1) {
639n/a msg = PyTuple_GET_ITEM(args, 0);
640n/a Py_INCREF(msg);
641n/a }
642n/a Py_XSETREF(self->msg, msg);
643n/a
644n/a return 0;
645n/a}
646n/a
647n/astatic int
648n/aImportError_clear(PyImportErrorObject *self)
649n/a{
650n/a Py_CLEAR(self->msg);
651n/a Py_CLEAR(self->name);
652n/a Py_CLEAR(self->path);
653n/a return BaseException_clear((PyBaseExceptionObject *)self);
654n/a}
655n/a
656n/astatic void
657n/aImportError_dealloc(PyImportErrorObject *self)
658n/a{
659n/a _PyObject_GC_UNTRACK(self);
660n/a ImportError_clear(self);
661n/a Py_TYPE(self)->tp_free((PyObject *)self);
662n/a}
663n/a
664n/astatic int
665n/aImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg)
666n/a{
667n/a Py_VISIT(self->msg);
668n/a Py_VISIT(self->name);
669n/a Py_VISIT(self->path);
670n/a return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
671n/a}
672n/a
673n/astatic PyObject *
674n/aImportError_str(PyImportErrorObject *self)
675n/a{
676n/a if (self->msg && PyUnicode_CheckExact(self->msg)) {
677n/a Py_INCREF(self->msg);
678n/a return self->msg;
679n/a }
680n/a else {
681n/a return BaseException_str((PyBaseExceptionObject *)self);
682n/a }
683n/a}
684n/a
685n/astatic PyMemberDef ImportError_members[] = {
686n/a {"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
687n/a PyDoc_STR("exception message")},
688n/a {"name", T_OBJECT, offsetof(PyImportErrorObject, name), 0,
689n/a PyDoc_STR("module name")},
690n/a {"path", T_OBJECT, offsetof(PyImportErrorObject, path), 0,
691n/a PyDoc_STR("module path")},
692n/a {NULL} /* Sentinel */
693n/a};
694n/a
695n/astatic PyMethodDef ImportError_methods[] = {
696n/a {NULL}
697n/a};
698n/a
699n/aComplexExtendsException(PyExc_Exception, ImportError,
700n/a ImportError, 0 /* new */,
701n/a ImportError_methods, ImportError_members,
702n/a 0 /* getset */, ImportError_str,
703n/a "Import can't find module, or can't find name in "
704n/a "module.");
705n/a
706n/a/*
707n/a * ModuleNotFoundError extends ImportError
708n/a */
709n/a
710n/aMiddlingExtendsException(PyExc_ImportError, ModuleNotFoundError, ImportError,
711n/a "Module not found.");
712n/a
713n/a/*
714n/a * OSError extends Exception
715n/a */
716n/a
717n/a#ifdef MS_WINDOWS
718n/a#include "errmap.h"
719n/a#endif
720n/a
721n/a/* Where a function has a single filename, such as open() or some
722n/a * of the os module functions, PyErr_SetFromErrnoWithFilename() is
723n/a * called, giving a third argument which is the filename. But, so
724n/a * that old code using in-place unpacking doesn't break, e.g.:
725n/a *
726n/a * except OSError, (errno, strerror):
727n/a *
728n/a * we hack args so that it only contains two items. This also
729n/a * means we need our own __str__() which prints out the filename
730n/a * when it was supplied.
731n/a *
732n/a * (If a function has two filenames, such as rename(), symlink(),
733n/a * or copy(), PyErr_SetFromErrnoWithFilenameObjects() is called,
734n/a * which allows passing in a second filename.)
735n/a */
736n/a
737n/a/* This function doesn't cleanup on error, the caller should */
738n/astatic int
739n/aoserror_parse_args(PyObject **p_args,
740n/a PyObject **myerrno, PyObject **strerror,
741n/a PyObject **filename, PyObject **filename2
742n/a#ifdef MS_WINDOWS
743n/a , PyObject **winerror
744n/a#endif
745n/a )
746n/a{
747n/a Py_ssize_t nargs;
748n/a PyObject *args = *p_args;
749n/a#ifndef MS_WINDOWS
750n/a /*
751n/a * ignored on non-Windows platforms,
752n/a * but parsed so OSError has a consistent signature
753n/a */
754n/a PyObject *_winerror = NULL;
755n/a PyObject **winerror = &_winerror;
756n/a#endif /* MS_WINDOWS */
757n/a
758n/a nargs = PyTuple_GET_SIZE(args);
759n/a
760n/a if (nargs >= 2 && nargs <= 5) {
761n/a if (!PyArg_UnpackTuple(args, "OSError", 2, 5,
762n/a myerrno, strerror,
763n/a filename, winerror, filename2))
764n/a return -1;
765n/a#ifdef MS_WINDOWS
766n/a if (*winerror && PyLong_Check(*winerror)) {
767n/a long errcode, winerrcode;
768n/a PyObject *newargs;
769n/a Py_ssize_t i;
770n/a
771n/a winerrcode = PyLong_AsLong(*winerror);
772n/a if (winerrcode == -1 && PyErr_Occurred())
773n/a return -1;
774n/a /* Set errno to the corresponding POSIX errno (overriding
775n/a first argument). Windows Socket error codes (>= 10000)
776n/a have the same value as their POSIX counterparts.
777n/a */
778n/a if (winerrcode < 10000)
779n/a errcode = winerror_to_errno(winerrcode);
780n/a else
781n/a errcode = winerrcode;
782n/a *myerrno = PyLong_FromLong(errcode);
783n/a if (!*myerrno)
784n/a return -1;
785n/a newargs = PyTuple_New(nargs);
786n/a if (!newargs)
787n/a return -1;
788n/a PyTuple_SET_ITEM(newargs, 0, *myerrno);
789n/a for (i = 1; i < nargs; i++) {
790n/a PyObject *val = PyTuple_GET_ITEM(args, i);
791n/a Py_INCREF(val);
792n/a PyTuple_SET_ITEM(newargs, i, val);
793n/a }
794n/a Py_DECREF(args);
795n/a args = *p_args = newargs;
796n/a }
797n/a#endif /* MS_WINDOWS */
798n/a }
799n/a
800n/a return 0;
801n/a}
802n/a
803n/astatic int
804n/aoserror_init(PyOSErrorObject *self, PyObject **p_args,
805n/a PyObject *myerrno, PyObject *strerror,
806n/a PyObject *filename, PyObject *filename2
807n/a#ifdef MS_WINDOWS
808n/a , PyObject *winerror
809n/a#endif
810n/a )
811n/a{
812n/a PyObject *args = *p_args;
813n/a Py_ssize_t nargs = PyTuple_GET_SIZE(args);
814n/a
815n/a /* self->filename will remain Py_None otherwise */
816n/a if (filename && filename != Py_None) {
817n/a if (Py_TYPE(self) == (PyTypeObject *) PyExc_BlockingIOError &&
818n/a PyNumber_Check(filename)) {
819n/a /* BlockingIOError's 3rd argument can be the number of
820n/a * characters written.
821n/a */
822n/a self->written = PyNumber_AsSsize_t(filename, PyExc_ValueError);
823n/a if (self->written == -1 && PyErr_Occurred())
824n/a return -1;
825n/a }
826n/a else {
827n/a Py_INCREF(filename);
828n/a self->filename = filename;
829n/a
830n/a if (filename2 && filename2 != Py_None) {
831n/a Py_INCREF(filename2);
832n/a self->filename2 = filename2;
833n/a }
834n/a
835n/a if (nargs >= 2 && nargs <= 5) {
836n/a /* filename, filename2, and winerror are removed from the args tuple
837n/a (for compatibility purposes, see test_exceptions.py) */
838n/a PyObject *subslice = PyTuple_GetSlice(args, 0, 2);
839n/a if (!subslice)
840n/a return -1;
841n/a
842n/a Py_DECREF(args); /* replacing args */
843n/a *p_args = args = subslice;
844n/a }
845n/a }
846n/a }
847n/a Py_XINCREF(myerrno);
848n/a self->myerrno = myerrno;
849n/a
850n/a Py_XINCREF(strerror);
851n/a self->strerror = strerror;
852n/a
853n/a#ifdef MS_WINDOWS
854n/a Py_XINCREF(winerror);
855n/a self->winerror = winerror;
856n/a#endif
857n/a
858n/a /* Steals the reference to args */
859n/a Py_XSETREF(self->args, args);
860n/a *p_args = args = NULL;
861n/a
862n/a return 0;
863n/a}
864n/a
865n/astatic PyObject *
866n/aOSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
867n/astatic int
868n/aOSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds);
869n/a
870n/astatic int
871n/aoserror_use_init(PyTypeObject *type)
872n/a{
873n/a /* When __init__ is defined in an OSError subclass, we want any
874n/a extraneous argument to __new__ to be ignored. The only reasonable
875n/a solution, given __new__ takes a variable number of arguments,
876n/a is to defer arg parsing and initialization to __init__.
877n/a
878n/a But when __new__ is overridden as well, it should call our __new__
879n/a with the right arguments.
880n/a
881n/a (see http://bugs.python.org/issue12555#msg148829 )
882n/a */
883n/a if (type->tp_init != (initproc) OSError_init &&
884n/a type->tp_new == (newfunc) OSError_new) {
885n/a assert((PyObject *) type != PyExc_OSError);
886n/a return 1;
887n/a }
888n/a return 0;
889n/a}
890n/a
891n/astatic PyObject *
892n/aOSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
893n/a{
894n/a PyOSErrorObject *self = NULL;
895n/a PyObject *myerrno = NULL, *strerror = NULL;
896n/a PyObject *filename = NULL, *filename2 = NULL;
897n/a#ifdef MS_WINDOWS
898n/a PyObject *winerror = NULL;
899n/a#endif
900n/a
901n/a Py_INCREF(args);
902n/a
903n/a if (!oserror_use_init(type)) {
904n/a if (!_PyArg_NoKeywords(type->tp_name, kwds))
905n/a goto error;
906n/a
907n/a if (oserror_parse_args(&args, &myerrno, &strerror,
908n/a &filename, &filename2
909n/a#ifdef MS_WINDOWS
910n/a , &winerror
911n/a#endif
912n/a ))
913n/a goto error;
914n/a
915n/a if (myerrno && PyLong_Check(myerrno) &&
916n/a errnomap && (PyObject *) type == PyExc_OSError) {
917n/a PyObject *newtype;
918n/a newtype = PyDict_GetItem(errnomap, myerrno);
919n/a if (newtype) {
920n/a assert(PyType_Check(newtype));
921n/a type = (PyTypeObject *) newtype;
922n/a }
923n/a else if (PyErr_Occurred())
924n/a goto error;
925n/a }
926n/a }
927n/a
928n/a self = (PyOSErrorObject *) type->tp_alloc(type, 0);
929n/a if (!self)
930n/a goto error;
931n/a
932n/a self->dict = NULL;
933n/a self->traceback = self->cause = self->context = NULL;
934n/a self->written = -1;
935n/a
936n/a if (!oserror_use_init(type)) {
937n/a if (oserror_init(self, &args, myerrno, strerror, filename, filename2
938n/a#ifdef MS_WINDOWS
939n/a , winerror
940n/a#endif
941n/a ))
942n/a goto error;
943n/a }
944n/a else {
945n/a self->args = PyTuple_New(0);
946n/a if (self->args == NULL)
947n/a goto error;
948n/a }
949n/a
950n/a Py_XDECREF(args);
951n/a return (PyObject *) self;
952n/a
953n/aerror:
954n/a Py_XDECREF(args);
955n/a Py_XDECREF(self);
956n/a return NULL;
957n/a}
958n/a
959n/astatic int
960n/aOSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
961n/a{
962n/a PyObject *myerrno = NULL, *strerror = NULL;
963n/a PyObject *filename = NULL, *filename2 = NULL;
964n/a#ifdef MS_WINDOWS
965n/a PyObject *winerror = NULL;
966n/a#endif
967n/a
968n/a if (!oserror_use_init(Py_TYPE(self)))
969n/a /* Everything already done in OSError_new */
970n/a return 0;
971n/a
972n/a if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
973n/a return -1;
974n/a
975n/a Py_INCREF(args);
976n/a if (oserror_parse_args(&args, &myerrno, &strerror, &filename, &filename2
977n/a#ifdef MS_WINDOWS
978n/a , &winerror
979n/a#endif
980n/a ))
981n/a goto error;
982n/a
983n/a if (oserror_init(self, &args, myerrno, strerror, filename, filename2
984n/a#ifdef MS_WINDOWS
985n/a , winerror
986n/a#endif
987n/a ))
988n/a goto error;
989n/a
990n/a return 0;
991n/a
992n/aerror:
993n/a Py_DECREF(args);
994n/a return -1;
995n/a}
996n/a
997n/astatic int
998n/aOSError_clear(PyOSErrorObject *self)
999n/a{
1000n/a Py_CLEAR(self->myerrno);
1001n/a Py_CLEAR(self->strerror);
1002n/a Py_CLEAR(self->filename);
1003n/a Py_CLEAR(self->filename2);
1004n/a#ifdef MS_WINDOWS
1005n/a Py_CLEAR(self->winerror);
1006n/a#endif
1007n/a return BaseException_clear((PyBaseExceptionObject *)self);
1008n/a}
1009n/a
1010n/astatic void
1011n/aOSError_dealloc(PyOSErrorObject *self)
1012n/a{
1013n/a _PyObject_GC_UNTRACK(self);
1014n/a OSError_clear(self);
1015n/a Py_TYPE(self)->tp_free((PyObject *)self);
1016n/a}
1017n/a
1018n/astatic int
1019n/aOSError_traverse(PyOSErrorObject *self, visitproc visit,
1020n/a void *arg)
1021n/a{
1022n/a Py_VISIT(self->myerrno);
1023n/a Py_VISIT(self->strerror);
1024n/a Py_VISIT(self->filename);
1025n/a Py_VISIT(self->filename2);
1026n/a#ifdef MS_WINDOWS
1027n/a Py_VISIT(self->winerror);
1028n/a#endif
1029n/a return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1030n/a}
1031n/a
1032n/astatic PyObject *
1033n/aOSError_str(PyOSErrorObject *self)
1034n/a{
1035n/a#define OR_NONE(x) ((x)?(x):Py_None)
1036n/a#ifdef MS_WINDOWS
1037n/a /* If available, winerror has the priority over myerrno */
1038n/a if (self->winerror && self->filename) {
1039n/a if (self->filename2) {
1040n/a return PyUnicode_FromFormat("[WinError %S] %S: %R -> %R",
1041n/a OR_NONE(self->winerror),
1042n/a OR_NONE(self->strerror),
1043n/a self->filename,
1044n/a self->filename2);
1045n/a } else {
1046n/a return PyUnicode_FromFormat("[WinError %S] %S: %R",
1047n/a OR_NONE(self->winerror),
1048n/a OR_NONE(self->strerror),
1049n/a self->filename);
1050n/a }
1051n/a }
1052n/a if (self->winerror && self->strerror)
1053n/a return PyUnicode_FromFormat("[WinError %S] %S",
1054n/a self->winerror ? self->winerror: Py_None,
1055n/a self->strerror ? self->strerror: Py_None);
1056n/a#endif
1057n/a if (self->filename) {
1058n/a if (self->filename2) {
1059n/a return PyUnicode_FromFormat("[Errno %S] %S: %R -> %R",
1060n/a OR_NONE(self->myerrno),
1061n/a OR_NONE(self->strerror),
1062n/a self->filename,
1063n/a self->filename2);
1064n/a } else {
1065n/a return PyUnicode_FromFormat("[Errno %S] %S: %R",
1066n/a OR_NONE(self->myerrno),
1067n/a OR_NONE(self->strerror),
1068n/a self->filename);
1069n/a }
1070n/a }
1071n/a if (self->myerrno && self->strerror)
1072n/a return PyUnicode_FromFormat("[Errno %S] %S",
1073n/a self->myerrno, self->strerror);
1074n/a return BaseException_str((PyBaseExceptionObject *)self);
1075n/a}
1076n/a
1077n/astatic PyObject *
1078n/aOSError_reduce(PyOSErrorObject *self)
1079n/a{
1080n/a PyObject *args = self->args;
1081n/a PyObject *res = NULL, *tmp;
1082n/a
1083n/a /* self->args is only the first two real arguments if there was a
1084n/a * file name given to OSError. */
1085n/a if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
1086n/a Py_ssize_t size = self->filename2 ? 5 : 3;
1087n/a args = PyTuple_New(size);
1088n/a if (!args)
1089n/a return NULL;
1090n/a
1091n/a tmp = PyTuple_GET_ITEM(self->args, 0);
1092n/a Py_INCREF(tmp);
1093n/a PyTuple_SET_ITEM(args, 0, tmp);
1094n/a
1095n/a tmp = PyTuple_GET_ITEM(self->args, 1);
1096n/a Py_INCREF(tmp);
1097n/a PyTuple_SET_ITEM(args, 1, tmp);
1098n/a
1099n/a Py_INCREF(self->filename);
1100n/a PyTuple_SET_ITEM(args, 2, self->filename);
1101n/a
1102n/a if (self->filename2) {
1103n/a /*
1104n/a * This tuple is essentially used as OSError(*args).
1105n/a * So, to recreate filename2, we need to pass in
1106n/a * winerror as well.
1107n/a */
1108n/a Py_INCREF(Py_None);
1109n/a PyTuple_SET_ITEM(args, 3, Py_None);
1110n/a
1111n/a /* filename2 */
1112n/a Py_INCREF(self->filename2);
1113n/a PyTuple_SET_ITEM(args, 4, self->filename2);
1114n/a }
1115n/a } else
1116n/a Py_INCREF(args);
1117n/a
1118n/a if (self->dict)
1119n/a res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
1120n/a else
1121n/a res = PyTuple_Pack(2, Py_TYPE(self), args);
1122n/a Py_DECREF(args);
1123n/a return res;
1124n/a}
1125n/a
1126n/astatic PyObject *
1127n/aOSError_written_get(PyOSErrorObject *self, void *context)
1128n/a{
1129n/a if (self->written == -1) {
1130n/a PyErr_SetString(PyExc_AttributeError, "characters_written");
1131n/a return NULL;
1132n/a }
1133n/a return PyLong_FromSsize_t(self->written);
1134n/a}
1135n/a
1136n/astatic int
1137n/aOSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
1138n/a{
1139n/a Py_ssize_t n;
1140n/a n = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1141n/a if (n == -1 && PyErr_Occurred())
1142n/a return -1;
1143n/a self->written = n;
1144n/a return 0;
1145n/a}
1146n/a
1147n/astatic PyMemberDef OSError_members[] = {
1148n/a {"errno", T_OBJECT, offsetof(PyOSErrorObject, myerrno), 0,
1149n/a PyDoc_STR("POSIX exception code")},
1150n/a {"strerror", T_OBJECT, offsetof(PyOSErrorObject, strerror), 0,
1151n/a PyDoc_STR("exception strerror")},
1152n/a {"filename", T_OBJECT, offsetof(PyOSErrorObject, filename), 0,
1153n/a PyDoc_STR("exception filename")},
1154n/a {"filename2", T_OBJECT, offsetof(PyOSErrorObject, filename2), 0,
1155n/a PyDoc_STR("second exception filename")},
1156n/a#ifdef MS_WINDOWS
1157n/a {"winerror", T_OBJECT, offsetof(PyOSErrorObject, winerror), 0,
1158n/a PyDoc_STR("Win32 exception code")},
1159n/a#endif
1160n/a {NULL} /* Sentinel */
1161n/a};
1162n/a
1163n/astatic PyMethodDef OSError_methods[] = {
1164n/a {"__reduce__", (PyCFunction)OSError_reduce, METH_NOARGS},
1165n/a {NULL}
1166n/a};
1167n/a
1168n/astatic PyGetSetDef OSError_getset[] = {
1169n/a {"characters_written", (getter) OSError_written_get,
1170n/a (setter) OSError_written_set, NULL},
1171n/a {NULL}
1172n/a};
1173n/a
1174n/a
1175n/aComplexExtendsException(PyExc_Exception, OSError,
1176n/a OSError, OSError_new,
1177n/a OSError_methods, OSError_members, OSError_getset,
1178n/a OSError_str,
1179n/a "Base class for I/O related errors.");
1180n/a
1181n/a
1182n/a/*
1183n/a * Various OSError subclasses
1184n/a */
1185n/aMiddlingExtendsException(PyExc_OSError, BlockingIOError, OSError,
1186n/a "I/O operation would block.");
1187n/aMiddlingExtendsException(PyExc_OSError, ConnectionError, OSError,
1188n/a "Connection error.");
1189n/aMiddlingExtendsException(PyExc_OSError, ChildProcessError, OSError,
1190n/a "Child process error.");
1191n/aMiddlingExtendsException(PyExc_ConnectionError, BrokenPipeError, OSError,
1192n/a "Broken pipe.");
1193n/aMiddlingExtendsException(PyExc_ConnectionError, ConnectionAbortedError, OSError,
1194n/a "Connection aborted.");
1195n/aMiddlingExtendsException(PyExc_ConnectionError, ConnectionRefusedError, OSError,
1196n/a "Connection refused.");
1197n/aMiddlingExtendsException(PyExc_ConnectionError, ConnectionResetError, OSError,
1198n/a "Connection reset.");
1199n/aMiddlingExtendsException(PyExc_OSError, FileExistsError, OSError,
1200n/a "File already exists.");
1201n/aMiddlingExtendsException(PyExc_OSError, FileNotFoundError, OSError,
1202n/a "File not found.");
1203n/aMiddlingExtendsException(PyExc_OSError, IsADirectoryError, OSError,
1204n/a "Operation doesn't work on directories.");
1205n/aMiddlingExtendsException(PyExc_OSError, NotADirectoryError, OSError,
1206n/a "Operation only works on directories.");
1207n/aMiddlingExtendsException(PyExc_OSError, InterruptedError, OSError,
1208n/a "Interrupted by signal.");
1209n/aMiddlingExtendsException(PyExc_OSError, PermissionError, OSError,
1210n/a "Not enough permissions.");
1211n/aMiddlingExtendsException(PyExc_OSError, ProcessLookupError, OSError,
1212n/a "Process not found.");
1213n/aMiddlingExtendsException(PyExc_OSError, TimeoutError, OSError,
1214n/a "Timeout expired.");
1215n/a
1216n/a/*
1217n/a * EOFError extends Exception
1218n/a */
1219n/aSimpleExtendsException(PyExc_Exception, EOFError,
1220n/a "Read beyond end of file.");
1221n/a
1222n/a
1223n/a/*
1224n/a * RuntimeError extends Exception
1225n/a */
1226n/aSimpleExtendsException(PyExc_Exception, RuntimeError,
1227n/a "Unspecified run-time error.");
1228n/a
1229n/a/*
1230n/a * RecursionError extends RuntimeError
1231n/a */
1232n/aSimpleExtendsException(PyExc_RuntimeError, RecursionError,
1233n/a "Recursion limit exceeded.");
1234n/a
1235n/a/*
1236n/a * NotImplementedError extends RuntimeError
1237n/a */
1238n/aSimpleExtendsException(PyExc_RuntimeError, NotImplementedError,
1239n/a "Method or function hasn't been implemented yet.");
1240n/a
1241n/a/*
1242n/a * NameError extends Exception
1243n/a */
1244n/aSimpleExtendsException(PyExc_Exception, NameError,
1245n/a "Name not found globally.");
1246n/a
1247n/a/*
1248n/a * UnboundLocalError extends NameError
1249n/a */
1250n/aSimpleExtendsException(PyExc_NameError, UnboundLocalError,
1251n/a "Local name referenced but not bound to a value.");
1252n/a
1253n/a/*
1254n/a * AttributeError extends Exception
1255n/a */
1256n/aSimpleExtendsException(PyExc_Exception, AttributeError,
1257n/a "Attribute not found.");
1258n/a
1259n/a
1260n/a/*
1261n/a * SyntaxError extends Exception
1262n/a */
1263n/a
1264n/a/* Helper function to customize error message for some syntax errors */
1265n/astatic int _report_missing_parentheses(PySyntaxErrorObject *self);
1266n/a
1267n/astatic int
1268n/aSyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
1269n/a{
1270n/a PyObject *info = NULL;
1271n/a Py_ssize_t lenargs = PyTuple_GET_SIZE(args);
1272n/a
1273n/a if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1274n/a return -1;
1275n/a
1276n/a if (lenargs >= 1) {
1277n/a Py_INCREF(PyTuple_GET_ITEM(args, 0));
1278n/a Py_XSETREF(self->msg, PyTuple_GET_ITEM(args, 0));
1279n/a }
1280n/a if (lenargs == 2) {
1281n/a info = PyTuple_GET_ITEM(args, 1);
1282n/a info = PySequence_Tuple(info);
1283n/a if (!info)
1284n/a return -1;
1285n/a
1286n/a if (PyTuple_GET_SIZE(info) != 4) {
1287n/a /* not a very good error message, but it's what Python 2.4 gives */
1288n/a PyErr_SetString(PyExc_IndexError, "tuple index out of range");
1289n/a Py_DECREF(info);
1290n/a return -1;
1291n/a }
1292n/a
1293n/a Py_INCREF(PyTuple_GET_ITEM(info, 0));
1294n/a Py_XSETREF(self->filename, PyTuple_GET_ITEM(info, 0));
1295n/a
1296n/a Py_INCREF(PyTuple_GET_ITEM(info, 1));
1297n/a Py_XSETREF(self->lineno, PyTuple_GET_ITEM(info, 1));
1298n/a
1299n/a Py_INCREF(PyTuple_GET_ITEM(info, 2));
1300n/a Py_XSETREF(self->offset, PyTuple_GET_ITEM(info, 2));
1301n/a
1302n/a Py_INCREF(PyTuple_GET_ITEM(info, 3));
1303n/a Py_XSETREF(self->text, PyTuple_GET_ITEM(info, 3));
1304n/a
1305n/a Py_DECREF(info);
1306n/a
1307n/a /* Issue #21669: Custom error for 'print' & 'exec' as statements */
1308n/a if (self->text && PyUnicode_Check(self->text)) {
1309n/a if (_report_missing_parentheses(self) < 0) {
1310n/a return -1;
1311n/a }
1312n/a }
1313n/a }
1314n/a return 0;
1315n/a}
1316n/a
1317n/astatic int
1318n/aSyntaxError_clear(PySyntaxErrorObject *self)
1319n/a{
1320n/a Py_CLEAR(self->msg);
1321n/a Py_CLEAR(self->filename);
1322n/a Py_CLEAR(self->lineno);
1323n/a Py_CLEAR(self->offset);
1324n/a Py_CLEAR(self->text);
1325n/a Py_CLEAR(self->print_file_and_line);
1326n/a return BaseException_clear((PyBaseExceptionObject *)self);
1327n/a}
1328n/a
1329n/astatic void
1330n/aSyntaxError_dealloc(PySyntaxErrorObject *self)
1331n/a{
1332n/a _PyObject_GC_UNTRACK(self);
1333n/a SyntaxError_clear(self);
1334n/a Py_TYPE(self)->tp_free((PyObject *)self);
1335n/a}
1336n/a
1337n/astatic int
1338n/aSyntaxError_traverse(PySyntaxErrorObject *self, visitproc visit, void *arg)
1339n/a{
1340n/a Py_VISIT(self->msg);
1341n/a Py_VISIT(self->filename);
1342n/a Py_VISIT(self->lineno);
1343n/a Py_VISIT(self->offset);
1344n/a Py_VISIT(self->text);
1345n/a Py_VISIT(self->print_file_and_line);
1346n/a return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1347n/a}
1348n/a
1349n/a/* This is called "my_basename" instead of just "basename" to avoid name
1350n/a conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1351n/a defined, and Python does define that. */
1352n/astatic PyObject*
1353n/amy_basename(PyObject *name)
1354n/a{
1355n/a Py_ssize_t i, size, offset;
1356n/a int kind;
1357n/a void *data;
1358n/a
1359n/a if (PyUnicode_READY(name))
1360n/a return NULL;
1361n/a kind = PyUnicode_KIND(name);
1362n/a data = PyUnicode_DATA(name);
1363n/a size = PyUnicode_GET_LENGTH(name);
1364n/a offset = 0;
1365n/a for(i=0; i < size; i++) {
1366n/a if (PyUnicode_READ(kind, data, i) == SEP)
1367n/a offset = i + 1;
1368n/a }
1369n/a if (offset != 0)
1370n/a return PyUnicode_Substring(name, offset, size);
1371n/a else {
1372n/a Py_INCREF(name);
1373n/a return name;
1374n/a }
1375n/a}
1376n/a
1377n/a
1378n/astatic PyObject *
1379n/aSyntaxError_str(PySyntaxErrorObject *self)
1380n/a{
1381n/a int have_lineno = 0;
1382n/a PyObject *filename;
1383n/a PyObject *result;
1384n/a /* Below, we always ignore overflow errors, just printing -1.
1385n/a Still, we cannot allow an OverflowError to be raised, so
1386n/a we need to call PyLong_AsLongAndOverflow. */
1387n/a int overflow;
1388n/a
1389n/a /* XXX -- do all the additional formatting with filename and
1390n/a lineno here */
1391n/a
1392n/a if (self->filename && PyUnicode_Check(self->filename)) {
1393n/a filename = my_basename(self->filename);
1394n/a if (filename == NULL)
1395n/a return NULL;
1396n/a } else {
1397n/a filename = NULL;
1398n/a }
1399n/a have_lineno = (self->lineno != NULL) && PyLong_CheckExact(self->lineno);
1400n/a
1401n/a if (!filename && !have_lineno)
1402n/a return PyObject_Str(self->msg ? self->msg : Py_None);
1403n/a
1404n/a if (filename && have_lineno)
1405n/a result = PyUnicode_FromFormat("%S (%U, line %ld)",
1406n/a self->msg ? self->msg : Py_None,
1407n/a filename,
1408n/a PyLong_AsLongAndOverflow(self->lineno, &overflow));
1409n/a else if (filename)
1410n/a result = PyUnicode_FromFormat("%S (%U)",
1411n/a self->msg ? self->msg : Py_None,
1412n/a filename);
1413n/a else /* only have_lineno */
1414n/a result = PyUnicode_FromFormat("%S (line %ld)",
1415n/a self->msg ? self->msg : Py_None,
1416n/a PyLong_AsLongAndOverflow(self->lineno, &overflow));
1417n/a Py_XDECREF(filename);
1418n/a return result;
1419n/a}
1420n/a
1421n/astatic PyMemberDef SyntaxError_members[] = {
1422n/a {"msg", T_OBJECT, offsetof(PySyntaxErrorObject, msg), 0,
1423n/a PyDoc_STR("exception msg")},
1424n/a {"filename", T_OBJECT, offsetof(PySyntaxErrorObject, filename), 0,
1425n/a PyDoc_STR("exception filename")},
1426n/a {"lineno", T_OBJECT, offsetof(PySyntaxErrorObject, lineno), 0,
1427n/a PyDoc_STR("exception lineno")},
1428n/a {"offset", T_OBJECT, offsetof(PySyntaxErrorObject, offset), 0,
1429n/a PyDoc_STR("exception offset")},
1430n/a {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1431n/a PyDoc_STR("exception text")},
1432n/a {"print_file_and_line", T_OBJECT,
1433n/a offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1434n/a PyDoc_STR("exception print_file_and_line")},
1435n/a {NULL} /* Sentinel */
1436n/a};
1437n/a
1438n/aComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
1439n/a 0, 0, SyntaxError_members, 0,
1440n/a SyntaxError_str, "Invalid syntax.");
1441n/a
1442n/a
1443n/a/*
1444n/a * IndentationError extends SyntaxError
1445n/a */
1446n/aMiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
1447n/a "Improper indentation.");
1448n/a
1449n/a
1450n/a/*
1451n/a * TabError extends IndentationError
1452n/a */
1453n/aMiddlingExtendsException(PyExc_IndentationError, TabError, SyntaxError,
1454n/a "Improper mixture of spaces and tabs.");
1455n/a
1456n/a
1457n/a/*
1458n/a * LookupError extends Exception
1459n/a */
1460n/aSimpleExtendsException(PyExc_Exception, LookupError,
1461n/a "Base class for lookup errors.");
1462n/a
1463n/a
1464n/a/*
1465n/a * IndexError extends LookupError
1466n/a */
1467n/aSimpleExtendsException(PyExc_LookupError, IndexError,
1468n/a "Sequence index out of range.");
1469n/a
1470n/a
1471n/a/*
1472n/a * KeyError extends LookupError
1473n/a */
1474n/astatic PyObject *
1475n/aKeyError_str(PyBaseExceptionObject *self)
1476n/a{
1477n/a /* If args is a tuple of exactly one item, apply repr to args[0].
1478n/a This is done so that e.g. the exception raised by {}[''] prints
1479n/a KeyError: ''
1480n/a rather than the confusing
1481n/a KeyError
1482n/a alone. The downside is that if KeyError is raised with an explanatory
1483n/a string, that string will be displayed in quotes. Too bad.
1484n/a If args is anything else, use the default BaseException__str__().
1485n/a */
1486n/a if (PyTuple_GET_SIZE(self->args) == 1) {
1487n/a return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
1488n/a }
1489n/a return BaseException_str(self);
1490n/a}
1491n/a
1492n/aComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1493n/a 0, 0, 0, 0, KeyError_str, "Mapping key not found.");
1494n/a
1495n/a
1496n/a/*
1497n/a * ValueError extends Exception
1498n/a */
1499n/aSimpleExtendsException(PyExc_Exception, ValueError,
1500n/a "Inappropriate argument value (of correct type).");
1501n/a
1502n/a/*
1503n/a * UnicodeError extends ValueError
1504n/a */
1505n/a
1506n/aSimpleExtendsException(PyExc_ValueError, UnicodeError,
1507n/a "Unicode related error.");
1508n/a
1509n/astatic PyObject *
1510n/aget_string(PyObject *attr, const char *name)
1511n/a{
1512n/a if (!attr) {
1513n/a PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1514n/a return NULL;
1515n/a }
1516n/a
1517n/a if (!PyBytes_Check(attr)) {
1518n/a PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
1519n/a return NULL;
1520n/a }
1521n/a Py_INCREF(attr);
1522n/a return attr;
1523n/a}
1524n/a
1525n/astatic PyObject *
1526n/aget_unicode(PyObject *attr, const char *name)
1527n/a{
1528n/a if (!attr) {
1529n/a PyErr_Format(PyExc_TypeError, "%.200s attribute not set", name);
1530n/a return NULL;
1531n/a }
1532n/a
1533n/a if (!PyUnicode_Check(attr)) {
1534n/a PyErr_Format(PyExc_TypeError,
1535n/a "%.200s attribute must be unicode", name);
1536n/a return NULL;
1537n/a }
1538n/a Py_INCREF(attr);
1539n/a return attr;
1540n/a}
1541n/a
1542n/astatic int
1543n/aset_unicodefromstring(PyObject **attr, const char *value)
1544n/a{
1545n/a PyObject *obj = PyUnicode_FromString(value);
1546n/a if (!obj)
1547n/a return -1;
1548n/a Py_XSETREF(*attr, obj);
1549n/a return 0;
1550n/a}
1551n/a
1552n/aPyObject *
1553n/aPyUnicodeEncodeError_GetEncoding(PyObject *exc)
1554n/a{
1555n/a return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1556n/a}
1557n/a
1558n/aPyObject *
1559n/aPyUnicodeDecodeError_GetEncoding(PyObject *exc)
1560n/a{
1561n/a return get_unicode(((PyUnicodeErrorObject *)exc)->encoding, "encoding");
1562n/a}
1563n/a
1564n/aPyObject *
1565n/aPyUnicodeEncodeError_GetObject(PyObject *exc)
1566n/a{
1567n/a return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1568n/a}
1569n/a
1570n/aPyObject *
1571n/aPyUnicodeDecodeError_GetObject(PyObject *exc)
1572n/a{
1573n/a return get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1574n/a}
1575n/a
1576n/aPyObject *
1577n/aPyUnicodeTranslateError_GetObject(PyObject *exc)
1578n/a{
1579n/a return get_unicode(((PyUnicodeErrorObject *)exc)->object, "object");
1580n/a}
1581n/a
1582n/aint
1583n/aPyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1584n/a{
1585n/a Py_ssize_t size;
1586n/a PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1587n/a "object");
1588n/a if (!obj)
1589n/a return -1;
1590n/a *start = ((PyUnicodeErrorObject *)exc)->start;
1591n/a size = PyUnicode_GET_LENGTH(obj);
1592n/a if (*start<0)
1593n/a *start = 0; /*XXX check for values <0*/
1594n/a if (*start>=size)
1595n/a *start = size-1;
1596n/a Py_DECREF(obj);
1597n/a return 0;
1598n/a}
1599n/a
1600n/a
1601n/aint
1602n/aPyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1603n/a{
1604n/a Py_ssize_t size;
1605n/a PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1606n/a if (!obj)
1607n/a return -1;
1608n/a size = PyBytes_GET_SIZE(obj);
1609n/a *start = ((PyUnicodeErrorObject *)exc)->start;
1610n/a if (*start<0)
1611n/a *start = 0;
1612n/a if (*start>=size)
1613n/a *start = size-1;
1614n/a Py_DECREF(obj);
1615n/a return 0;
1616n/a}
1617n/a
1618n/a
1619n/aint
1620n/aPyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)
1621n/a{
1622n/a return PyUnicodeEncodeError_GetStart(exc, start);
1623n/a}
1624n/a
1625n/a
1626n/aint
1627n/aPyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)
1628n/a{
1629n/a ((PyUnicodeErrorObject *)exc)->start = start;
1630n/a return 0;
1631n/a}
1632n/a
1633n/a
1634n/aint
1635n/aPyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)
1636n/a{
1637n/a ((PyUnicodeErrorObject *)exc)->start = start;
1638n/a return 0;
1639n/a}
1640n/a
1641n/a
1642n/aint
1643n/aPyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)
1644n/a{
1645n/a ((PyUnicodeErrorObject *)exc)->start = start;
1646n/a return 0;
1647n/a}
1648n/a
1649n/a
1650n/aint
1651n/aPyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1652n/a{
1653n/a Py_ssize_t size;
1654n/a PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1655n/a "object");
1656n/a if (!obj)
1657n/a return -1;
1658n/a *end = ((PyUnicodeErrorObject *)exc)->end;
1659n/a size = PyUnicode_GET_LENGTH(obj);
1660n/a if (*end<1)
1661n/a *end = 1;
1662n/a if (*end>size)
1663n/a *end = size;
1664n/a Py_DECREF(obj);
1665n/a return 0;
1666n/a}
1667n/a
1668n/a
1669n/aint
1670n/aPyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1671n/a{
1672n/a Py_ssize_t size;
1673n/a PyObject *obj = get_string(((PyUnicodeErrorObject *)exc)->object, "object");
1674n/a if (!obj)
1675n/a return -1;
1676n/a size = PyBytes_GET_SIZE(obj);
1677n/a *end = ((PyUnicodeErrorObject *)exc)->end;
1678n/a if (*end<1)
1679n/a *end = 1;
1680n/a if (*end>size)
1681n/a *end = size;
1682n/a Py_DECREF(obj);
1683n/a return 0;
1684n/a}
1685n/a
1686n/a
1687n/aint
1688n/aPyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *start)
1689n/a{
1690n/a return PyUnicodeEncodeError_GetEnd(exc, start);
1691n/a}
1692n/a
1693n/a
1694n/aint
1695n/aPyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1696n/a{
1697n/a ((PyUnicodeErrorObject *)exc)->end = end;
1698n/a return 0;
1699n/a}
1700n/a
1701n/a
1702n/aint
1703n/aPyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)
1704n/a{
1705n/a ((PyUnicodeErrorObject *)exc)->end = end;
1706n/a return 0;
1707n/a}
1708n/a
1709n/a
1710n/aint
1711n/aPyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)
1712n/a{
1713n/a ((PyUnicodeErrorObject *)exc)->end = end;
1714n/a return 0;
1715n/a}
1716n/a
1717n/aPyObject *
1718n/aPyUnicodeEncodeError_GetReason(PyObject *exc)
1719n/a{
1720n/a return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
1721n/a}
1722n/a
1723n/a
1724n/aPyObject *
1725n/aPyUnicodeDecodeError_GetReason(PyObject *exc)
1726n/a{
1727n/a return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
1728n/a}
1729n/a
1730n/a
1731n/aPyObject *
1732n/aPyUnicodeTranslateError_GetReason(PyObject *exc)
1733n/a{
1734n/a return get_unicode(((PyUnicodeErrorObject *)exc)->reason, "reason");
1735n/a}
1736n/a
1737n/a
1738n/aint
1739n/aPyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)
1740n/a{
1741n/a return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1742n/a reason);
1743n/a}
1744n/a
1745n/a
1746n/aint
1747n/aPyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)
1748n/a{
1749n/a return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1750n/a reason);
1751n/a}
1752n/a
1753n/a
1754n/aint
1755n/aPyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)
1756n/a{
1757n/a return set_unicodefromstring(&((PyUnicodeErrorObject *)exc)->reason,
1758n/a reason);
1759n/a}
1760n/a
1761n/a
1762n/astatic int
1763n/aUnicodeError_clear(PyUnicodeErrorObject *self)
1764n/a{
1765n/a Py_CLEAR(self->encoding);
1766n/a Py_CLEAR(self->object);
1767n/a Py_CLEAR(self->reason);
1768n/a return BaseException_clear((PyBaseExceptionObject *)self);
1769n/a}
1770n/a
1771n/astatic void
1772n/aUnicodeError_dealloc(PyUnicodeErrorObject *self)
1773n/a{
1774n/a _PyObject_GC_UNTRACK(self);
1775n/a UnicodeError_clear(self);
1776n/a Py_TYPE(self)->tp_free((PyObject *)self);
1777n/a}
1778n/a
1779n/astatic int
1780n/aUnicodeError_traverse(PyUnicodeErrorObject *self, visitproc visit, void *arg)
1781n/a{
1782n/a Py_VISIT(self->encoding);
1783n/a Py_VISIT(self->object);
1784n/a Py_VISIT(self->reason);
1785n/a return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1786n/a}
1787n/a
1788n/astatic PyMemberDef UnicodeError_members[] = {
1789n/a {"encoding", T_OBJECT, offsetof(PyUnicodeErrorObject, encoding), 0,
1790n/a PyDoc_STR("exception encoding")},
1791n/a {"object", T_OBJECT, offsetof(PyUnicodeErrorObject, object), 0,
1792n/a PyDoc_STR("exception object")},
1793n/a {"start", T_PYSSIZET, offsetof(PyUnicodeErrorObject, start), 0,
1794n/a PyDoc_STR("exception start")},
1795n/a {"end", T_PYSSIZET, offsetof(PyUnicodeErrorObject, end), 0,
1796n/a PyDoc_STR("exception end")},
1797n/a {"reason", T_OBJECT, offsetof(PyUnicodeErrorObject, reason), 0,
1798n/a PyDoc_STR("exception reason")},
1799n/a {NULL} /* Sentinel */
1800n/a};
1801n/a
1802n/a
1803n/a/*
1804n/a * UnicodeEncodeError extends UnicodeError
1805n/a */
1806n/a
1807n/astatic int
1808n/aUnicodeEncodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1809n/a{
1810n/a PyUnicodeErrorObject *err;
1811n/a
1812n/a if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1813n/a return -1;
1814n/a
1815n/a err = (PyUnicodeErrorObject *)self;
1816n/a
1817n/a Py_CLEAR(err->encoding);
1818n/a Py_CLEAR(err->object);
1819n/a Py_CLEAR(err->reason);
1820n/a
1821n/a if (!PyArg_ParseTuple(args, "UUnnU",
1822n/a &err->encoding, &err->object,
1823n/a &err->start, &err->end, &err->reason)) {
1824n/a err->encoding = err->object = err->reason = NULL;
1825n/a return -1;
1826n/a }
1827n/a
1828n/a Py_INCREF(err->encoding);
1829n/a Py_INCREF(err->object);
1830n/a Py_INCREF(err->reason);
1831n/a
1832n/a return 0;
1833n/a}
1834n/a
1835n/astatic PyObject *
1836n/aUnicodeEncodeError_str(PyObject *self)
1837n/a{
1838n/a PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
1839n/a PyObject *result = NULL;
1840n/a PyObject *reason_str = NULL;
1841n/a PyObject *encoding_str = NULL;
1842n/a
1843n/a if (!uself->object)
1844n/a /* Not properly initialized. */
1845n/a return PyUnicode_FromString("");
1846n/a
1847n/a /* Get reason and encoding as strings, which they might not be if
1848n/a they've been modified after we were constructed. */
1849n/a reason_str = PyObject_Str(uself->reason);
1850n/a if (reason_str == NULL)
1851n/a goto done;
1852n/a encoding_str = PyObject_Str(uself->encoding);
1853n/a if (encoding_str == NULL)
1854n/a goto done;
1855n/a
1856n/a if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) {
1857n/a Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
1858n/a const char *fmt;
1859n/a if (badchar <= 0xff)
1860n/a fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
1861n/a else if (badchar <= 0xffff)
1862n/a fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
1863n/a else
1864n/a fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
1865n/a result = PyUnicode_FromFormat(
1866n/a fmt,
1867n/a encoding_str,
1868n/a (int)badchar,
1869n/a uself->start,
1870n/a reason_str);
1871n/a }
1872n/a else {
1873n/a result = PyUnicode_FromFormat(
1874n/a "'%U' codec can't encode characters in position %zd-%zd: %U",
1875n/a encoding_str,
1876n/a uself->start,
1877n/a uself->end-1,
1878n/a reason_str);
1879n/a }
1880n/adone:
1881n/a Py_XDECREF(reason_str);
1882n/a Py_XDECREF(encoding_str);
1883n/a return result;
1884n/a}
1885n/a
1886n/astatic PyTypeObject _PyExc_UnicodeEncodeError = {
1887n/a PyVarObject_HEAD_INIT(NULL, 0)
1888n/a "UnicodeEncodeError",
1889n/a sizeof(PyUnicodeErrorObject), 0,
1890n/a (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1891n/a (reprfunc)UnicodeEncodeError_str, 0, 0, 0,
1892n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1893n/a PyDoc_STR("Unicode encoding error."), (traverseproc)UnicodeError_traverse,
1894n/a (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1895n/a 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
1896n/a (initproc)UnicodeEncodeError_init, 0, BaseException_new,
1897n/a};
1898n/aPyObject *PyExc_UnicodeEncodeError = (PyObject *)&_PyExc_UnicodeEncodeError;
1899n/a
1900n/aPyObject *
1901n/aPyUnicodeEncodeError_Create(
1902n/a const char *encoding, const Py_UNICODE *object, Py_ssize_t length,
1903n/a Py_ssize_t start, Py_ssize_t end, const char *reason)
1904n/a{
1905n/a return PyObject_CallFunction(PyExc_UnicodeEncodeError, "su#nns",
1906n/a encoding, object, length, start, end, reason);
1907n/a}
1908n/a
1909n/a
1910n/a/*
1911n/a * UnicodeDecodeError extends UnicodeError
1912n/a */
1913n/a
1914n/astatic int
1915n/aUnicodeDecodeError_init(PyObject *self, PyObject *args, PyObject *kwds)
1916n/a{
1917n/a PyUnicodeErrorObject *ude;
1918n/a
1919n/a if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
1920n/a return -1;
1921n/a
1922n/a ude = (PyUnicodeErrorObject *)self;
1923n/a
1924n/a Py_CLEAR(ude->encoding);
1925n/a Py_CLEAR(ude->object);
1926n/a Py_CLEAR(ude->reason);
1927n/a
1928n/a if (!PyArg_ParseTuple(args, "UOnnU",
1929n/a &ude->encoding, &ude->object,
1930n/a &ude->start, &ude->end, &ude->reason)) {
1931n/a ude->encoding = ude->object = ude->reason = NULL;
1932n/a return -1;
1933n/a }
1934n/a
1935n/a Py_INCREF(ude->encoding);
1936n/a Py_INCREF(ude->object);
1937n/a Py_INCREF(ude->reason);
1938n/a
1939n/a if (!PyBytes_Check(ude->object)) {
1940n/a Py_buffer view;
1941n/a if (PyObject_GetBuffer(ude->object, &view, PyBUF_SIMPLE) != 0)
1942n/a goto error;
1943n/a Py_XSETREF(ude->object, PyBytes_FromStringAndSize(view.buf, view.len));
1944n/a PyBuffer_Release(&view);
1945n/a if (!ude->object)
1946n/a goto error;
1947n/a }
1948n/a return 0;
1949n/a
1950n/aerror:
1951n/a Py_CLEAR(ude->encoding);
1952n/a Py_CLEAR(ude->object);
1953n/a Py_CLEAR(ude->reason);
1954n/a return -1;
1955n/a}
1956n/a
1957n/astatic PyObject *
1958n/aUnicodeDecodeError_str(PyObject *self)
1959n/a{
1960n/a PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
1961n/a PyObject *result = NULL;
1962n/a PyObject *reason_str = NULL;
1963n/a PyObject *encoding_str = NULL;
1964n/a
1965n/a if (!uself->object)
1966n/a /* Not properly initialized. */
1967n/a return PyUnicode_FromString("");
1968n/a
1969n/a /* Get reason and encoding as strings, which they might not be if
1970n/a they've been modified after we were constructed. */
1971n/a reason_str = PyObject_Str(uself->reason);
1972n/a if (reason_str == NULL)
1973n/a goto done;
1974n/a encoding_str = PyObject_Str(uself->encoding);
1975n/a if (encoding_str == NULL)
1976n/a goto done;
1977n/a
1978n/a if (uself->start < PyBytes_GET_SIZE(uself->object) && uself->end == uself->start+1) {
1979n/a int byte = (int)(PyBytes_AS_STRING(((PyUnicodeErrorObject *)self)->object)[uself->start]&0xff);
1980n/a result = PyUnicode_FromFormat(
1981n/a "'%U' codec can't decode byte 0x%02x in position %zd: %U",
1982n/a encoding_str,
1983n/a byte,
1984n/a uself->start,
1985n/a reason_str);
1986n/a }
1987n/a else {
1988n/a result = PyUnicode_FromFormat(
1989n/a "'%U' codec can't decode bytes in position %zd-%zd: %U",
1990n/a encoding_str,
1991n/a uself->start,
1992n/a uself->end-1,
1993n/a reason_str
1994n/a );
1995n/a }
1996n/adone:
1997n/a Py_XDECREF(reason_str);
1998n/a Py_XDECREF(encoding_str);
1999n/a return result;
2000n/a}
2001n/a
2002n/astatic PyTypeObject _PyExc_UnicodeDecodeError = {
2003n/a PyVarObject_HEAD_INIT(NULL, 0)
2004n/a "UnicodeDecodeError",
2005n/a sizeof(PyUnicodeErrorObject), 0,
2006n/a (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2007n/a (reprfunc)UnicodeDecodeError_str, 0, 0, 0,
2008n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
2009n/a PyDoc_STR("Unicode decoding error."), (traverseproc)UnicodeError_traverse,
2010n/a (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
2011n/a 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
2012n/a (initproc)UnicodeDecodeError_init, 0, BaseException_new,
2013n/a};
2014n/aPyObject *PyExc_UnicodeDecodeError = (PyObject *)&_PyExc_UnicodeDecodeError;
2015n/a
2016n/aPyObject *
2017n/aPyUnicodeDecodeError_Create(
2018n/a const char *encoding, const char *object, Py_ssize_t length,
2019n/a Py_ssize_t start, Py_ssize_t end, const char *reason)
2020n/a{
2021n/a return PyObject_CallFunction(PyExc_UnicodeDecodeError, "sy#nns",
2022n/a encoding, object, length, start, end, reason);
2023n/a}
2024n/a
2025n/a
2026n/a/*
2027n/a * UnicodeTranslateError extends UnicodeError
2028n/a */
2029n/a
2030n/astatic int
2031n/aUnicodeTranslateError_init(PyUnicodeErrorObject *self, PyObject *args,
2032n/a PyObject *kwds)
2033n/a{
2034n/a if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
2035n/a return -1;
2036n/a
2037n/a Py_CLEAR(self->object);
2038n/a Py_CLEAR(self->reason);
2039n/a
2040n/a if (!PyArg_ParseTuple(args, "UnnU",
2041n/a &self->object,
2042n/a &self->start, &self->end, &self->reason)) {
2043n/a self->object = self->reason = NULL;
2044n/a return -1;
2045n/a }
2046n/a
2047n/a Py_INCREF(self->object);
2048n/a Py_INCREF(self->reason);
2049n/a
2050n/a return 0;
2051n/a}
2052n/a
2053n/a
2054n/astatic PyObject *
2055n/aUnicodeTranslateError_str(PyObject *self)
2056n/a{
2057n/a PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
2058n/a PyObject *result = NULL;
2059n/a PyObject *reason_str = NULL;
2060n/a
2061n/a if (!uself->object)
2062n/a /* Not properly initialized. */
2063n/a return PyUnicode_FromString("");
2064n/a
2065n/a /* Get reason as a string, which it might not be if it's been
2066n/a modified after we were constructed. */
2067n/a reason_str = PyObject_Str(uself->reason);
2068n/a if (reason_str == NULL)
2069n/a goto done;
2070n/a
2071n/a if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == uself->start+1) {
2072n/a Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
2073n/a const char *fmt;
2074n/a if (badchar <= 0xff)
2075n/a fmt = "can't translate character '\\x%02x' in position %zd: %U";
2076n/a else if (badchar <= 0xffff)
2077n/a fmt = "can't translate character '\\u%04x' in position %zd: %U";
2078n/a else
2079n/a fmt = "can't translate character '\\U%08x' in position %zd: %U";
2080n/a result = PyUnicode_FromFormat(
2081n/a fmt,
2082n/a (int)badchar,
2083n/a uself->start,
2084n/a reason_str
2085n/a );
2086n/a } else {
2087n/a result = PyUnicode_FromFormat(
2088n/a "can't translate characters in position %zd-%zd: %U",
2089n/a uself->start,
2090n/a uself->end-1,
2091n/a reason_str
2092n/a );
2093n/a }
2094n/adone:
2095n/a Py_XDECREF(reason_str);
2096n/a return result;
2097n/a}
2098n/a
2099n/astatic PyTypeObject _PyExc_UnicodeTranslateError = {
2100n/a PyVarObject_HEAD_INIT(NULL, 0)
2101n/a "UnicodeTranslateError",
2102n/a sizeof(PyUnicodeErrorObject), 0,
2103n/a (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2104n/a (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
2105n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
2106n/a PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse,
2107n/a (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
2108n/a 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
2109n/a (initproc)UnicodeTranslateError_init, 0, BaseException_new,
2110n/a};
2111n/aPyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateError;
2112n/a
2113n/a/* Deprecated. */
2114n/aPyObject *
2115n/aPyUnicodeTranslateError_Create(
2116n/a const Py_UNICODE *object, Py_ssize_t length,
2117n/a Py_ssize_t start, Py_ssize_t end, const char *reason)
2118n/a{
2119n/a return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
2120n/a object, length, start, end, reason);
2121n/a}
2122n/a
2123n/aPyObject *
2124n/a_PyUnicodeTranslateError_Create(
2125n/a PyObject *object,
2126n/a Py_ssize_t start, Py_ssize_t end, const char *reason)
2127n/a{
2128n/a return PyObject_CallFunction(PyExc_UnicodeTranslateError, "Onns",
2129n/a object, start, end, reason);
2130n/a}
2131n/a
2132n/a/*
2133n/a * AssertionError extends Exception
2134n/a */
2135n/aSimpleExtendsException(PyExc_Exception, AssertionError,
2136n/a "Assertion failed.");
2137n/a
2138n/a
2139n/a/*
2140n/a * ArithmeticError extends Exception
2141n/a */
2142n/aSimpleExtendsException(PyExc_Exception, ArithmeticError,
2143n/a "Base class for arithmetic errors.");
2144n/a
2145n/a
2146n/a/*
2147n/a * FloatingPointError extends ArithmeticError
2148n/a */
2149n/aSimpleExtendsException(PyExc_ArithmeticError, FloatingPointError,
2150n/a "Floating point operation failed.");
2151n/a
2152n/a
2153n/a/*
2154n/a * OverflowError extends ArithmeticError
2155n/a */
2156n/aSimpleExtendsException(PyExc_ArithmeticError, OverflowError,
2157n/a "Result too large to be represented.");
2158n/a
2159n/a
2160n/a/*
2161n/a * ZeroDivisionError extends ArithmeticError
2162n/a */
2163n/aSimpleExtendsException(PyExc_ArithmeticError, ZeroDivisionError,
2164n/a "Second argument to a division or modulo operation was zero.");
2165n/a
2166n/a
2167n/a/*
2168n/a * SystemError extends Exception
2169n/a */
2170n/aSimpleExtendsException(PyExc_Exception, SystemError,
2171n/a "Internal error in the Python interpreter.\n"
2172n/a "\n"
2173n/a "Please report this to the Python maintainer, along with the traceback,\n"
2174n/a "the Python version, and the hardware/OS platform and version.");
2175n/a
2176n/a
2177n/a/*
2178n/a * ReferenceError extends Exception
2179n/a */
2180n/aSimpleExtendsException(PyExc_Exception, ReferenceError,
2181n/a "Weak ref proxy used after referent went away.");
2182n/a
2183n/a
2184n/a/*
2185n/a * MemoryError extends Exception
2186n/a */
2187n/a
2188n/a#define MEMERRORS_SAVE 16
2189n/astatic PyBaseExceptionObject *memerrors_freelist = NULL;
2190n/astatic int memerrors_numfree = 0;
2191n/a
2192n/astatic PyObject *
2193n/aMemoryError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2194n/a{
2195n/a PyBaseExceptionObject *self;
2196n/a
2197n/a if (type != (PyTypeObject *) PyExc_MemoryError)
2198n/a return BaseException_new(type, args, kwds);
2199n/a if (memerrors_freelist == NULL)
2200n/a return BaseException_new(type, args, kwds);
2201n/a /* Fetch object from freelist and revive it */
2202n/a self = memerrors_freelist;
2203n/a self->args = PyTuple_New(0);
2204n/a /* This shouldn't happen since the empty tuple is persistent */
2205n/a if (self->args == NULL)
2206n/a return NULL;
2207n/a memerrors_freelist = (PyBaseExceptionObject *) self->dict;
2208n/a memerrors_numfree--;
2209n/a self->dict = NULL;
2210n/a _Py_NewReference((PyObject *)self);
2211n/a _PyObject_GC_TRACK(self);
2212n/a return (PyObject *)self;
2213n/a}
2214n/a
2215n/astatic void
2216n/aMemoryError_dealloc(PyBaseExceptionObject *self)
2217n/a{
2218n/a _PyObject_GC_UNTRACK(self);
2219n/a BaseException_clear(self);
2220n/a if (memerrors_numfree >= MEMERRORS_SAVE)
2221n/a Py_TYPE(self)->tp_free((PyObject *)self);
2222n/a else {
2223n/a self->dict = (PyObject *) memerrors_freelist;
2224n/a memerrors_freelist = self;
2225n/a memerrors_numfree++;
2226n/a }
2227n/a}
2228n/a
2229n/astatic void
2230n/apreallocate_memerrors(void)
2231n/a{
2232n/a /* We create enough MemoryErrors and then decref them, which will fill
2233n/a up the freelist. */
2234n/a int i;
2235n/a PyObject *errors[MEMERRORS_SAVE];
2236n/a for (i = 0; i < MEMERRORS_SAVE; i++) {
2237n/a errors[i] = MemoryError_new((PyTypeObject *) PyExc_MemoryError,
2238n/a NULL, NULL);
2239n/a if (!errors[i])
2240n/a Py_FatalError("Could not preallocate MemoryError object");
2241n/a }
2242n/a for (i = 0; i < MEMERRORS_SAVE; i++) {
2243n/a Py_DECREF(errors[i]);
2244n/a }
2245n/a}
2246n/a
2247n/astatic void
2248n/afree_preallocated_memerrors(void)
2249n/a{
2250n/a while (memerrors_freelist != NULL) {
2251n/a PyObject *self = (PyObject *) memerrors_freelist;
2252n/a memerrors_freelist = (PyBaseExceptionObject *) memerrors_freelist->dict;
2253n/a Py_TYPE(self)->tp_free((PyObject *)self);
2254n/a }
2255n/a}
2256n/a
2257n/a
2258n/astatic PyTypeObject _PyExc_MemoryError = {
2259n/a PyVarObject_HEAD_INIT(NULL, 0)
2260n/a "MemoryError",
2261n/a sizeof(PyBaseExceptionObject),
2262n/a 0, (destructor)MemoryError_dealloc, 0, 0, 0, 0, 0, 0, 0,
2263n/a 0, 0, 0, 0, 0, 0, 0,
2264n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
2265n/a PyDoc_STR("Out of memory."), (traverseproc)BaseException_traverse,
2266n/a (inquiry)BaseException_clear, 0, 0, 0, 0, 0, 0, 0, &_PyExc_Exception,
2267n/a 0, 0, 0, offsetof(PyBaseExceptionObject, dict),
2268n/a (initproc)BaseException_init, 0, MemoryError_new
2269n/a};
2270n/aPyObject *PyExc_MemoryError = (PyObject *) &_PyExc_MemoryError;
2271n/a
2272n/a
2273n/a/*
2274n/a * BufferError extends Exception
2275n/a */
2276n/aSimpleExtendsException(PyExc_Exception, BufferError, "Buffer error.");
2277n/a
2278n/a
2279n/a/* Warning category docstrings */
2280n/a
2281n/a/*
2282n/a * Warning extends Exception
2283n/a */
2284n/aSimpleExtendsException(PyExc_Exception, Warning,
2285n/a "Base class for warning categories.");
2286n/a
2287n/a
2288n/a/*
2289n/a * UserWarning extends Warning
2290n/a */
2291n/aSimpleExtendsException(PyExc_Warning, UserWarning,
2292n/a "Base class for warnings generated by user code.");
2293n/a
2294n/a
2295n/a/*
2296n/a * DeprecationWarning extends Warning
2297n/a */
2298n/aSimpleExtendsException(PyExc_Warning, DeprecationWarning,
2299n/a "Base class for warnings about deprecated features.");
2300n/a
2301n/a
2302n/a/*
2303n/a * PendingDeprecationWarning extends Warning
2304n/a */
2305n/aSimpleExtendsException(PyExc_Warning, PendingDeprecationWarning,
2306n/a "Base class for warnings about features which will be deprecated\n"
2307n/a "in the future.");
2308n/a
2309n/a
2310n/a/*
2311n/a * SyntaxWarning extends Warning
2312n/a */
2313n/aSimpleExtendsException(PyExc_Warning, SyntaxWarning,
2314n/a "Base class for warnings about dubious syntax.");
2315n/a
2316n/a
2317n/a/*
2318n/a * RuntimeWarning extends Warning
2319n/a */
2320n/aSimpleExtendsException(PyExc_Warning, RuntimeWarning,
2321n/a "Base class for warnings about dubious runtime behavior.");
2322n/a
2323n/a
2324n/a/*
2325n/a * FutureWarning extends Warning
2326n/a */
2327n/aSimpleExtendsException(PyExc_Warning, FutureWarning,
2328n/a "Base class for warnings about constructs that will change semantically\n"
2329n/a "in the future.");
2330n/a
2331n/a
2332n/a/*
2333n/a * ImportWarning extends Warning
2334n/a */
2335n/aSimpleExtendsException(PyExc_Warning, ImportWarning,
2336n/a "Base class for warnings about probable mistakes in module imports");
2337n/a
2338n/a
2339n/a/*
2340n/a * UnicodeWarning extends Warning
2341n/a */
2342n/aSimpleExtendsException(PyExc_Warning, UnicodeWarning,
2343n/a "Base class for warnings about Unicode related problems, mostly\n"
2344n/a "related to conversion problems.");
2345n/a
2346n/a
2347n/a/*
2348n/a * BytesWarning extends Warning
2349n/a */
2350n/aSimpleExtendsException(PyExc_Warning, BytesWarning,
2351n/a "Base class for warnings about bytes and buffer related problems, mostly\n"
2352n/a "related to conversion from str or comparing to str.");
2353n/a
2354n/a
2355n/a/*
2356n/a * ResourceWarning extends Warning
2357n/a */
2358n/aSimpleExtendsException(PyExc_Warning, ResourceWarning,
2359n/a "Base class for warnings about resource usage.");
2360n/a
2361n/a
2362n/a
2363n/a/* Pre-computed RecursionError instance for when recursion depth is reached.
2364n/a Meant to be used when normalizing the exception for exceeding the recursion
2365n/a depth will cause its own infinite recursion.
2366n/a*/
2367n/aPyObject *PyExc_RecursionErrorInst = NULL;
2368n/a
2369n/a#define PRE_INIT(TYPE) \
2370n/a if (!(_PyExc_ ## TYPE.tp_flags & Py_TPFLAGS_READY)) { \
2371n/a if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
2372n/a Py_FatalError("exceptions bootstrapping error."); \
2373n/a Py_INCREF(PyExc_ ## TYPE); \
2374n/a }
2375n/a
2376n/a#define POST_INIT(TYPE) \
2377n/a if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
2378n/a Py_FatalError("Module dictionary insertion problem.");
2379n/a
2380n/a#define INIT_ALIAS(NAME, TYPE) Py_INCREF(PyExc_ ## TYPE); \
2381n/a Py_XDECREF(PyExc_ ## NAME); \
2382n/a PyExc_ ## NAME = PyExc_ ## TYPE; \
2383n/a if (PyDict_SetItemString(bdict, # NAME, PyExc_ ## NAME)) \
2384n/a Py_FatalError("Module dictionary insertion problem.");
2385n/a
2386n/a#define ADD_ERRNO(TYPE, CODE) { \
2387n/a PyObject *_code = PyLong_FromLong(CODE); \
2388n/a assert(_PyObject_RealIsSubclass(PyExc_ ## TYPE, PyExc_OSError)); \
2389n/a if (!_code || PyDict_SetItem(errnomap, _code, PyExc_ ## TYPE)) \
2390n/a Py_FatalError("errmap insertion problem."); \
2391n/a Py_DECREF(_code); \
2392n/a }
2393n/a
2394n/a#ifdef MS_WINDOWS
2395n/a#include <winsock2.h>
2396n/a/* The following constants were added to errno.h in VS2010 but have
2397n/a preferred WSA equivalents. */
2398n/a#undef EADDRINUSE
2399n/a#undef EADDRNOTAVAIL
2400n/a#undef EAFNOSUPPORT
2401n/a#undef EALREADY
2402n/a#undef ECONNABORTED
2403n/a#undef ECONNREFUSED
2404n/a#undef ECONNRESET
2405n/a#undef EDESTADDRREQ
2406n/a#undef EHOSTUNREACH
2407n/a#undef EINPROGRESS
2408n/a#undef EISCONN
2409n/a#undef ELOOP
2410n/a#undef EMSGSIZE
2411n/a#undef ENETDOWN
2412n/a#undef ENETRESET
2413n/a#undef ENETUNREACH
2414n/a#undef ENOBUFS
2415n/a#undef ENOPROTOOPT
2416n/a#undef ENOTCONN
2417n/a#undef ENOTSOCK
2418n/a#undef EOPNOTSUPP
2419n/a#undef EPROTONOSUPPORT
2420n/a#undef EPROTOTYPE
2421n/a#undef ETIMEDOUT
2422n/a#undef EWOULDBLOCK
2423n/a
2424n/a#if defined(WSAEALREADY) && !defined(EALREADY)
2425n/a#define EALREADY WSAEALREADY
2426n/a#endif
2427n/a#if defined(WSAECONNABORTED) && !defined(ECONNABORTED)
2428n/a#define ECONNABORTED WSAECONNABORTED
2429n/a#endif
2430n/a#if defined(WSAECONNREFUSED) && !defined(ECONNREFUSED)
2431n/a#define ECONNREFUSED WSAECONNREFUSED
2432n/a#endif
2433n/a#if defined(WSAECONNRESET) && !defined(ECONNRESET)
2434n/a#define ECONNRESET WSAECONNRESET
2435n/a#endif
2436n/a#if defined(WSAEINPROGRESS) && !defined(EINPROGRESS)
2437n/a#define EINPROGRESS WSAEINPROGRESS
2438n/a#endif
2439n/a#if defined(WSAESHUTDOWN) && !defined(ESHUTDOWN)
2440n/a#define ESHUTDOWN WSAESHUTDOWN
2441n/a#endif
2442n/a#if defined(WSAETIMEDOUT) && !defined(ETIMEDOUT)
2443n/a#define ETIMEDOUT WSAETIMEDOUT
2444n/a#endif
2445n/a#if defined(WSAEWOULDBLOCK) && !defined(EWOULDBLOCK)
2446n/a#define EWOULDBLOCK WSAEWOULDBLOCK
2447n/a#endif
2448n/a#endif /* MS_WINDOWS */
2449n/a
2450n/avoid
2451n/a_PyExc_Init(PyObject *bltinmod)
2452n/a{
2453n/a PyObject *bdict;
2454n/a
2455n/a PRE_INIT(BaseException)
2456n/a PRE_INIT(Exception)
2457n/a PRE_INIT(TypeError)
2458n/a PRE_INIT(StopAsyncIteration)
2459n/a PRE_INIT(StopIteration)
2460n/a PRE_INIT(GeneratorExit)
2461n/a PRE_INIT(SystemExit)
2462n/a PRE_INIT(KeyboardInterrupt)
2463n/a PRE_INIT(ImportError)
2464n/a PRE_INIT(ModuleNotFoundError)
2465n/a PRE_INIT(OSError)
2466n/a PRE_INIT(EOFError)
2467n/a PRE_INIT(RuntimeError)
2468n/a PRE_INIT(RecursionError)
2469n/a PRE_INIT(NotImplementedError)
2470n/a PRE_INIT(NameError)
2471n/a PRE_INIT(UnboundLocalError)
2472n/a PRE_INIT(AttributeError)
2473n/a PRE_INIT(SyntaxError)
2474n/a PRE_INIT(IndentationError)
2475n/a PRE_INIT(TabError)
2476n/a PRE_INIT(LookupError)
2477n/a PRE_INIT(IndexError)
2478n/a PRE_INIT(KeyError)
2479n/a PRE_INIT(ValueError)
2480n/a PRE_INIT(UnicodeError)
2481n/a PRE_INIT(UnicodeEncodeError)
2482n/a PRE_INIT(UnicodeDecodeError)
2483n/a PRE_INIT(UnicodeTranslateError)
2484n/a PRE_INIT(AssertionError)
2485n/a PRE_INIT(ArithmeticError)
2486n/a PRE_INIT(FloatingPointError)
2487n/a PRE_INIT(OverflowError)
2488n/a PRE_INIT(ZeroDivisionError)
2489n/a PRE_INIT(SystemError)
2490n/a PRE_INIT(ReferenceError)
2491n/a PRE_INIT(BufferError)
2492n/a PRE_INIT(MemoryError)
2493n/a PRE_INIT(BufferError)
2494n/a PRE_INIT(Warning)
2495n/a PRE_INIT(UserWarning)
2496n/a PRE_INIT(DeprecationWarning)
2497n/a PRE_INIT(PendingDeprecationWarning)
2498n/a PRE_INIT(SyntaxWarning)
2499n/a PRE_INIT(RuntimeWarning)
2500n/a PRE_INIT(FutureWarning)
2501n/a PRE_INIT(ImportWarning)
2502n/a PRE_INIT(UnicodeWarning)
2503n/a PRE_INIT(BytesWarning)
2504n/a PRE_INIT(ResourceWarning)
2505n/a
2506n/a /* OSError subclasses */
2507n/a PRE_INIT(ConnectionError);
2508n/a
2509n/a PRE_INIT(BlockingIOError);
2510n/a PRE_INIT(BrokenPipeError);
2511n/a PRE_INIT(ChildProcessError);
2512n/a PRE_INIT(ConnectionAbortedError);
2513n/a PRE_INIT(ConnectionRefusedError);
2514n/a PRE_INIT(ConnectionResetError);
2515n/a PRE_INIT(FileExistsError);
2516n/a PRE_INIT(FileNotFoundError);
2517n/a PRE_INIT(IsADirectoryError);
2518n/a PRE_INIT(NotADirectoryError);
2519n/a PRE_INIT(InterruptedError);
2520n/a PRE_INIT(PermissionError);
2521n/a PRE_INIT(ProcessLookupError);
2522n/a PRE_INIT(TimeoutError);
2523n/a
2524n/a bdict = PyModule_GetDict(bltinmod);
2525n/a if (bdict == NULL)
2526n/a Py_FatalError("exceptions bootstrapping error.");
2527n/a
2528n/a POST_INIT(BaseException)
2529n/a POST_INIT(Exception)
2530n/a POST_INIT(TypeError)
2531n/a POST_INIT(StopAsyncIteration)
2532n/a POST_INIT(StopIteration)
2533n/a POST_INIT(GeneratorExit)
2534n/a POST_INIT(SystemExit)
2535n/a POST_INIT(KeyboardInterrupt)
2536n/a POST_INIT(ImportError)
2537n/a POST_INIT(ModuleNotFoundError)
2538n/a POST_INIT(OSError)
2539n/a INIT_ALIAS(EnvironmentError, OSError)
2540n/a INIT_ALIAS(IOError, OSError)
2541n/a#ifdef MS_WINDOWS
2542n/a INIT_ALIAS(WindowsError, OSError)
2543n/a#endif
2544n/a POST_INIT(EOFError)
2545n/a POST_INIT(RuntimeError)
2546n/a POST_INIT(RecursionError)
2547n/a POST_INIT(NotImplementedError)
2548n/a POST_INIT(NameError)
2549n/a POST_INIT(UnboundLocalError)
2550n/a POST_INIT(AttributeError)
2551n/a POST_INIT(SyntaxError)
2552n/a POST_INIT(IndentationError)
2553n/a POST_INIT(TabError)
2554n/a POST_INIT(LookupError)
2555n/a POST_INIT(IndexError)
2556n/a POST_INIT(KeyError)
2557n/a POST_INIT(ValueError)
2558n/a POST_INIT(UnicodeError)
2559n/a POST_INIT(UnicodeEncodeError)
2560n/a POST_INIT(UnicodeDecodeError)
2561n/a POST_INIT(UnicodeTranslateError)
2562n/a POST_INIT(AssertionError)
2563n/a POST_INIT(ArithmeticError)
2564n/a POST_INIT(FloatingPointError)
2565n/a POST_INIT(OverflowError)
2566n/a POST_INIT(ZeroDivisionError)
2567n/a POST_INIT(SystemError)
2568n/a POST_INIT(ReferenceError)
2569n/a POST_INIT(BufferError)
2570n/a POST_INIT(MemoryError)
2571n/a POST_INIT(BufferError)
2572n/a POST_INIT(Warning)
2573n/a POST_INIT(UserWarning)
2574n/a POST_INIT(DeprecationWarning)
2575n/a POST_INIT(PendingDeprecationWarning)
2576n/a POST_INIT(SyntaxWarning)
2577n/a POST_INIT(RuntimeWarning)
2578n/a POST_INIT(FutureWarning)
2579n/a POST_INIT(ImportWarning)
2580n/a POST_INIT(UnicodeWarning)
2581n/a POST_INIT(BytesWarning)
2582n/a POST_INIT(ResourceWarning)
2583n/a
2584n/a if (!errnomap) {
2585n/a errnomap = PyDict_New();
2586n/a if (!errnomap)
2587n/a Py_FatalError("Cannot allocate map from errnos to OSError subclasses");
2588n/a }
2589n/a
2590n/a /* OSError subclasses */
2591n/a POST_INIT(ConnectionError);
2592n/a
2593n/a POST_INIT(BlockingIOError);
2594n/a ADD_ERRNO(BlockingIOError, EAGAIN);
2595n/a ADD_ERRNO(BlockingIOError, EALREADY);
2596n/a ADD_ERRNO(BlockingIOError, EINPROGRESS);
2597n/a ADD_ERRNO(BlockingIOError, EWOULDBLOCK);
2598n/a POST_INIT(BrokenPipeError);
2599n/a ADD_ERRNO(BrokenPipeError, EPIPE);
2600n/a#ifdef ESHUTDOWN
2601n/a ADD_ERRNO(BrokenPipeError, ESHUTDOWN);
2602n/a#endif
2603n/a POST_INIT(ChildProcessError);
2604n/a ADD_ERRNO(ChildProcessError, ECHILD);
2605n/a POST_INIT(ConnectionAbortedError);
2606n/a ADD_ERRNO(ConnectionAbortedError, ECONNABORTED);
2607n/a POST_INIT(ConnectionRefusedError);
2608n/a ADD_ERRNO(ConnectionRefusedError, ECONNREFUSED);
2609n/a POST_INIT(ConnectionResetError);
2610n/a ADD_ERRNO(ConnectionResetError, ECONNRESET);
2611n/a POST_INIT(FileExistsError);
2612n/a ADD_ERRNO(FileExistsError, EEXIST);
2613n/a POST_INIT(FileNotFoundError);
2614n/a ADD_ERRNO(FileNotFoundError, ENOENT);
2615n/a POST_INIT(IsADirectoryError);
2616n/a ADD_ERRNO(IsADirectoryError, EISDIR);
2617n/a POST_INIT(NotADirectoryError);
2618n/a ADD_ERRNO(NotADirectoryError, ENOTDIR);
2619n/a POST_INIT(InterruptedError);
2620n/a ADD_ERRNO(InterruptedError, EINTR);
2621n/a POST_INIT(PermissionError);
2622n/a ADD_ERRNO(PermissionError, EACCES);
2623n/a ADD_ERRNO(PermissionError, EPERM);
2624n/a POST_INIT(ProcessLookupError);
2625n/a ADD_ERRNO(ProcessLookupError, ESRCH);
2626n/a POST_INIT(TimeoutError);
2627n/a ADD_ERRNO(TimeoutError, ETIMEDOUT);
2628n/a
2629n/a preallocate_memerrors();
2630n/a
2631n/a if (!PyExc_RecursionErrorInst) {
2632n/a PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RecursionError, NULL, NULL);
2633n/a if (!PyExc_RecursionErrorInst)
2634n/a Py_FatalError("Cannot pre-allocate RecursionError instance for "
2635n/a "recursion errors");
2636n/a else {
2637n/a PyBaseExceptionObject *err_inst =
2638n/a (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2639n/a PyObject *args_tuple;
2640n/a PyObject *exc_message;
2641n/a exc_message = PyUnicode_FromString("maximum recursion depth exceeded");
2642n/a if (!exc_message)
2643n/a Py_FatalError("cannot allocate argument for RecursionError "
2644n/a "pre-allocation");
2645n/a args_tuple = PyTuple_Pack(1, exc_message);
2646n/a if (!args_tuple)
2647n/a Py_FatalError("cannot allocate tuple for RecursionError "
2648n/a "pre-allocation");
2649n/a Py_DECREF(exc_message);
2650n/a if (BaseException_init(err_inst, args_tuple, NULL))
2651n/a Py_FatalError("init of pre-allocated RecursionError failed");
2652n/a Py_DECREF(args_tuple);
2653n/a }
2654n/a }
2655n/a}
2656n/a
2657n/avoid
2658n/a_PyExc_Fini(void)
2659n/a{
2660n/a Py_CLEAR(PyExc_RecursionErrorInst);
2661n/a free_preallocated_memerrors();
2662n/a Py_CLEAR(errnomap);
2663n/a}
2664n/a
2665n/a/* Helper to do the equivalent of "raise X from Y" in C, but always using
2666n/a * the current exception rather than passing one in.
2667n/a *
2668n/a * We currently limit this to *only* exceptions that use the BaseException
2669n/a * tp_init and tp_new methods, since we can be reasonably sure we can wrap
2670n/a * those correctly without losing data and without losing backwards
2671n/a * compatibility.
2672n/a *
2673n/a * We also aim to rule out *all* exceptions that might be storing additional
2674n/a * state, whether by having a size difference relative to BaseException,
2675n/a * additional arguments passed in during construction or by having a
2676n/a * non-empty instance dict.
2677n/a *
2678n/a * We need to be very careful with what we wrap, since changing types to
2679n/a * a broader exception type would be backwards incompatible for
2680n/a * existing codecs, and with different init or new method implementations
2681n/a * may either not support instantiation with PyErr_Format or lose
2682n/a * information when instantiated that way.
2683n/a *
2684n/a * XXX (ncoghlan): This could be made more comprehensive by exploiting the
2685n/a * fact that exceptions are expected to support pickling. If more builtin
2686n/a * exceptions (e.g. AttributeError) start to be converted to rich
2687n/a * exceptions with additional attributes, that's probably a better approach
2688n/a * to pursue over adding special cases for particular stateful subclasses.
2689n/a *
2690n/a * Returns a borrowed reference to the new exception (if any), NULL if the
2691n/a * existing exception was left in place.
2692n/a */
2693n/aPyObject *
2694n/a_PyErr_TrySetFromCause(const char *format, ...)
2695n/a{
2696n/a PyObject* msg_prefix;
2697n/a PyObject *exc, *val, *tb;
2698n/a PyTypeObject *caught_type;
2699n/a PyObject **dictptr;
2700n/a PyObject *instance_args;
2701n/a Py_ssize_t num_args, caught_type_size, base_exc_size;
2702n/a PyObject *new_exc, *new_val, *new_tb;
2703n/a va_list vargs;
2704n/a int same_basic_size;
2705n/a
2706n/a PyErr_Fetch(&exc, &val, &tb);
2707n/a caught_type = (PyTypeObject *)exc;
2708n/a /* Ensure type info indicates no extra state is stored at the C level
2709n/a * and that the type can be reinstantiated using PyErr_Format
2710n/a */
2711n/a caught_type_size = caught_type->tp_basicsize;
2712n/a base_exc_size = _PyExc_BaseException.tp_basicsize;
2713n/a same_basic_size = (
2714n/a caught_type_size == base_exc_size ||
2715n/a (PyType_SUPPORTS_WEAKREFS(caught_type) &&
2716n/a (caught_type_size == base_exc_size + (Py_ssize_t)sizeof(PyObject *))
2717n/a )
2718n/a );
2719n/a if (caught_type->tp_init != (initproc)BaseException_init ||
2720n/a caught_type->tp_new != BaseException_new ||
2721n/a !same_basic_size ||
2722n/a caught_type->tp_itemsize != _PyExc_BaseException.tp_itemsize) {
2723n/a /* We can't be sure we can wrap this safely, since it may contain
2724n/a * more state than just the exception type. Accordingly, we just
2725n/a * leave it alone.
2726n/a */
2727n/a PyErr_Restore(exc, val, tb);
2728n/a return NULL;
2729n/a }
2730n/a
2731n/a /* Check the args are empty or contain a single string */
2732n/a PyErr_NormalizeException(&exc, &val, &tb);
2733n/a instance_args = ((PyBaseExceptionObject *)val)->args;
2734n/a num_args = PyTuple_GET_SIZE(instance_args);
2735n/a if (num_args > 1 ||
2736n/a (num_args == 1 &&
2737n/a !PyUnicode_CheckExact(PyTuple_GET_ITEM(instance_args, 0)))) {
2738n/a /* More than 1 arg, or the one arg we do have isn't a string
2739n/a */
2740n/a PyErr_Restore(exc, val, tb);
2741n/a return NULL;
2742n/a }
2743n/a
2744n/a /* Ensure the instance dict is also empty */
2745n/a dictptr = _PyObject_GetDictPtr(val);
2746n/a if (dictptr != NULL && *dictptr != NULL &&
2747n/a PyObject_Length(*dictptr) > 0) {
2748n/a /* While we could potentially copy a non-empty instance dictionary
2749n/a * to the replacement exception, for now we take the more
2750n/a * conservative path of leaving exceptions with attributes set
2751n/a * alone.
2752n/a */
2753n/a PyErr_Restore(exc, val, tb);
2754n/a return NULL;
2755n/a }
2756n/a
2757n/a /* For exceptions that we can wrap safely, we chain the original
2758n/a * exception to a new one of the exact same type with an
2759n/a * error message that mentions the additional details and the
2760n/a * original exception.
2761n/a *
2762n/a * It would be nice to wrap OSError and various other exception
2763n/a * types as well, but that's quite a bit trickier due to the extra
2764n/a * state potentially stored on OSError instances.
2765n/a */
2766n/a /* Ensure the traceback is set correctly on the existing exception */
2767n/a if (tb != NULL) {
2768n/a PyException_SetTraceback(val, tb);
2769n/a Py_DECREF(tb);
2770n/a }
2771n/a
2772n/a#ifdef HAVE_STDARG_PROTOTYPES
2773n/a va_start(vargs, format);
2774n/a#else
2775n/a va_start(vargs);
2776n/a#endif
2777n/a msg_prefix = PyUnicode_FromFormatV(format, vargs);
2778n/a va_end(vargs);
2779n/a if (msg_prefix == NULL) {
2780n/a Py_DECREF(exc);
2781n/a Py_DECREF(val);
2782n/a return NULL;
2783n/a }
2784n/a
2785n/a PyErr_Format(exc, "%U (%s: %S)",
2786n/a msg_prefix, Py_TYPE(val)->tp_name, val);
2787n/a Py_DECREF(exc);
2788n/a Py_DECREF(msg_prefix);
2789n/a PyErr_Fetch(&new_exc, &new_val, &new_tb);
2790n/a PyErr_NormalizeException(&new_exc, &new_val, &new_tb);
2791n/a PyException_SetCause(new_val, val);
2792n/a PyErr_Restore(new_exc, new_val, new_tb);
2793n/a return new_val;
2794n/a}
2795n/a
2796n/a
2797n/a/* To help with migration from Python 2, SyntaxError.__init__ applies some
2798n/a * heuristics to try to report a more meaningful exception when print and
2799n/a * exec are used like statements.
2800n/a *
2801n/a * The heuristics are currently expected to detect the following cases:
2802n/a * - top level statement
2803n/a * - statement in a nested suite
2804n/a * - trailing section of a one line complex statement
2805n/a *
2806n/a * They're currently known not to trigger:
2807n/a * - after a semi-colon
2808n/a *
2809n/a * The error message can be a bit odd in cases where the "arguments" are
2810n/a * completely illegal syntactically, but that isn't worth the hassle of
2811n/a * fixing.
2812n/a *
2813n/a * We also can't do anything about cases that are legal Python 3 syntax
2814n/a * but mean something entirely different from what they did in Python 2
2815n/a * (omitting the arguments entirely, printing items preceded by a unary plus
2816n/a * or minus, using the stream redirection syntax).
2817n/a */
2818n/a
2819n/astatic int
2820n/a_check_for_legacy_statements(PySyntaxErrorObject *self, Py_ssize_t start)
2821n/a{
2822n/a /* Return values:
2823n/a * -1: an error occurred
2824n/a * 0: nothing happened
2825n/a * 1: the check triggered & the error message was changed
2826n/a */
2827n/a static PyObject *print_prefix = NULL;
2828n/a static PyObject *exec_prefix = NULL;
2829n/a Py_ssize_t text_len = PyUnicode_GET_LENGTH(self->text);
2830n/a int kind = PyUnicode_KIND(self->text);
2831n/a void *data = PyUnicode_DATA(self->text);
2832n/a
2833n/a /* Ignore leading whitespace */
2834n/a while (start < text_len) {
2835n/a Py_UCS4 ch = PyUnicode_READ(kind, data, start);
2836n/a if (!Py_UNICODE_ISSPACE(ch))
2837n/a break;
2838n/a start++;
2839n/a }
2840n/a /* Checking against an empty or whitespace-only part of the string */
2841n/a if (start == text_len) {
2842n/a return 0;
2843n/a }
2844n/a
2845n/a /* Check for legacy print statements */
2846n/a if (print_prefix == NULL) {
2847n/a print_prefix = PyUnicode_InternFromString("print ");
2848n/a if (print_prefix == NULL) {
2849n/a return -1;
2850n/a }
2851n/a }
2852n/a if (PyUnicode_Tailmatch(self->text, print_prefix,
2853n/a start, text_len, -1)) {
2854n/a Py_XSETREF(self->msg,
2855n/a PyUnicode_FromString("Missing parentheses in call to 'print'"));
2856n/a return 1;
2857n/a }
2858n/a
2859n/a /* Check for legacy exec statements */
2860n/a if (exec_prefix == NULL) {
2861n/a exec_prefix = PyUnicode_InternFromString("exec ");
2862n/a if (exec_prefix == NULL) {
2863n/a return -1;
2864n/a }
2865n/a }
2866n/a if (PyUnicode_Tailmatch(self->text, exec_prefix,
2867n/a start, text_len, -1)) {
2868n/a Py_XSETREF(self->msg,
2869n/a PyUnicode_FromString("Missing parentheses in call to 'exec'"));
2870n/a return 1;
2871n/a }
2872n/a /* Fall back to the default error message */
2873n/a return 0;
2874n/a}
2875n/a
2876n/astatic int
2877n/a_report_missing_parentheses(PySyntaxErrorObject *self)
2878n/a{
2879n/a Py_UCS4 left_paren = 40;
2880n/a Py_ssize_t left_paren_index;
2881n/a Py_ssize_t text_len = PyUnicode_GET_LENGTH(self->text);
2882n/a int legacy_check_result = 0;
2883n/a
2884n/a /* Skip entirely if there is an opening parenthesis */
2885n/a left_paren_index = PyUnicode_FindChar(self->text, left_paren,
2886n/a 0, text_len, 1);
2887n/a if (left_paren_index < -1) {
2888n/a return -1;
2889n/a }
2890n/a if (left_paren_index != -1) {
2891n/a /* Use default error message for any line with an opening paren */
2892n/a return 0;
2893n/a }
2894n/a /* Handle the simple statement case */
2895n/a legacy_check_result = _check_for_legacy_statements(self, 0);
2896n/a if (legacy_check_result < 0) {
2897n/a return -1;
2898n/a
2899n/a }
2900n/a if (legacy_check_result == 0) {
2901n/a /* Handle the one-line complex statement case */
2902n/a Py_UCS4 colon = 58;
2903n/a Py_ssize_t colon_index;
2904n/a colon_index = PyUnicode_FindChar(self->text, colon,
2905n/a 0, text_len, 1);
2906n/a if (colon_index < -1) {
2907n/a return -1;
2908n/a }
2909n/a if (colon_index >= 0 && colon_index < text_len) {
2910n/a /* Check again, starting from just after the colon */
2911n/a if (_check_for_legacy_statements(self, colon_index+1) < 0) {
2912n/a return -1;
2913n/a }
2914n/a }
2915n/a }
2916n/a return 0;
2917n/a}