1992-08-04 09:41:02 -03:00
|
|
|
/***********************************************************
|
1995-01-04 15:12:13 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03: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 09:41:02 -03:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1992-08-04 09:41:02 -03:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03: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 09:41:02 -03:00
|
|
|
|
1996-10-25 11:44:06 -03: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 09:41:02 -03:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Python interpreter top-level routines, including init/exit */
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
#include "Python.h"
|
1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
#include "grammar.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "parsetok.h"
|
1995-09-18 18:42:42 -03:00
|
|
|
#undef argument /* Avoid conflict on Mac */
|
1992-08-04 09:41:02 -03:00
|
|
|
#include "errcode.h"
|
|
|
|
#include "compile.h"
|
1992-08-05 16:58:53 -03:00
|
|
|
#include "eval.h"
|
1994-09-14 10:31:04 -03:00
|
|
|
#include "marshal.h"
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1996-12-05 19:27:02 -04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
1992-10-18 15:53:57 -03:00
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
1996-09-11 20:12:24 -03:00
|
|
|
#ifdef MS_WIN32
|
1995-03-14 11:01:17 -04:00
|
|
|
#undef BYTE
|
1996-04-08 23:37:03 -03:00
|
|
|
#undef arglist
|
1995-03-14 11:01:17 -04:00
|
|
|
#include "windows.h"
|
|
|
|
#endif
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
extern char *Py_GetPath();
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
extern grammar _PyParser_Grammar; /* From graminit.c */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1993-11-01 12:28:59 -04:00
|
|
|
/* Forward */
|
1997-03-04 20:20:32 -04: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 15:45:36 -04:00
|
|
|
int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
/* Initialize the current interpreter; pass in the Python path. */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
|
|
|
void
|
1997-05-05 17:56:21 -03:00
|
|
|
Py_Setup()
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyImport_Init();
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
/* Modules '__builtin__' and 'sys' are initialized here,
|
1992-08-04 09:41:02 -03: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-04 20:20:32 -04:00
|
|
|
PyBuiltin_Init(); /* Also initializes builtin exceptions */
|
|
|
|
PySys_Init();
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
PySys_SetPath(Py_GetPath());
|
1992-10-18 15:53:57 -03:00
|
|
|
|
|
|
|
initsigs(); /* Signal handling stuff, including initintr() */
|
1995-01-09 13:53:26 -04:00
|
|
|
|
|
|
|
initmain();
|
|
|
|
}
|
|
|
|
|
1997-05-05 17:56:21 -03: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 13:53:26 -04:00
|
|
|
/* Create __main__ module */
|
|
|
|
|
|
|
|
static void
|
|
|
|
initmain()
|
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *m, *d;
|
|
|
|
m = PyImport_AddModule("__main__");
|
1995-01-09 13:53:26 -04:00
|
|
|
if (m == NULL)
|
1997-03-04 20:20:32 -04: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 13:53:26 -04:00
|
|
|
}
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse input from a file and execute it */
|
|
|
|
|
|
|
|
int
|
1997-03-04 20:20:32 -04:00
|
|
|
PyRun_AnyFile(fp, filename)
|
1992-08-04 09:41:02 -03:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
if (filename == NULL)
|
|
|
|
filename = "???";
|
1997-02-14 15:45:36 -04:00
|
|
|
if (Py_FdIsInteractive(fp, filename))
|
1997-03-04 20:20:32 -04:00
|
|
|
return PyRun_InteractiveLoop(fp, filename);
|
1992-08-04 09:41:02 -03:00
|
|
|
else
|
1997-03-04 20:20:32 -04:00
|
|
|
return PyRun_SimpleFile(fp, filename);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-03-04 20:20:32 -04:00
|
|
|
PyRun_InteractiveLoop(fp, filename)
|
1992-08-04 09:41:02 -03:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *v;
|
1992-08-04 09:41:02 -03:00
|
|
|
int ret;
|
1997-03-04 20:20:32 -04:00
|
|
|
v = PySys_GetObject("ps1");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PySys_SetObject("ps1", v = PyString_FromString(">>> "));
|
|
|
|
Py_XDECREF(v);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
v = PySys_GetObject("ps2");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PySys_SetObject("ps2", v = PyString_FromString("... "));
|
|
|
|
Py_XDECREF(v);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
for (;;) {
|
1997-03-04 20:20:32 -04:00
|
|
|
ret = PyRun_InteractiveOne(fp, filename);
|
1996-05-22 13:35:33 -03:00
|
|
|
#ifdef Py_REF_DEBUG
|
1995-03-29 12:57:48 -04:00
|
|
|
fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
|
1992-08-04 09:41:02 -03:00
|
|
|
#endif
|
|
|
|
if (ret == E_EOF)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
if (ret == E_NOMEM)
|
|
|
|
return -1;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-03-04 20:20:32 -04:00
|
|
|
PyRun_InteractiveOne(fp, filename)
|
1992-08-04 09:41:02 -03:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *m, *d, *v, *w;
|
1992-08-04 09:41:02 -03:00
|
|
|
node *n;
|
1994-08-29 09:50:44 -03:00
|
|
|
perrdetail err;
|
1992-08-04 09:41:02 -03:00
|
|
|
char *ps1, *ps2;
|
1997-03-04 20:20:32 -04: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 09:41:02 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
v = NULL;
|
|
|
|
ps1 = "";
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
if (w != NULL && PyString_Check(w)) {
|
|
|
|
Py_INCREF(w);
|
|
|
|
ps2 = PyString_AsString(w);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
w = NULL;
|
|
|
|
ps2 = "";
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar,
|
1997-05-07 14:46:13 -03:00
|
|
|
Py_single_input, ps1, ps2, &err);
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(w);
|
1994-08-29 09:50:44 -03:00
|
|
|
if (n == NULL) {
|
|
|
|
if (err.error == E_EOF) {
|
|
|
|
if (err.text)
|
|
|
|
free(err.text);
|
|
|
|
return E_EOF;
|
|
|
|
}
|
|
|
|
err_input(&err);
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
1994-08-29 09:50:44 -03:00
|
|
|
return err.error;
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
m = PyImport_AddModule("__main__");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-03-04 20:20:32 -04:00
|
|
|
d = PyModule_GetDict(m);
|
1992-08-04 09:41:02 -03:00
|
|
|
v = run_node(n, filename, d, d);
|
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
1992-08-04 09:41:02 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_DECREF(v);
|
1997-05-22 19:35:04 -03:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1992-08-04 09:41:02 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-03-04 20:20:32 -04:00
|
|
|
PyRun_SimpleFile(fp, filename)
|
1992-08-04 09:41:02 -03:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *m, *d, *v;
|
1994-09-14 10:31:04 -03:00
|
|
|
char *ext;
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
m = PyImport_AddModule("__main__");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-03-04 20:20:32 -04:00
|
|
|
d = PyModule_GetDict(m);
|
1994-09-14 10:31:04 -03:00
|
|
|
ext = filename + strlen(filename) - 4;
|
1997-04-02 01:28:38 -04:00
|
|
|
if (strcmp(ext, ".pyc") == 0 || strcmp(ext, ".pyo") == 0
|
1995-02-13 07:44:56 -04:00
|
|
|
#ifdef macintosh
|
|
|
|
/* On a mac, we also assume a pyc file for types 'PYC ' and 'APPL' */
|
1997-04-02 01:28:38 -04:00
|
|
|
|| getfiletype(filename) == 'PYC '
|
|
|
|
|| getfiletype(filename) == 'APPL'
|
1995-02-13 07:44:56 -04:00
|
|
|
#endif /* macintosh */
|
1997-04-02 01:28:38 -04:00
|
|
|
) {
|
1994-09-14 10:31:04 -03:00
|
|
|
/* Try to run a pyc file. First, re-open in binary */
|
1994-12-14 08:58:37 -04:00
|
|
|
/* Don't close, done in main: fclose(fp); */
|
1994-09-14 10:31:04 -03:00
|
|
|
if( (fp = fopen(filename, "rb")) == NULL ) {
|
|
|
|
fprintf(stderr, "python: Can't reopen .pyc file\n");
|
|
|
|
return -1;
|
|
|
|
}
|
1997-04-02 01:28:38 -04:00
|
|
|
/* Turn on optimization if a .pyo file is given */
|
|
|
|
if (strcmp(ext, ".pyo") == 0)
|
|
|
|
Py_OptimizeFlag = 1;
|
1994-09-14 10:31:04 -03:00
|
|
|
v = run_pyc_file(fp, filename, d, d);
|
|
|
|
} else {
|
1997-05-07 14:46:13 -03:00
|
|
|
v = PyRun_File(fp, filename, Py_file_input, d, d);
|
1994-09-14 10:31:04 -03:00
|
|
|
}
|
1992-08-04 09:41:02 -03:00
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
1992-08-04 09:41:02 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_DECREF(v);
|
1997-05-22 19:35:04 -03:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1992-08-04 09:41:02 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-03-04 20:20:32 -04:00
|
|
|
PyRun_SimpleString(command)
|
1992-08-04 09:41:02 -03:00
|
|
|
char *command;
|
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *m, *d, *v;
|
|
|
|
m = PyImport_AddModule("__main__");
|
1992-08-04 09:41:02 -03:00
|
|
|
if (m == NULL)
|
|
|
|
return -1;
|
1997-03-04 20:20:32 -04:00
|
|
|
d = PyModule_GetDict(m);
|
1997-05-07 14:46:13 -03:00
|
|
|
v = PyRun_String(command, Py_file_input, d, d);
|
1992-08-04 09:41:02 -03:00
|
|
|
if (v == NULL) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
1992-08-04 09:41:02 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_DECREF(v);
|
1997-05-22 19:35:04 -03:00
|
|
|
if (Py_FlushLine())
|
|
|
|
PyErr_Clear();
|
1992-08-04 09:41:02 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print()
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-05-22 19:35:04 -03:00
|
|
|
int err = 0;
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *exception, *v, *tb, *f;
|
|
|
|
PyErr_Fetch(&exception, &v, &tb);
|
1995-01-02 15:04:15 -04:00
|
|
|
if (exception == NULL)
|
1997-05-22 21:19:20 -03:00
|
|
|
return;
|
1997-03-04 20:20:32 -04:00
|
|
|
if (exception == PyExc_SystemExit) {
|
1997-05-22 19:35:04 -03:00
|
|
|
err = Py_FlushLine();
|
|
|
|
fflush(stdout);
|
1997-03-04 20:20:32 -04:00
|
|
|
if (v == NULL || v == Py_None)
|
|
|
|
Py_Exit(0);
|
|
|
|
if (PyInt_Check(v))
|
|
|
|
Py_Exit((int)PyInt_AsLong(v));
|
1992-08-04 09:41:02 -03:00
|
|
|
else {
|
1992-09-25 18:59:05 -03:00
|
|
|
/* OK to use real stderr here */
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject_Print(v, stderr, Py_PRINT_RAW);
|
1992-08-04 09:41:02 -03:00
|
|
|
fprintf(stderr, "\n");
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_Exit(1);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
PySys_SetObject("last_type", exception);
|
|
|
|
PySys_SetObject("last_value", v);
|
|
|
|
PySys_SetObject("last_traceback", tb);
|
|
|
|
f = PySys_GetObject("stderr");
|
1992-09-25 18:59:05 -03:00
|
|
|
if (f == NULL)
|
|
|
|
fprintf(stderr, "lost sys.stderr\n");
|
|
|
|
else {
|
1997-05-22 19:35:04 -03:00
|
|
|
err = Py_FlushLine();
|
|
|
|
fflush(stdout);
|
|
|
|
if (err == 0)
|
|
|
|
err = PyTraceBack_Print(tb, f);
|
|
|
|
if (err == 0 && exception == PyExc_SyntaxError) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *message;
|
1994-08-29 09:50:44 -03:00
|
|
|
char *filename, *text;
|
|
|
|
int lineno, offset;
|
1997-03-04 20:20:32 -04:00
|
|
|
if (!PyArg_Parse(v, "(O(ziiz))", &message,
|
1994-08-29 09:50:44 -03:00
|
|
|
&filename, &lineno, &offset, &text))
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Clear();
|
1994-08-29 09:50:44 -03:00
|
|
|
else {
|
|
|
|
char buf[10];
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString(" File \"", f);
|
1994-08-29 09:50:44 -03:00
|
|
|
if (filename == NULL)
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString("<string>", f);
|
1994-08-29 09:50:44 -03:00
|
|
|
else
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString(filename, f);
|
|
|
|
PyFile_WriteString("\", line ", f);
|
1994-08-29 09:50:44 -03:00
|
|
|
sprintf(buf, "%d", lineno);
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString(buf, f);
|
|
|
|
PyFile_WriteString("\n", f);
|
1994-08-29 09:50:44 -03:00
|
|
|
if (text != NULL) {
|
1994-09-19 05:08:50 -03:00
|
|
|
char *nl;
|
|
|
|
if (offset > 0 &&
|
1997-05-06 12:24:59 -03:00
|
|
|
offset == (int)strlen(text))
|
1994-09-19 05:08:50 -03:00
|
|
|
offset--;
|
|
|
|
for (;;) {
|
|
|
|
nl = strchr(text, '\n');
|
|
|
|
if (nl == NULL ||
|
|
|
|
nl-text >= offset)
|
|
|
|
break;
|
|
|
|
offset -= (nl+1-text);
|
|
|
|
text = nl+1;
|
|
|
|
}
|
1994-08-29 09:50:44 -03:00
|
|
|
while (*text == ' ' || *text == '\t') {
|
|
|
|
text++;
|
|
|
|
offset--;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString(" ", f);
|
|
|
|
PyFile_WriteString(text, f);
|
1994-08-29 09:50:44 -03:00
|
|
|
if (*text == '\0' ||
|
|
|
|
text[strlen(text)-1] != '\n')
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString("\n", f);
|
|
|
|
PyFile_WriteString(" ", f);
|
1994-08-29 09:50:44 -03:00
|
|
|
offset--;
|
|
|
|
while (offset > 0) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString(" ", f);
|
1994-08-29 09:50:44 -03:00
|
|
|
offset--;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
PyFile_WriteString("^\n", f);
|
1994-08-29 09:50:44 -03:00
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_INCREF(message);
|
|
|
|
Py_DECREF(v);
|
1994-08-29 09:50:44 -03:00
|
|
|
v = message;
|
1997-05-22 19:35:04 -03:00
|
|
|
/* Can't be bothered to check all those
|
|
|
|
PyFile_WriteString() calls */
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
err = -1;
|
1994-08-29 09:50:44 -03:00
|
|
|
}
|
|
|
|
}
|
1997-05-22 19:35:04 -03:00
|
|
|
if (err) {
|
|
|
|
/* Don't do anything else */
|
|
|
|
}
|
|
|
|
else if (PyClass_Check(exception)) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject* className =
|
|
|
|
((PyClassObject*)exception)->cl_name;
|
1995-02-07 11:30:45 -04:00
|
|
|
if (className == NULL)
|
1997-05-22 19:35:04 -03:00
|
|
|
err = PyFile_WriteString("<unknown>", f);
|
|
|
|
else
|
|
|
|
err = PyFile_WriteObject(className, f,
|
|
|
|
Py_PRINT_RAW);
|
1995-02-07 11:30:45 -04:00
|
|
|
}
|
1997-05-22 19:35:04 -03: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 18:59:05 -03:00
|
|
|
}
|
1997-05-22 19:35:04 -03:00
|
|
|
if (err == 0)
|
|
|
|
err = PyFile_WriteString("\n", f);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_XDECREF(exception);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(tb);
|
1997-05-22 19:35:04 -03: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 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *
|
|
|
|
PyRun_String(str, start, globals, locals)
|
1992-08-04 09:41:02 -03:00
|
|
|
char *str;
|
|
|
|
int start;
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *globals, *locals;
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
return run_err_node(PyParser_SimpleParseString(str, start),
|
1994-08-29 09:50:44 -03:00
|
|
|
"<string>", globals, locals);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *
|
|
|
|
PyRun_File(fp, filename, start, globals, locals)
|
1992-08-04 09:41:02 -03:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
int start;
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *globals, *locals;
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
return run_err_node(PyParser_SimpleParseFile(fp, filename, start),
|
1994-08-29 09:50:44 -03:00
|
|
|
filename, globals, locals);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
static PyObject *
|
1994-08-29 09:50:44 -03:00
|
|
|
run_err_node(n, filename, globals, locals)
|
1992-08-04 09:41:02 -03:00
|
|
|
node *n;
|
|
|
|
char *filename;
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *globals, *locals;
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1994-08-29 09:50:44 -03:00
|
|
|
if (n == NULL)
|
|
|
|
return NULL;
|
1992-08-04 09:41:02 -03:00
|
|
|
return run_node(n, filename, globals, locals);
|
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
static PyObject *
|
1992-08-04 09:41:02 -03:00
|
|
|
run_node(n, filename, globals, locals)
|
|
|
|
node *n;
|
|
|
|
char *filename;
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *globals, *locals;
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *v;
|
|
|
|
co = PyNode_Compile(n, filename);
|
|
|
|
PyNode_Free(n);
|
1992-08-04 09:41:02 -03:00
|
|
|
if (co == NULL)
|
|
|
|
return NULL;
|
1997-03-04 20:20:32 -04:00
|
|
|
v = PyEval_EvalCode(co, globals, locals);
|
|
|
|
Py_DECREF(co);
|
1992-08-04 09:41:02 -03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
static PyObject *
|
1994-09-14 10:31:04 -03:00
|
|
|
run_pyc_file(fp, filename, globals, locals)
|
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *globals, *locals;
|
1994-09-14 10:31:04 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyCodeObject *co;
|
|
|
|
PyObject *v;
|
1994-09-14 10:31:04 -03:00
|
|
|
long magic;
|
1997-03-04 20:20:32 -04:00
|
|
|
long PyImport_GetMagicNumber();
|
1994-09-14 10:31:04 -03:00
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
magic = PyMarshal_ReadLongFromFile(fp);
|
|
|
|
if (magic != PyImport_GetMagicNumber()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1994-09-14 10:31:04 -03:00
|
|
|
"Bad magic number in .pyc file");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
(void) PyMarshal_ReadLongFromFile(fp);
|
|
|
|
v = PyMarshal_ReadObjectFromFile(fp);
|
1994-09-14 10:31:04 -03:00
|
|
|
fclose(fp);
|
1997-03-04 20:20:32 -04:00
|
|
|
if (v == NULL || !PyCode_Check(v)) {
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1994-09-14 10:31:04 -03:00
|
|
|
"Bad code object in .pyc file");
|
|
|
|
return NULL;
|
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
co = (PyCodeObject *)v;
|
|
|
|
v = PyEval_EvalCode(co, globals, locals);
|
|
|
|
Py_DECREF(co);
|
1994-09-14 10:31:04 -03:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *
|
|
|
|
Py_CompileString(str, filename, start)
|
1993-03-30 13:46:03 -04:00
|
|
|
char *str;
|
|
|
|
char *filename;
|
|
|
|
int start;
|
|
|
|
{
|
|
|
|
node *n;
|
1997-03-04 20:20:32 -04:00
|
|
|
PyCodeObject *co;
|
|
|
|
n = PyParser_SimpleParseString(str, start);
|
1994-08-29 09:50:44 -03:00
|
|
|
if (n == NULL)
|
1993-03-30 13:46:03 -04:00
|
|
|
return NULL;
|
1997-03-04 20:20:32 -04:00
|
|
|
co = PyNode_Compile(n, filename);
|
|
|
|
PyNode_Free(n);
|
|
|
|
return (PyObject *)co;
|
1993-03-30 13:46:03 -04:00
|
|
|
}
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
/* Simplified interface to parsefile -- return node or set exception */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
node *
|
1997-03-04 20:20:32 -04:00
|
|
|
PyParser_SimpleParseFile(fp, filename, start)
|
1992-08-04 09:41:02 -03:00
|
|
|
FILE *fp;
|
|
|
|
char *filename;
|
|
|
|
int start;
|
|
|
|
{
|
1994-08-29 09:50:44 -03:00
|
|
|
node *n;
|
|
|
|
perrdetail err;
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
n = PyParser_ParseFile(fp, filename, &_PyParser_Grammar, start,
|
1994-08-29 09:50:44 -03:00
|
|
|
(char *)0, (char *)0, &err);
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_END_ALLOW_THREADS
|
1994-08-29 09:50:44 -03:00
|
|
|
if (n == NULL)
|
|
|
|
err_input(&err);
|
|
|
|
return n;
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
/* Simplified interface to parsestring -- return node or set exception */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
node *
|
1997-03-04 20:20:32 -04:00
|
|
|
PyParser_SimpleParseString(str, start)
|
1992-08-04 09:41:02 -03:00
|
|
|
char *str;
|
|
|
|
int start;
|
|
|
|
{
|
1994-08-29 09:50:44 -03:00
|
|
|
node *n;
|
|
|
|
perrdetail err;
|
1997-03-04 20:20:32 -04:00
|
|
|
n = PyParser_ParseString(str, &_PyParser_Grammar, start, &err);
|
1994-08-29 09:50:44 -03: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-04 20:20:32 -04:00
|
|
|
PyObject *v, *w;
|
1994-08-29 09:50:44 -03:00
|
|
|
char *msg = NULL;
|
1997-03-04 20:20:32 -04:00
|
|
|
v = Py_BuildValue("(ziiz)", err->filename,
|
1994-08-29 09:50:44 -03: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-04 20:20:32 -04:00
|
|
|
PyErr_SetNone(PyExc_KeyboardInterrupt);
|
1994-08-29 09:50:44 -03:00
|
|
|
return;
|
|
|
|
case E_NOMEM:
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_NoMemory();
|
1994-08-29 09:50:44 -03: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-04 20:20:32 -04:00
|
|
|
w = Py_BuildValue("(sO)", msg, v);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyErr_SetObject(PyExc_SyntaxError, w);
|
|
|
|
Py_XDECREF(w);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print fatal error message and abort */
|
|
|
|
|
|
|
|
void
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_FatalError(msg)
|
1992-08-04 09:41:02 -03:00
|
|
|
char *msg;
|
|
|
|
{
|
1994-09-29 06:38:33 -03:00
|
|
|
fprintf(stderr, "Fatal Python error: %s\n", msg);
|
1995-01-25 20:40:38 -04:00
|
|
|
#ifdef macintosh
|
|
|
|
for (;;);
|
1995-03-14 11:01:17 -04:00
|
|
|
#endif
|
1996-09-11 20:12:24 -03:00
|
|
|
#ifdef MS_WIN32
|
1997-05-22 17:21:30 -03:00
|
|
|
OutputDebugString("Fatal Python error: ");
|
1995-03-14 11:01:17 -04:00
|
|
|
OutputDebugString(msg);
|
|
|
|
OutputDebugString("\n");
|
1995-01-25 20:40:38 -04:00
|
|
|
#endif
|
1992-08-04 09:41:02 -03:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up and exit */
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
#ifdef WITH_THREAD
|
|
|
|
#include "thread.h"
|
1997-03-04 20:20:32 -04:00
|
|
|
int _PyThread_Started = 0; /* Set by threadmodule.c and maybe others */
|
1992-08-17 05:59:08 -03:00
|
|
|
#endif
|
|
|
|
|
1994-09-07 11:38:28 -03:00
|
|
|
#define NEXITFUNCS 32
|
|
|
|
static void (*exitfuncs[NEXITFUNCS])();
|
|
|
|
static int nexitfuncs = 0;
|
|
|
|
|
|
|
|
int Py_AtExit(func)
|
1997-03-04 20:20:32 -04:00
|
|
|
void (*func) Py_PROTO((void));
|
1994-09-07 11:38:28 -03:00
|
|
|
{
|
|
|
|
if (nexitfuncs >= NEXITFUNCS)
|
|
|
|
return -1;
|
|
|
|
exitfuncs[nexitfuncs++] = func;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
void
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_Cleanup()
|
1992-08-04 09:41:02 -03:00
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *exitfunc = PySys_GetObject("exitfunc");
|
1992-09-03 17:28:00 -03:00
|
|
|
|
|
|
|
if (exitfunc) {
|
1997-03-04 20:20:32 -04:00
|
|
|
PyObject *res;
|
|
|
|
Py_INCREF(exitfunc);
|
|
|
|
PySys_SetObject("exitfunc", (PyObject *)NULL);
|
|
|
|
res = PyEval_CallObject(exitfunc, (PyObject *)NULL);
|
1992-09-03 17:28:00 -03:00
|
|
|
if (res == NULL) {
|
|
|
|
fprintf(stderr, "Error in sys.exitfunc:\n");
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Print();
|
1992-09-03 17:28:00 -03:00
|
|
|
}
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_DECREF(exitfunc);
|
1992-09-03 17:28:00 -03:00
|
|
|
}
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_FlushLine();
|
1994-09-07 11:38:28 -03:00
|
|
|
|
|
|
|
while (nexitfuncs > 0)
|
|
|
|
(*exitfuncs[--nexitfuncs])();
|
1992-10-18 15:53:57 -03:00
|
|
|
}
|
|
|
|
|
1993-10-11 09:54:31 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
1997-03-04 20:20:32 -04:00
|
|
|
extern void dump_counts Py_PROTO((void));
|
1993-10-11 09:54:31 -03:00
|
|
|
#endif
|
|
|
|
|
1992-10-18 15:53:57 -03:00
|
|
|
void
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_Exit(sts)
|
1992-10-18 15:53:57 -03:00
|
|
|
int sts;
|
|
|
|
{
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_Cleanup();
|
1992-08-05 16:58:53 -03:00
|
|
|
|
1993-10-11 09:54:31 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
dump_counts();
|
|
|
|
#endif
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
#ifdef WITH_THREAD
|
1992-08-05 16:58:53 -03:00
|
|
|
|
|
|
|
/* Other threads may still be active, so skip most of the
|
|
|
|
cleanup actions usually done (these are mostly for
|
|
|
|
debugging anyway). */
|
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
(void) PyEval_SaveThread();
|
1994-08-29 09:50:44 -03:00
|
|
|
#ifndef NO_EXIT_PROG
|
1997-03-04 20:20:32 -04:00
|
|
|
if (_PyThread_Started)
|
1992-08-17 05:59:08 -03:00
|
|
|
_exit_prog(sts);
|
|
|
|
else
|
|
|
|
exit_prog(sts);
|
1994-08-29 09:50:44 -03:00
|
|
|
#else /* !NO_EXIT_PROG */
|
1997-03-04 20:20:32 -04:00
|
|
|
if (_PyThread_Started)
|
1994-08-29 09:50:44 -03:00
|
|
|
_exit(sts);
|
|
|
|
else
|
|
|
|
exit(sts);
|
|
|
|
#endif /* !NO_EXIT_PROG */
|
1992-08-05 16:58:53 -03:00
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
#else /* WITH_THREAD */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
PyImport_Cleanup();
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1997-03-04 20:20:32 -04:00
|
|
|
PyErr_Clear();
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1996-05-22 13:35:33 -03:00
|
|
|
#ifdef Py_REF_DEBUG
|
1995-03-29 12:57:48 -04:00
|
|
|
fprintf(stderr, "[%ld refs]\n", _Py_RefTotal);
|
1992-08-04 09:41:02 -03:00
|
|
|
#endif
|
|
|
|
|
1996-05-22 13:35:33 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
1997-03-04 20:20:32 -04:00
|
|
|
if (_Py_AskYesNo("Print left references?")) {
|
1996-05-24 17:48:31 -03:00
|
|
|
_Py_PrintReferences(stderr);
|
1992-08-04 09:41:02 -03:00
|
|
|
}
|
1996-05-22 13:35:33 -03:00
|
|
|
#endif /* Py_TRACE_REFS */
|
1992-08-04 09:41:02 -03:00
|
|
|
|
1995-10-27 10:22:14 -03:00
|
|
|
#ifdef macintosh
|
|
|
|
PyMac_Exit(sts);
|
|
|
|
#else
|
1992-08-04 09:41:02 -03:00
|
|
|
exit(sts);
|
1995-10-27 10:22:14 -03:00
|
|
|
#endif
|
1994-08-29 09:50:44 -03:00
|
|
|
#endif /* WITH_THREAD */
|
1992-08-04 09:41:02 -03:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
1994-08-29 09:50:44 -03:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
|
|
|
static RETSIGTYPE
|
1992-10-18 15:53:57 -03:00
|
|
|
sighandler(sig)
|
|
|
|
int sig;
|
|
|
|
{
|
|
|
|
signal(sig, SIG_DFL); /* Don't catch recursive signals */
|
1997-03-04 20:20:32 -04:00
|
|
|
Py_Cleanup(); /* Do essential clean-up */
|
1997-05-06 12:24:59 -03:00
|
|
|
#ifdef HAVE_KILL
|
1992-10-18 15:53:57 -03:00
|
|
|
kill(getpid(), sig); /* Pretend the signal killed us */
|
1994-08-29 09:50:44 -03:00
|
|
|
#else
|
|
|
|
exit(1);
|
|
|
|
#endif
|
1992-10-18 15:53:57 -03:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1993-07-05 07:31:29 -03:00
|
|
|
static void
|
1992-10-18 15:53:57 -03:00
|
|
|
initsigs()
|
|
|
|
{
|
1994-08-29 09:50:44 -03: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 15:53:57 -03:00
|
|
|
signal(SIGHUP, sighandler);
|
1994-08-29 09:50:44 -03:00
|
|
|
else
|
|
|
|
signal(SIGHUP, t);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
t = signal(SIGTERM, SIG_IGN);
|
|
|
|
if (t == SIG_DFL)
|
1992-10-18 15:53:57 -03:00
|
|
|
signal(SIGTERM, sighandler);
|
1994-08-29 09:50:44 -03:00
|
|
|
else
|
|
|
|
signal(SIGTERM, t);
|
1992-10-18 15:53:57 -03:00
|
|
|
#endif
|
1994-08-29 09:50:44 -03:00
|
|
|
#endif /* HAVE_SIGNAL_H */
|
1997-03-04 20:20:32 -04:00
|
|
|
PyOS_InitInterrupts(); /* May imply initsignal() */
|
1992-10-18 15:53:57 -03:00
|
|
|
}
|
|
|
|
|
1996-05-22 13:35:33 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
1992-08-04 09:41:02 -03:00
|
|
|
/* Ask a yes/no question */
|
|
|
|
|
1992-09-03 17:28:00 -03:00
|
|
|
int
|
1997-03-04 20:20:32 -04:00
|
|
|
_Py_AskYesNo(prompt)
|
1992-08-04 09:41:02 -03: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 09:50:44 -03:00
|
|
|
#ifdef MPW
|
1992-08-04 09:41:02 -03: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 15:45:36 -04: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);
|
|
|
|
}
|