annotate Python/pythonrun.c @ 5438:e926c331ebbf legacy-trunk

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