pythonrun.c revision a027efa5bfa7911b5c4b522b6a0698749a6f2e4a
1/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5                        All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
29
30******************************************************************/
31
32/* Python interpreter top-level routines, including init/exit */
33
34#include "Python.h"
35
36#include "grammar.h"
37#include "node.h"
38#include "parsetok.h"
39#include "graminit.h"
40#undef argument /* Avoid conflict on Mac */
41#include "errcode.h"
42#include "compile.h"
43#include "eval.h"
44#include "marshal.h"
45
46#ifdef HAVE_UNISTD_H
47#include <unistd.h>
48#endif
49
50#ifdef HAVE_SIGNAL_H
51#include <signal.h>
52#endif
53
54#ifdef MS_WIN32
55#undef BYTE
56#undef arglist
57#include "windows.h"
58#endif
59
60extern char *Py_GetPath();
61
62extern grammar _PyParser_Grammar; /* From graminit.c */
63
64/* Forward */
65static void initmain Py_PROTO((void));
66static PyObject *run_err_node Py_PROTO((node *n, char *filename,
67				   PyObject *globals, PyObject *locals));
68static PyObject *run_node Py_PROTO((node *n, char *filename,
69			       PyObject *globals, PyObject *locals));
70static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename,
71				   PyObject *globals, PyObject *locals));
72static void err_input Py_PROTO((perrdetail *));
73static void initsigs Py_PROTO((void));
74
75int Py_DebugFlag; /* Needed by parser.c */
76int Py_VerboseFlag; /* Needed by import.c */
77int Py_SuppressPrintingFlag; /* Needed by ceval.c */
78int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
79
80/* Initialize the current interpreter; pass in the Python path. */
81
82void
83Py_Setup()
84{
85	PyImport_Init();
86
87	/* Modules '__builtin__' and 'sys' are initialized here,
88	   they are needed by random bits of the interpreter.
89	   All other modules are optional and are initialized
90	   when they are first imported. */
91
92	PyBuiltin_Init(); /* Also initializes builtin exceptions */
93	PySys_Init();
94
95	PySys_SetPath(Py_GetPath());
96
97	initsigs(); /* Signal handling stuff, including initintr() */
98
99	initmain();
100}
101
102/* Create and interpreter and thread state and initialize them;
103   if we already have an interpreter and thread, do nothing.
104   Fatal error if the creation fails. */
105
106void
107Py_Initialize()
108{
109	PyThreadState *tstate;
110	PyInterpreterState *interp;
111	if (PyThreadState_Get())
112		return;
113	interp = PyInterpreterState_New();
114	if (interp == NULL)
115		Py_FatalError("PyInterpreterState_New() failed");
116	tstate = PyThreadState_New(interp);
117	if (tstate == NULL)
118		Py_FatalError("PyThreadState_New() failed");
119	(void) PyThreadState_Swap(tstate);
120
121	Py_Setup();
122
123	PySys_SetPath(Py_GetPath());
124}
125
126/*
127  Py_Initialize()
128  -- do everything, no-op on second call, call fatal on failure, set path
129
130  #2
131  -- create new interp+tstate & make it current, return NULL on failure,
132     make it current, do all setup, set path
133
134  #3
135  -- #2 without set path
136
137  #4
138  -- is there any point to #3 for caller-provided current interp+tstate?
139
140*/
141
142/* Create __main__ module */
143
144static void
145initmain()
146{
147	PyObject *m, *d;
148	m = PyImport_AddModule("__main__");
149	if (m == NULL)
150		Py_FatalError("can't create __main__ module");
151	d = PyModule_GetDict(m);
152	if (PyDict_GetItemString(d, "__builtins__") == NULL) {
153		if (PyDict_SetItemString(d, "__builtins__",
154					 PyEval_GetBuiltins()))
155			Py_FatalError("can't add __builtins__ to __main__");
156	}
157}
158
159/* Parse input from a file and execute it */
160
161int
162PyRun_AnyFile(fp, filename)
163	FILE *fp;
164	char *filename;
165{
166	if (filename == NULL)
167		filename = "???";
168	if (Py_FdIsInteractive(fp, filename))
169		return PyRun_InteractiveLoop(fp, filename);
170	else
171		return PyRun_SimpleFile(fp, filename);
172}
173
174int
175PyRun_InteractiveLoop(fp, filename)
176	FILE *fp;
177	char *filename;
178{
179	PyObject *v;
180	int ret;
181	v = PySys_GetObject("ps1");
182	if (v == NULL) {
183		PySys_SetObject("ps1", v = PyString_FromString(">>> "));
184		Py_XDECREF(v);
185	}
186	v = PySys_GetObject("ps2");
187	if (v == NULL) {
188		PySys_SetObject("ps2", v = PyString_FromString("... "));
189		Py_XDECREF(v);
190	}
191	for (;;) {
192		ret = PyRun_InteractiveOne(fp, filename);
193#ifdef Py_REF_DEBUG
194		fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
195#endif
196		if (ret == E_EOF)
197			return 0;
198		/*
199		if (ret == E_NOMEM)
200			return -1;
201		*/
202	}
203}
204
205int
206PyRun_InteractiveOne(fp, filename)
207	FILE *fp;
208	char *filename;
209{
210	PyObject *m, *d, *v, *w;
211	node *n;
212	perrdetail err;
213	char *ps1, *ps2;
214	v = PySys_GetObject("ps1");
215	w = PySys_GetObject("ps2");
216	if (v != NULL && PyString_Check(v)) {
217		Py_INCREF(v);
218		ps1 = PyString_AsString(v);
219	}
220	else {
221		v = NULL;
222		ps1 = "";
223	}
224	if (w != NULL && PyString_Check(w)) {
225		Py_INCREF(w);
226		ps2 = PyString_AsString(w);
227	}
228	else {
229		w = NULL;
230		ps2 = "";
231	}
232	Py_BEGIN_ALLOW_THREADS
233	n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
234			       single_input, ps1, ps2, &err);
235	Py_END_ALLOW_THREADS
236	Py_XDECREF(v);
237	Py_XDECREF(w);
238	if (n == NULL) {
239		if (err.error == E_EOF) {
240			if (err.text)
241				free(err.text);
242			return E_EOF;
243		}
244		err_input(&err);
245		PyErr_Print();
246		return err.error;
247	}
248	m = PyImport_AddModule("__main__");
249	if (m == NULL)
250		return -1;
251	d = PyModule_GetDict(m);
252	v = run_node(n, filename, d, d);
253	if (v == NULL) {
254		PyErr_Print();
255		return -1;
256	}
257	Py_DECREF(v);
258	Py_FlushLine();
259	return 0;
260}
261
262int
263PyRun_SimpleFile(fp, filename)
264	FILE *fp;
265	char *filename;
266{
267	PyObject *m, *d, *v;
268	char *ext;
269
270	m = PyImport_AddModule("__main__");
271	if (m == NULL)
272		return -1;
273	d = PyModule_GetDict(m);
274	ext = filename + strlen(filename) - 4;
275	if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0
276#ifdef macintosh
277	/* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
278	    || getfiletype(filename) == 'PYC '
279	    || getfiletype(filename) == 'APPL'
280#endif /* macintosh */
281		) {
282		/* Try to run a pyc file. First, re-open in binary */
283		/* Don't close, done in main: fclose(fp); */
284		if( (fp = fopen(filename, "rb")) == NULL ) {
285			fprintf(stderr, "python: Can't reopen .pyc file\n");
286			return -1;
287		}
288		/* Turn on optimization if a .pyo file is given */
289		if (strcmp(ext, ".pyo") == 0)
290			Py_OptimizeFlag = 1;
291		v = run_pyc_file(fp, filename, d, d);
292	} else {
293		v = PyRun_File(fp, filename, file_input, d, d);
294	}
295	if (v == NULL) {
296		PyErr_Print();
297		return -1;
298	}
299	Py_DECREF(v);
300	Py_FlushLine();
301	return 0;
302}
303
304int
305PyRun_SimpleString(command)
306	char *command;
307{
308	PyObject *m, *d, *v;
309	m = PyImport_AddModule("__main__");
310	if (m == NULL)
311		return -1;
312	d = PyModule_GetDict(m);
313	v = PyRun_String(command, file_input, d, d);
314	if (v == NULL) {
315		PyErr_Print();
316		return -1;
317	}
318	Py_DECREF(v);
319	Py_FlushLine();
320	return 0;
321}
322
323void
324PyErr_Print()
325{
326	PyObject *exception, *v, *tb, *f;
327	PyErr_Fetch(&exception, &v, &tb);
328	Py_FlushLine();
329	fflush(stdout);
330	if (exception == NULL)
331		Py_FatalError("PyErr_Print called but no exception");
332	if (exception == PyExc_SystemExit) {
333		if (v == NULL || v == Py_None)
334			Py_Exit(0);
335		if (PyInt_Check(v))
336			Py_Exit((int)PyInt_AsLong(v));
337		else {
338			/* OK to use real stderr here */
339			PyObject_Print(v, stderr, Py_PRINT_RAW);
340			fprintf(stderr, "\n");
341			Py_Exit(1);
342		}
343	}
344	PySys_SetObject("last_type", exception);
345	PySys_SetObject("last_value", v);
346	PySys_SetObject("last_traceback", tb);
347	f = PySys_GetObject("stderr");
348	if (f == NULL)
349		fprintf(stderr, "lost sys.stderr\n");
350	else {
351		PyTraceBack_Print(tb, f);
352		if (exception == PyExc_SyntaxError) {
353			PyObject *message;
354			char *filename, *text;
355			int lineno, offset;
356			if (!PyArg_Parse(v, "(O(ziiz))", &message,
357				     &filename, &lineno, &offset, &text))
358				PyErr_Clear();
359			else {
360				char buf[10];
361				PyFile_WriteString("  File \"", f);
362				if (filename == NULL)
363					PyFile_WriteString("<string>", f);
364				else
365					PyFile_WriteString(filename, f);
366				PyFile_WriteString("\", line ", f);
367				sprintf(buf, "%d", lineno);
368				PyFile_WriteString(buf, f);
369				PyFile_WriteString("\n", f);
370				if (text != NULL) {
371					char *nl;
372					if (offset > 0 &&
373					    offset == strlen(text))
374						offset--;
375					for (;;) {
376						nl = strchr(text, '\n');
377						if (nl == NULL ||
378						    nl-text >= offset)
379							break;
380						offset -= (nl+1-text);
381						text = nl+1;
382					}
383					while (*text == ' ' || *text == '\t') {
384						text++;
385						offset--;
386					}
387					PyFile_WriteString("    ", f);
388					PyFile_WriteString(text, f);
389					if (*text == '\0' ||
390					    text[strlen(text)-1] != '\n')
391						PyFile_WriteString("\n", f);
392					PyFile_WriteString("    ", f);
393					offset--;
394					while (offset > 0) {
395						PyFile_WriteString(" ", f);
396						offset--;
397					}
398					PyFile_WriteString("^\n", f);
399				}
400				Py_INCREF(message);
401				Py_DECREF(v);
402				v = message;
403			}
404		}
405		if (PyClass_Check(exception)) {
406			PyObject* className =
407				((PyClassObject*)exception)->cl_name;
408			if (className == NULL)
409				PyFile_WriteString("<unknown>", f);
410			else {
411				if (PyFile_WriteObject(className, f,
412						       Py_PRINT_RAW) != 0)
413					PyErr_Clear();
414			}
415		} else {
416			if (PyFile_WriteObject(exception, f,
417					       Py_PRINT_RAW) != 0)
418				PyErr_Clear();
419		}
420		if (v != NULL && v != Py_None) {
421			PyFile_WriteString(": ", f);
422			if (PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0)
423				PyErr_Clear();
424		}
425		PyFile_WriteString("\n", f);
426	}
427	Py_XDECREF(exception);
428	Py_XDECREF(v);
429	Py_XDECREF(tb);
430}
431
432PyObject *
433PyRun_String(str, start, globals, locals)
434	char *str;
435	int start;
436	PyObject *globals, *locals;
437{
438	return run_err_node(PyParser_SimpleParseString(str, start),
439			    "<string>", globals, locals);
440}
441
442PyObject *
443PyRun_File(fp, filename, start, globals, locals)
444	FILE *fp;
445	char *filename;
446	int start;
447	PyObject *globals, *locals;
448{
449	return run_err_node(PyParser_SimpleParseFile(fp, filename, start),
450			    filename, globals, locals);
451}
452
453static PyObject *
454run_err_node(n, filename, globals, locals)
455	node *n;
456	char *filename;
457	PyObject *globals, *locals;
458{
459	if (n == NULL)
460		return  NULL;
461	return run_node(n, filename, globals, locals);
462}
463
464static PyObject *
465run_node(n, filename, globals, locals)
466	node *n;
467	char *filename;
468	PyObject *globals, *locals;
469{
470	PyCodeObject *co;
471	PyObject *v;
472	co = PyNode_Compile(n, filename);
473	PyNode_Free(n);
474	if (co == NULL)
475		return NULL;
476	v = PyEval_EvalCode(co, globals, locals);
477	Py_DECREF(co);
478	return v;
479}
480
481static PyObject *
482run_pyc_file(fp, filename, globals, locals)
483	FILE *fp;
484	char *filename;
485	PyObject *globals, *locals;
486{
487	PyCodeObject *co;
488	PyObject *v;
489	long magic;
490	long PyImport_GetMagicNumber();
491
492	magic = PyMarshal_ReadLongFromFile(fp);
493	if (magic != PyImport_GetMagicNumber()) {
494		PyErr_SetString(PyExc_RuntimeError,
495			   "Bad magic number in .pyc file");
496		return NULL;
497	}
498	(void) PyMarshal_ReadLongFromFile(fp);
499	v = PyMarshal_ReadObjectFromFile(fp);
500	fclose(fp);
501	if (v == NULL || !PyCode_Check(v)) {
502		Py_XDECREF(v);
503		PyErr_SetString(PyExc_RuntimeError,
504			   "Bad code object in .pyc file");
505		return NULL;
506	}
507	co = (PyCodeObject *)v;
508	v = PyEval_EvalCode(co, globals, locals);
509	Py_DECREF(co);
510	return v;
511}
512
513PyObject *
514Py_CompileString(str, filename, start)
515	char *str;
516	char *filename;
517	int start;
518{
519	node *n;
520	PyCodeObject *co;
521	n = PyParser_SimpleParseString(str, start);
522	if (n == NULL)
523		return NULL;
524	co = PyNode_Compile(n, filename);
525	PyNode_Free(n);
526	return (PyObject *)co;
527}
528
529/* Simplified interface to parsefile -- return node or set exception */
530
531node *
532PyParser_SimpleParseFile(fp, filename, start)
533	FILE *fp;
534	char *filename;
535	int start;
536{
537	node *n;
538	perrdetail err;
539	Py_BEGIN_ALLOW_THREADS
540	n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
541				(char *)0, (char *)0, &err);
542	Py_END_ALLOW_THREADS
543	if (n == NULL)
544		err_input(&err);
545	return n;
546}
547
548/* Simplified interface to parsestring -- return node or set exception */
549
550node *
551PyParser_SimpleParseString(str, start)
552	char *str;
553	int start;
554{
555	node *n;
556	perrdetail err;
557	n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
558	if (n == NULL)
559		err_input(&err);
560	return n;
561}
562
563/* Set the error appropriate to the given input error code (see errcode.h) */
564
565static void
566err_input(err)
567	perrdetail *err;
568{
569	PyObject *v, *w;
570	char *msg = NULL;
571	v = Py_BuildValue("(ziiz)", err->filename,
572			    err->lineno, err->offset, err->text);
573	if (err->text != NULL) {
574		free(err->text);
575		err->text = NULL;
576	}
577	switch (err->error) {
578	case E_SYNTAX:
579		msg = "invalid syntax";
580		break;
581	case E_TOKEN:
582		msg = "invalid token";
583
584		break;
585	case E_INTR:
586		PyErr_SetNone(PyExc_KeyboardInterrupt);
587		return;
588	case E_NOMEM:
589		PyErr_NoMemory();
590		return;
591	case E_EOF:
592		msg = "unexpected EOF while parsing";
593		break;
594	default:
595		fprintf(stderr, "error=%d\n", err->error);
596		msg = "unknown parsing error";
597		break;
598	}
599	w = Py_BuildValue("(sO)", msg, v);
600	Py_XDECREF(v);
601	PyErr_SetObject(PyExc_SyntaxError, w);
602	Py_XDECREF(w);
603}
604
605/* Print fatal error message and abort */
606
607void
608Py_FatalError(msg)
609	char *msg;
610{
611	fprintf(stderr, "Fatal Python error: %s\n", msg);
612#ifdef macintosh
613	for (;;);
614#endif
615#ifdef MS_WIN32
616	OutputDebugString("Fatal Python error:");
617	OutputDebugString(msg);
618	OutputDebugString("\n");
619#endif
620	abort();
621}
622
623/* Clean up and exit */
624
625#ifdef WITH_THREAD
626#include "thread.h"
627int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
628#endif
629
630#define NEXITFUNCS 32
631static void (*exitfuncs[NEXITFUNCS])();
632static int nexitfuncs = 0;
633
634int Py_AtExit(func)
635	void (*func) Py_PROTO((void));
636{
637	if (nexitfuncs >= NEXITFUNCS)
638		return -1;
639	exitfuncs[nexitfuncs++] = func;
640	return 0;
641}
642
643void
644Py_Cleanup()
645{
646	PyObject *exitfunc = PySys_GetObject("exitfunc");
647
648	if (exitfunc) {
649		PyObject *res;
650		Py_INCREF(exitfunc);
651		PySys_SetObject("exitfunc", (PyObject *)NULL);
652		res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
653		if (res == NULL) {
654			fprintf(stderr, "Error in sys.exitfunc:\n");
655			PyErr_Print();
656		}
657		Py_DECREF(exitfunc);
658	}
659
660	Py_FlushLine();
661
662	while (nexitfuncs > 0)
663		(*exitfuncs[--nexitfuncs])();
664}
665
666#ifdef COUNT_ALLOCS
667extern void dump_counts Py_PROTO((void));
668#endif
669
670void
671Py_Exit(sts)
672	int sts;
673{
674	Py_Cleanup();
675
676#ifdef COUNT_ALLOCS
677	dump_counts();
678#endif
679
680#ifdef WITH_THREAD
681
682	/* Other threads may still be active, so skip most of the
683	   cleanup actions usually done (these are mostly for
684	   debugging anyway). */
685
686	(void) PyEval_SaveThread();
687#ifndef NO_EXIT_PROG
688	if (_PyThread_Started)
689		_exit_prog(sts);
690	else
691		exit_prog(sts);
692#else /* !NO_EXIT_PROG */
693	if (_PyThread_Started)
694		_exit(sts);
695	else
696		exit(sts);
697#endif /* !NO_EXIT_PROG */
698
699#else /* WITH_THREAD */
700
701	PyImport_Cleanup();
702
703	PyErr_Clear();
704
705#ifdef Py_REF_DEBUG
706	fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
707#endif
708
709#ifdef Py_TRACE_REFS
710	if (_Py_AskYesNo("Print left references?")) {
711		_Py_PrintReferences(stderr);
712	}
713#endif /* Py_TRACE_REFS */
714
715#ifdef macintosh
716	PyMac_Exit(sts);
717#else
718	exit(sts);
719#endif
720#endif /* WITH_THREAD */
721	/*NOTREACHED*/
722}
723
724#ifdef HAVE_SIGNAL_H
725static RETSIGTYPE
726sighandler(sig)
727	int sig;
728{
729	signal(sig, SIG_DFL); /* Don't catch recursive signals */
730	Py_Cleanup(); /* Do essential clean-up */
731#ifdef HAVE_GETPID
732	kill(getpid(), sig); /* Pretend the signal killed us */
733#else
734	exit(1);
735#endif
736	/*NOTREACHED*/
737}
738#endif
739
740static void
741initsigs()
742{
743	RETSIGTYPE (*t)();
744#ifdef HAVE_SIGNAL_H
745#ifdef SIGPIPE
746	signal(SIGPIPE, SIG_IGN);
747#endif
748#ifdef SIGHUP
749	t = signal(SIGHUP, SIG_IGN);
750	if (t == SIG_DFL)
751		signal(SIGHUP, sighandler);
752	else
753		signal(SIGHUP, t);
754#endif
755#ifdef SIGTERM
756	t = signal(SIGTERM, SIG_IGN);
757	if (t == SIG_DFL)
758		signal(SIGTERM, sighandler);
759	else
760		signal(SIGTERM, t);
761#endif
762#endif /* HAVE_SIGNAL_H */
763	PyOS_InitInterrupts(); /* May imply initsignal() */
764}
765
766#ifdef Py_TRACE_REFS
767/* Ask a yes/no question */
768
769int
770_Py_AskYesNo(prompt)
771	char *prompt;
772{
773	char buf[256];
774
775	printf("%s [ny] ", prompt);
776	if (fgets(buf, sizeof buf, stdin) == NULL)
777		return 0;
778	return buf[0] == 'y' || buf[0] == 'Y';
779}
780#endif
781
782#ifdef MPW
783
784/* Check for file descriptor connected to interactive device.
785   Pretend that stdin is always interactive, other files never. */
786
787int
788isatty(fd)
789	int fd;
790{
791	return fd == fileno(stdin);
792}
793
794#endif
795
796/*
797 * The file descriptor fd is considered ``interactive'' if either
798 *   a) isatty(fd) is TRUE, or
799 *   b) the -i flag was given, and the filename associated with
800 *      the descriptor is NULL or "<stdin>" or "???".
801 */
802int
803Py_FdIsInteractive(fp, filename)
804	FILE *fp;
805	char *filename;
806{
807	if (isatty((int)fileno(fp)))
808		return 1;
809	if (!Py_InteractiveFlag)
810		return 0;
811	return (filename == NULL) ||
812	       (strcmp(filename, "<stdin>") == 0) ||
813	       (strcmp(filename, "???") == 0);
814}
815