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