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