annotate Python/pythonrun.c @ 18615:0a90e5ec61b6 legacy-trunk

Ugly. A pile of new xxxFlags() functions, to communicate to the parser that 'yield' is a keyword. This doesn't help test_generators at all! I don't know why not. These things do work now (and didn't before this patch): 1. "from __future__ import generators" now works in a native shell. 2. Similarly "python -i xxx.py" now has generators enabled in the shell if xxx.py had them enabled. 3. This program (which was my doctest proxy) works fine: from __future__ import generators source = """\ def f(): yield 1 """ exec compile(source, "", "single") in globals() print type(f())
author Tim Peters <tim.peters@gmail.com>
date Mon, 16 Jul 2001 05:37:24 +0000
parents 4ac98fe7abd2
children c9b4c8ae6179
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Python interpreter top-level routines, including init/exit */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
4 #include "Python.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include "grammar.h"
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #include "node.h"
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
8 #include "token.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 #include "parsetok.h"
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 #include "errcode.h"
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #include "compile.h"
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
12 #include "symtable.h"
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 923
diff changeset
13 #include "eval.h"
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
14 #include "marshal.h"
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
4596
4fa19659591e Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
16 #ifdef HAVE_UNISTD_H
4fa19659591e Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
17 #include <unistd.h>
4fa19659591e Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
18 #endif
4fa19659591e Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
19
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
20 #ifdef HAVE_SIGNAL_H
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
21 #include <signal.h>
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
22 #endif
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
23
4291
1427d61f1307 Rationalized MS ifdefs
Guido van Rossum <guido@python.org>
parents: 3609
diff changeset
24 #ifdef MS_WIN32
2529
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
25 #undef BYTE
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
26 #include "windows.h"
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
27 #endif
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
28
13552
dd2b5218e069 Include macglue.h for some function prototypes, and renamed a few
Jack Jansen <jack.jansen@cwi.nl>
parents: 13539
diff changeset
29 #ifdef macintosh
dd2b5218e069 Include macglue.h for some function prototypes, and renamed a few
Jack Jansen <jack.jansen@cwi.nl>
parents: 13539
diff changeset
30 #include "macglue.h"
dd2b5218e069 Include macglue.h for some function prototypes, and renamed a few
Jack Jansen <jack.jansen@cwi.nl>
parents: 13539
diff changeset
31 #endif
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
32 extern char *Py_GetPath(void);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
34 extern grammar _PyParser_Grammar; /* From graminit.c */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
1391
7c3245c18677 * ceval.c, longobject.c, methodobject.c, listnode.c, arraymodule.c,
Guido van Rossum <guido@python.org>
parents: 1347
diff changeset
36 /* Forward */
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
37 static void initmain(void);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
38 static void initsite(void);
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
39 static PyObject *run_err_node(node *, char *, PyObject *, PyObject *,
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
40 PyCompilerFlags *);
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
41 static PyObject *run_node(node *, char *, PyObject *, PyObject *,
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
42 PyCompilerFlags *);
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
43 static PyObject *run_pyc_file(FILE *, char *, PyObject *, PyObject *,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
44 PyCompilerFlags *);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
45 static void err_input(perrdetail *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
46 static void initsigs(void);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
47 static void call_sys_exitfunc(void);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
48 static void call_ll_exitfuncs(void);
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
49
11673
5e6e9e893acd The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents: 11113
diff changeset
50 #ifdef Py_TRACE_REFS
5e6e9e893acd The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents: 11113
diff changeset
51 int _Py_AskYesNo(char *prompt);
5e6e9e893acd The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents: 11113
diff changeset
52 #endif
5e6e9e893acd The rest of the changes by Trent Mick and Dale Nagata for warning-free
Guido van Rossum <guido@python.org>
parents: 11113
diff changeset
53
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
54 extern void _PyUnicode_Init(void);
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
55 extern void _PyUnicode_Fini(void);
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
56 extern void _PyCodecRegistry_Init(void);
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
57 extern void _PyCodecRegistry_Fini(void);
11977
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
58
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
59 int Py_DebugFlag; /* Needed by parser.c */
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
60 int Py_VerboseFlag; /* Needed by import.c */
5108
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
61 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
62 int Py_NoSiteFlag; /* Suppress 'import site' */
12555
04cf788d862b Py_UseClassExceptionsFlag is deprecated. We keep the C variable for C
Barry Warsaw <barry@python.org>
parents: 12539
diff changeset
63 int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
64 int Py_FrozenFlag; /* Needed by getpath.c */
12539
d7484a743354 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12518
diff changeset
65 int Py_UnicodeFlag = 0; /* Needed by compile.c */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
67 static int initialized = 0;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
69 /* API to access the initialized flag -- useful for esoteric use */
6136
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
70
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
71 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
72 Py_IsInitialized(void)
6136
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
73 {
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
74 return initialized;
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
75 }
57045f51dd12 Added new Py_IsInitalized() API function to test the 'initialized' flag.
Guido van Rossum <guido@python.org>
parents: 6117
diff changeset
76
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
77 /* Global initializations. Can be undone by Py_Finalize(). Don't
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
78 call this twice without an intervening Py_Finalize() call. When
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
79 initializations fail, a fatal error is issued and the function does
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
80 not return. On return, the first thread and interpreter state have
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
81 been created.
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
83 Locking: you must hold the interpreter lock while calling this.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
84 (If the lock has not yet been initialized, that's equivalent to
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
85 having the lock, but you cannot use multiple threads.)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
86
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
87 */
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
88
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
89 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
90 Py_Initialize(void)
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
91 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
92 PyInterpreterState *interp;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
93 PyThreadState *tstate;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
94 PyObject *bimod, *sysmod;
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
95 char *p;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
96
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
97 if (initialized)
6117
98a282cfb013 Use a counter instead of a Boolean to check for initialized; n calls
Guido van Rossum <guido@python.org>
parents: 5939
diff changeset
98 return;
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
99 initialized = 1;
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
100
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
101 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
14195
999671f69df4 Fix to [ Bug #111165 ] doc-string removal masked by PYTHONOPTIMIZE
Marc-André Lemburg <mal@egenix.com>
parents: 13995
diff changeset
102 Py_DebugFlag = Py_DebugFlag ? Py_DebugFlag : 1;
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
103 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
14195
999671f69df4 Fix to [ Bug #111165 ] doc-string removal masked by PYTHONOPTIMIZE
Marc-André Lemburg <mal@egenix.com>
parents: 13995
diff changeset
104 Py_VerboseFlag = Py_VerboseFlag ? Py_VerboseFlag : 1;
9285
5156cf8e4269 Support PYTHONOPTIMIZE variable; by Marc Lemburg.
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
105 if ((p = getenv("PYTHONOPTIMIZE")) && *p != '\0')
14195
999671f69df4 Fix to [ Bug #111165 ] doc-string removal masked by PYTHONOPTIMIZE
Marc-André Lemburg <mal@egenix.com>
parents: 13995
diff changeset
106 Py_OptimizeFlag = Py_OptimizeFlag ? Py_OptimizeFlag : 1;
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
107
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
108 interp = PyInterpreterState_New();
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
109 if (interp == NULL)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
110 Py_FatalError("Py_Initialize: can't make first interpreter");
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
111
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
112 tstate = PyThreadState_New(interp);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
113 if (tstate == NULL)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
114 Py_FatalError("Py_Initialize: can't make first thread");
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
115 (void) PyThreadState_Swap(tstate);
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
116
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
117 interp->modules = PyDict_New();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
118 if (interp->modules == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
119 Py_FatalError("Py_Initialize: can't make modules dictionary");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
120
11977
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
121 /* Init codec registry */
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
122 _PyCodecRegistry_Init();
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
123
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
124 /* Init Unicode implementation; relies on the codec registry */
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
125 _PyUnicode_Init();
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
126
12754
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
127 bimod = _PyBuiltin_Init();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
128 if (bimod == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
129 Py_FatalError("Py_Initialize: can't initialize __builtin__");
6557
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
130 interp->builtins = PyModule_GetDict(bimod);
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
131 Py_INCREF(interp->builtins);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
132
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
133 sysmod = _PySys_Init();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
134 if (sysmod == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
135 Py_FatalError("Py_Initialize: can't initialize sys");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
136 interp->sysdict = PyModule_GetDict(sysmod);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
137 Py_INCREF(interp->sysdict);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
138 _PyImport_FixupExtension("sys", "sys");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
139 PySys_SetPath(Py_GetPath());
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
140 PyDict_SetItemString(interp->sysdict, "modules",
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
141 interp->modules);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
142
11113
be8ec828c072 Marc-Andre Lemburg discovered that the switch from .pyc to .pyo files,
Guido van Rossum <guido@python.org>
parents: 10534
diff changeset
143 _PyImport_Init();
be8ec828c072 Marc-Andre Lemburg discovered that the switch from .pyc to .pyo files,
Guido van Rossum <guido@python.org>
parents: 10534
diff changeset
144
12754
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
145 /* initialize builtin exceptions */
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
146 init_exceptions();
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
147
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
148 /* phase 2 of builtins */
6328
dfeaf965f48a Py_Initialize(): move the call to _PyImport_FixupExtension() to after
Barry Warsaw <barry@python.org>
parents: 6322
diff changeset
149 _PyImport_FixupExtension("__builtin__", "__builtin__");
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
150
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
151 initsigs(); /* Signal handling stuff, including initintr() */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
152
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
153 initmain(); /* Module __main__ */
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
154 if (!Py_NoSiteFlag)
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
155 initsite(); /* Module site */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
156 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
157
9641
d152adf3f094 Move the prototype for dump_counts() to before where it is used.
Guido van Rossum <guido@python.org>
parents: 9285
diff changeset
158 #ifdef COUNT_ALLOCS
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
159 extern void dump_counts(void);
9641
d152adf3f094 Move the prototype for dump_counts() to before where it is used.
Guido van Rossum <guido@python.org>
parents: 9285
diff changeset
160 #endif
d152adf3f094 Move the prototype for dump_counts() to before where it is used.
Guido van Rossum <guido@python.org>
parents: 9285
diff changeset
161
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
162 /* Undo the effect of Py_Initialize().
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
163
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
164 Beware: if multiple interpreter and/or thread states exist, these
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
165 are not wiped out; only the current thread and interpreter state
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
166 are deleted. But since everything else is deleted, those other
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
167 interpreter and thread states should no longer be used.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
168
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
169 (XXX We should do better, e.g. wipe out all interpreters and
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
170 threads.)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
171
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
172 Locking: as above.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
173
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
174 */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
175
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
176 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
177 Py_Finalize(void)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
178 {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
179 PyInterpreterState *interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
180 PyThreadState *tstate;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
181
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
182 if (!initialized)
6117
98a282cfb013 Use a counter instead of a Boolean to check for initialized; n calls
Guido van Rossum <guido@python.org>
parents: 5939
diff changeset
183 return;
16073
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
184
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
185 /* The interpreter is still entirely intact at this point, and the
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
186 * exit funcs may be relying on that. In particular, if some thread
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
187 * or exit func is still waiting to do an import, the import machinery
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
188 * expects Py_IsInitialized() to return true. So don't say the
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
189 * interpreter is uninitialized until after the exit funcs have run.
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
190 * Note that Threading.py uses an exit func to do a join on all the
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
191 * threads created thru it, so this also protects pending imports in
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
192 * the threads created via Threading.
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
193 */
713ed6c6bcb5 Bug #128475: mimetools.encode (sometimes) fails when called from a thread.
Tim Peters <tim.peters@gmail.com>
parents: 15963
diff changeset
194 call_sys_exitfunc();
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
195 initialized = 0;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
196
6547
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
197 /* Get current thread state and interpreter pointer */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
198 tstate = PyThreadState_Get();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
199 interp = tstate->interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
200
6547
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
201 /* Disable signal handling */
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
202 PyOS_FiniInterrupts();
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
203
11977
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
204 /* Cleanup Unicode implementation */
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
205 _PyUnicode_Fini();
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
206
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
207 /* Cleanup Codec registry */
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
208 _PyCodecRegistry_Fini();
8b88e1bc0e1b Marc-Andre Lemburg: add calls to initialize and finalize Unicode and
Guido van Rossum <guido@python.org>
parents: 11673
diff changeset
209
6547
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
210 /* Destroy all modules */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
211 PyImport_Cleanup();
6547
724382af5339 Two independent changes (alas):
Guido van Rossum <guido@python.org>
parents: 6376
diff changeset
212
6786
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
213 /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
214 _PyImport_Fini();
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
215
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
216 /* Debugging stuff */
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
217 #ifdef COUNT_ALLOCS
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
218 dump_counts();
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
219 #endif
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
220
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
221 #ifdef Py_REF_DEBUG
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
222 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
223 #endif
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
224
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
225 #ifdef Py_TRACE_REFS
12518
9e53a7112395 Mark Hammond: For Windows debug builds, we now only offer to dump
Guido van Rossum <guido@python.org>
parents: 12406
diff changeset
226 if (
9e53a7112395 Mark Hammond: For Windows debug builds, we now only offer to dump
Guido van Rossum <guido@python.org>
parents: 12406
diff changeset
227 #ifdef MS_WINDOWS /* Only ask on Windows if env var set */
9e53a7112395 Mark Hammond: For Windows debug builds, we now only offer to dump
Guido van Rossum <guido@python.org>
parents: 12406
diff changeset
228 getenv("PYTHONDUMPREFS") &&
9e53a7112395 Mark Hammond: For Windows debug builds, we now only offer to dump
Guido van Rossum <guido@python.org>
parents: 12406
diff changeset
229 #endif /* MS_WINDOWS */
9e53a7112395 Mark Hammond: For Windows debug builds, we now only offer to dump
Guido van Rossum <guido@python.org>
parents: 12406
diff changeset
230 _Py_AskYesNo("Print left references?")) {
6786
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
231 _Py_PrintReferences(stderr);
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
232 }
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
233 #endif /* Py_TRACE_REFS */
bc007cf1d6ba Changed the finalization order again so that the reference count
Guido van Rossum <guido@python.org>
parents: 6661
diff changeset
234
12754
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
235 /* Now we decref the exception classes. After this point nothing
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
236 can raise an exception. That's okay, because each Fini() method
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
237 below has been checked to make sure no exceptions are ever
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
238 raised.
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
239 */
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
240 fini_exceptions();
5213abfe5230 Py_Initialize(): Now that standard exceptions are builtin, we don't
Barry Warsaw <barry@python.org>
parents: 12576
diff changeset
241
6557
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
242 /* Delete current thread */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
243 PyInterpreterState_Clear(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
244 PyThreadState_Swap(NULL);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
245 PyInterpreterState_Delete(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
246
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
247 PyMethod_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
248 PyFrame_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
249 PyCFunction_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
250 PyTuple_Fini();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
251 PyString_Fini();
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
252 PyInt_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
253 PyFloat_Fini();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
254
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
255 /* XXX Still allocated:
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
256 - various static ad-hoc pointers to interned strings
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
257 - int and float free list blocks
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
258 - whatever various modules and libraries allocate
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
259 */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
260
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
261 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
262
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
263 call_ll_exitfuncs();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
264
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
265 #ifdef Py_TRACE_REFS
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
266 _Py_ResetReferences();
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
267 #endif /* Py_TRACE_REFS */
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
268 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
269
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
270 /* Create and initialize a new interpreter and thread, and return the
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
271 new thread. This requires that Py_Initialize() has been called
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
272 first.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
273
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
274 Unsuccessful initialization yields a NULL pointer. Note that *no*
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
275 exception information is available even in this case -- the
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
276 exception information is held in the thread, and there is no
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
277 thread.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
278
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
279 Locking: as above.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
280
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
281 */
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
282
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
283 PyThreadState *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
284 Py_NewInterpreter(void)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
285 {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
286 PyInterpreterState *interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
287 PyThreadState *tstate, *save_tstate;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
288 PyObject *bimod, *sysmod;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
289
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
290 if (!initialized)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
291 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
292
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
293 interp = PyInterpreterState_New();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
294 if (interp == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
295 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
296
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
297 tstate = PyThreadState_New(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
298 if (tstate == NULL) {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
299 PyInterpreterState_Delete(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
300 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
301 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
302
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
303 save_tstate = PyThreadState_Swap(tstate);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
304
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
305 /* XXX The following is lax in error checking */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
306
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
307 interp->modules = PyDict_New();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
308
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
309 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
310 if (bimod != NULL) {
6557
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
311 interp->builtins = PyModule_GetDict(bimod);
b331cd47e0bc Undo half of the previous change :-(
Guido van Rossum <guido@python.org>
parents: 6547
diff changeset
312 Py_INCREF(interp->builtins);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
313 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
314 sysmod = _PyImport_FindExtension("sys", "sys");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
315 if (bimod != NULL && sysmod != NULL) {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
316 interp->sysdict = PyModule_GetDict(sysmod);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
317 Py_INCREF(interp->sysdict);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
318 PySys_SetPath(Py_GetPath());
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
319 PyDict_SetItemString(interp->sysdict, "modules",
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
320 interp->modules);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
321 initmain();
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
322 if (!Py_NoSiteFlag)
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
323 initsite();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
324 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
325
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
326 if (!PyErr_Occurred())
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
327 return tstate;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
328
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
329 /* Oops, it didn't work. Undo it all. */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
330
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
331 PyErr_Print();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
332 PyThreadState_Clear(tstate);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
333 PyThreadState_Swap(save_tstate);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
334 PyThreadState_Delete(tstate);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
335 PyInterpreterState_Delete(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
336
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
337 return NULL;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
338 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
339
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
340 /* Delete an interpreter and its last thread. This requires that the
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
341 given thread state is current, that the thread has no remaining
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
342 frames, and that it is its interpreter's only remaining thread.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
343 It is a fatal error to violate these constraints.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
344
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
345 (Py_Finalize() doesn't have these constraints -- it zaps
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
346 everything, regardless.)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
347
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
348 Locking: as above.
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
349
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
350 */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
351
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
352 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
353 Py_EndInterpreter(PyThreadState *tstate)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
354 {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
355 PyInterpreterState *interp = tstate->interp;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
356
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
357 if (tstate != PyThreadState_Get())
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
358 Py_FatalError("Py_EndInterpreter: thread is not current");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
359 if (tstate->frame != NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
360 Py_FatalError("Py_EndInterpreter: thread still has a frame");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
361 if (tstate != interp->tstate_head || tstate->next != NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
362 Py_FatalError("Py_EndInterpreter: not the last thread");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
363
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
364 PyImport_Cleanup();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
365 PyInterpreterState_Clear(interp);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
366 PyThreadState_Swap(NULL);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
367 PyInterpreterState_Delete(interp);
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
368 }
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
369
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
370 static char *progname = "python";
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
371
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
372 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
373 Py_SetProgramName(char *pn)
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
374 {
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
375 if (pn && *pn)
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
376 progname = pn;
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
377 }
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
378
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
379 char *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
380 Py_GetProgramName(void)
5850
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
381 {
6f67a1bda15a Moved some stuff here from main.c (part of a big restructuring - wait
Guido van Rossum <guido@python.org>
parents: 5611
diff changeset
382 return progname;
5405
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
383 }
b7871ca930ad Massive changes for separate thread state management.
Guido van Rossum <guido@python.org>
parents: 5234
diff changeset
384
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
385 static char *default_home = NULL;
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
386
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
387 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
388 Py_SetPythonHome(char *home)
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
389 {
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
390 default_home = home;
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
391 }
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
392
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
393 char *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
394 Py_GetPythonHome(void)
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
395 {
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
396 char *home = default_home;
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
397 if (home == NULL)
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
398 home = getenv("PYTHONHOME");
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
399 return home;
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
400 }
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
401
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
402 /* Create __main__ module */
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
403
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
404 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
405 initmain(void)
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
406 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
407 PyObject *m, *d;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
408 m = PyImport_AddModule("__main__");
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
409 if (m == NULL)
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
410 Py_FatalError("can't create __main__ module");
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
411 d = PyModule_GetDict(m);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
412 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
6600
83ccf44bf195 Two changes (here we go again :-( ).
Guido van Rossum <guido@python.org>
parents: 6557
diff changeset
413 PyObject *bimod = PyImport_ImportModule("__builtin__");
83ccf44bf195 Two changes (here we go again :-( ).
Guido van Rossum <guido@python.org>
parents: 6557
diff changeset
414 if (bimod == NULL ||
83ccf44bf195 Two changes (here we go again :-( ).
Guido van Rossum <guido@python.org>
parents: 6557
diff changeset
415 PyDict_SetItemString(d, "__builtins__", bimod) != 0)
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
416 Py_FatalError("can't add __builtins__ to __main__");
10024
caf2c65a6bcf initmain(): Nailed a memory leak. bimod must be DECREF'd!
Barry Warsaw <barry@python.org>
parents: 9965
diff changeset
417 Py_DECREF(bimod);
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
418 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
421 /* Import the site module (not into __main__ though) */
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
422
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
423 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
424 initsite(void)
6201
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
425 {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
426 PyObject *m, *f;
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
427 m = PyImport_ImportModule("site");
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
428 if (m == NULL) {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
429 f = PySys_GetObject("stderr");
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
430 if (Py_VerboseFlag) {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
431 PyFile_WriteString(
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
432 "'import site' failed; traceback:\n", f);
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
433 PyErr_Print();
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
434 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
435 else {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
436 PyFile_WriteString(
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
437 "'import site' failed; use -v for traceback\n", f);
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
438 PyErr_Clear();
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
439 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
440 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
441 else {
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
442 Py_DECREF(m);
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
443 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
444 }
dc974b3a6cf4 Two independent changes (oops):
Guido van Rossum <guido@python.org>
parents: 6197
diff changeset
445
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 /* Parse input from a file and execute it */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
449 PyRun_AnyFile(FILE *fp, char *filename)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
451 return PyRun_AnyFileExFlags(fp, filename, 0, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
452 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
453
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
454 int
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
455 PyRun_AnyFileFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
456 {
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
457 return PyRun_AnyFileExFlags(fp, filename, 0, flags);
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
458 }
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
459
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
460 int
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
461 PyRun_AnyFileEx(FILE *fp, char *filename, int closeit)
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
462 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
463 return PyRun_AnyFileExFlags(fp, filename, closeit, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
464 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
465
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
466 int
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
467 PyRun_AnyFileExFlags(FILE *fp, char *filename, int closeit,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
468 PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
469 {
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 if (filename == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 filename = "???";
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
472 if (Py_FdIsInteractive(fp, filename)) {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
473 int err = PyRun_InteractiveLoopFlags(fp, filename, flags);
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
474 if (closeit)
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
475 fclose(fp);
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
476 return err;
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
477 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 else
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
479 return PyRun_SimpleFileExFlags(fp, filename, closeit, flags);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
481
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
483 PyRun_InteractiveLoop(FILE *fp, char *filename)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
485 return PyRun_InteractiveLoopFlags(fp, filename, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
486 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
487
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
488 int
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
489 PyRun_InteractiveLoopFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
490 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
491 PyObject *v;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 int ret;
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
493 PyCompilerFlags local_flags;
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
494
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
495 if (flags == NULL) {
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
496 flags = &local_flags;
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18101
diff changeset
497 local_flags.cf_flags = 0;
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
498 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
499 v = PySys_GetObject("ps1");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
501 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
502 Py_XDECREF(v);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
504 v = PySys_GetObject("ps2");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
506 PySys_SetObject("ps2", v = PyString_FromString("... "));
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
507 Py_XDECREF(v);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 for (;;) {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
510 ret = PyRun_InteractiveOneFlags(fp, filename, flags);
3575
e2b3a696d69c Use new names for debug macros. Don't include pythonrun.h.
Guido van Rossum <guido@python.org>
parents: 3489
diff changeset
511 #ifdef Py_REF_DEBUG
2612
ca6b60d4ea90 fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents: 2529
diff changeset
512 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 #endif
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 if (ret == E_EOF)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 return 0;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 /*
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 if (ret == E_NOMEM)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 return -1;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
522
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
524 PyRun_InteractiveOne(FILE *fp, char *filename)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 {
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
526 return PyRun_InteractiveOneFlags(fp, filename, NULL);
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
527 }
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
528
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
529 int
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
530 PyRun_InteractiveOneFlags(FILE *fp, char *filename, PyCompilerFlags *flags)
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
531 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
532 PyObject *m, *d, *v, *w;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 node *n;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
534 perrdetail err;
6661
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
535 char *ps1 = "", *ps2 = "";
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
536
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
537 v = PySys_GetObject("ps1");
6661
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
538 if (v != NULL) {
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
539 v = PyObject_Str(v);
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
540 if (v == NULL)
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
541 PyErr_Clear();
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
542 else if (PyString_Check(v))
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
543 ps1 = PyString_AsString(v);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 }
6661
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
545 w = PySys_GetObject("ps2");
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
546 if (w != NULL) {
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
547 w = PyObject_Str(w);
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
548 if (w == NULL)
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
549 PyErr_Clear();
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
550 else if (PyString_Check(w))
99955b65408d Apply str() to sys.ps1 or sys.ps2 before using them as a prompt, so
Guido van Rossum <guido@python.org>
parents: 6600
diff changeset
551 ps2 = PyString_AsString(w);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 }
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
553 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar,
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
554 Py_single_input, ps1, ps2, &err,
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
555 (flags &&
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
556 flags->cf_flags & PyCF_GENERATORS) ?
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
557 PyPARSE_YIELD_IS_KEYWORD : 0);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
558 Py_XDECREF(v);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
559 Py_XDECREF(w);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
560 if (n == NULL) {
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
561 if (err.error == E_EOF) {
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
562 if (err.text)
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12555
diff changeset
563 PyMem_DEL(err.text);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
564 return E_EOF;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
565 }
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
566 err_input(&err);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
567 PyErr_Print();
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
568 return err.error;
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
570 m = PyImport_AddModule("__main__");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 if (m == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 return -1;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
573 d = PyModule_GetDict(m);
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
574 v = run_node(n, filename, d, d, flags);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
576 PyErr_Print();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 return -1;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
579 Py_DECREF(v);
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
580 if (Py_FlushLine())
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
581 PyErr_Clear();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 return 0;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
584
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
586 PyRun_SimpleFile(FILE *fp, char *filename)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
587 {
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
588 return PyRun_SimpleFileEx(fp, filename, 0);
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
589 }
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
590
15738
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
591 /* Check whether a file maybe a pyc file: Look at the extension,
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
592 the file type, and, if we may close it, at the first few bytes. */
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
593
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
594 static int
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
595 maybe_pyc_file(FILE *fp, char* filename, char* ext, int closeit)
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
596 {
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
597 if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0)
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
598 return 1;
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
599
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
600 #ifdef macintosh
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
601 /* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
602 if (PyMac_getfiletype(filename) == 'PYC '
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
603 || PyMac_getfiletype(filename) == 'APPL')
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
604 return 1;
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
605 #endif /* macintosh */
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
606
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
607 /* Only look into the file if we are allowed to close it, since
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
608 it then should also be seekable. */
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
609 if (closeit) {
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
610 /* Read only two bytes of the magic. If the file was opened in
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
611 text mode, the bytes 3 and 4 of the magic (\r\n) might not
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
612 be read as they are on disk. */
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
613 unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
614 unsigned char buf[2];
16560
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
615 /* Mess: In case of -x, the stream is NOT at its start now,
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
616 and ungetc() was used to push back the first newline,
16648
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
617 which makes the current stream position formally undefined,
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
618 and a x-platform nightmare.
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
619 Unfortunately, we have no direct way to know whether -x
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
620 was specified. So we use a terrible hack: if the current
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
621 stream position is not 0, we assume -x was specified, and
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
622 give up. Bug 132850 on SourceForge spells out the
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
623 hopelessness of trying anything else (fseek and ftell
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
624 don't work predictably x-platform for text-mode files).
16560
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
625 */
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
626 int ispyc = 0;
16648
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
627 if (ftell(fp) == 0) {
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
628 if (fread(buf, 1, 2, fp) == 2 &&
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
629 ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
630 ispyc = 1;
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
631 rewind(fp);
a51b9961f3bb Bug #132850 unix line terminator on windows.
Tim Peters <tim.peters@gmail.com>
parents: 16560
diff changeset
632 }
16560
e8e636cc1a09 Ugly fix for SF bug 131239 (-x flag busted).
Tim Peters <tim.peters@gmail.com>
parents: 16421
diff changeset
633 return ispyc;
15738
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
634 }
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
635 return 0;
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
636 }
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
637
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
638 int
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
639 PyRun_SimpleFileEx(FILE *fp, char *filename, int closeit)
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
640 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
641 return PyRun_SimpleFileExFlags(fp, filename, closeit, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
642 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
643
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
644 int
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
645 PyRun_SimpleFileExFlags(FILE *fp, char *filename, int closeit,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
646 PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
647 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
648 PyObject *m, *d, *v;
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
649 char *ext;
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
650
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
651 m = PyImport_AddModule("__main__");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 if (m == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 return -1;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
654 d = PyModule_GetDict(m);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
655 ext = filename + strlen(filename) - 4;
15738
76caf0855d1f Recognize pyc files even if they don't end in pyc.
Martin v. Löwis <martin@v.loewis.de>
parents: 14637
diff changeset
656 if (maybe_pyc_file(fp, filename, ext, closeit)) {
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
657 /* Try to run a pyc file. First, re-open in binary */
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
658 if (closeit)
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
659 fclose(fp);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
660 if( (fp = fopen(filename, "rb")) == NULL ) {
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
661 fprintf(stderr, "python: Can't reopen .pyc file\n");
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
662 return -1;
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
663 }
5234
07ed52e200db Allow passing a .pyo file.
Guido van Rossum <guido@python.org>
parents: 5170
diff changeset
664 /* Turn on optimization if a .pyo file is given */
07ed52e200db Allow passing a .pyo file.
Guido van Rossum <guido@python.org>
parents: 5170
diff changeset
665 if (strcmp(ext, ".pyo") == 0)
07ed52e200db Allow passing a .pyo file.
Guido van Rossum <guido@python.org>
parents: 5170
diff changeset
666 Py_OptimizeFlag = 1;
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
667 v = run_pyc_file(fp, filename, d, d, flags);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
668 } else {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
669 v = PyRun_FileExFlags(fp, filename, Py_file_input, d, d,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
670 closeit, flags);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
671 }
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
673 PyErr_Print();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 return -1;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
676 Py_DECREF(v);
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
677 if (Py_FlushLine())
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
678 PyErr_Clear();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 return 0;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
681
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
683 PyRun_SimpleString(char *command)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
685 PyObject *m, *d, *v;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
686 m = PyImport_AddModule("__main__");
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 if (m == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 return -1;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
689 d = PyModule_GetDict(m);
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5415
diff changeset
690 v = PyRun_String(command, Py_file_input, d, d);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 if (v == NULL) {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
692 PyErr_Print();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 return -1;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
695 Py_DECREF(v);
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
696 if (Py_FlushLine())
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
697 PyErr_Clear();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 return 0;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
699 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
700
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
701 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
702 parse_syntax_error(PyObject *err, PyObject **message, char **filename,
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
703 int *lineno, int *offset, char **text)
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
704 {
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
705 long hold;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
706 PyObject *v;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
707
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
708 /* old style errors */
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
709 if (PyTuple_Check(err))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
710 return PyArg_Parse(err, "(O(ziiz))", message, filename,
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
711 lineno, offset, text);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
712
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
713 /* new style errors. `err' is an instance */
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
714
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
715 if (! (v = PyObject_GetAttrString(err, "msg")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
716 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
717 *message = v;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
718
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
719 if (!(v = PyObject_GetAttrString(err, "filename")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
720 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
721 if (v == Py_None)
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
722 *filename = NULL;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
723 else if (! (*filename = PyString_AsString(v)))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
724 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
725
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
726 Py_DECREF(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
727 if (!(v = PyObject_GetAttrString(err, "lineno")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
728 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
729 hold = PyInt_AsLong(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
730 Py_DECREF(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
731 v = NULL;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
732 if (hold < 0 && PyErr_Occurred())
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
733 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
734 *lineno = (int)hold;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
735
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
736 if (!(v = PyObject_GetAttrString(err, "offset")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
737 goto finally;
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
738 if (v == Py_None) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
739 *offset = -1;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
740 Py_DECREF(v);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
741 v = NULL;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
742 } else {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
743 hold = PyInt_AsLong(v);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
744 Py_DECREF(v);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
745 v = NULL;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
746 if (hold < 0 && PyErr_Occurred())
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
747 goto finally;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
748 *offset = (int)hold;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
749 }
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
750
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
751 if (!(v = PyObject_GetAttrString(err, "text")))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
752 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
753 if (v == Py_None)
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
754 *text = NULL;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
755 else if (! (*text = PyString_AsString(v)))
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
756 goto finally;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
757 Py_DECREF(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
758 return 1;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
759
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
760 finally:
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
761 Py_XDECREF(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
762 return 0;
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
763 }
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
764
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
766 PyErr_Print(void)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 {
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
768 PyErr_PrintEx(1);
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
769 }
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
770
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
771 static void
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
772 print_error_text(PyObject *f, int offset, char *text)
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
773 {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
774 char *nl;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
775 if (offset >= 0) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
776 if (offset > 0 && offset == (int)strlen(text))
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
777 offset--;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
778 for (;;) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
779 nl = strchr(text, '\n');
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
780 if (nl == NULL || nl-text >= offset)
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
781 break;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
782 offset -= (nl+1-text);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
783 text = nl+1;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
784 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
785 while (*text == ' ' || *text == '\t') {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
786 text++;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
787 offset--;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
788 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
789 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
790 PyFile_WriteString(" ", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
791 PyFile_WriteString(text, f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
792 if (*text == '\0' || text[strlen(text)-1] != '\n')
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
793 PyFile_WriteString("\n", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
794 if (offset == -1)
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
795 return;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
796 PyFile_WriteString(" ", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
797 offset--;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
798 while (offset > 0) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
799 PyFile_WriteString(" ", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
800 offset--;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
801 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
802 PyFile_WriteString("^\n", f);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
803 }
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
804
17212
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
805 static void
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
806 handle_system_exit(void)
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
807 {
17212
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
808 PyObject *exception, *value, *tb;
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
809 PyErr_Fetch(&exception, &value, &tb);
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
810 if (Py_FlushLine())
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
811 PyErr_Clear();
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
812 fflush(stdout);
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
813 if (value == NULL || value == Py_None)
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
814 Py_Exit(0);
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
815 if (PyInstance_Check(value)) {
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
816 /* The error code should be in the `code' attribute. */
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
817 PyObject *code = PyObject_GetAttrString(value, "code");
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
818 if (code) {
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
819 Py_DECREF(value);
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
820 value = code;
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
821 if (value == Py_None)
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
822 Py_Exit(0);
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
823 }
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
824 /* If we failed to dig out the 'code' attribute,
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
825 just let the else clause below print the error. */
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
826 }
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
827 if (PyInt_Check(value))
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
828 Py_Exit((int)PyInt_AsLong(value));
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
829 else {
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
830 PyObject_Print(value, stderr, Py_PRINT_RAW);
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
831 PySys_WriteStderr("\n");
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
832 Py_Exit(1);
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
833 }
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
834 }
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
835
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
836 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
837 PyErr_PrintEx(int set_sys_last_vars)
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
838 {
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
839 PyObject *exception, *v, *tb, *hook;
17212
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
840
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
841 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
842 handle_system_exit();
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
843 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
844 PyErr_Fetch(&exception, &v, &tb);
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
845 PyErr_NormalizeException(&exception, &v, &tb);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2022
diff changeset
846 if (exception == NULL)
5611
5ed3a21cc122 Can't return 0 from void function...
Guido van Rossum <guido@python.org>
parents: 5607
diff changeset
847 return;
7206
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
848 if (set_sys_last_vars) {
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
849 PySys_SetObject("last_type", exception);
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
850 PySys_SetObject("last_value", v);
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
851 PySys_SetObject("last_traceback", tb);
3f255aedf621 Ehm, three unrelated changes.
Guido van Rossum <guido@python.org>
parents: 7113
diff changeset
852 }
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
853 hook = PySys_GetObject("excepthook");
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
854 if (hook) {
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
855 PyObject *args = Py_BuildValue("(OOO)",
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
856 exception, v ? v : Py_None, tb ? tb : Py_None);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
857 PyObject *result = PyEval_CallObject(hook, args);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
858 if (result == NULL) {
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
859 PyObject *exception2, *v2, *tb2;
17212
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
860 if (PyErr_ExceptionMatches(PyExc_SystemExit)) {
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
861 handle_system_exit();
215dc725db84 Finishing touch to Ping's changes. This is a patch that Ping sent me
Guido van Rossum <guido@python.org>
parents: 17207
diff changeset
862 }
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
863 PyErr_Fetch(&exception2, &v2, &tb2);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
864 PyErr_NormalizeException(&exception2, &v2, &tb2);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
865 if (Py_FlushLine())
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
866 PyErr_Clear();
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
867 fflush(stdout);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
868 PySys_WriteStderr("Error in sys.excepthook:\n");
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
869 PyErr_Display(exception2, v2, tb2);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
870 PySys_WriteStderr("\nOriginal exception was:\n");
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
871 PyErr_Display(exception, v, tb);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
872 }
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
873 Py_XDECREF(result);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
874 Py_XDECREF(args);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
875 } else {
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
876 PySys_WriteStderr("sys.excepthook is missing\n");
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
877 PyErr_Display(exception, v, tb);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
878 }
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
879 Py_XDECREF(exception);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
880 Py_XDECREF(v);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
881 Py_XDECREF(tb);
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
882 }
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
883
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
884 void PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
885 {
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
886 int err = 0;
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
887 PyObject *v = value;
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
888 PyObject *f = PySys_GetObject("stderr");
1073
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1015
diff changeset
889 if (f == NULL)
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1015
diff changeset
890 fprintf(stderr, "lost sys.stderr\n");
2c0432fcc270 Modified most (but not yet all) I/O to always go through sys.stdout or
Guido van Rossum <guido@python.org>
parents: 1015
diff changeset
891 else {
7509
b5d52c415b15 Fix the handling of errors in Py_FlushLine() in a few places.
Guido van Rossum <guido@python.org>
parents: 7206
diff changeset
892 if (Py_FlushLine())
b5d52c415b15 Fix the handling of errors in Py_FlushLine() in a few places.
Guido van Rossum <guido@python.org>
parents: 7206
diff changeset
893 PyErr_Clear();
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
894 fflush(stdout);
17164
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
895 if (tb && tb != Py_None)
b44e77a12978 Add sys.excepthook.
Ka-Ping Yee <ping@zesty.ca>
parents: 17125
diff changeset
896 err = PyTraceBack_Print(tb, f);
6165
29ddf1049842 PyErr_Print(): Use PyErr_GivenExceptionMatches() instead of pointer
Barry Warsaw <barry@python.org>
parents: 6136
diff changeset
897 if (err == 0 &&
29ddf1049842 PyErr_Print(): Use PyErr_GivenExceptionMatches() instead of pointer
Barry Warsaw <barry@python.org>
parents: 6136
diff changeset
898 PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError))
29ddf1049842 PyErr_Print(): Use PyErr_GivenExceptionMatches() instead of pointer
Barry Warsaw <barry@python.org>
parents: 6136
diff changeset
899 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
900 PyObject *message;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
901 char *filename, *text;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
902 int lineno, offset;
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
903 if (!parse_syntax_error(v, &message, &filename,
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
904 &lineno, &offset, &text))
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
905 PyErr_Clear();
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
906 else {
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
907 char buf[10];
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
908 PyFile_WriteString(" File \"", f);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
909 if (filename == NULL)
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
910 PyFile_WriteString("<string>", f);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
911 else
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
912 PyFile_WriteString(filename, f);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
913 PyFile_WriteString("\", line ", f);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
914 sprintf(buf, "%d", lineno);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
915 PyFile_WriteString(buf, f);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
916 PyFile_WriteString("\n", f);
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
917 if (text != NULL)
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16648
diff changeset
918 print_error_text(f, offset, text);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
919 v = message;
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
920 /* Can't be bothered to check all those
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
921 PyFile_WriteString() calls */
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
922 if (PyErr_Occurred())
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
923 err = -1;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
924 }
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
925 }
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
926 if (err) {
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
927 /* Don't do anything else */
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
928 }
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
929 else if (PyClass_Check(exception)) {
6322
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
930 PyClassObject* exc = (PyClassObject*)exception;
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
931 PyObject* className = exc->cl_name;
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
932 PyObject* moduleName =
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
933 PyDict_GetItemString(exc->cl_dict, "__module__");
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
934
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
935 if (moduleName == NULL)
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
936 err = PyFile_WriteString("<unknown>", f);
6322
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
937 else {
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
938 char* modstr = PyString_AsString(moduleName);
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
939 if (modstr && strcmp(modstr, "exceptions"))
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
940 {
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
941 err = PyFile_WriteString(modstr, f);
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
942 err += PyFile_WriteString(".", f);
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
943 }
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
944 }
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
945 if (err == 0) {
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
946 if (className == NULL)
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
947 err = PyFile_WriteString("<unknown>", f);
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
948 else
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
949 err = PyFile_WriteObject(className, f,
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
950 Py_PRINT_RAW);
05d943c4597c PyErr_Print(): When printing a class exception, try to dig out the
Barry Warsaw <barry@python.org>
parents: 6246
diff changeset
951 }
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
952 }
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
953 else
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
954 err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
955 if (err == 0) {
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
956 if (v != NULL && v != Py_None) {
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
957 PyObject *s = PyObject_Str(v);
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
958 /* only print colon if the str() of the
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
959 object is not the empty string
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
960 */
6246
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
961 if (s == NULL)
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
962 err = -1;
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
963 else if (!PyString_Check(s) ||
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
964 PyString_GET_SIZE(s) != 0)
6197
7da7ba5c6525 Added Py_UseClassExceptionsFlag, the variable containing the state of
Barry Warsaw <barry@python.org>
parents: 6165
diff changeset
965 err = PyFile_WriteString(": ", f);
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
966 if (err == 0)
6246
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
967 err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
4cf20fa883a9 Fixed some details of printing the str() of an exception. This fixes
Guido van Rossum <guido@python.org>
parents: 6201
diff changeset
968 Py_XDECREF(s);
2293
a24146241991 print class name for exceptions that are classes
Guido van Rossum <guido@python.org>
parents: 2279
diff changeset
969 }
a24146241991 print class name for exceptions that are classes
Guido van Rossum <guido@python.org>
parents: 2279
diff changeset
970 }
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
971 if (err == 0)
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
972 err = PyFile_WriteString("\n", f);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 }
5607
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
974 /* If an error happened here, don't show it.
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
975 XXX This is wrong, but too many callers rely on this behavior. */
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
976 if (err != 0)
cbb4e3cf086e Py_FlushLine and PyFile_WriteString now return error indicators
Guido van Rossum <guido@python.org>
parents: 5593
diff changeset
977 PyErr_Clear();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
978 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
979
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
980 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
981 PyRun_String(char *str, int start, PyObject *globals, PyObject *locals)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
983 return run_err_node(PyParser_SimpleParseString(str, start),
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
984 "<string>", globals, locals, NULL);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
986
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
987 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
988 PyRun_File(FILE *fp, char *filename, int start, PyObject *globals,
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
989 PyObject *locals)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
990 {
14231
396b4ab3b9c2 Hard to believe Guido compiled this! Function lacked a return stmt.
Tim Peters <tim.peters@gmail.com>
parents: 14228
diff changeset
991 return PyRun_FileEx(fp, filename, start, globals, locals, 0);
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
992 }
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
993
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
994 PyObject *
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
995 PyRun_FileEx(FILE *fp, char *filename, int start, PyObject *globals,
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
996 PyObject *locals, int closeit)
14228
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
997 {
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
998 node *n = PyParser_SimpleParseFile(fp, filename, start);
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
999 if (closeit)
20377f15b96a Add three new APIs: PyRun_AnyFileEx(), PyRun_SimpleFileEx(),
Guido van Rossum <guido@python.org>
parents: 14227
diff changeset
1000 fclose(fp);
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1001 return run_err_node(n, filename, globals, locals, NULL);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1004 PyObject *
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1005 PyRun_StringFlags(char *str, int start, PyObject *globals, PyObject *locals,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1006 PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1007 {
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1008 return run_err_node(PyParser_SimpleParseString(str, start),
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1009 "<string>", globals, locals, flags);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1010 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1011
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1012 PyObject *
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1013 PyRun_FileFlags(FILE *fp, char *filename, int start, PyObject *globals,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1014 PyObject *locals, PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1015 {
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1016 return PyRun_FileExFlags(fp, filename, start, globals, locals, 0,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1017 flags);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1018 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1019
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1020 PyObject *
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1021 PyRun_FileExFlags(FILE *fp, char *filename, int start, PyObject *globals,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1022 PyObject *locals, int closeit, PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1023 {
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1024 node *n = PyParser_SimpleParseFileFlags(fp, filename, start,
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1025 (flags && flags->cf_flags & PyCF_GENERATORS) ?
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1026 PyPARSE_YIELD_IS_KEYWORD : 0);
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1027 if (closeit)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1028 fclose(fp);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1029 return run_err_node(n, filename, globals, locals, flags);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1030 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1031
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1032 static PyObject *
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1033 run_err_node(node *n, char *filename, PyObject *globals, PyObject *locals,
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1034 PyCompilerFlags *flags)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035 {
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1036 if (n == NULL)
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1037 return NULL;
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1038 return run_node(n, filename, globals, locals, flags);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1040
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1041 static PyObject *
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1042 run_node(node *n, char *filename, PyObject *globals, PyObject *locals,
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1043 PyCompilerFlags *flags)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1044 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1045 PyCodeObject *co;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1046 PyObject *v;
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16845
diff changeset
1047 co = PyNode_CompileFlags(n, filename, flags);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1048 PyNode_Free(n);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049 if (co == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050 return NULL;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1051 v = PyEval_EvalCode(co, globals, locals);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1052 Py_DECREF(co);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 return v;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1056 static PyObject *
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1057 run_pyc_file(FILE *fp, char *filename, PyObject *globals, PyObject *locals,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1058 PyCompilerFlags *flags)
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1059 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1060 PyCodeObject *co;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1061 PyObject *v;
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1062 long magic;
14278
fb8fb53a2eff Add a comment explaining the return value of PyOS_CheckStack().
Fred Drake <fdrake@acm.org>
parents: 14231
diff changeset
1063 long PyImport_GetMagicNumber(void);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1064
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1065 magic = PyMarshal_ReadLongFromFile(fp);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1066 if (magic != PyImport_GetMagicNumber()) {
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1067 PyErr_SetString(PyExc_RuntimeError,
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1068 "Bad magic number in .pyc file");
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1069 return NULL;
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1070 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1071 (void) PyMarshal_ReadLongFromFile(fp);
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16073
diff changeset
1072 v = PyMarshal_ReadLastObjectFromFile(fp);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1073 fclose(fp);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1074 if (v == NULL || !PyCode_Check(v)) {
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1075 Py_XDECREF(v);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1076 PyErr_SetString(PyExc_RuntimeError,
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1077 "Bad code object in .pyc file");
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1078 return NULL;
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1079 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1080 co = (PyCodeObject *)v;
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1081 v = PyEval_EvalCode(co, globals, locals);
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1082 if (v && flags) {
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1083 if (co->co_flags & CO_NESTED)
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18101
diff changeset
1084 flags->cf_flags |= PyCF_NESTED_SCOPES;
18612
4ac98fe7abd2 future.c: insert a cosmetic space.
Tim Peters <tim.peters@gmail.com>
parents: 18611
diff changeset
1085 if (co->co_flags & CO_GENERATOR_ALLOWED)
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18101
diff changeset
1086 flags->cf_flags |= PyCF_GENERATORS;
18101
9dda0c11e006 Temporarily disable the message to stderr. Jeremy will know what to do
Marc-André Lemburg <mal@egenix.com>
parents: 17228
diff changeset
1087 #if 0
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1088 fprintf(stderr, "run_pyc_file: nested_scopes: %d\n",
18611
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18101
diff changeset
1089 flags->cf_flags & PyCF_NESTED_SCOPES);
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18101
diff changeset
1090 fprintf(stderr, "run_pyc_file: generators: %d\n",
fdb36a0c7e6e Part way to allowing "from __future__ import generators" to communicate
Tim Peters <tim.peters@gmail.com>
parents: 18101
diff changeset
1091 flags->cf_flags & PyCF_GENERATORS);
18101
9dda0c11e006 Temporarily disable the message to stderr. Jeremy will know what to do
Marc-André Lemburg <mal@egenix.com>
parents: 17228
diff changeset
1092 #endif
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1093 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1094 Py_DECREF(co);
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1095 return v;
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1096 }
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1870
diff changeset
1097
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1098 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1099 Py_CompileString(char *str, char *filename, int start)
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1100 {
17125
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1101 return Py_CompileStringFlags(str, filename, start, NULL);
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1102 }
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1103
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1104 PyObject *
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1105 Py_CompileStringFlags(char *str, char *filename, int start,
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1106 PyCompilerFlags *flags)
2054cea38754 Extend support for from __future__ import nested_scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16916
diff changeset
1107 {
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1108 node *n;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1109 PyCodeObject *co;
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1110 n = PyParser_SimpleParseStringFlags(str, start,
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1111 (flags && flags->cf_flags & PyCF_GENERATORS) ?
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1112 PyPARSE_YIELD_IS_KEYWORD : 0);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1113 if (n == NULL)
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1114 return NULL;
17228
32fb6643adf1 Bug fix: compile() called from a nested-scopes-enable Python was not
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17212
diff changeset
1115 co = PyNode_CompileFlags(n, filename, flags);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1116 PyNode_Free(n);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1117 return (PyObject *)co;
1263
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1118 }
65a1e4e9cd54 * Fixed some subtleties with fastlocals. You can no longer access
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1119
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1120 struct symtable *
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1121 Py_SymtableString(char *str, char *filename, int start)
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1122 {
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1123 node *n;
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1124 struct symtable *st;
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1125 n = PyParser_SimpleParseString(str, start);
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1126 if (n == NULL)
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1127 return NULL;
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1128 st = PyNode_CompileSymtable(n, filename);
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1129 PyNode_Free(n);
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1130 return st;
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1131 }
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16311
diff changeset
1132
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1133 /* Simplified interface to parsefile -- return node or set exception */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1134
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1135 node *
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1136 PyParser_SimpleParseFileFlags(FILE *fp, char *filename, int start, int flags)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137 {
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1138 node *n;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1139 perrdetail err;
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1140 n = PyParser_ParseFileFlags(fp, filename, &_PyParser_Grammar, start,
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1141 (char *)0, (char *)0, &err, flags);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1142 if (n == NULL)
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1143 err_input(&err);
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1144 return n;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1145 }
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1146
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1147 node *
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1148 PyParser_SimpleParseFile(FILE *fp, char *filename, int start)
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1149 {
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1150 return PyParser_SimpleParseFileFlags(fp, filename, start, 0);
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1151 }
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1152
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1153 /* Simplified interface to parsestring -- return node or set exception */
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1154
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1155 node *
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1156 PyParser_SimpleParseStringFlags(char *str, int start, int flags)
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1157 {
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1158 node *n;
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1159 perrdetail err;
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1160 n = PyParser_ParseStringFlags(str, &_PyParser_Grammar, start, &err,
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1161 flags);
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1162 if (n == NULL)
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1163 err_input(&err);
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1164 return n;
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1165 }
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1166
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1167 node *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1168 PyParser_SimpleParseString(char *str, int start)
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1169 {
18615
0a90e5ec61b6 Ugly. A pile of new xxxFlags() functions, to communicate to the parser
Tim Peters <tim.peters@gmail.com>
parents: 18612
diff changeset
1170 return PyParser_SimpleParseStringFlags(str, start, 0);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1173 /* Set the error appropriate to the given input error code (see errcode.h) */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1174
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1175 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1176 err_input(perrdetail *err)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177 {
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1178 PyObject *v, *w, *errtype;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1179 char *msg = NULL;
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1180 errtype = PyExc_SyntaxError;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1181 v = Py_BuildValue("(ziiz)", err->filename,
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1182 err->lineno, err->offset, err->text);
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1183 if (err->text != NULL) {
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12555
diff changeset
1184 PyMem_DEL(err->text);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1185 err->text = NULL;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1186 }
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1187 switch (err->error) {
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1188 case E_SYNTAX:
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1189 errtype = PyExc_IndentationError;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1190 if (err->expected == INDENT)
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1191 msg = "expected an indented block";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1192 else if (err->token == INDENT)
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1193 msg = "unexpected indent";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1194 else if (err->token == DEDENT)
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1195 msg = "unexpected unindent";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1196 else {
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1197 errtype = PyExc_SyntaxError;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1198 msg = "invalid syntax";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1199 }
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1200 break;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1201 case E_TOKEN:
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1202 msg = "invalid token";
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1203 break;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1204 case E_INTR:
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1205 PyErr_SetNone(PyExc_KeyboardInterrupt);
9965
c1091e82e5aa err_input(): Nailed a small memory leak. If the error is E_INTR, the
Barry Warsaw <barry@python.org>
parents: 9819
diff changeset
1206 Py_XDECREF(v);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1207 return;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1208 case E_NOMEM:
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1209 PyErr_NoMemory();
9965
c1091e82e5aa err_input(): Nailed a small memory leak. If the error is E_INTR, the
Barry Warsaw <barry@python.org>
parents: 9819
diff changeset
1210 Py_XDECREF(v);
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1211 return;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1212 case E_EOF:
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1213 msg = "unexpected EOF while parsing";
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1214 break;
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1215 case E_TABSPACE:
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1216 errtype = PyExc_TabError;
7967
bbd2b5d8d0b6 Translate E_INDENT to the clearest error message I can think of.
Guido van Rossum <guido@python.org>
parents: 7847
diff changeset
1217 msg = "inconsistent use of tabs and spaces in indentation";
bbd2b5d8d0b6 Translate E_INDENT to the clearest error message I can think of.
Guido van Rossum <guido@python.org>
parents: 7847
diff changeset
1218 break;
12970
63436719d12e Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12754
diff changeset
1219 case E_OVERFLOW:
63436719d12e Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12754
diff changeset
1220 msg = "expression too long";
63436719d12e Add new parser error code, E_OVERFLOW. This error is returned when
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12754
diff changeset
1221 break;
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1222 case E_DEDENT:
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1223 errtype = PyExc_IndentationError;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1224 msg = "unindent does not match any outer indentation level";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1225 break;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1226 case E_TOODEEP:
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1227 errtype = PyExc_IndentationError;
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1228 msg = "too many levels of indentation";
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1229 break;
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1230 default:
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1231 fprintf(stderr, "error=%d\n", err->error);
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1232 msg = "unknown parsing error";
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1233 break;
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1234 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1235 w = Py_BuildValue("(sO)", msg, v);
17168
0d52358cb288 Fix memory leak with SyntaxError. (The DECREF was originally hidden
Guido van Rossum <guido@python.org>
parents: 17164
diff changeset
1236 Py_XDECREF(v);
13539
feab677269e0 Create two new exceptions: IndentationError and TabError. These are
Fred Drake <fdrake@acm.org>
parents: 13447
diff changeset
1237 PyErr_SetObject(errtype, w);
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1238 Py_XDECREF(w);
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1241 /* Print fatal error message and abort */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1242
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1243 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1244 Py_FatalError(char *msg)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1245 {
1911
a7f269610ec2 * Python/pythonrun.c: add string "Python" to fatal error message
Guido van Rossum <guido@python.org>
parents: 1898
diff changeset
1246 fprintf(stderr, "Fatal Python error: %s\n", msg);
2242
d3b5be0c872f make call_pyc_file static
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
1247 #ifdef macintosh
d3b5be0c872f make call_pyc_file static
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
1248 for (;;);
d3b5be0c872f make call_pyc_file static
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
1249 #endif
4291
1427d61f1307 Rationalized MS ifdefs
Guido van Rossum <guido@python.org>
parents: 3609
diff changeset
1250 #ifdef MS_WIN32
5593
451c7ccf36b6 Added a space in an error message
Guido van Rossum <guido@python.org>
parents: 5438
diff changeset
1251 OutputDebugString("Fatal Python error: ");
2529
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
1252 OutputDebugString(msg);
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
1253 OutputDebugString("\n");
8988
070de4d39ddd Add DebugBreak() call to Py_FatalError() for Mark Hammond (only on
Guido van Rossum <guido@python.org>
parents: 7967
diff changeset
1254 #ifdef _DEBUG
070de4d39ddd Add DebugBreak() call to Py_FatalError() for Mark Hammond (only on
Guido van Rossum <guido@python.org>
parents: 7967
diff changeset
1255 DebugBreak();
2529
16ea57c38c4f In NT, write fatal() msg to debugging device
Guido van Rossum <guido@python.org>
parents: 2318
diff changeset
1256 #endif
8988
070de4d39ddd Add DebugBreak() call to Py_FatalError() for Mark Hammond (only on
Guido van Rossum <guido@python.org>
parents: 7967
diff changeset
1257 #endif /* MS_WIN32 */
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1258 abort();
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1259 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1260
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261 /* Clean up and exit */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1262
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1263 #ifdef WITH_THREAD
9203
07dae6fdae79 Renamed thread.h to pythread.h.
Guido van Rossum <guido@python.org>
parents: 9192
diff changeset
1264 #include "pythread.h"
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1265 int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
961
a31e07847063 New thread.c from Sjoerd, supports _exit_prog(). Use this in goaway()
Guido van Rossum <guido@python.org>
parents: 941
diff changeset
1266 #endif
a31e07847063 New thread.c from Sjoerd, supports _exit_prog(). Use this in goaway()
Guido van Rossum <guido@python.org>
parents: 941
diff changeset
1267
9192
ac6c29d1f10c On second though, NEXITFUNCS should be defined here and not in
Guido van Rossum <guido@python.org>
parents: 9161
diff changeset
1268 #define NEXITFUNCS 32
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1269 static void (*exitfuncs[NEXITFUNCS])(void);
1870
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1270 static int nexitfuncs = 0;
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1271
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1272 int Py_AtExit(void (*func)(void))
1870
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1273 {
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1274 if (nexitfuncs >= NEXITFUNCS)
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1275 return -1;
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1276 exitfuncs[nexitfuncs++] = func;
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1277 return 0;
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1278 }
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1279
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1280 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1281 call_sys_exitfunc(void)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282 {
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1283 PyObject *exitfunc = PySys_GetObject("exitfunc");
1015
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1284
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1285 if (exitfunc) {
17207
16f145aac2ec call_sys_exitfunc(): Remove unused variable f.
Fred Drake <fdrake@acm.org>
parents: 17191
diff changeset
1286 PyObject *res;
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1287 Py_INCREF(exitfunc);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1288 PySys_SetObject("exitfunc", (PyObject *)NULL);
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1289 res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
1015
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1290 if (res == NULL) {
17191
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1291 if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1292 PySys_WriteStderr("Error in sys.exitfunc:\n");
eb06a02e0507 Allow sys.excepthook and sys.exitfunc to quietly exit with a sys.exit().
Ka-Ping Yee <ping@zesty.ca>
parents: 17168
diff changeset
1293 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1294 PyErr_Print();
1015
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1295 }
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1296 Py_DECREF(exitfunc);
1015
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1297 }
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1298
7509
b5d52c415b15 Fix the handling of errors in Py_FlushLine() in a few places.
Guido van Rossum <guido@python.org>
parents: 7206
diff changeset
1299 if (Py_FlushLine())
b5d52c415b15 Fix the handling of errors in Py_FlushLine() in a few places.
Guido van Rossum <guido@python.org>
parents: 7206
diff changeset
1300 PyErr_Clear();
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1301 }
1870
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1302
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1303 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1304 call_ll_exitfuncs(void)
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1305 {
1870
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1306 while (nexitfuncs > 0)
61738ed6cc10 added Py_AtExit() -- register cleanup functions for C modules
Guido van Rossum <guido@python.org>
parents: 1839
diff changeset
1307 (*exitfuncs[--nexitfuncs])();
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
1308
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
1309 fflush(stdout);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5850
diff changeset
1310 fflush(stderr);
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1311 }
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1312
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1313 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1314 Py_Exit(int sts)
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1315 {
5939
2c65bb6ab87c Merge Py_Cleanup() into Py_Finalize(). Call the various small Fini()
Guido van Rossum <guido@python.org>
parents: 5926
diff changeset
1316 Py_Finalize();
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317
3235
3df9ab03badf Moved mac-specific exit handling to macmain.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 3152
diff changeset
1318 #ifdef macintosh
3df9ab03badf Moved mac-specific exit handling to macmain.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 3152
diff changeset
1319 PyMac_Exit(sts);
3df9ab03badf Moved mac-specific exit handling to macmain.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 3152
diff changeset
1320 #else
3df9ab03badf Moved mac-specific exit handling to macmain.c
Jack Jansen <jack.jansen@cwi.nl>
parents: 3152
diff changeset
1321 exit(sts);
2269
a8c6da3bd305 Think C mod to suppress pausing at normal exit
Guido van Rossum <guido@python.org>
parents: 2242
diff changeset
1322 #endif
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1323 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324
1319
cd397266dc1c * Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents: 1289
diff changeset
1325 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1326 initsigs(void)
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1327 {
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1328 #ifdef HAVE_SIGNAL_H
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1329 #ifdef SIGPIPE
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1330 signal(SIGPIPE, SIG_IGN);
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1331 #endif
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1332 #endif /* HAVE_SIGNAL_H */
5170
f5d707ce4223 Greatly renamed. Not a very thorough job -- I'm going to restructure
Guido van Rossum <guido@python.org>
parents: 5108
diff changeset
1333 PyOS_InitInterrupts(); /* May imply initsignal() */
1079
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1334 }
e4c6a0dd5c2b * bltinmodule.c: added built-in function cmp(a, b)
Guido van Rossum <guido@python.org>
parents: 1073
diff changeset
1335
3575
e2b3a696d69c Use new names for debug macros. Don't include pythonrun.h.
Guido van Rossum <guido@python.org>
parents: 3489
diff changeset
1336 #ifdef Py_TRACE_REFS
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1337 /* Ask a yes/no question */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1338
1015
f3d5e2c3b9a3 Add sys.exitfunc feature. Make askync non-static.
Guido van Rossum <guido@python.org>
parents: 961
diff changeset
1339 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1340 _Py_AskYesNo(char *prompt)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1341 {
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 char buf[256];
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1343
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1344 printf("%s [ny] ", prompt);
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 if (fgets(buf, sizeof buf, stdin) == NULL)
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346 return 0;
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1347 return buf[0] == 'y' || buf[0] == 'Y';
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1349 #endif
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1350
1839
1f4a86dbcd79 * Python/pythonrun.c (print_error): added INCREF/DECREF pair --
Guido van Rossum <guido@python.org>
parents: 1413
diff changeset
1351 #ifdef MPW
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1352
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1353 /* Check for file descriptor connected to interactive device.
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1354 Pretend that stdin is always interactive, other files never. */
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1356 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1357 isatty(int fd)
923
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1358 {
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1359 return fd == fileno(stdin);
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1360 }
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1361
72aceed365d4 * Makefile adapted to changes below.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 #endif
5108
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1363
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1364 /*
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1365 * The file descriptor fd is considered ``interactive'' if either
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1366 * a) isatty(fd) is TRUE, or
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1367 * b) the -i flag was given, and the filename associated with
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1368 * the descriptor is NULL or "<stdin>" or "???".
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1369 */
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1370 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
1371 Py_FdIsInteractive(FILE *fp, char *filename)
5108
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1372 {
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1373 if (isatty((int)fileno(fp)))
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1374 return 1;
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1375 if (!Py_InteractiveFlag)
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1376 return 0;
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1377 return (filename == NULL) ||
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1378 (strcmp(filename, "<stdin>") == 0) ||
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1379 (strcmp(filename, "???") == 0);
a9a6d416b945 Added new global flag variable Py_InteractiveFlag and new function
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1380 }
14227
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1381
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1382
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1383 #if defined(USE_STACKCHECK)
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1384 #if defined(WIN32) && defined(_MSC_VER)
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1385
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1386 /* Stack checking for Microsoft C */
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1387
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1388 #include <malloc.h>
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1389 #include <excpt.h>
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1390
14278
fb8fb53a2eff Add a comment explaining the return value of PyOS_CheckStack().
Fred Drake <fdrake@acm.org>
parents: 14231
diff changeset
1391 /*
fb8fb53a2eff Add a comment explaining the return value of PyOS_CheckStack().
Fred Drake <fdrake@acm.org>
parents: 14231
diff changeset
1392 * Return non-zero when we run out of memory on the stack; zero otherwise.
fb8fb53a2eff Add a comment explaining the return value of PyOS_CheckStack().
Fred Drake <fdrake@acm.org>
parents: 14231
diff changeset
1393 */
14227
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1394 int
14280
68b61aa1aa4a PyOS_CheckStack(): Better ANSI'fy this while we're at it.
Fred Drake <fdrake@acm.org>
parents: 14278
diff changeset
1395 PyOS_CheckStack(void)
14227
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1396 {
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1397 __try {
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1398 /* _alloca throws a stack overflow exception if there's
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1399 not enough space left on the stack */
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1400 _alloca(PYOS_STACK_MARGIN * sizeof(void*));
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1401 return 0;
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1402 } __except (EXCEPTION_EXECUTE_HANDLER) {
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1403 /* just ignore all errors */
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1404 }
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1405 return 1;
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1406 }
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1407
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1408 #endif /* WIN32 && _MSC_VER */
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1409
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1410 /* Alternate implementations can be added here... */
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1411
9f9f3280dc68 implements PyOS_CheckStack for Windows and MSVC. this fixes a
Fredrik Lundh <fredrik@pythonware.com>
parents: 14195
diff changeset
1412 #endif /* USE_STACKCHECK */
14637
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1413
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1414
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1415 /* Wrappers around sigaction() or signal(). */
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1416
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1417 PyOS_sighandler_t
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1418 PyOS_getsig(int sig)
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1419 {
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1420 #ifdef HAVE_SIGACTION
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1421 struct sigaction context;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1422 sigaction(sig, NULL, &context);
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1423 return context.sa_handler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1424 #else
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1425 PyOS_sighandler_t handler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1426 handler = signal(sig, SIG_IGN);
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1427 signal(sig, handler);
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1428 return handler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1429 #endif
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1430 }
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1431
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1432 PyOS_sighandler_t
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1433 PyOS_setsig(int sig, PyOS_sighandler_t handler)
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1434 {
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1435 #ifdef HAVE_SIGACTION
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1436 struct sigaction context;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1437 PyOS_sighandler_t oldhandler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1438 sigaction(sig, NULL, &context);
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1439 oldhandler = context.sa_handler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1440 context.sa_handler = handler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1441 sigaction(sig, &context, NULL);
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1442 return oldhandler;
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1443 #else
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1444 return signal(sig, handler);
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1445 #endif
37397bd1f8a8 Add PyOS_getsig() and PyOS_setsig() -- wrappers around signal() or
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
1446 }