comparison Python/pythonrun.c @ 5926:8d2194e22e67 legacy-trunk

The last of the mass checkins for separate (sub)interpreters. Everything should now work again. See the comments for the .h files mass checkin (e.g. pystate.h) for more detail.
author Guido van Rossum <guido@python.org>
date Sat, 02 Aug 1997 03:10:38 +0000
parents 6f67a1bda15a
children 2c65bb6ab87c
comparison
equal deleted inserted replaced
5925:93304b2e14cb 5926:8d2194e22e67
34 #include "Python.h" 34 #include "Python.h"
35 35
36 #include "grammar.h" 36 #include "grammar.h"
37 #include "node.h" 37 #include "node.h"
38 #include "parsetok.h" 38 #include "parsetok.h"
39 #undef argument /* Avoid conflict on Mac */
40 #include "errcode.h" 39 #include "errcode.h"
41 #include "compile.h" 40 #include "compile.h"
42 #include "eval.h" 41 #include "eval.h"
43 #include "marshal.h" 42 #include "marshal.h"
44 43
50 #include <signal.h> 49 #include <signal.h>
51 #endif 50 #endif
52 51
53 #ifdef MS_WIN32 52 #ifdef MS_WIN32
54 #undef BYTE 53 #undef BYTE
55 #undef arglist
56 #include "windows.h" 54 #include "windows.h"
57 #endif 55 #endif
58 56
59 extern char *Py_GetPath(); 57 extern char *Py_GetPath();
60 58
68 PyObject *globals, PyObject *locals)); 66 PyObject *globals, PyObject *locals));
69 static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename, 67 static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename,
70 PyObject *globals, PyObject *locals)); 68 PyObject *globals, PyObject *locals));
71 static void err_input Py_PROTO((perrdetail *)); 69 static void err_input Py_PROTO((perrdetail *));
72 static void initsigs Py_PROTO((void)); 70 static void initsigs Py_PROTO((void));
71 static void finisigs Py_PROTO((void));
73 72
74 int Py_DebugFlag; /* Needed by parser.c */ 73 int Py_DebugFlag; /* Needed by parser.c */
75 int Py_VerboseFlag; /* Needed by import.c */ 74 int Py_VerboseFlag; /* Needed by import.c */
76 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */ 75 int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
77 76
78 /* Initialize the current interpreter; pass in the Python path. */ 77 static int initialized = 0;
79 78
80 void 79 /* Global initializations. Can be undone by Py_Finalize(). Don't
81 Py_Setup() 80 call this twice without an intervening Py_Finalize() call. When
82 { 81 initializations fail, a fatal error is issued and the function does
83 PyImport_Init(); 82 not return. On return, the first thread and interpreter state have
84 83 been created.
85 /* Modules '__builtin__' and 'sys' are initialized here, 84
86 they are needed by random bits of the interpreter. 85 Locking: you must hold the interpreter lock while calling this.
87 All other modules are optional and are initialized 86 (If the lock has not yet been initialized, that's equivalent to
88 when they are first imported. */ 87 having the lock, but you cannot use multiple threads.)
89 88
90 PyBuiltin_Init(); /* Also initializes builtin exceptions */ 89 */
91 PySys_Init();
92
93 PySys_SetPath(Py_GetPath());
94
95 initsigs(); /* Signal handling stuff, including initintr() */
96
97 initmain();
98 }
99
100 /* Create and interpreter and thread state and initialize them;
101 if we already have an interpreter and thread, do nothing.
102 Fatal error if the creation fails. */
103 90
104 void 91 void
105 Py_Initialize() 92 Py_Initialize()
106 { 93 {
94 PyInterpreterState *interp;
107 PyThreadState *tstate; 95 PyThreadState *tstate;
108 PyInterpreterState *interp; 96 PyObject *bimod, *sysmod;
109 char *p; 97 char *p;
98
99 if (initialized)
100 Py_FatalError("Py_Initialize: already initialized");
101 initialized = 1;
110 102
111 if (PyThreadState_Get())
112 return;
113
114 if ((p = getenv("PYTHONDEBUG")) && *p != '\0') 103 if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
115 Py_DebugFlag = 1; 104 Py_DebugFlag = 1;
116 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0') 105 if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
117 Py_VerboseFlag = 1; 106 Py_VerboseFlag = 1;
118 107
119 interp = PyInterpreterState_New(); 108 interp = PyInterpreterState_New();
120 if (interp == NULL) 109 if (interp == NULL)
121 Py_FatalError("PyInterpreterState_New() failed"); 110 Py_FatalError("Py_Initialize: can't make first interpreter");
122 111
123 tstate = PyThreadState_New(interp); 112 tstate = PyThreadState_New(interp);
124 if (tstate == NULL) 113 if (tstate == NULL)
125 Py_FatalError("PyThreadState_New() failed"); 114 Py_FatalError("Py_Initialize: can't make first thread");
126 (void) PyThreadState_Swap(tstate); 115 (void) PyThreadState_Swap(tstate);
127 116
128 Py_Setup(); 117 interp->modules = PyDict_New();
129 118 if (interp->modules == NULL)
119 Py_FatalError("Py_Initialize: can't make modules dictionary");
120
121 bimod = _PyBuiltin_Init();
122 if (bimod == NULL)
123 Py_FatalError("Py_Initialize: can't initialize __builtin__");
124 interp->builtins = PyModule_GetDict(bimod);
125 Py_INCREF(interp->builtins);
126 _PyImport_FixupExtension("__builtin__", "__builtin__");
127
128 sysmod = _PySys_Init();
129 if (sysmod == NULL)
130 Py_FatalError("Py_Initialize: can't initialize sys");
131 interp->sysdict = PyModule_GetDict(sysmod);
132 Py_INCREF(interp->sysdict);
133 _PyImport_FixupExtension("sys", "sys");
130 PySys_SetPath(Py_GetPath()); 134 PySys_SetPath(Py_GetPath());
131 /* XXX Who should set the path -- Setup or Initialize? */ 135 PyDict_SetItemString(interp->sysdict, "modules",
136 interp->modules);
137
138 _PyImport_Init();
139
140 initsigs(); /* Signal handling stuff, including initintr() */
141
142 initmain(); /* Module __main__ */
143 }
144
145 /* Undo the effect of Py_Initialize().
146
147 Beware: if multiple interpreter and/or thread states exist, these
148 are not wiped out; only the current thread and interpreter state
149 are deleted. But since everything else is deleted, those other
150 interpreter and thread states should no longer be used.
151
152 (XXX We should do better, e.g. wipe out all interpreters and
153 threads.)
154
155 Locking: as above.
156
157 */
158
159 void
160 Py_Finalize()
161 {
162 PyInterpreterState *interp;
163 PyThreadState *tstate;
164
165 if (!initialized)
166 Py_FatalError("Py_Finalize: not initialized");
167 initialized = 0;
168
169 tstate = PyThreadState_Get();
170 interp = tstate->interp;
171
172 PyImport_Cleanup();
173 PyInterpreterState_Clear(interp);
174 PyThreadState_Swap(NULL);
175 PyInterpreterState_Delete(interp);
176
177 finisigs();
178 _PyImport_Fini();
179 _PyBuiltin_Fini();
180 PyString_Fini();
181
182 PyGrammar_RemoveAccelerators(&_PyParser_Grammar);
183 }
184
185 /* Create and initialize a new interpreter and thread, and return the
186 new thread. This requires that Py_Initialize() has been called
187 first.
188
189 Unsuccessful initialization yields a NULL pointer. Note that *no*
190 exception information is available even in this case -- the
191 exception information is held in the thread, and there is no
192 thread.
193
194 Locking: as above.
195
196 */
197
198 PyThreadState *
199 Py_NewInterpreter()
200 {
201 PyInterpreterState *interp;
202 PyThreadState *tstate, *save_tstate;
203 PyObject *bimod, *sysmod;
204
205 if (!initialized)
206 Py_FatalError("Py_NewInterpreter: call Py_Initialize first");
207
208 interp = PyInterpreterState_New();
209 if (interp == NULL)
210 return NULL;
211
212 tstate = PyThreadState_New(interp);
213 if (tstate == NULL) {
214 PyInterpreterState_Delete(interp);
215 return NULL;
216 }
217
218 save_tstate = PyThreadState_Swap(tstate);
219
220 /* XXX The following is lax in error checking */
221
222 interp->modules = PyDict_New();
223
224 bimod = _PyImport_FindExtension("__builtin__", "__builtin__");
225 if (bimod != NULL) {
226 interp->builtins = PyModule_GetDict(bimod);
227 Py_INCREF(interp->builtins);
228 }
229 sysmod = _PyImport_FindExtension("sys", "sys");
230 if (bimod != NULL && sysmod != NULL) {
231 interp->sysdict = PyModule_GetDict(sysmod);
232 Py_INCREF(interp->sysdict);
233 PySys_SetPath(Py_GetPath());
234 PyDict_SetItemString(interp->sysdict, "modules",
235 interp->modules);
236 initmain();
237 }
238
239 if (!PyErr_Occurred())
240 return tstate;
241
242 /* Oops, it didn't work. Undo it all. */
243
244 PyErr_Print();
245 PyThreadState_Clear(tstate);
246 PyThreadState_Swap(save_tstate);
247 PyThreadState_Delete(tstate);
248 PyInterpreterState_Delete(interp);
249
250 return NULL;
251 }
252
253 /* Delete an interpreter and its last thread. This requires that the
254 given thread state is current, that the thread has no remaining
255 frames, and that it is its interpreter's only remaining thread.
256 It is a fatal error to violate these constraints.
257
258 (Py_Finalize() doesn't have these constraints -- it zaps
259 everything, regardless.)
260
261 Locking: as above.
262
263 */
264
265 void
266 Py_EndInterpreter(tstate)
267 PyThreadState *tstate;
268 {
269 PyInterpreterState *interp = tstate->interp;
270
271 if (tstate != PyThreadState_Get())
272 Py_FatalError("Py_EndInterpreter: thread is not current");
273 if (tstate->frame != NULL)
274 Py_FatalError("Py_EndInterpreter: thread still has a frame");
275 if (tstate != interp->tstate_head || tstate->next != NULL)
276 Py_FatalError("Py_EndInterpreter: not the last thread");
277
278 PyImport_Cleanup();
279 PyInterpreterState_Clear(interp);
280 PyThreadState_Swap(NULL);
281 PyInterpreterState_Delete(interp);
132 } 282 }
133 283
134 static char *progname = "python"; 284 static char *progname = "python";
135 285
136 void 286 void
698 Py_DECREF(exitfunc); 848 Py_DECREF(exitfunc);
699 } 849 }
700 850
701 Py_FlushLine(); 851 Py_FlushLine();
702 852
853 Py_Finalize();
854
703 while (nexitfuncs > 0) 855 while (nexitfuncs > 0)
704 (*exitfuncs[--nexitfuncs])(); 856 (*exitfuncs[--nexitfuncs])();
857
858 fflush(stdout);
859 fflush(stderr);
705 } 860 }
706 861
707 #ifdef COUNT_ALLOCS 862 #ifdef COUNT_ALLOCS
708 extern void dump_counts Py_PROTO((void)); 863 extern void dump_counts Py_PROTO((void));
709 #endif 864 #endif
715 Py_Cleanup(); 870 Py_Cleanup();
716 871
717 #ifdef COUNT_ALLOCS 872 #ifdef COUNT_ALLOCS
718 dump_counts(); 873 dump_counts();
719 #endif 874 #endif
720
721 #ifdef WITH_THREAD
722
723 /* Other threads may still be active, so skip most of the
724 cleanup actions usually done (these are mostly for
725 debugging anyway). */
726
727 (void) PyEval_SaveThread();
728 #ifndef NO_EXIT_PROG
729 if (_PyThread_Started)
730 _exit_prog(sts);
731 else
732 exit_prog(sts);
733 #else /* !NO_EXIT_PROG */
734 if (_PyThread_Started)
735 _exit(sts);
736 else
737 exit(sts);
738 #endif /* !NO_EXIT_PROG */
739
740 #else /* WITH_THREAD */
741
742 PyImport_Cleanup();
743
744 PyErr_Clear();
745 875
746 #ifdef Py_REF_DEBUG 876 #ifdef Py_REF_DEBUG
747 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal); 877 fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
748 #endif 878 #endif
749 879
756 #ifdef macintosh 886 #ifdef macintosh
757 PyMac_Exit(sts); 887 PyMac_Exit(sts);
758 #else 888 #else
759 exit(sts); 889 exit(sts);
760 #endif 890 #endif
761 #endif /* WITH_THREAD */
762 /*NOTREACHED*/
763 } 891 }
764 892
765 #ifdef HAVE_SIGNAL_H 893 #ifdef HAVE_SIGNAL_H
766 static RETSIGTYPE 894 static RETSIGTYPE
767 sighandler(sig) 895 sighandler(sig)
802 #endif 930 #endif
803 #endif /* HAVE_SIGNAL_H */ 931 #endif /* HAVE_SIGNAL_H */
804 PyOS_InitInterrupts(); /* May imply initsignal() */ 932 PyOS_InitInterrupts(); /* May imply initsignal() */
805 } 933 }
806 934
935 static void
936 finisigs()
937 {
938 PyOS_FiniInterrupts(); /* May imply finisignal() */
939 }
940
807 #ifdef Py_TRACE_REFS 941 #ifdef Py_TRACE_REFS
808 /* Ask a yes/no question */ 942 /* Ask a yes/no question */
809 943
810 int 944 int
811 _Py_AskYesNo(prompt) 945 _Py_AskYesNo(prompt)