ยปCore Development>Code coverage>Python/pythonrun.c

Python code coverage for Python/pythonrun.c

#countcontent
1n/a
2n/a/* Python interpreter top-level routines, including init/exit */
3n/a
4n/a#include "Python.h"
5n/a
6n/a#include "Python-ast.h"
7n/a#undef Yield /* undefine macro conflicting with winbase.h */
8n/a#include "grammar.h"
9n/a#include "node.h"
10n/a#include "token.h"
11n/a#include "parsetok.h"
12n/a#include "errcode.h"
13n/a#include "code.h"
14n/a#include "symtable.h"
15n/a#include "ast.h"
16n/a#include "marshal.h"
17n/a#include "osdefs.h"
18n/a#include <locale.h>
19n/a
20n/a#ifdef HAVE_SIGNAL_H
21n/a#include <signal.h>
22n/a#endif
23n/a
24n/a#ifdef MS_WINDOWS
25n/a#include "malloc.h" /* for alloca */
26n/a#endif
27n/a
28n/a#ifdef HAVE_LANGINFO_H
29n/a#include <langinfo.h>
30n/a#endif
31n/a
32n/a#ifdef MS_WINDOWS
33n/a#undef BYTE
34n/a#include "windows.h"
35n/a#endif
36n/a
37n/a_Py_IDENTIFIER(builtins);
38n/a_Py_IDENTIFIER(excepthook);
39n/a_Py_IDENTIFIER(flush);
40n/a_Py_IDENTIFIER(last_traceback);
41n/a_Py_IDENTIFIER(last_type);
42n/a_Py_IDENTIFIER(last_value);
43n/a_Py_IDENTIFIER(ps1);
44n/a_Py_IDENTIFIER(ps2);
45n/a_Py_IDENTIFIER(stdin);
46n/a_Py_IDENTIFIER(stdout);
47n/a_Py_IDENTIFIER(stderr);
48n/a_Py_static_string(PyId_string, "<string>");
49n/a
50n/a#ifdef __cplusplus
51n/aextern "C" {
52n/a#endif
53n/a
54n/aextern grammar _PyParser_Grammar; /* From graminit.c */
55n/a
56n/a/* Forward */
57n/astatic void flush_io(void);
58n/astatic PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
59n/a PyCompilerFlags *, PyArena *);
60n/astatic PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
61n/a PyCompilerFlags *);
62n/astatic void err_input(perrdetail *);
63n/astatic void err_free(perrdetail *);
64n/a
65n/a/* Parse input from a file and execute it */
66n/a
67n/aint
68n/aPyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
69n/a PyCompilerFlags *flags)
70n/a{
71n/a if (filename == NULL)
72n/a filename = "???";
73n/a if (Py_FdIsInteractive(fp, filename)) {
74n/a int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
75n/a if (closeit)
76n/a fclose(fp);
77n/a return err;
78n/a }
79n/a else
80n/a return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
81n/a}
82n/a
83n/aint
84n/aPyRun_InteractiveLoopFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
85n/a{
86n/a PyObject *filename, *v;
87n/a int ret, err;
88n/a PyCompilerFlags local_flags;
89n/a
90n/a filename = PyUnicode_DecodeFSDefault(filename_str);
91n/a if (filename == NULL) {
92n/a PyErr_Print();
93n/a return -1;
94n/a }
95n/a
96n/a if (flags == NULL) {
97n/a flags = &local_flags;
98n/a local_flags.cf_flags = 0;
99n/a }
100n/a v = _PySys_GetObjectId(&PyId_ps1);
101n/a if (v == NULL) {
102n/a _PySys_SetObjectId(&PyId_ps1, v = PyUnicode_FromString(">>> "));
103n/a Py_XDECREF(v);
104n/a }
105n/a v = _PySys_GetObjectId(&PyId_ps2);
106n/a if (v == NULL) {
107n/a _PySys_SetObjectId(&PyId_ps2, v = PyUnicode_FromString("... "));
108n/a Py_XDECREF(v);
109n/a }
110n/a err = -1;
111n/a for (;;) {
112n/a ret = PyRun_InteractiveOneObject(fp, filename, flags);
113n/a _PY_DEBUG_PRINT_TOTAL_REFS();
114n/a if (ret == E_EOF) {
115n/a err = 0;
116n/a break;
117n/a }
118n/a /*
119n/a if (ret == E_NOMEM)
120n/a break;
121n/a */
122n/a }
123n/a Py_DECREF(filename);
124n/a return err;
125n/a}
126n/a
127n/a/* compute parser flags based on compiler flags */
128n/astatic int PARSER_FLAGS(PyCompilerFlags *flags)
129n/a{
130n/a int parser_flags = 0;
131n/a if (!flags)
132n/a return 0;
133n/a if (flags->cf_flags & PyCF_DONT_IMPLY_DEDENT)
134n/a parser_flags |= PyPARSE_DONT_IMPLY_DEDENT;
135n/a if (flags->cf_flags & PyCF_IGNORE_COOKIE)
136n/a parser_flags |= PyPARSE_IGNORE_COOKIE;
137n/a if (flags->cf_flags & CO_FUTURE_BARRY_AS_BDFL)
138n/a parser_flags |= PyPARSE_BARRY_AS_BDFL;
139n/a return parser_flags;
140n/a}
141n/a
142n/a#if 0
143n/a/* Keep an example of flags with future keyword support. */
144n/a#define PARSER_FLAGS(flags) \
145n/a ((flags) ? ((((flags)->cf_flags & PyCF_DONT_IMPLY_DEDENT) ? \
146n/a PyPARSE_DONT_IMPLY_DEDENT : 0) \
147n/a | ((flags)->cf_flags & CO_FUTURE_WITH_STATEMENT ? \
148n/a PyPARSE_WITH_IS_KEYWORD : 0)) : 0)
149n/a#endif
150n/a
151n/aint
152n/aPyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
153n/a{
154n/a PyObject *m, *d, *v, *w, *oenc = NULL, *mod_name;
155n/a mod_ty mod;
156n/a PyArena *arena;
157n/a const char *ps1 = "", *ps2 = "", *enc = NULL;
158n/a int errcode = 0;
159n/a _Py_IDENTIFIER(encoding);
160n/a _Py_IDENTIFIER(__main__);
161n/a
162n/a mod_name = _PyUnicode_FromId(&PyId___main__); /* borrowed */
163n/a if (mod_name == NULL) {
164n/a PyErr_Print();
165n/a return -1;
166n/a }
167n/a
168n/a if (fp == stdin) {
169n/a /* Fetch encoding from sys.stdin if possible. */
170n/a v = _PySys_GetObjectId(&PyId_stdin);
171n/a if (v && v != Py_None) {
172n/a oenc = _PyObject_GetAttrId(v, &PyId_encoding);
173n/a if (oenc)
174n/a enc = PyUnicode_AsUTF8(oenc);
175n/a if (!enc)
176n/a PyErr_Clear();
177n/a }
178n/a }
179n/a v = _PySys_GetObjectId(&PyId_ps1);
180n/a if (v != NULL) {
181n/a v = PyObject_Str(v);
182n/a if (v == NULL)
183n/a PyErr_Clear();
184n/a else if (PyUnicode_Check(v)) {
185n/a ps1 = PyUnicode_AsUTF8(v);
186n/a if (ps1 == NULL) {
187n/a PyErr_Clear();
188n/a ps1 = "";
189n/a }
190n/a }
191n/a }
192n/a w = _PySys_GetObjectId(&PyId_ps2);
193n/a if (w != NULL) {
194n/a w = PyObject_Str(w);
195n/a if (w == NULL)
196n/a PyErr_Clear();
197n/a else if (PyUnicode_Check(w)) {
198n/a ps2 = PyUnicode_AsUTF8(w);
199n/a if (ps2 == NULL) {
200n/a PyErr_Clear();
201n/a ps2 = "";
202n/a }
203n/a }
204n/a }
205n/a arena = PyArena_New();
206n/a if (arena == NULL) {
207n/a Py_XDECREF(v);
208n/a Py_XDECREF(w);
209n/a Py_XDECREF(oenc);
210n/a return -1;
211n/a }
212n/a mod = PyParser_ASTFromFileObject(fp, filename, enc,
213n/a Py_single_input, ps1, ps2,
214n/a flags, &errcode, arena);
215n/a Py_XDECREF(v);
216n/a Py_XDECREF(w);
217n/a Py_XDECREF(oenc);
218n/a if (mod == NULL) {
219n/a PyArena_Free(arena);
220n/a if (errcode == E_EOF) {
221n/a PyErr_Clear();
222n/a return E_EOF;
223n/a }
224n/a PyErr_Print();
225n/a return -1;
226n/a }
227n/a m = PyImport_AddModuleObject(mod_name);
228n/a if (m == NULL) {
229n/a PyArena_Free(arena);
230n/a return -1;
231n/a }
232n/a d = PyModule_GetDict(m);
233n/a v = run_mod(mod, filename, d, d, flags, arena);
234n/a PyArena_Free(arena);
235n/a if (v == NULL) {
236n/a PyErr_Print();
237n/a flush_io();
238n/a return -1;
239n/a }
240n/a Py_DECREF(v);
241n/a flush_io();
242n/a return 0;
243n/a}
244n/a
245n/aint
246n/aPyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
247n/a{
248n/a PyObject *filename;
249n/a int res;
250n/a
251n/a filename = PyUnicode_DecodeFSDefault(filename_str);
252n/a if (filename == NULL) {
253n/a PyErr_Print();
254n/a return -1;
255n/a }
256n/a res = PyRun_InteractiveOneObject(fp, filename, flags);
257n/a Py_DECREF(filename);
258n/a return res;
259n/a}
260n/a
261n/a
262n/a/* Check whether a file maybe a pyc file: Look at the extension,
263n/a the file type, and, if we may close it, at the first few bytes. */
264n/a
265n/astatic int
266n/amaybe_pyc_file(FILE *fp, const char* filename, const char* ext, int closeit)
267n/a{
268n/a if (strcmp(ext, ".pyc") == 0)
269n/a return 1;
270n/a
271n/a /* Only look into the file if we are allowed to close it, since
272n/a it then should also be seekable. */
273n/a if (closeit) {
274n/a /* Read only two bytes of the magic. If the file was opened in
275n/a text mode, the bytes 3 and 4 of the magic (\r\n) might not
276n/a be read as they are on disk. */
277n/a unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
278n/a unsigned char buf[2];
279n/a /* Mess: In case of -x, the stream is NOT at its start now,
280n/a and ungetc() was used to push back the first newline,
281n/a which makes the current stream position formally undefined,
282n/a and a x-platform nightmare.
283n/a Unfortunately, we have no direct way to know whether -x
284n/a was specified. So we use a terrible hack: if the current
285n/a stream position is not 0, we assume -x was specified, and
286n/a give up. Bug 132850 on SourceForge spells out the
287n/a hopelessness of trying anything else (fseek and ftell
288n/a don't work predictably x-platform for text-mode files).
289n/a */
290n/a int ispyc = 0;
291n/a if (ftell(fp) == 0) {
292n/a if (fread(buf, 1, 2, fp) == 2 &&
293n/a ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
294n/a ispyc = 1;
295n/a rewind(fp);
296n/a }
297n/a return ispyc;
298n/a }
299n/a return 0;
300n/a}
301n/a
302n/astatic int
303n/aset_main_loader(PyObject *d, const char *filename, const char *loader_name)
304n/a{
305n/a PyInterpreterState *interp;
306n/a PyThreadState *tstate;
307n/a PyObject *filename_obj, *bootstrap, *loader_type = NULL, *loader;
308n/a int result = 0;
309n/a
310n/a filename_obj = PyUnicode_DecodeFSDefault(filename);
311n/a if (filename_obj == NULL)
312n/a return -1;
313n/a /* Get current thread state and interpreter pointer */
314n/a tstate = PyThreadState_GET();
315n/a interp = tstate->interp;
316n/a bootstrap = PyObject_GetAttrString(interp->importlib,
317n/a "_bootstrap_external");
318n/a if (bootstrap != NULL) {
319n/a loader_type = PyObject_GetAttrString(bootstrap, loader_name);
320n/a Py_DECREF(bootstrap);
321n/a }
322n/a if (loader_type == NULL) {
323n/a Py_DECREF(filename_obj);
324n/a return -1;
325n/a }
326n/a loader = PyObject_CallFunction(loader_type, "sN", "__main__", filename_obj);
327n/a Py_DECREF(loader_type);
328n/a if (loader == NULL) {
329n/a return -1;
330n/a }
331n/a if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
332n/a result = -1;
333n/a }
334n/a Py_DECREF(loader);
335n/a return result;
336n/a}
337n/a
338n/aint
339n/aPyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
340n/a PyCompilerFlags *flags)
341n/a{
342n/a PyObject *m, *d, *v;
343n/a const char *ext;
344n/a int set_file_name = 0, ret = -1;
345n/a size_t len;
346n/a
347n/a m = PyImport_AddModule("__main__");
348n/a if (m == NULL)
349n/a return -1;
350n/a Py_INCREF(m);
351n/a d = PyModule_GetDict(m);
352n/a if (PyDict_GetItemString(d, "__file__") == NULL) {
353n/a PyObject *f;
354n/a f = PyUnicode_DecodeFSDefault(filename);
355n/a if (f == NULL)
356n/a goto done;
357n/a if (PyDict_SetItemString(d, "__file__", f) < 0) {
358n/a Py_DECREF(f);
359n/a goto done;
360n/a }
361n/a if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
362n/a Py_DECREF(f);
363n/a goto done;
364n/a }
365n/a set_file_name = 1;
366n/a Py_DECREF(f);
367n/a }
368n/a len = strlen(filename);
369n/a ext = filename + len - (len > 4 ? 4 : 0);
370n/a if (maybe_pyc_file(fp, filename, ext, closeit)) {
371n/a FILE *pyc_fp;
372n/a /* Try to run a pyc file. First, re-open in binary */
373n/a if (closeit)
374n/a fclose(fp);
375n/a if ((pyc_fp = _Py_fopen(filename, "rb")) == NULL) {
376n/a fprintf(stderr, "python: Can't reopen .pyc file\n");
377n/a goto done;
378n/a }
379n/a
380n/a if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
381n/a fprintf(stderr, "python: failed to set __main__.__loader__\n");
382n/a ret = -1;
383n/a fclose(pyc_fp);
384n/a goto done;
385n/a }
386n/a v = run_pyc_file(pyc_fp, filename, d, d, flags);
387n/a fclose(pyc_fp);
388n/a } else {
389n/a /* When running from stdin, leave __main__.__loader__ alone */
390n/a if (strcmp(filename, "<stdin>") != 0 &&
391n/a set_main_loader(d, filename, "SourceFileLoader") < 0) {
392n/a fprintf(stderr, "python: failed to set __main__.__loader__\n");
393n/a ret = -1;
394n/a goto done;
395n/a }
396n/a v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
397n/a closeit, flags);
398n/a }
399n/a flush_io();
400n/a if (v == NULL) {
401n/a PyErr_Print();
402n/a goto done;
403n/a }
404n/a Py_DECREF(v);
405n/a ret = 0;
406n/a done:
407n/a if (set_file_name && PyDict_DelItemString(d, "__file__"))
408n/a PyErr_Clear();
409n/a Py_DECREF(m);
410n/a return ret;
411n/a}
412n/a
413n/aint
414n/aPyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
415n/a{
416n/a PyObject *m, *d, *v;
417n/a m = PyImport_AddModule("__main__");
418n/a if (m == NULL)
419n/a return -1;
420n/a d = PyModule_GetDict(m);
421n/a v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
422n/a if (v == NULL) {
423n/a PyErr_Print();
424n/a return -1;
425n/a }
426n/a Py_DECREF(v);
427n/a return 0;
428n/a}
429n/a
430n/astatic int
431n/aparse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
432n/a int *lineno, int *offset, PyObject **text)
433n/a{
434n/a int hold;
435n/a PyObject *v;
436n/a _Py_IDENTIFIER(msg);
437n/a _Py_IDENTIFIER(filename);
438n/a _Py_IDENTIFIER(lineno);
439n/a _Py_IDENTIFIER(offset);
440n/a _Py_IDENTIFIER(text);
441n/a
442n/a *message = NULL;
443n/a *filename = NULL;
444n/a
445n/a /* new style errors. `err' is an instance */
446n/a *message = _PyObject_GetAttrId(err, &PyId_msg);
447n/a if (!*message)
448n/a goto finally;
449n/a
450n/a v = _PyObject_GetAttrId(err, &PyId_filename);
451n/a if (!v)
452n/a goto finally;
453n/a if (v == Py_None) {
454n/a Py_DECREF(v);
455n/a *filename = _PyUnicode_FromId(&PyId_string);
456n/a if (*filename == NULL)
457n/a goto finally;
458n/a Py_INCREF(*filename);
459n/a }
460n/a else {
461n/a *filename = v;
462n/a }
463n/a
464n/a v = _PyObject_GetAttrId(err, &PyId_lineno);
465n/a if (!v)
466n/a goto finally;
467n/a hold = _PyLong_AsInt(v);
468n/a Py_DECREF(v);
469n/a if (hold < 0 && PyErr_Occurred())
470n/a goto finally;
471n/a *lineno = hold;
472n/a
473n/a v = _PyObject_GetAttrId(err, &PyId_offset);
474n/a if (!v)
475n/a goto finally;
476n/a if (v == Py_None) {
477n/a *offset = -1;
478n/a Py_DECREF(v);
479n/a } else {
480n/a hold = _PyLong_AsInt(v);
481n/a Py_DECREF(v);
482n/a if (hold < 0 && PyErr_Occurred())
483n/a goto finally;
484n/a *offset = hold;
485n/a }
486n/a
487n/a v = _PyObject_GetAttrId(err, &PyId_text);
488n/a if (!v)
489n/a goto finally;
490n/a if (v == Py_None) {
491n/a Py_DECREF(v);
492n/a *text = NULL;
493n/a }
494n/a else {
495n/a *text = v;
496n/a }
497n/a return 1;
498n/a
499n/afinally:
500n/a Py_XDECREF(*message);
501n/a Py_XDECREF(*filename);
502n/a return 0;
503n/a}
504n/a
505n/avoid
506n/aPyErr_Print(void)
507n/a{
508n/a PyErr_PrintEx(1);
509n/a}
510n/a
511n/astatic void
512n/aprint_error_text(PyObject *f, int offset, PyObject *text_obj)
513n/a{
514n/a const char *text;
515n/a const char *nl;
516n/a
517n/a text = PyUnicode_AsUTF8(text_obj);
518n/a if (text == NULL)
519n/a return;
520n/a
521n/a if (offset >= 0) {
522n/a if (offset > 0 && (size_t)offset == strlen(text) && text[offset - 1] == '\n')
523n/a offset--;
524n/a for (;;) {
525n/a nl = strchr(text, '\n');
526n/a if (nl == NULL || nl-text >= offset)
527n/a break;
528n/a offset -= (int)(nl+1-text);
529n/a text = nl+1;
530n/a }
531n/a while (*text == ' ' || *text == '\t' || *text == '\f') {
532n/a text++;
533n/a offset--;
534n/a }
535n/a }
536n/a PyFile_WriteString(" ", f);
537n/a PyFile_WriteString(text, f);
538n/a if (*text == '\0' || text[strlen(text)-1] != '\n')
539n/a PyFile_WriteString("\n", f);
540n/a if (offset == -1)
541n/a return;
542n/a PyFile_WriteString(" ", f);
543n/a while (--offset > 0)
544n/a PyFile_WriteString(" ", f);
545n/a PyFile_WriteString("^\n", f);
546n/a}
547n/a
548n/astatic void
549n/ahandle_system_exit(void)
550n/a{
551n/a PyObject *exception, *value, *tb;
552n/a int exitcode = 0;
553n/a
554n/a if (Py_InspectFlag)
555n/a /* Don't exit if -i flag was given. This flag is set to 0
556n/a * when entering interactive mode for inspecting. */
557n/a return;
558n/a
559n/a PyErr_Fetch(&exception, &value, &tb);
560n/a fflush(stdout);
561n/a if (value == NULL || value == Py_None)
562n/a goto done;
563n/a if (PyExceptionInstance_Check(value)) {
564n/a /* The error code should be in the `code' attribute. */
565n/a _Py_IDENTIFIER(code);
566n/a PyObject *code = _PyObject_GetAttrId(value, &PyId_code);
567n/a if (code) {
568n/a Py_DECREF(value);
569n/a value = code;
570n/a if (value == Py_None)
571n/a goto done;
572n/a }
573n/a /* If we failed to dig out the 'code' attribute,
574n/a just let the else clause below print the error. */
575n/a }
576n/a if (PyLong_Check(value))
577n/a exitcode = (int)PyLong_AsLong(value);
578n/a else {
579n/a PyObject *sys_stderr = _PySys_GetObjectId(&PyId_stderr);
580n/a /* We clear the exception here to avoid triggering the assertion
581n/a * in PyObject_Str that ensures it won't silently lose exception
582n/a * details.
583n/a */
584n/a PyErr_Clear();
585n/a if (sys_stderr != NULL && sys_stderr != Py_None) {
586n/a PyFile_WriteObject(value, sys_stderr, Py_PRINT_RAW);
587n/a } else {
588n/a PyObject_Print(value, stderr, Py_PRINT_RAW);
589n/a fflush(stderr);
590n/a }
591n/a PySys_WriteStderr("\n");
592n/a exitcode = 1;
593n/a }
594n/a done:
595n/a /* Restore and clear the exception info, in order to properly decref
596n/a * the exception, value, and traceback. If we just exit instead,
597n/a * these leak, which confuses PYTHONDUMPREFS output, and may prevent
598n/a * some finalizers from running.
599n/a */
600n/a PyErr_Restore(exception, value, tb);
601n/a PyErr_Clear();
602n/a Py_Exit(exitcode);
603n/a /* NOTREACHED */
604n/a}
605n/a
606n/avoid
607n/aPyErr_PrintEx(int set_sys_last_vars)
608n/a{
609n/a PyObject *exception, *v, *tb, *hook;
610n/a
611n/a if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
612n/a handle_system_exit();
613n/a }
614n/a PyErr_Fetch(&exception, &v, &tb);
615n/a if (exception == NULL)
616n/a return;
617n/a PyErr_NormalizeException(&exception, &v, &tb);
618n/a if (tb == NULL) {
619n/a tb = Py_None;
620n/a Py_INCREF(tb);
621n/a }
622n/a PyException_SetTraceback(v, tb);
623n/a if (exception == NULL)
624n/a return;
625n/a /* Now we know v != NULL too */
626n/a if (set_sys_last_vars) {
627n/a _PySys_SetObjectId(&PyId_last_type, exception);
628n/a _PySys_SetObjectId(&PyId_last_value, v);
629n/a _PySys_SetObjectId(&PyId_last_traceback, tb);
630n/a }
631n/a hook = _PySys_GetObjectId(&PyId_excepthook);
632n/a if (hook) {
633n/a PyObject* stack[3];
634n/a PyObject *result;
635n/a
636n/a stack[0] = exception;
637n/a stack[1] = v;
638n/a stack[2] = tb;
639n/a result = _PyObject_FastCall(hook, stack, 3);
640n/a if (result == NULL) {
641n/a PyObject *exception2, *v2, *tb2;
642n/a if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
643n/a handle_system_exit();
644n/a }
645n/a PyErr_Fetch(&exception2, &v2, &tb2);
646n/a PyErr_NormalizeException(&exception2, &v2, &tb2);
647n/a /* It should not be possible for exception2 or v2
648n/a to be NULL. However PyErr_Display() can't
649n/a tolerate NULLs, so just be safe. */
650n/a if (exception2 == NULL) {
651n/a exception2 = Py_None;
652n/a Py_INCREF(exception2);
653n/a }
654n/a if (v2 == NULL) {
655n/a v2 = Py_None;
656n/a Py_INCREF(v2);
657n/a }
658n/a fflush(stdout);
659n/a PySys_WriteStderr("Error in sys.excepthook:\n");
660n/a PyErr_Display(exception2, v2, tb2);
661n/a PySys_WriteStderr("\nOriginal exception was:\n");
662n/a PyErr_Display(exception, v, tb);
663n/a Py_DECREF(exception2);
664n/a Py_DECREF(v2);
665n/a Py_XDECREF(tb2);
666n/a }
667n/a Py_XDECREF(result);
668n/a } else {
669n/a PySys_WriteStderr("sys.excepthook is missing\n");
670n/a PyErr_Display(exception, v, tb);
671n/a }
672n/a Py_XDECREF(exception);
673n/a Py_XDECREF(v);
674n/a Py_XDECREF(tb);
675n/a}
676n/a
677n/astatic void
678n/aprint_exception(PyObject *f, PyObject *value)
679n/a{
680n/a int err = 0;
681n/a PyObject *type, *tb;
682n/a _Py_IDENTIFIER(print_file_and_line);
683n/a
684n/a if (!PyExceptionInstance_Check(value)) {
685n/a err = PyFile_WriteString("TypeError: print_exception(): Exception expected for value, ", f);
686n/a err += PyFile_WriteString(Py_TYPE(value)->tp_name, f);
687n/a err += PyFile_WriteString(" found\n", f);
688n/a if (err)
689n/a PyErr_Clear();
690n/a return;
691n/a }
692n/a
693n/a Py_INCREF(value);
694n/a fflush(stdout);
695n/a type = (PyObject *) Py_TYPE(value);
696n/a tb = PyException_GetTraceback(value);
697n/a if (tb && tb != Py_None)
698n/a err = PyTraceBack_Print(tb, f);
699n/a if (err == 0 &&
700n/a _PyObject_HasAttrId(value, &PyId_print_file_and_line))
701n/a {
702n/a PyObject *message, *filename, *text;
703n/a int lineno, offset;
704n/a if (!parse_syntax_error(value, &message, &filename,
705n/a &lineno, &offset, &text))
706n/a PyErr_Clear();
707n/a else {
708n/a PyObject *line;
709n/a
710n/a Py_DECREF(value);
711n/a value = message;
712n/a
713n/a line = PyUnicode_FromFormat(" File \"%U\", line %d\n",
714n/a filename, lineno);
715n/a Py_DECREF(filename);
716n/a if (line != NULL) {
717n/a PyFile_WriteObject(line, f, Py_PRINT_RAW);
718n/a Py_DECREF(line);
719n/a }
720n/a
721n/a if (text != NULL) {
722n/a print_error_text(f, offset, text);
723n/a Py_DECREF(text);
724n/a }
725n/a
726n/a /* Can't be bothered to check all those
727n/a PyFile_WriteString() calls */
728n/a if (PyErr_Occurred())
729n/a err = -1;
730n/a }
731n/a }
732n/a if (err) {
733n/a /* Don't do anything else */
734n/a }
735n/a else {
736n/a PyObject* moduleName;
737n/a char* className;
738n/a _Py_IDENTIFIER(__module__);
739n/a assert(PyExceptionClass_Check(type));
740n/a className = PyExceptionClass_Name(type);
741n/a if (className != NULL) {
742n/a char *dot = strrchr(className, '.');
743n/a if (dot != NULL)
744n/a className = dot+1;
745n/a }
746n/a
747n/a moduleName = _PyObject_GetAttrId(type, &PyId___module__);
748n/a if (moduleName == NULL || !PyUnicode_Check(moduleName))
749n/a {
750n/a Py_XDECREF(moduleName);
751n/a err = PyFile_WriteString("<unknown>", f);
752n/a }
753n/a else {
754n/a if (!_PyUnicode_EqualToASCIIId(moduleName, &PyId_builtins))
755n/a {
756n/a err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
757n/a err += PyFile_WriteString(".", f);
758n/a }
759n/a Py_DECREF(moduleName);
760n/a }
761n/a if (err == 0) {
762n/a if (className == NULL)
763n/a err = PyFile_WriteString("<unknown>", f);
764n/a else
765n/a err = PyFile_WriteString(className, f);
766n/a }
767n/a }
768n/a if (err == 0 && (value != Py_None)) {
769n/a PyObject *s = PyObject_Str(value);
770n/a /* only print colon if the str() of the
771n/a object is not the empty string
772n/a */
773n/a if (s == NULL) {
774n/a PyErr_Clear();
775n/a err = -1;
776n/a PyFile_WriteString(": <exception str() failed>", f);
777n/a }
778n/a else if (!PyUnicode_Check(s) ||
779n/a PyUnicode_GetLength(s) != 0)
780n/a err = PyFile_WriteString(": ", f);
781n/a if (err == 0)
782n/a err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
783n/a Py_XDECREF(s);
784n/a }
785n/a /* try to write a newline in any case */
786n/a if (err < 0) {
787n/a PyErr_Clear();
788n/a }
789n/a err += PyFile_WriteString("\n", f);
790n/a Py_XDECREF(tb);
791n/a Py_DECREF(value);
792n/a /* If an error happened here, don't show it.
793n/a XXX This is wrong, but too many callers rely on this behavior. */
794n/a if (err != 0)
795n/a PyErr_Clear();
796n/a}
797n/a
798n/astatic const char cause_message[] =
799n/a "\nThe above exception was the direct cause "
800n/a "of the following exception:\n\n";
801n/a
802n/astatic const char context_message[] =
803n/a "\nDuring handling of the above exception, "
804n/a "another exception occurred:\n\n";
805n/a
806n/astatic void
807n/aprint_exception_recursive(PyObject *f, PyObject *value, PyObject *seen)
808n/a{
809n/a int err = 0, res;
810n/a PyObject *cause, *context;
811n/a
812n/a if (seen != NULL) {
813n/a /* Exception chaining */
814n/a if (PySet_Add(seen, value) == -1)
815n/a PyErr_Clear();
816n/a else if (PyExceptionInstance_Check(value)) {
817n/a cause = PyException_GetCause(value);
818n/a context = PyException_GetContext(value);
819n/a if (cause) {
820n/a res = PySet_Contains(seen, cause);
821n/a if (res == -1)
822n/a PyErr_Clear();
823n/a if (res == 0) {
824n/a print_exception_recursive(
825n/a f, cause, seen);
826n/a err |= PyFile_WriteString(
827n/a cause_message, f);
828n/a }
829n/a }
830n/a else if (context &&
831n/a !((PyBaseExceptionObject *)value)->suppress_context) {
832n/a res = PySet_Contains(seen, context);
833n/a if (res == -1)
834n/a PyErr_Clear();
835n/a if (res == 0) {
836n/a print_exception_recursive(
837n/a f, context, seen);
838n/a err |= PyFile_WriteString(
839n/a context_message, f);
840n/a }
841n/a }
842n/a Py_XDECREF(context);
843n/a Py_XDECREF(cause);
844n/a }
845n/a }
846n/a print_exception(f, value);
847n/a if (err != 0)
848n/a PyErr_Clear();
849n/a}
850n/a
851n/avoid
852n/aPyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
853n/a{
854n/a PyObject *seen;
855n/a PyObject *f = _PySys_GetObjectId(&PyId_stderr);
856n/a if (PyExceptionInstance_Check(value)
857n/a && tb != NULL && PyTraceBack_Check(tb)) {
858n/a /* Put the traceback on the exception, otherwise it won't get
859n/a displayed. See issue #18776. */
860n/a PyObject *cur_tb = PyException_GetTraceback(value);
861n/a if (cur_tb == NULL)
862n/a PyException_SetTraceback(value, tb);
863n/a else
864n/a Py_DECREF(cur_tb);
865n/a }
866n/a if (f == Py_None) {
867n/a /* pass */
868n/a }
869n/a else if (f == NULL) {
870n/a _PyObject_Dump(value);
871n/a fprintf(stderr, "lost sys.stderr\n");
872n/a }
873n/a else {
874n/a /* We choose to ignore seen being possibly NULL, and report
875n/a at least the main exception (it could be a MemoryError).
876n/a */
877n/a seen = PySet_New(NULL);
878n/a if (seen == NULL)
879n/a PyErr_Clear();
880n/a print_exception_recursive(f, value, seen);
881n/a Py_XDECREF(seen);
882n/a }
883n/a}
884n/a
885n/aPyObject *
886n/aPyRun_StringFlags(const char *str, int start, PyObject *globals,
887n/a PyObject *locals, PyCompilerFlags *flags)
888n/a{
889n/a PyObject *ret = NULL;
890n/a mod_ty mod;
891n/a PyArena *arena;
892n/a PyObject *filename;
893n/a
894n/a filename = _PyUnicode_FromId(&PyId_string); /* borrowed */
895n/a if (filename == NULL)
896n/a return NULL;
897n/a
898n/a arena = PyArena_New();
899n/a if (arena == NULL)
900n/a return NULL;
901n/a
902n/a mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
903n/a if (mod != NULL)
904n/a ret = run_mod(mod, filename, globals, locals, flags, arena);
905n/a PyArena_Free(arena);
906n/a return ret;
907n/a}
908n/a
909n/aPyObject *
910n/aPyRun_FileExFlags(FILE *fp, const char *filename_str, int start, PyObject *globals,
911n/a PyObject *locals, int closeit, PyCompilerFlags *flags)
912n/a{
913n/a PyObject *ret = NULL;
914n/a mod_ty mod;
915n/a PyArena *arena = NULL;
916n/a PyObject *filename;
917n/a
918n/a filename = PyUnicode_DecodeFSDefault(filename_str);
919n/a if (filename == NULL)
920n/a goto exit;
921n/a
922n/a arena = PyArena_New();
923n/a if (arena == NULL)
924n/a goto exit;
925n/a
926n/a mod = PyParser_ASTFromFileObject(fp, filename, NULL, start, 0, 0,
927n/a flags, NULL, arena);
928n/a if (closeit)
929n/a fclose(fp);
930n/a if (mod == NULL) {
931n/a goto exit;
932n/a }
933n/a ret = run_mod(mod, filename, globals, locals, flags, arena);
934n/a
935n/aexit:
936n/a Py_XDECREF(filename);
937n/a if (arena != NULL)
938n/a PyArena_Free(arena);
939n/a return ret;
940n/a}
941n/a
942n/astatic void
943n/aflush_io(void)
944n/a{
945n/a PyObject *f, *r;
946n/a PyObject *type, *value, *traceback;
947n/a
948n/a /* Save the current exception */
949n/a PyErr_Fetch(&type, &value, &traceback);
950n/a
951n/a f = _PySys_GetObjectId(&PyId_stderr);
952n/a if (f != NULL) {
953n/a r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
954n/a if (r)
955n/a Py_DECREF(r);
956n/a else
957n/a PyErr_Clear();
958n/a }
959n/a f = _PySys_GetObjectId(&PyId_stdout);
960n/a if (f != NULL) {
961n/a r = _PyObject_CallMethodId(f, &PyId_flush, NULL);
962n/a if (r)
963n/a Py_DECREF(r);
964n/a else
965n/a PyErr_Clear();
966n/a }
967n/a
968n/a PyErr_Restore(type, value, traceback);
969n/a}
970n/a
971n/astatic PyObject *
972n/arun_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
973n/a PyCompilerFlags *flags, PyArena *arena)
974n/a{
975n/a PyCodeObject *co;
976n/a PyObject *v;
977n/a co = PyAST_CompileObject(mod, filename, flags, -1, arena);
978n/a if (co == NULL)
979n/a return NULL;
980n/a v = PyEval_EvalCode((PyObject*)co, globals, locals);
981n/a Py_DECREF(co);
982n/a return v;
983n/a}
984n/a
985n/astatic PyObject *
986n/arun_pyc_file(FILE *fp, const char *filename, PyObject *globals,
987n/a PyObject *locals, PyCompilerFlags *flags)
988n/a{
989n/a PyCodeObject *co;
990n/a PyObject *v;
991n/a long magic;
992n/a long PyImport_GetMagicNumber(void);
993n/a
994n/a magic = PyMarshal_ReadLongFromFile(fp);
995n/a if (magic != PyImport_GetMagicNumber()) {
996n/a if (!PyErr_Occurred())
997n/a PyErr_SetString(PyExc_RuntimeError,
998n/a "Bad magic number in .pyc file");
999n/a return NULL;
1000n/a }
1001n/a /* Skip mtime and size */
1002n/a (void) PyMarshal_ReadLongFromFile(fp);
1003n/a (void) PyMarshal_ReadLongFromFile(fp);
1004n/a if (PyErr_Occurred())
1005n/a return NULL;
1006n/a
1007n/a v = PyMarshal_ReadLastObjectFromFile(fp);
1008n/a if (v == NULL || !PyCode_Check(v)) {
1009n/a Py_XDECREF(v);
1010n/a PyErr_SetString(PyExc_RuntimeError,
1011n/a "Bad code object in .pyc file");
1012n/a return NULL;
1013n/a }
1014n/a co = (PyCodeObject *)v;
1015n/a v = PyEval_EvalCode((PyObject*)co, globals, locals);
1016n/a if (v && flags)
1017n/a flags->cf_flags |= (co->co_flags & PyCF_MASK);
1018n/a Py_DECREF(co);
1019n/a return v;
1020n/a}
1021n/a
1022n/aPyObject *
1023n/aPy_CompileStringObject(const char *str, PyObject *filename, int start,
1024n/a PyCompilerFlags *flags, int optimize)
1025n/a{
1026n/a PyCodeObject *co;
1027n/a mod_ty mod;
1028n/a PyArena *arena = PyArena_New();
1029n/a if (arena == NULL)
1030n/a return NULL;
1031n/a
1032n/a mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
1033n/a if (mod == NULL) {
1034n/a PyArena_Free(arena);
1035n/a return NULL;
1036n/a }
1037n/a if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1038n/a PyObject *result = PyAST_mod2obj(mod);
1039n/a PyArena_Free(arena);
1040n/a return result;
1041n/a }
1042n/a co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
1043n/a PyArena_Free(arena);
1044n/a return (PyObject *)co;
1045n/a}
1046n/a
1047n/aPyObject *
1048n/aPy_CompileStringExFlags(const char *str, const char *filename_str, int start,
1049n/a PyCompilerFlags *flags, int optimize)
1050n/a{
1051n/a PyObject *filename, *co;
1052n/a filename = PyUnicode_DecodeFSDefault(filename_str);
1053n/a if (filename == NULL)
1054n/a return NULL;
1055n/a co = Py_CompileStringObject(str, filename, start, flags, optimize);
1056n/a Py_DECREF(filename);
1057n/a return co;
1058n/a}
1059n/a
1060n/a/* For use in Py_LIMITED_API */
1061n/a#undef Py_CompileString
1062n/aPyObject *
1063n/aPyCompileString(const char *str, const char *filename, int start)
1064n/a{
1065n/a return Py_CompileStringFlags(str, filename, start, NULL);
1066n/a}
1067n/a
1068n/astruct symtable *
1069n/aPy_SymtableStringObject(const char *str, PyObject *filename, int start)
1070n/a{
1071n/a struct symtable *st;
1072n/a mod_ty mod;
1073n/a PyCompilerFlags flags;
1074n/a PyArena *arena;
1075n/a
1076n/a arena = PyArena_New();
1077n/a if (arena == NULL)
1078n/a return NULL;
1079n/a
1080n/a flags.cf_flags = 0;
1081n/a mod = PyParser_ASTFromStringObject(str, filename, start, &flags, arena);
1082n/a if (mod == NULL) {
1083n/a PyArena_Free(arena);
1084n/a return NULL;
1085n/a }
1086n/a st = PySymtable_BuildObject(mod, filename, 0);
1087n/a PyArena_Free(arena);
1088n/a return st;
1089n/a}
1090n/a
1091n/astruct symtable *
1092n/aPy_SymtableString(const char *str, const char *filename_str, int start)
1093n/a{
1094n/a PyObject *filename;
1095n/a struct symtable *st;
1096n/a
1097n/a filename = PyUnicode_DecodeFSDefault(filename_str);
1098n/a if (filename == NULL)
1099n/a return NULL;
1100n/a st = Py_SymtableStringObject(str, filename, start);
1101n/a Py_DECREF(filename);
1102n/a return st;
1103n/a}
1104n/a
1105n/a/* Preferred access to parser is through AST. */
1106n/amod_ty
1107n/aPyParser_ASTFromStringObject(const char *s, PyObject *filename, int start,
1108n/a PyCompilerFlags *flags, PyArena *arena)
1109n/a{
1110n/a mod_ty mod;
1111n/a PyCompilerFlags localflags;
1112n/a perrdetail err;
1113n/a int iflags = PARSER_FLAGS(flags);
1114n/a
1115n/a node *n = PyParser_ParseStringObject(s, filename,
1116n/a &_PyParser_Grammar, start, &err,
1117n/a &iflags);
1118n/a if (flags == NULL) {
1119n/a localflags.cf_flags = 0;
1120n/a flags = &localflags;
1121n/a }
1122n/a if (n) {
1123n/a flags->cf_flags |= iflags & PyCF_MASK;
1124n/a mod = PyAST_FromNodeObject(n, flags, filename, arena);
1125n/a PyNode_Free(n);
1126n/a }
1127n/a else {
1128n/a err_input(&err);
1129n/a mod = NULL;
1130n/a }
1131n/a err_free(&err);
1132n/a return mod;
1133n/a}
1134n/a
1135n/amod_ty
1136n/aPyParser_ASTFromString(const char *s, const char *filename_str, int start,
1137n/a PyCompilerFlags *flags, PyArena *arena)
1138n/a{
1139n/a PyObject *filename;
1140n/a mod_ty mod;
1141n/a filename = PyUnicode_DecodeFSDefault(filename_str);
1142n/a if (filename == NULL)
1143n/a return NULL;
1144n/a mod = PyParser_ASTFromStringObject(s, filename, start, flags, arena);
1145n/a Py_DECREF(filename);
1146n/a return mod;
1147n/a}
1148n/a
1149n/amod_ty
1150n/aPyParser_ASTFromFileObject(FILE *fp, PyObject *filename, const char* enc,
1151n/a int start, const char *ps1,
1152n/a const char *ps2, PyCompilerFlags *flags, int *errcode,
1153n/a PyArena *arena)
1154n/a{
1155n/a mod_ty mod;
1156n/a PyCompilerFlags localflags;
1157n/a perrdetail err;
1158n/a int iflags = PARSER_FLAGS(flags);
1159n/a
1160n/a node *n = PyParser_ParseFileObject(fp, filename, enc,
1161n/a &_PyParser_Grammar,
1162n/a start, ps1, ps2, &err, &iflags);
1163n/a if (flags == NULL) {
1164n/a localflags.cf_flags = 0;
1165n/a flags = &localflags;
1166n/a }
1167n/a if (n) {
1168n/a flags->cf_flags |= iflags & PyCF_MASK;
1169n/a mod = PyAST_FromNodeObject(n, flags, filename, arena);
1170n/a PyNode_Free(n);
1171n/a }
1172n/a else {
1173n/a err_input(&err);
1174n/a if (errcode)
1175n/a *errcode = err.error;
1176n/a mod = NULL;
1177n/a }
1178n/a err_free(&err);
1179n/a return mod;
1180n/a}
1181n/a
1182n/amod_ty
1183n/aPyParser_ASTFromFile(FILE *fp, const char *filename_str, const char* enc,
1184n/a int start, const char *ps1,
1185n/a const char *ps2, PyCompilerFlags *flags, int *errcode,
1186n/a PyArena *arena)
1187n/a{
1188n/a mod_ty mod;
1189n/a PyObject *filename;
1190n/a filename = PyUnicode_DecodeFSDefault(filename_str);
1191n/a if (filename == NULL)
1192n/a return NULL;
1193n/a mod = PyParser_ASTFromFileObject(fp, filename, enc, start, ps1, ps2,
1194n/a flags, errcode, arena);
1195n/a Py_DECREF(filename);
1196n/a return mod;
1197n/a}
1198n/a
1199n/a/* Simplified interface to parsefile -- return node or set exception */
1200n/a
1201n/anode *
1202n/aPyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)
1203n/a{
1204n/a perrdetail err;
1205n/a node *n = PyParser_ParseFileFlags(fp, filename, NULL,
1206n/a &_PyParser_Grammar,
1207n/a start, NULL, NULL, &err, flags);
1208n/a if (n == NULL)
1209n/a err_input(&err);
1210n/a err_free(&err);
1211n/a
1212n/a return n;
1213n/a}
1214n/a
1215n/a/* Simplified interface to parsestring -- return node or set exception */
1216n/a
1217n/anode *
1218n/aPyParser_SimpleParseStringFlags(const char *str, int start, int flags)
1219n/a{
1220n/a perrdetail err;
1221n/a node *n = PyParser_ParseStringFlags(str, &_PyParser_Grammar,
1222n/a start, &err, flags);
1223n/a if (n == NULL)
1224n/a err_input(&err);
1225n/a err_free(&err);
1226n/a return n;
1227n/a}
1228n/a
1229n/anode *
1230n/aPyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename,
1231n/a int start, int flags)
1232n/a{
1233n/a perrdetail err;
1234n/a node *n = PyParser_ParseStringFlagsFilename(str, filename,
1235n/a &_PyParser_Grammar, start, &err, flags);
1236n/a if (n == NULL)
1237n/a err_input(&err);
1238n/a err_free(&err);
1239n/a return n;
1240n/a}
1241n/a
1242n/anode *
1243n/aPyParser_SimpleParseStringFilename(const char *str, const char *filename, int start)
1244n/a{
1245n/a return PyParser_SimpleParseStringFlagsFilename(str, filename, start, 0);
1246n/a}
1247n/a
1248n/a/* May want to move a more generalized form of this to parsetok.c or
1249n/a even parser modules. */
1250n/a
1251n/avoid
1252n/aPyParser_ClearError(perrdetail *err)
1253n/a{
1254n/a err_free(err);
1255n/a}
1256n/a
1257n/avoid
1258n/aPyParser_SetError(perrdetail *err)
1259n/a{
1260n/a err_input(err);
1261n/a}
1262n/a
1263n/astatic void
1264n/aerr_free(perrdetail *err)
1265n/a{
1266n/a Py_CLEAR(err->filename);
1267n/a}
1268n/a
1269n/a/* Set the error appropriate to the given input error code (see errcode.h) */
1270n/a
1271n/astatic void
1272n/aerr_input(perrdetail *err)
1273n/a{
1274n/a PyObject *v, *w, *errtype, *errtext;
1275n/a PyObject *msg_obj = NULL;
1276n/a char *msg = NULL;
1277n/a int offset = err->offset;
1278n/a
1279n/a errtype = PyExc_SyntaxError;
1280n/a switch (err->error) {
1281n/a case E_ERROR:
1282n/a return;
1283n/a case E_SYNTAX:
1284n/a errtype = PyExc_IndentationError;
1285n/a if (err->expected == INDENT)
1286n/a msg = "expected an indented block";
1287n/a else if (err->token == INDENT)
1288n/a msg = "unexpected indent";
1289n/a else if (err->token == DEDENT)
1290n/a msg = "unexpected unindent";
1291n/a else {
1292n/a errtype = PyExc_SyntaxError;
1293n/a msg = "invalid syntax";
1294n/a }
1295n/a break;
1296n/a case E_TOKEN:
1297n/a msg = "invalid token";
1298n/a break;
1299n/a case E_EOFS:
1300n/a msg = "EOF while scanning triple-quoted string literal";
1301n/a break;
1302n/a case E_EOLS:
1303n/a msg = "EOL while scanning string literal";
1304n/a break;
1305n/a case E_INTR:
1306n/a if (!PyErr_Occurred())
1307n/a PyErr_SetNone(PyExc_KeyboardInterrupt);
1308n/a goto cleanup;
1309n/a case E_NOMEM:
1310n/a PyErr_NoMemory();
1311n/a goto cleanup;
1312n/a case E_EOF:
1313n/a msg = "unexpected EOF while parsing";
1314n/a break;
1315n/a case E_TABSPACE:
1316n/a errtype = PyExc_TabError;
1317n/a msg = "inconsistent use of tabs and spaces in indentation";
1318n/a break;
1319n/a case E_OVERFLOW:
1320n/a msg = "expression too long";
1321n/a break;
1322n/a case E_DEDENT:
1323n/a errtype = PyExc_IndentationError;
1324n/a msg = "unindent does not match any outer indentation level";
1325n/a break;
1326n/a case E_TOODEEP:
1327n/a errtype = PyExc_IndentationError;
1328n/a msg = "too many levels of indentation";
1329n/a break;
1330n/a case E_DECODE: {
1331n/a PyObject *type, *value, *tb;
1332n/a PyErr_Fetch(&type, &value, &tb);
1333n/a msg = "unknown decode error";
1334n/a if (value != NULL)
1335n/a msg_obj = PyObject_Str(value);
1336n/a Py_XDECREF(type);
1337n/a Py_XDECREF(value);
1338n/a Py_XDECREF(tb);
1339n/a break;
1340n/a }
1341n/a case E_LINECONT:
1342n/a msg = "unexpected character after line continuation character";
1343n/a break;
1344n/a
1345n/a case E_IDENTIFIER:
1346n/a msg = "invalid character in identifier";
1347n/a break;
1348n/a case E_BADSINGLE:
1349n/a msg = "multiple statements found while compiling a single statement";
1350n/a break;
1351n/a default:
1352n/a fprintf(stderr, "error=%d\n", err->error);
1353n/a msg = "unknown parsing error";
1354n/a break;
1355n/a }
1356n/a /* err->text may not be UTF-8 in case of decoding errors.
1357n/a Explicitly convert to an object. */
1358n/a if (!err->text) {
1359n/a errtext = Py_None;
1360n/a Py_INCREF(Py_None);
1361n/a } else {
1362n/a errtext = PyUnicode_DecodeUTF8(err->text, err->offset,
1363n/a "replace");
1364n/a if (errtext != NULL) {
1365n/a Py_ssize_t len = strlen(err->text);
1366n/a offset = (int)PyUnicode_GET_LENGTH(errtext);
1367n/a if (len != err->offset) {
1368n/a Py_DECREF(errtext);
1369n/a errtext = PyUnicode_DecodeUTF8(err->text, len,
1370n/a "replace");
1371n/a }
1372n/a }
1373n/a }
1374n/a v = Py_BuildValue("(OiiN)", err->filename,
1375n/a err->lineno, offset, errtext);
1376n/a if (v != NULL) {
1377n/a if (msg_obj)
1378n/a w = Py_BuildValue("(OO)", msg_obj, v);
1379n/a else
1380n/a w = Py_BuildValue("(sO)", msg, v);
1381n/a } else
1382n/a w = NULL;
1383n/a Py_XDECREF(v);
1384n/a PyErr_SetObject(errtype, w);
1385n/a Py_XDECREF(w);
1386n/acleanup:
1387n/a Py_XDECREF(msg_obj);
1388n/a if (err->text != NULL) {
1389n/a PyObject_FREE(err->text);
1390n/a err->text = NULL;
1391n/a }
1392n/a}
1393n/a
1394n/a
1395n/a#if defined(USE_STACKCHECK)
1396n/a#if defined(WIN32) && defined(_MSC_VER)
1397n/a
1398n/a/* Stack checking for Microsoft C */
1399n/a
1400n/a#include <malloc.h>
1401n/a#include <excpt.h>
1402n/a
1403n/a/*
1404n/a * Return non-zero when we run out of memory on the stack; zero otherwise.
1405n/a */
1406n/aint
1407n/aPyOS_CheckStack(void)
1408n/a{
1409n/a __try {
1410n/a /* alloca throws a stack overflow exception if there's
1411n/a not enough space left on the stack */
1412n/a alloca(PYOS_STACK_MARGIN * sizeof(void*));
1413n/a return 0;
1414n/a } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
1415n/a EXCEPTION_EXECUTE_HANDLER :
1416n/a EXCEPTION_CONTINUE_SEARCH) {
1417n/a int errcode = _resetstkoflw();
1418n/a if (errcode == 0)
1419n/a {
1420n/a Py_FatalError("Could not reset the stack!");
1421n/a }
1422n/a }
1423n/a return 1;
1424n/a}
1425n/a
1426n/a#endif /* WIN32 && _MSC_VER */
1427n/a
1428n/a/* Alternate implementations can be added here... */
1429n/a
1430n/a#endif /* USE_STACKCHECK */
1431n/a
1432n/a/* Deprecated C API functions still provided for binary compatibility */
1433n/a
1434n/a#undef PyParser_SimpleParseFile
1435n/aPyAPI_FUNC(node *)
1436n/aPyParser_SimpleParseFile(FILE *fp, const char *filename, int start)
1437n/a{
1438n/a return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
1439n/a}
1440n/a
1441n/a#undef PyParser_SimpleParseString
1442n/aPyAPI_FUNC(node *)
1443n/aPyParser_SimpleParseString(const char *str, int start)
1444n/a{
1445n/a return PyParser_SimpleParseStringFlags(str, start, 0);
1446n/a}
1447n/a
1448n/a#undef PyRun_AnyFile
1449n/aPyAPI_FUNC(int)
1450n/aPyRun_AnyFile(FILE *fp, const char *name)
1451n/a{
1452n/a return PyRun_AnyFileExFlags(fp, name, 0, NULL);
1453n/a}
1454n/a
1455n/a#undef PyRun_AnyFileEx
1456n/aPyAPI_FUNC(int)
1457n/aPyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
1458n/a{
1459n/a return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
1460n/a}
1461n/a
1462n/a#undef PyRun_AnyFileFlags
1463n/aPyAPI_FUNC(int)
1464n/aPyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
1465n/a{
1466n/a return PyRun_AnyFileExFlags(fp, name, 0, flags);
1467n/a}
1468n/a
1469n/a#undef PyRun_File
1470n/aPyAPI_FUNC(PyObject *)
1471n/aPyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
1472n/a{
1473n/a return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
1474n/a}
1475n/a
1476n/a#undef PyRun_FileEx
1477n/aPyAPI_FUNC(PyObject *)
1478n/aPyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
1479n/a{
1480n/a return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
1481n/a}
1482n/a
1483n/a#undef PyRun_FileFlags
1484n/aPyAPI_FUNC(PyObject *)
1485n/aPyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
1486n/a PyCompilerFlags *flags)
1487n/a{
1488n/a return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
1489n/a}
1490n/a
1491n/a#undef PyRun_SimpleFile
1492n/aPyAPI_FUNC(int)
1493n/aPyRun_SimpleFile(FILE *f, const char *p)
1494n/a{
1495n/a return PyRun_SimpleFileExFlags(f, p, 0, NULL);
1496n/a}
1497n/a
1498n/a#undef PyRun_SimpleFileEx
1499n/aPyAPI_FUNC(int)
1500n/aPyRun_SimpleFileEx(FILE *f, const char *p, int c)
1501n/a{
1502n/a return PyRun_SimpleFileExFlags(f, p, c, NULL);
1503n/a}
1504n/a
1505n/a
1506n/a#undef PyRun_String
1507n/aPyAPI_FUNC(PyObject *)
1508n/aPyRun_String(const char *str, int s, PyObject *g, PyObject *l)
1509n/a{
1510n/a return PyRun_StringFlags(str, s, g, l, NULL);
1511n/a}
1512n/a
1513n/a#undef PyRun_SimpleString
1514n/aPyAPI_FUNC(int)
1515n/aPyRun_SimpleString(const char *s)
1516n/a{
1517n/a return PyRun_SimpleStringFlags(s, NULL);
1518n/a}
1519n/a
1520n/a#undef Py_CompileString
1521n/aPyAPI_FUNC(PyObject *)
1522n/aPy_CompileString(const char *str, const char *p, int s)
1523n/a{
1524n/a return Py_CompileStringExFlags(str, p, s, NULL, -1);
1525n/a}
1526n/a
1527n/a#undef Py_CompileStringFlags
1528n/aPyAPI_FUNC(PyObject *)
1529n/aPy_CompileStringFlags(const char *str, const char *p, int s,
1530n/a PyCompilerFlags *flags)
1531n/a{
1532n/a return Py_CompileStringExFlags(str, p, s, flags, -1);
1533n/a}
1534n/a
1535n/a#undef PyRun_InteractiveOne
1536n/aPyAPI_FUNC(int)
1537n/aPyRun_InteractiveOne(FILE *f, const char *p)
1538n/a{
1539n/a return PyRun_InteractiveOneFlags(f, p, NULL);
1540n/a}
1541n/a
1542n/a#undef PyRun_InteractiveLoop
1543n/aPyAPI_FUNC(int)
1544n/aPyRun_InteractiveLoop(FILE *f, const char *p)
1545n/a{
1546n/a return PyRun_InteractiveLoopFlags(f, p, NULL);
1547n/a}
1548n/a
1549n/a#ifdef __cplusplus
1550n/a}
1551n/a#endif