1992-08-04 12:41:02 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:12:13 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1992-08-04 12:41:02 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1992-08-04 12:41:02 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Python interpreter top-level routines, including init/exit */
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
#include "Python.h"
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
#include "grammar.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "parsetok.h"
|
1995-09-18 21:42:42 +00:00
|
|
|
#undef argument /* Avoid conflict on Mac */
|
1992-08-04 12:41:02 +00:00
|
|
|
#include "errcode.h"
|
|
|
|
#include "compile.h"
|
1992-08-05 19:58:53 +00:00
|
|
|
#include "eval.h"
|
1994-09-14 13:31:04 +00:00
|
|
|
#include "marshal.h"
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1996-12-05 23:27:02 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
1992-10-18 18:53:57 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
1996-09-11 23:12:24 +00:00
|
|
|
#ifdef MS_WIN32
|
1995-03-14 15:01:17 +00:00
|
|
|
#undef BYTE
|
1996-04-09 02:37:03 +00:00
|
|
|
#undef arglist
|
1995-03-14 15:01:17 +00:00
|
|
|
#include "windows.h"
|
|
|
|
#endif
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
extern char *Py_GetPath();
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
extern grammar _PyParser_Grammar; /* From graminit.c */
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1993-11-01 16:28:59 +00:00
|
|
|
/* Forward */
|
1997-03-05 00:20:32 +00:00
|
|
|
static void initmain Py_PROTO((void));
|
|
|
|
static PyObject *run_err_node Py_PROTO((node *n, char *filename,
|
|
|
|
PyObject *globals, PyObject *locals));
|
|
|
|
static PyObject *run_node Py_PROTO((node *n, char *filename,
|
|
|
|
PyObject *globals, PyObject *locals));
|
|
|
|
static PyObject *run_pyc_file Py_PROTO((FILE *fp, char *filename,
|
|
|
|
PyObject *globals, PyObject *locals));
|
|
|
|
static void err_input Py_PROTO((perrdetail *));
|
|
|
|
static void initsigs Py_PROTO((void));
|
|
|
|
|
|
|
|
int Py_DebugFlag; /* Needed by parser.c */
|
|
|
|
int Py_VerboseFlag; /* Needed by import.c */
|
|
|
|
int Py_SuppressPrintingFlag; /* Needed by ceval.c */
|
1997-02-14 19:45:36 +00:00
|
|
|
int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1997-05-05 20:56:21 +00:00
|
|
|
/* Initialize the current interpreter; pass in the Python path. */
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
void
|
1997-05-05 20:56:21 +00:00
|
|
|
Py_Setup()
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyImport_Init();
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
/* Modules '__builtin__' and 'sys' are initialized here,
|
1992-08-04 12:41:02 +00:00
|
|
|
they are needed by random bits of the interpreter.
|
|
|
|
All other modules are optional and are initialized
|
|
|
|
when they are first imported. */
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
PyBuiltin_Init(); /* Also initializes builtin exceptions */
|
|
|
|
PySys_Init();
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
PySys_SetPath(Py_GetPath());
|
1992-10-18 18:53:57 +00:00
|
|
|
|
|
|
|
initsigs(); /* Signal handling stuff, including initintr() */
|
1995-01-09 17:53:26 +00:00
|
|
|
|
|
|
|
initmain();
|
|
|
|
}
|
|
|
|
|
1997-05-05 20:56:21 +00:00
|
|
|
/* Create and interpreter and thread state and initialize them;
|
|
|
|
if we already have an interpreter and thread, do nothing.
|
|
|
|
Fatal error if the creation fails. */
|
|
|
|
|
|
|
|
void
|
|
|
|
Py_Initialize()
|
|
|
|
{
|
|
|
|
PyThreadState *tstate;
|
|
|
|
PyInterpreterState *interp;
|
|
|
|
if (PyThreadState_Get())
|
|
|
|
return;
|
|
|
|
interp = PyInterpreterState_New();
|
|
|
|
if (interp == NULL)
|
|
|
|
Py_FatalError("PyInterpreterState_New() failed");
|
|
|
|
tstate = PyThreadState_New(interp);
|
|
|
|
if (tstate == NULL)
|
|
|
|
Py_FatalError("PyThreadState_New() failed");
|
|
|
|
(void) PyThreadState_Swap(tstate);
|
|
|
|
|
|
|
|
Py_Setup();
|
|
|
|
|
|
|
|
PySys_SetPath(Py_GetPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Py_Initialize()
|
|
|
|
-- do everything, no-op on second call, call fatal on failure, set path
|
|
|
|
|
|
|
|
#2
|
|
|
|
-- create new interp+tstate & make it current, return NULL on failure,
|
|
|
|
make it current, do all setup, set path
|
|
|
|
|
|
|
|
#3
|
|
|
|
-- #2 without set path
|
|
|
|
|
|
|
|
#4
|
|
|
|
-- is there any point to #3 for caller-provided current interp+tstate?
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
1995-01-09 17:53:26 +00:00
|
|
|
/* Create __main__ module */
|
|
|
|
|
|
|
|
static void
|
|
|
|
initmain()
|
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *m, *d;
|
|
|
|
m = PyImport_AddModule("__main__");
|
1995-01-09 17:53:26 +00:00
|
|
|
if (m == NULL)
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_FatalError("can't create __main__ module");
|
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
if (PyDict_GetItemString(d, "__builtins__") == NULL) {
|
|
|
|
if (PyDict_SetItemString(d, "__builtins__",
|
|
|
|
PyEval_GetBuiltins()))
|
|
|
|
Py_FatalError("can't add __builtins__ to __main__");
|
1995-01-09 17:53:26 +00:00
|
|
|
}
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse input from a file and execute it */
|
|
|
|
|
|
|
|
int
|
1997-03-05 00:20:32 +00:00
|
|
|
PyRun_AnyFile(fp, filename)
|
1992-08-04 12:41:02 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
if (filename == NULL)
|
|
|
|
filename = "???";
|
1997-02-14 19:45:36 +00:00
|
|
|
if (Py_FdIsInteractive(fp, filename))
|
1997-03-05 00:20:32 +00:00
|
|
|
return PyRun_InteractiveLoop(fp, filename);
|
1992-08-04 12:41:02 +00:00
|
|
|
else
|
1997-03-05 00:20:32 +00:00
|
|
|
return PyRun_SimpleFile(fp, filename);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-03-05 00:20:32 +00:00
|
|
|
PyRun_InteractiveLoop(fp, filename)
|
1992-08-04 12:41:02 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *v;
|
1992-08-04 12:41:02 +00:00
|
|
|
int ret;
|
1997-03-05 00:20:32 +00:00
|
|
|
v = PySys_GetObject("ps1");
|
1992-08-04 12:41:02 +00:00
|
|
|
if (v == NULL) {
|
1997-03-05 00:20:32 +00:00
|
|
|
PySys_SetObject("ps1", v = PyString_FromString(">>> "));
|
|
|
|
Py_XDECREF(v);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
v = PySys_GetObject("ps2");
|
1992-08-04 12:41:02 +00:00
|
|
|
if (v == NULL) {
|
1997-03-05 00:20:32 +00:00
|
|
|
PySys_SetObject("ps2", v = PyString_FromString("... "));
|
|
|
|
Py_XDECREF(v);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
for (;;) {
|
1997-03-05 00:20:32 +00:00
|
|
|
ret = PyRun_InteractiveOne(fp, filename);
|
1996-05-22 16:35:33 +00:00
|
|
|
#ifdef Py_REF_DEBUG
|
1995-03-29 16:57:48 +00:00
|
|
|
fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
|
1992-08-04 12:41:02 +00:00
|
|
|
#endif
|
|
|
|
if (ret == E_EOF)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
if (ret == E_NOMEM)
|
|
|
|
return -1;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-03-05 00:20:32 +00:00
|
|
|
PyRun_InteractiveOne(fp, filename)
|
1992-08-04 12:41:02 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *m, *d, *v, *w;
|
1992-08-04 12:41:02 +00:00
|
|
|
node *n;
|
1994-08-29 12:50:44 +00:00
|
|
|
perrdetail err;
|
1992-08-04 12:41:02 +00:00
|
|
|
char *ps1, *ps2;
|
1997-03-05 00:20:32 +00:00
|
|
|
v = PySys_GetObject("ps1");
|
|
|
|
w = PySys_GetObject("ps2");
|
|
|
|
if (v != NULL && PyString_Check(v)) {
|
|
|
|
Py_INCREF(v);
|
|
|
|
ps1 = PyString_AsString(v);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
v = NULL;
|
|
|
|
ps1 = "";
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
if (w != NULL && PyString_Check(w)) {
|
|
|
|
Py_INCREF(w);
|
|
|
|
ps2 = PyString_AsString(w);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
w = NULL;
|
|
|
|
ps2 = "";
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
|
1997-05-07 17:46:13 +00:00
|
|
|
Py_single_input, ps1, ps2, &err);
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(w);
|
1994-08-29 12:50:44 +00:00
|
|
|
if (n == NULL) {
|
|
|
|
if (err.error == E_EOF) {
|
|
|
|
if (err.text)
|
|
|
|
free(err.text);
|
|
|
|
return E_EOF;
|
|
|
|
}
|
|
|
|
err_input(&err);
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_Print();
|
1994-08-29 12:50:44 +00:00
|
|
|
return err.error;
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
m = PyImport_AddModule("__main__");
|
1992-08-04 12:41:02 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-03-05 00:20:32 +00:00
|
|
|
d = PyModule_GetDict(m);
|
1992-08-04 12:41:02 +00:00
|
|
|
v = run_node(n, filename, d, d);
|
|
|
|
if (v == NULL) {
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_Print();
|
1992-08-04 12:41:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_DECREF(v);
|
1997-05-22 22:35:04 +00:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1992-08-04 12:41:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-03-05 00:20:32 +00:00
|
|
|
PyRun_SimpleFile(fp, filename)
|
1992-08-04 12:41:02 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *m, *d, *v;
|
1994-09-14 13:31:04 +00:00
|
|
|
char *ext;
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
m = PyImport_AddModule("__main__");
|
1992-08-04 12:41:02 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-03-05 00:20:32 +00:00
|
|
|
d = PyModule_GetDict(m);
|
1994-09-14 13:31:04 +00:00
|
|
|
ext = filename + strlen(filename) - 4;
|
1997-04-02 05:28:38 +00:00
|
|
|
if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0
|
1995-02-13 11:44:56 +00:00
|
|
|
#ifdef macintosh
|
|
|
|
/* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
|
1997-04-02 05:28:38 +00:00
|
|
|
|| getfiletype(filename) == 'PYC '
|
|
|
|
|| getfiletype(filename) == 'APPL'
|
1995-02-13 11:44:56 +00:00
|
|
|
#endif /* macintosh */
|
1997-04-02 05:28:38 +00:00
|
|
|
) {
|
1994-09-14 13:31:04 +00:00
|
|
|
/* Try to run a pyc file. First, re-open in binary */
|
1994-12-14 12:58:37 +00:00
|
|
|
/* Don't close, done in main: fclose(fp); */
|
1994-09-14 13:31:04 +00:00
|
|
|
if( (fp = fopen(filename, "rb")) == NULL ) {
|
|
|
|
fprintf(stderr, "python: Can't reopen .pyc file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
1997-04-02 05:28:38 +00:00
|
|
|
/* Turn on optimization if a .pyo file is given */
|
|
|
|
if (strcmp(ext, ".pyo") == 0)
|
|
|
|
Py_OptimizeFlag = 1;
|
1994-09-14 13:31:04 +00:00
|
|
|
v = run_pyc_file(fp, filename, d, d);
|
|
|
|
} else {
|
1997-05-07 17:46:13 +00:00
|
|
|
v = PyRun_File(fp, filename, Py_file_input, d, d);
|
1994-09-14 13:31:04 +00:00
|
|
|
}
|
1992-08-04 12:41:02 +00:00
|
|
|
if (v == NULL) {
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_Print();
|
1992-08-04 12:41:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_DECREF(v);
|
1997-05-22 22:35:04 +00:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1992-08-04 12:41:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-03-05 00:20:32 +00:00
|
|
|
PyRun_SimpleString(command)
|
1992-08-04 12:41:02 +00:00
|
|
|
char *command;
|
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *m, *d, *v;
|
|
|
|
m = PyImport_AddModule("__main__");
|
1992-08-04 12:41:02 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-03-05 00:20:32 +00:00
|
|
|
d = PyModule_GetDict(m);
|
1997-05-07 17:46:13 +00:00
|
|
|
v = PyRun_String(command, Py_file_input, d, d);
|
1992-08-04 12:41:02 +00:00
|
|
|
if (v == NULL) {
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_Print();
|
1992-08-04 12:41:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_DECREF(v);
|
1997-05-22 22:35:04 +00:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1992-08-04 12:41:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_Print()
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1997-05-22 22:35:04 +00:00
|
|
|
int err = 0;
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *exception, *v, *tb, *f;
|
|
|
|
PyErr_Fetch(&exception, &v, &tb);
|
1995-01-02 19:04:15 +00:00
|
|
|
if (exception == NULL)
|
1997-05-23 00:19:20 +00:00
|
|
|
return;
|
1997-03-05 00:20:32 +00:00
|
|
|
if (exception == PyExc_SystemExit) {
|
1997-05-22 22:35:04 +00:00
|
|
|
err = Py_FlushLine();
|
|
|
|
fflush(stdout);
|
1997-03-05 00:20:32 +00:00
|
|
|
if (v == NULL || v == Py_None)
|
|
|
|
Py_Exit(0);
|
|
|
|
if (PyInt_Check(v))
|
|
|
|
Py_Exit((int)PyInt_AsLong(v));
|
1992-08-04 12:41:02 +00:00
|
|
|
else {
|
1992-09-25 21:59:05 +00:00
|
|
|
/* OK to use real stderr here */
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject_Print(v, stderr, Py_PRINT_RAW);
|
1992-08-04 12:41:02 +00:00
|
|
|
fprintf(stderr, "\n");
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_Exit(1);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
PySys_SetObject("last_type", exception);
|
|
|
|
PySys_SetObject("last_value", v);
|
|
|
|
PySys_SetObject("last_traceback", tb);
|
|
|
|
f = PySys_GetObject("stderr");
|
1992-09-25 21:59:05 +00:00
|
|
|
if (f == NULL)
|
|
|
|
fprintf(stderr, "lost sys.stderr\n");
|
|
|
|
else {
|
1997-05-22 22:35:04 +00:00
|
|
|
err = Py_FlushLine();
|
|
|
|
fflush(stdout);
|
|
|
|
if (err == 0)
|
|
|
|
err = PyTraceBack_Print(tb, f);
|
|
|
|
if (err == 0 && exception == PyExc_SyntaxError) {
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *message;
|
1994-08-29 12:50:44 +00:00
|
|
|
char *filename, *text;
|
|
|
|
int lineno, offset;
|
1997-03-05 00:20:32 +00:00
|
|
|
if (!PyArg_Parse(v, "(O(ziiz))", &message,
|
1994-08-29 12:50:44 +00:00
|
|
|
&filename, &lineno, &offset, &text))
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_Clear();
|
1994-08-29 12:50:44 +00:00
|
|
|
else {
|
|
|
|
char buf[10];
|
1997-03-05 00:20:32 +00:00
|
|
|
PyFile_WriteString(" File \"", f);
|
1994-08-29 12:50:44 +00:00
|
|
|
if (filename == NULL)
|
1997-03-05 00:20:32 +00:00
|
|
|
PyFile_WriteString("<string>", f);
|
1994-08-29 12:50:44 +00:00
|
|
|
else
|
1997-03-05 00:20:32 +00:00
|
|
|
PyFile_WriteString(filename, f);
|
|
|
|
PyFile_WriteString("\", line ", f);
|
1994-08-29 12:50:44 +00:00
|
|
|
sprintf(buf, "%d", lineno);
|
1997-03-05 00:20:32 +00:00
|
|
|
PyFile_WriteString(buf, f);
|
|
|
|
PyFile_WriteString("\n", f);
|
1994-08-29 12:50:44 +00:00
|
|
|
if (text != NULL) {
|
1994-09-19 08:08:50 +00:00
|
|
|
char *nl;
|
|
|
|
if (offset > 0 &&
|
1997-05-06 15:24:59 +00:00
|
|
|
offset == (int)strlen(text))
|
1994-09-19 08:08:50 +00:00
|
|
|
offset--;
|
|
|
|
for (;;) {
|
|
|
|
nl = strchr(text, '\n');
|
|
|
|
if (nl == NULL ||
|
|
|
|
nl-text >= offset)
|
|
|
|
break;
|
|
|
|
offset -= (nl+1-text);
|
|
|
|
text = nl+1;
|
|
|
|
}
|
1994-08-29 12:50:44 +00:00
|
|
|
while (*text == ' ' || *text == '\t') {
|
|
|
|
text++;
|
|
|
|
offset--;
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
PyFile_WriteString(" ", f);
|
|
|
|
PyFile_WriteString(text, f);
|
1994-08-29 12:50:44 +00:00
|
|
|
if (*text == '\0' ||
|
|
|
|
text[strlen(text)-1] != '\n')
|
1997-03-05 00:20:32 +00:00
|
|
|
PyFile_WriteString("\n", f);
|
|
|
|
PyFile_WriteString(" ", f);
|
1994-08-29 12:50:44 +00:00
|
|
|
offset--;
|
|
|
|
while (offset > 0) {
|
1997-03-05 00:20:32 +00:00
|
|
|
PyFile_WriteString(" ", f);
|
1994-08-29 12:50:44 +00:00
|
|
|
offset--;
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
PyFile_WriteString("^\n", f);
|
1994-08-29 12:50:44 +00:00
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_INCREF(message);
|
|
|
|
Py_DECREF(v);
|
1994-08-29 12:50:44 +00:00
|
|
|
v = message;
|
1997-05-22 22:35:04 +00:00
|
|
|
/* Can't be bothered to check all those
|
|
|
|
PyFile_WriteString() calls */
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
err = -1;
|
1994-08-29 12:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
1997-05-22 22:35:04 +00:00
|
|
|
if (err) {
|
|
|
|
/* Don't do anything else */
|
|
|
|
}
|
|
|
|
else if (PyClass_Check(exception)) {
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject* className =
|
|
|
|
((PyClassObject*)exception)->cl_name;
|
1995-02-07 15:30:45 +00:00
|
|
|
if (className == NULL)
|
1997-05-22 22:35:04 +00:00
|
|
|
err = PyFile_WriteString("<unknown>", f);
|
|
|
|
else
|
|
|
|
err = PyFile_WriteObject(className, f,
|
|
|
|
Py_PRINT_RAW);
|
1995-02-07 15:30:45 +00:00
|
|
|
}
|
1997-05-22 22:35:04 +00:00
|
|
|
else
|
|
|
|
err = PyFile_WriteObject(exception, f, Py_PRINT_RAW);
|
|
|
|
if (err == 0) {
|
|
|
|
if (v != NULL && v != Py_None) {
|
|
|
|
err = PyFile_WriteString(": ", f);
|
|
|
|
if (err == 0)
|
|
|
|
err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
|
|
|
|
}
|
1992-09-25 21:59:05 +00:00
|
|
|
}
|
1997-05-22 22:35:04 +00:00
|
|
|
if (err == 0)
|
|
|
|
err = PyFile_WriteString("\n", f);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_XDECREF(exception);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(tb);
|
1997-05-22 22:35:04 +00:00
|
|
|
/* If an error happened here, don't show it.
|
|
|
|
XXX This is wrong, but too many callers rely on this behavior. */
|
|
|
|
if (err != 0)
|
|
|
|
PyErr_Clear();
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *
|
|
|
|
PyRun_String(str, start, globals, locals)
|
1992-08-04 12:41:02 +00:00
|
|
|
char *str;
|
|
|
|
int start;
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *globals, *locals;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
return run_err_node(PyParser_SimpleParseString(str, start),
|
1994-08-29 12:50:44 +00:00
|
|
|
"<string>", globals, locals);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *
|
|
|
|
PyRun_File(fp, filename, start, globals, locals)
|
1992-08-04 12:41:02 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
int start;
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *globals, *locals;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
return run_err_node(PyParser_SimpleParseFile(fp, filename, start),
|
1994-08-29 12:50:44 +00:00
|
|
|
filename, globals, locals);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
static PyObject *
|
1994-08-29 12:50:44 +00:00
|
|
|
run_err_node(n, filename, globals, locals)
|
1992-08-04 12:41:02 +00:00
|
|
|
node *n;
|
|
|
|
char *filename;
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *globals, *locals;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1994-08-29 12:50:44 +00:00
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
1992-08-04 12:41:02 +00:00
|
|
|
return run_node(n, filename, globals, locals);
|
|
|
|
}
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
static PyObject *
|
1992-08-04 12:41:02 +00:00
|
|
|
run_node(n, filename, globals, locals)
|
|
|
|
node *n;
|
|
|
|
char *filename;
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *globals, *locals;
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *v;
|
|
|
|
co = PyNode_Compile(n, filename);
|
|
|
|
PyNode_Free(n);
|
1992-08-04 12:41:02 +00:00
|
|
|
if (co == NULL)
|
|
|
|
return NULL;
|
1997-03-05 00:20:32 +00:00
|
|
|
v = PyEval_EvalCode(co, globals, locals);
|
|
|
|
Py_DECREF(co);
|
1992-08-04 12:41:02 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
static PyObject *
|
1994-09-14 13:31:04 +00:00
|
|
|
run_pyc_file(fp, filename, globals, locals)
|
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *globals, *locals;
|
1994-09-14 13:31:04 +00:00
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *v;
|
1994-09-14 13:31:04 +00:00
|
|
|
long magic;
|
1997-03-05 00:20:32 +00:00
|
|
|
long PyImport_GetMagicNumber();
|
1994-09-14 13:31:04 +00:00
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
magic = PyMarshal_ReadLongFromFile(fp);
|
|
|
|
if (magic != PyImport_GetMagicNumber()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1994-09-14 13:31:04 +00:00
|
|
|
"Bad magic number in .pyc file");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
(void) PyMarshal_ReadLongFromFile(fp);
|
|
|
|
v = PyMarshal_ReadObjectFromFile(fp);
|
1994-09-14 13:31:04 +00:00
|
|
|
fclose(fp);
|
1997-03-05 00:20:32 +00:00
|
|
|
if (v == NULL || !PyCode_Check(v)) {
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1994-09-14 13:31:04 +00:00
|
|
|
"Bad code object in .pyc file");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
co = (PyCodeObject *)v;
|
|
|
|
v = PyEval_EvalCode(co, globals, locals);
|
|
|
|
Py_DECREF(co);
|
1994-09-14 13:31:04 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *
|
|
|
|
Py_CompileString(str, filename, start)
|
1993-03-30 17:46:03 +00:00
|
|
|
char *str;
|
|
|
|
char *filename;
|
|
|
|
int start;
|
|
|
|
{
|
|
|
|
node *n;
|
1997-03-05 00:20:32 +00:00
|
|
|
PyCodeObject *co;
|
|
|
|
n = PyParser_SimpleParseString(str, start);
|
1994-08-29 12:50:44 +00:00
|
|
|
if (n == NULL)
|
1993-03-30 17:46:03 +00:00
|
|
|
return NULL;
|
1997-03-05 00:20:32 +00:00
|
|
|
co = PyNode_Compile(n, filename);
|
|
|
|
PyNode_Free(n);
|
|
|
|
return (PyObject *)co;
|
1993-03-30 17:46:03 +00:00
|
|
|
}
|
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
/* Simplified interface to parsefile -- return node or set exception */
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
node *
|
1997-03-05 00:20:32 +00:00
|
|
|
PyParser_SimpleParseFile(fp, filename, start)
|
1992-08-04 12:41:02 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
int start;
|
|
|
|
{
|
1994-08-29 12:50:44 +00:00
|
|
|
node *n;
|
|
|
|
perrdetail err;
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
|
1994-08-29 12:50:44 +00:00
|
|
|
(char *)0, (char *)0, &err);
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_END_ALLOW_THREADS
|
1994-08-29 12:50:44 +00:00
|
|
|
if (n == NULL)
|
|
|
|
err_input(&err);
|
|
|
|
return n;
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
/* Simplified interface to parsestring -- return node or set exception */
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
node *
|
1997-03-05 00:20:32 +00:00
|
|
|
PyParser_SimpleParseString(str, start)
|
1992-08-04 12:41:02 +00:00
|
|
|
char *str;
|
|
|
|
int start;
|
|
|
|
{
|
1994-08-29 12:50:44 +00:00
|
|
|
node *n;
|
|
|
|
perrdetail err;
|
1997-03-05 00:20:32 +00:00
|
|
|
n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
|
1994-08-29 12:50:44 +00:00
|
|
|
if (n == NULL)
|
|
|
|
err_input(&err);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the error appropriate to the given input error code (see errcode.h) */
|
|
|
|
|
|
|
|
static void
|
|
|
|
err_input(err)
|
|
|
|
perrdetail *err;
|
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *v, *w;
|
1994-08-29 12:50:44 +00:00
|
|
|
char *msg = NULL;
|
1997-03-05 00:20:32 +00:00
|
|
|
v = Py_BuildValue("(ziiz)", err->filename,
|
1994-08-29 12:50:44 +00:00
|
|
|
err->lineno, err->offset, err->text);
|
|
|
|
if (err->text != NULL) {
|
|
|
|
free(err->text);
|
|
|
|
err->text = NULL;
|
|
|
|
}
|
|
|
|
switch (err->error) {
|
|
|
|
case E_SYNTAX:
|
|
|
|
msg = "invalid syntax";
|
|
|
|
break;
|
|
|
|
case E_TOKEN:
|
|
|
|
msg = "invalid token";
|
|
|
|
|
|
|
|
break;
|
|
|
|
case E_INTR:
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_SetNone(PyExc_KeyboardInterrupt);
|
1994-08-29 12:50:44 +00:00
|
|
|
return;
|
|
|
|
case E_NOMEM:
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_NoMemory();
|
1994-08-29 12:50:44 +00:00
|
|
|
return;
|
|
|
|
case E_EOF:
|
|
|
|
msg = "unexpected EOF while parsing";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "error=%d\n", err->error);
|
|
|
|
msg = "unknown parsing error";
|
|
|
|
break;
|
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
w = Py_BuildValue("(sO)", msg, v);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyErr_SetObject(PyExc_SyntaxError, w);
|
|
|
|
Py_XDECREF(w);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print fatal error message and abort */
|
|
|
|
|
|
|
|
void
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_FatalError(msg)
|
1992-08-04 12:41:02 +00:00
|
|
|
char *msg;
|
|
|
|
{
|
1994-09-29 09:38:33 +00:00
|
|
|
fprintf(stderr, "Fatal Python error: %s\n", msg);
|
1995-01-26 00:40:38 +00:00
|
|
|
#ifdef macintosh
|
|
|
|
for (;;);
|
1995-03-14 15:01:17 +00:00
|
|
|
#endif
|
1996-09-11 23:12:24 +00:00
|
|
|
#ifdef MS_WIN32
|
1997-05-22 20:21:30 +00:00
|
|
|
OutputDebugString("Fatal Python error: ");
|
1995-03-14 15:01:17 +00:00
|
|
|
OutputDebugString(msg);
|
|
|
|
OutputDebugString("\n");
|
1995-01-26 00:40:38 +00:00
|
|
|
#endif
|
1992-08-04 12:41:02 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up and exit */
|
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
#ifdef WITH_THREAD
|
|
|
|
#include "thread.h"
|
1997-03-05 00:20:32 +00:00
|
|
|
int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
|
1992-08-17 08:59:08 +00:00
|
|
|
#endif
|
|
|
|
|
1994-09-07 14:38:28 +00:00
|
|
|
#define NEXITFUNCS 32
|
|
|
|
static void (*exitfuncs[NEXITFUNCS])();
|
|
|
|
static int nexitfuncs = 0;
|
|
|
|
|
|
|
|
int Py_AtExit(func)
|
1997-03-05 00:20:32 +00:00
|
|
|
void (*func) Py_PROTO((void));
|
1994-09-07 14:38:28 +00:00
|
|
|
{
|
|
|
|
if (nexitfuncs >= NEXITFUNCS)
|
|
|
|
return -1;
|
|
|
|
exitfuncs[nexitfuncs++] = func;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
void
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_Cleanup()
|
1992-08-04 12:41:02 +00:00
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *exitfunc = PySys_GetObject("exitfunc");
|
1992-09-03 20:28:00 +00:00
|
|
|
|
|
|
|
if (exitfunc) {
|
1997-03-05 00:20:32 +00:00
|
|
|
PyObject *res;
|
|
|
|
Py_INCREF(exitfunc);
|
|
|
|
PySys_SetObject("exitfunc", (PyObject *)NULL);
|
|
|
|
res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
|
1992-09-03 20:28:00 +00:00
|
|
|
if (res == NULL) {
|
|
|
|
fprintf(stderr, "Error in sys.exitfunc:\n");
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_Print();
|
1992-09-03 20:28:00 +00:00
|
|
|
}
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_DECREF(exitfunc);
|
1992-09-03 20:28:00 +00:00
|
|
|
}
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_FlushLine();
|
1994-09-07 14:38:28 +00:00
|
|
|
|
|
|
|
while (nexitfuncs > 0)
|
|
|
|
(*exitfuncs[--nexitfuncs])();
|
1992-10-18 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
1993-10-11 12:54:31 +00:00
|
|
|
#ifdef COUNT_ALLOCS
|
1997-03-05 00:20:32 +00:00
|
|
|
extern void dump_counts Py_PROTO((void));
|
1993-10-11 12:54:31 +00:00
|
|
|
#endif
|
|
|
|
|
1992-10-18 18:53:57 +00:00
|
|
|
void
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_Exit(sts)
|
1992-10-18 18:53:57 +00:00
|
|
|
int sts;
|
|
|
|
{
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_Cleanup();
|
1992-08-05 19:58:53 +00:00
|
|
|
|
1993-10-11 12:54:31 +00:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
dump_counts();
|
|
|
|
#endif
|
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
#ifdef WITH_THREAD
|
1992-08-05 19:58:53 +00:00
|
|
|
|
|
|
|
/* Other threads may still be active, so skip most of the
|
|
|
|
cleanup actions usually done (these are mostly for
|
|
|
|
debugging anyway). */
|
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
(void) PyEval_SaveThread();
|
1994-08-29 12:50:44 +00:00
|
|
|
#ifndef NO_EXIT_PROG
|
1997-03-05 00:20:32 +00:00
|
|
|
if (_PyThread_Started)
|
1992-08-17 08:59:08 +00:00
|
|
|
_exit_prog(sts);
|
|
|
|
else
|
|
|
|
exit_prog(sts);
|
1994-08-29 12:50:44 +00:00
|
|
|
#else /* !NO_EXIT_PROG */
|
1997-03-05 00:20:32 +00:00
|
|
|
if (_PyThread_Started)
|
1994-08-29 12:50:44 +00:00
|
|
|
_exit(sts);
|
|
|
|
else
|
|
|
|
exit(sts);
|
|
|
|
#endif /* !NO_EXIT_PROG */
|
1992-08-05 19:58:53 +00:00
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
#else /* WITH_THREAD */
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
PyImport_Cleanup();
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1997-03-05 00:20:32 +00:00
|
|
|
PyErr_Clear();
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1996-05-22 16:35:33 +00:00
|
|
|
#ifdef Py_REF_DEBUG
|
1995-03-29 16:57:48 +00:00
|
|
|
fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
|
1992-08-04 12:41:02 +00:00
|
|
|
#endif
|
|
|
|
|
1996-05-22 16:35:33 +00:00
|
|
|
#ifdef Py_TRACE_REFS
|
1997-03-05 00:20:32 +00:00
|
|
|
if (_Py_AskYesNo("Print left references?")) {
|
1996-05-24 20:48:31 +00:00
|
|
|
_Py_PrintReferences(stderr);
|
1992-08-04 12:41:02 +00:00
|
|
|
}
|
1996-05-22 16:35:33 +00:00
|
|
|
#endif /* Py_TRACE_REFS */
|
1992-08-04 12:41:02 +00:00
|
|
|
|
1995-10-27 13:22:14 +00:00
|
|
|
#ifdef macintosh
|
|
|
|
PyMac_Exit(sts);
|
|
|
|
#else
|
1992-08-04 12:41:02 +00:00
|
|
|
exit(sts);
|
1995-10-27 13:22:14 +00:00
|
|
|
#endif
|
1994-08-29 12:50:44 +00:00
|
|
|
#endif /* WITH_THREAD */
|
1992-08-04 12:41:02 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
static RETSIGTYPE
|
1992-10-18 18:53:57 +00:00
|
|
|
sighandler(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
signal(sig, SIG_DFL); /* Don't catch recursive signals */
|
1997-03-05 00:20:32 +00:00
|
|
|
Py_Cleanup(); /* Do essential clean-up */
|
1997-05-06 15:24:59 +00:00
|
|
|
#ifdef HAVE_KILL
|
1992-10-18 18:53:57 +00:00
|
|
|
kill(getpid(), sig); /* Pretend the signal killed us */
|
1994-08-29 12:50:44 +00:00
|
|
|
#else
|
|
|
|
exit(1);
|
|
|
|
#endif
|
1992-10-18 18:53:57 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1993-07-05 10:31:29 +00:00
|
|
|
static void
|
1992-10-18 18:53:57 +00:00
|
|
|
initsigs()
|
|
|
|
{
|
1994-08-29 12:50:44 +00:00
|
|
|
RETSIGTYPE (*t)();
|
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGHUP
|
|
|
|
t = signal(SIGHUP, SIG_IGN);
|
|
|
|
if (t == SIG_DFL)
|
1992-10-18 18:53:57 +00:00
|
|
|
signal(SIGHUP, sighandler);
|
1994-08-29 12:50:44 +00:00
|
|
|
else
|
|
|
|
signal(SIGHUP, t);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
t = signal(SIGTERM, SIG_IGN);
|
|
|
|
if (t == SIG_DFL)
|
1992-10-18 18:53:57 +00:00
|
|
|
signal(SIGTERM, sighandler);
|
1994-08-29 12:50:44 +00:00
|
|
|
else
|
|
|
|
signal(SIGTERM, t);
|
1992-10-18 18:53:57 +00:00
|
|
|
#endif
|
1994-08-29 12:50:44 +00:00
|
|
|
#endif /* HAVE_SIGNAL_H */
|
1997-03-05 00:20:32 +00:00
|
|
|
PyOS_InitInterrupts(); /* May imply initsignal() */
|
1992-10-18 18:53:57 +00:00
|
|
|
}
|
|
|
|
|
1996-05-22 16:35:33 +00:00
|
|
|
#ifdef Py_TRACE_REFS
|
1992-08-04 12:41:02 +00:00
|
|
|
/* Ask a yes/no question */
|
|
|
|
|
1992-09-03 20:28:00 +00:00
|
|
|
int
|
1997-03-05 00:20:32 +00:00
|
|
|
_Py_AskYesNo(prompt)
|
1992-08-04 12:41:02 +00:00
|
|
|
char *prompt;
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
printf("%s [ny] ", prompt);
|
|
|
|
if (fgets(buf, sizeof buf, stdin) == NULL)
|
|
|
|
return 0;
|
|
|
|
return buf[0] == 'y' || buf[0] == 'Y';
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1994-08-29 12:50:44 +00:00
|
|
|
#ifdef MPW
|
1992-08-04 12:41:02 +00:00
|
|
|
|
|
|
|
/* Check for file descriptor connected to interactive device.
|
|
|
|
Pretend that stdin is always interactive, other files never. */
|
|
|
|
|
|
|
|
int
|
|
|
|
isatty(fd)
|
|
|
|
int fd;
|
|
|
|
{
|
|
|
|
return fd == fileno(stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
1997-02-14 19:45:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The file descriptor fd is considered ``interactive'' if either
|
|
|
|
* a) isatty(fd) is TRUE, or
|
|
|
|
* b) the -i flag was given, and the filename associated with
|
|
|
|
* the descriptor is NULL or "<stdin>" or "???".
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
Py_FdIsInteractive(fp, filename)
|
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
if (isatty((int)fileno(fp)))
|
|
|
|
return 1;
|
|
|
|
if (!Py_InteractiveFlag)
|
|
|
|
return 0;
|
|
|
|
return (filename == NULL) ||
|
|
|
|
(strcmp(filename, "<stdin>") == 0) ||
|
|
|
|
(strcmp(filename, "???") == 0);
|
|
|
|
}
|