1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* System module */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Various bits of information used by the interpreter are collected in
|
|
|
|
module 'sys'.
|
1990-12-20 11:06:42 -04:00
|
|
|
Function member:
|
1995-03-20 11:09:40 -04:00
|
|
|
- exit(sts): raise SystemExit
|
1990-10-14 09:07:46 -03:00
|
|
|
Data members:
|
|
|
|
- stdin, stdout, stderr: standard file objects
|
|
|
|
- modules: the table of modules (dictionary)
|
1990-12-20 11:06:42 -04:00
|
|
|
- path: module search path (list of strings)
|
|
|
|
- argv: script arguments (list of strings)
|
|
|
|
- ps1, ps2: optional primary and secondary prompts (strings)
|
1990-10-14 09:07:46 -03:00
|
|
|
*/
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
#include "Python.h"
|
2000-12-06 17:47:46 -04:00
|
|
|
#include "compile.h"
|
|
|
|
#include "frameobject.h"
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1992-03-23 14:20:18 -04:00
|
|
|
#include "osdefs.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1997-05-20 12:57:25 -03:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1997-04-25 12:38:31 -03:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1996-09-11 20:12:24 -03:00
|
|
|
#ifdef MS_COREDLL
|
1996-04-08 23:37:57 -03:00
|
|
|
extern void *PyWin_DLLhModule;
|
1997-09-29 20:34:23 -03:00
|
|
|
/* A string loaded from the DLL at startup: */
|
|
|
|
extern const char *PyWin_DLLVersionString;
|
1996-04-08 23:37:57 -03:00
|
|
|
#endif
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
PySys_GetObject(char *name)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-08-02 00:10:38 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
|
|
|
PyObject *sd = tstate->interp->sysdict;
|
1999-10-05 19:17:41 -03:00
|
|
|
if (sd == NULL)
|
|
|
|
return NULL;
|
1997-08-02 00:10:38 -03:00
|
|
|
return PyDict_GetItemString(sd, name);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
2000-07-22 15:47:25 -03:00
|
|
|
PySys_GetFile(char *name, FILE *def)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *v = PySys_GetObject(name);
|
|
|
|
if (v != NULL && PyFile_Check(v))
|
|
|
|
fp = PyFile_AsFile(v);
|
1990-10-14 09:07:46 -03:00
|
|
|
if (fp == NULL)
|
|
|
|
fp = def;
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-22 15:47:25 -03:00
|
|
|
PySys_SetObject(char *name, PyObject *v)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-08-02 00:10:38 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
|
|
|
PyObject *sd = tstate->interp->sysdict;
|
1992-01-26 14:15:48 -04:00
|
|
|
if (v == NULL) {
|
1997-08-02 00:10:38 -03:00
|
|
|
if (PyDict_GetItemString(sd, name) == NULL)
|
1992-01-26 14:15:48 -04:00
|
|
|
return 0;
|
|
|
|
else
|
1997-08-02 00:10:38 -03:00
|
|
|
return PyDict_DelItemString(sd, name);
|
1992-01-26 14:15:48 -04:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
else
|
1997-08-02 00:10:38 -03:00
|
|
|
return PyDict_SetItemString(sd, name, v);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2001-01-11 01:41:27 -04:00
|
|
|
static PyObject *
|
|
|
|
sys_displayhook(PyObject *self, PyObject *args)
|
|
|
|
{
|
2001-01-11 05:27:34 -04:00
|
|
|
PyObject *o, *outf;
|
2001-01-11 01:41:27 -04:00
|
|
|
PyInterpreterState *interp = PyThreadState_Get()->interp;
|
|
|
|
PyObject *modules = interp->modules;
|
|
|
|
PyObject *builtins = PyDict_GetItemString(modules, "__builtin__");
|
|
|
|
|
|
|
|
/* parse arguments */
|
|
|
|
if (!PyArg_ParseTuple(args, "O:displayhook", &o))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Print value except if None */
|
|
|
|
/* After printing, also assign to '_' */
|
|
|
|
/* Before, set '_' to None to avoid recursion */
|
|
|
|
if (o == Py_None) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
if (PyObject_SetAttrString(builtins, "_", Py_None) != 0)
|
|
|
|
return NULL;
|
|
|
|
if (Py_FlushLine() != 0)
|
|
|
|
return NULL;
|
2001-01-11 05:27:34 -04:00
|
|
|
outf = PySys_GetObject("stdout");
|
|
|
|
if (outf == NULL) {
|
2001-01-11 01:41:27 -04:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-11 05:27:34 -04:00
|
|
|
if (PyFile_WriteObject(o, outf, 0) != 0)
|
2001-01-11 01:41:27 -04:00
|
|
|
return NULL;
|
2001-01-11 05:27:34 -04:00
|
|
|
PyFile_SoftSpace(outf, 1);
|
2001-01-11 01:41:27 -04:00
|
|
|
if (Py_FlushLine() != 0)
|
|
|
|
return NULL;
|
|
|
|
if (PyObject_SetAttrString(builtins, "_", o) != 0)
|
|
|
|
return NULL;
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char displayhook_doc[] =
|
2001-03-22 22:46:52 -04:00
|
|
|
"displayhook(object) -> None\n"
|
2001-01-11 01:41:27 -04:00
|
|
|
"\n"
|
2001-03-22 22:46:52 -04:00
|
|
|
"Print an object to sys.stdout and also save it in __builtin__._\n";
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
sys_excepthook(PyObject* self, PyObject* args)
|
|
|
|
{
|
|
|
|
PyObject *exc, *value, *tb;
|
|
|
|
if (!PyArg_ParseTuple(args, "OOO:excepthook", &exc, &value, &tb))
|
|
|
|
return NULL;
|
|
|
|
PyErr_Display(exc, value, tb);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char excepthook_doc[] =
|
|
|
|
"excepthook(exctype, value, traceback) -> None\n"
|
|
|
|
"\n"
|
|
|
|
"Handle an exception by displaying it with a traceback on sys.stderr.\n";
|
2001-01-11 01:41:27 -04:00
|
|
|
|
1997-05-05 17:56:21 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_exc_info(PyObject *self, PyObject *args)
|
1997-05-05 17:56:21 -03:00
|
|
|
{
|
|
|
|
PyThreadState *tstate;
|
2000-03-30 20:38:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, ":exc_info"))
|
1997-05-05 17:56:21 -03:00
|
|
|
return NULL;
|
|
|
|
tstate = PyThreadState_Get();
|
|
|
|
return Py_BuildValue(
|
|
|
|
"(OOO)",
|
|
|
|
tstate->exc_type != NULL ? tstate->exc_type : Py_None,
|
|
|
|
tstate->exc_value != NULL ? tstate->exc_value : Py_None,
|
|
|
|
tstate->exc_traceback != NULL ?
|
|
|
|
tstate->exc_traceback : Py_None);
|
|
|
|
}
|
|
|
|
|
1998-06-27 16:43:25 -03:00
|
|
|
static char exc_info_doc[] =
|
|
|
|
"exc_info() -> (type, value, traceback)\n\
|
|
|
|
\n\
|
|
|
|
Return information about the exception that is currently being handled.\n\
|
|
|
|
This should be called from inside an except clause only.";
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_exit(PyObject *self, PyObject *args)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1991-12-31 09:15:35 -04:00
|
|
|
/* Raise SystemExit so callers may catch it or clean up. */
|
1997-04-29 15:33:38 -03:00
|
|
|
PyErr_SetObject(PyExc_SystemExit, args);
|
1991-12-31 09:15:35 -04:00
|
|
|
return NULL;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1998-06-27 16:43:25 -03:00
|
|
|
static char exit_doc[] =
|
|
|
|
"exit([status])\n\
|
|
|
|
\n\
|
|
|
|
Exit the interpreter by raising SystemExit(status).\n\
|
|
|
|
If the status is omitted or None, it defaults to zero (i.e., success).\n\
|
|
|
|
If the status numeric, it will be used as the system exit status.\n\
|
|
|
|
If it is another kind of object, it will be printed and the system\n\
|
|
|
|
exit status will be one (i.e., failure).";
|
|
|
|
|
2000-05-09 16:57:01 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_getdefaultencoding(PyObject *self, PyObject *args)
|
2000-05-09 16:57:01 -03:00
|
|
|
{
|
2000-06-07 06:13:41 -03:00
|
|
|
if (!PyArg_ParseTuple(args, ":getdefaultencoding"))
|
2000-05-09 16:57:01 -03:00
|
|
|
return NULL;
|
|
|
|
return PyString_FromString(PyUnicode_GetDefaultEncoding());
|
|
|
|
}
|
|
|
|
|
2000-06-07 06:13:41 -03:00
|
|
|
static char getdefaultencoding_doc[] =
|
|
|
|
"getdefaultencoding() -> string\n\
|
2000-05-09 16:57:01 -03:00
|
|
|
\n\
|
|
|
|
Return the current default string encoding used by the Unicode \n\
|
|
|
|
implementation.";
|
|
|
|
|
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_setdefaultencoding(PyObject *self, PyObject *args)
|
2000-05-09 16:57:01 -03:00
|
|
|
{
|
|
|
|
char *encoding;
|
2000-06-07 06:13:41 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "s:setdefaultencoding", &encoding))
|
2000-05-09 16:57:01 -03:00
|
|
|
return NULL;
|
|
|
|
if (PyUnicode_SetDefaultEncoding(encoding))
|
|
|
|
return NULL;
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2000-06-07 06:13:41 -03:00
|
|
|
static char setdefaultencoding_doc[] =
|
|
|
|
"setdefaultencoding(encoding)\n\
|
2000-05-09 16:57:01 -03:00
|
|
|
\n\
|
|
|
|
Set the current default string encoding used by the Unicode implementation.";
|
|
|
|
|
2001-06-16 18:02:31 -03:00
|
|
|
extern int _PyTrace_Init(void);
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_settrace(PyObject *self, PyObject *args)
|
1992-03-23 14:20:18 -04:00
|
|
|
{
|
1997-05-05 17:56:21 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
2001-06-16 18:02:31 -03:00
|
|
|
if (_PyTrace_Init() == -1)
|
|
|
|
return NULL;
|
1997-04-29 15:33:38 -03:00
|
|
|
if (args == Py_None)
|
1992-03-23 14:20:18 -04:00
|
|
|
args = NULL;
|
1992-04-05 11:17:55 -03:00
|
|
|
else
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_XINCREF(args);
|
1997-05-05 17:56:21 -03:00
|
|
|
Py_XDECREF(tstate->sys_tracefunc);
|
|
|
|
tstate->sys_tracefunc = args;
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-03-23 14:20:18 -04:00
|
|
|
}
|
|
|
|
|
1998-06-27 16:43:25 -03:00
|
|
|
static char settrace_doc[] =
|
|
|
|
"settrace(function)\n\
|
|
|
|
\n\
|
|
|
|
Set the global debug tracing function. It will be called on each\n\
|
|
|
|
function call. See the debugger chapter in the library manual.";
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_setprofile(PyObject *self, PyObject *args)
|
1992-03-23 14:20:18 -04:00
|
|
|
{
|
1997-05-05 17:56:21 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
2001-06-16 18:02:31 -03:00
|
|
|
if (_PyTrace_Init() == -1)
|
|
|
|
return NULL;
|
1997-04-29 15:33:38 -03:00
|
|
|
if (args == Py_None)
|
1992-03-23 14:20:18 -04:00
|
|
|
args = NULL;
|
1992-04-05 11:17:55 -03:00
|
|
|
else
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_XINCREF(args);
|
1997-05-05 17:56:21 -03:00
|
|
|
Py_XDECREF(tstate->sys_profilefunc);
|
|
|
|
tstate->sys_profilefunc = args;
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-03-23 14:20:18 -04:00
|
|
|
}
|
|
|
|
|
1998-06-27 16:43:25 -03:00
|
|
|
static char setprofile_doc[] =
|
|
|
|
"setprofile(function)\n\
|
|
|
|
\n\
|
|
|
|
Set the profiling function. It will be called on each function call\n\
|
|
|
|
and return. See the profiler chapter in the library manual.";
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_setcheckinterval(PyObject *self, PyObject *args)
|
1995-01-09 13:46:13 -04:00
|
|
|
{
|
1997-05-05 17:56:21 -03:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
2000-02-29 09:59:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "i:setcheckinterval", &tstate->interp->checkinterval))
|
1995-01-09 13:46:13 -04:00
|
|
|
return NULL;
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1995-01-09 13:46:13 -04:00
|
|
|
}
|
|
|
|
|
1998-06-27 16:43:25 -03:00
|
|
|
static char setcheckinterval_doc[] =
|
|
|
|
"setcheckinterval(n)\n\
|
|
|
|
\n\
|
|
|
|
Tell the Python interpreter to check for asynchronous events every\n\
|
|
|
|
n instructions. This also affects how often thread switches occur.";
|
|
|
|
|
2000-08-31 16:23:01 -03:00
|
|
|
static PyObject *
|
|
|
|
sys_setrecursionlimit(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
int new_limit;
|
|
|
|
if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
|
|
|
|
return NULL;
|
|
|
|
if (new_limit <= 0) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"recursion limit must be positive");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_SetRecursionLimit(new_limit);
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char setrecursionlimit_doc[] =
|
|
|
|
"setrecursionlimit(n)\n\
|
|
|
|
\n\
|
|
|
|
Set the maximum depth of the Python interpreter stack to n. This\n\
|
|
|
|
limit prevents infinite recursion from causing an overflow of the C\n\
|
|
|
|
stack and crashing Python. The highest possible limit is platform-\n\
|
|
|
|
dependent.";
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
sys_getrecursionlimit(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
if (!PyArg_ParseTuple(args, ":getrecursionlimit"))
|
|
|
|
return NULL;
|
|
|
|
return PyInt_FromLong(Py_GetRecursionLimit());
|
|
|
|
}
|
|
|
|
|
|
|
|
static char getrecursionlimit_doc[] =
|
|
|
|
"getrecursionlimit()\n\
|
|
|
|
\n\
|
|
|
|
Return the current value of the recursion limit, the maximum depth\n\
|
|
|
|
of the Python interpreter stack. This limit prevents infinite\n\
|
|
|
|
recursion from causing an overflow of the C stack and crashing Python.";
|
|
|
|
|
1992-09-03 17:25:30 -03:00
|
|
|
#ifdef USE_MALLOPT
|
|
|
|
/* Link with -lmalloc (or -lmpc) on an SGI */
|
|
|
|
#include <malloc.h>
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_mdebug(PyObject *self, PyObject *args)
|
1992-09-03 17:25:30 -03:00
|
|
|
{
|
|
|
|
int flag;
|
2000-03-30 20:38:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "i:mdebug", &flag))
|
1992-09-03 17:25:30 -03:00
|
|
|
return NULL;
|
|
|
|
mallopt(M_DEBUG, flag);
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-09-03 17:25:30 -03:00
|
|
|
}
|
|
|
|
#endif /* USE_MALLOPT */
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_getrefcount(PyObject *self, PyObject *args)
|
1995-08-29 06:18:14 -03:00
|
|
|
{
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *arg;
|
2000-03-30 20:38:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "O:getrefcount", &arg))
|
1995-08-29 06:18:14 -03:00
|
|
|
return NULL;
|
2000-06-20 05:12:48 -03:00
|
|
|
return PyInt_FromLong(arg->ob_refcnt);
|
1995-08-29 06:18:14 -03:00
|
|
|
}
|
|
|
|
|
2000-06-20 05:12:48 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
|
|
|
static PyObject *
|
|
|
|
sys_gettotalrefcount(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
extern long _Py_RefTotal;
|
|
|
|
if (!PyArg_ParseTuple(args, ":gettotalrefcount"))
|
|
|
|
return NULL;
|
|
|
|
return PyInt_FromLong(_Py_RefTotal);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Py_TRACE_REFS */
|
|
|
|
|
1998-06-27 16:43:25 -03:00
|
|
|
static char getrefcount_doc[] =
|
|
|
|
"getrefcount(object) -> integer\n\
|
|
|
|
\n\
|
|
|
|
Return the current reference count for the object. This includes the\n\
|
|
|
|
temporary reference in the argument list, so it is at least 2.";
|
|
|
|
|
1995-08-29 06:18:14 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
sys_getcounts(PyObject *self, PyObject *args)
|
1995-08-29 06:18:14 -03:00
|
|
|
{
|
2000-07-09 00:09:57 -03:00
|
|
|
extern PyObject *get_counts(void);
|
1995-08-29 06:18:14 -03:00
|
|
|
|
2000-03-30 20:38:29 -04:00
|
|
|
if (!PyArg_ParseTuple(args, ":getcounts"))
|
1995-08-29 06:18:14 -03:00
|
|
|
return NULL;
|
|
|
|
return get_counts();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-12-06 17:47:46 -04:00
|
|
|
static char getframe_doc[] =
|
|
|
|
"_getframe([depth]) -> frameobject\n\
|
|
|
|
\n\
|
|
|
|
Return a frame object from the call stack. If optional integer depth is\n\
|
|
|
|
given, return the frame object that many calls below the top of the stack.\n\
|
|
|
|
If that is deeper than the call stack, ValueError is raised. The default\n\
|
|
|
|
for depth is zero, returning the frame at the top of the call stack.\n\
|
|
|
|
\n\
|
|
|
|
This function should be used for internal and specialized\n\
|
|
|
|
purposes only.";
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
sys_getframe(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PyFrameObject *f = PyThreadState_Get()->frame;
|
|
|
|
int depth = -1;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "|i:_getframe", &depth))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (depth > 0 && f != NULL) {
|
|
|
|
f = f->f_back;
|
|
|
|
--depth;
|
|
|
|
}
|
|
|
|
if (f == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"call stack is not deep enough");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_INCREF(f);
|
|
|
|
return (PyObject*)f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-05-23 19:45:41 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
1996-05-24 17:48:31 -03:00
|
|
|
/* Defined in objects.c because it uses static globals if that file */
|
2000-07-09 00:09:57 -03:00
|
|
|
extern PyObject *_Py_GetObjects(PyObject *, PyObject *);
|
1995-08-29 06:18:14 -03:00
|
|
|
#endif
|
1996-05-24 17:48:31 -03:00
|
|
|
|
1997-01-24 00:07:45 -04:00
|
|
|
#ifdef DYNAMIC_EXECUTION_PROFILE
|
|
|
|
/* Defined in ceval.c because it uses static globals if that file */
|
2000-07-09 00:09:57 -03:00
|
|
|
extern PyObject *_Py_GetDXProfile(PyObject *, PyObject *);
|
1997-01-24 00:07:45 -04:00
|
|
|
#endif
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyMethodDef sys_methods[] = {
|
1997-01-24 00:07:45 -04:00
|
|
|
/* Might as well keep this in alphabetic order */
|
2001-01-11 01:41:27 -04:00
|
|
|
{"displayhook", sys_displayhook, 1, displayhook_doc},
|
2000-03-30 20:38:29 -04:00
|
|
|
{"exc_info", sys_exc_info, 1, exc_info_doc},
|
2001-03-22 22:46:52 -04:00
|
|
|
{"excepthook", sys_excepthook, 1, excepthook_doc},
|
1998-06-27 16:43:25 -03:00
|
|
|
{"exit", sys_exit, 0, exit_doc},
|
2000-08-31 16:23:01 -03:00
|
|
|
{"getdefaultencoding", sys_getdefaultencoding, 1,
|
|
|
|
getdefaultencoding_doc},
|
1995-08-29 06:18:14 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
2000-03-30 20:38:29 -04:00
|
|
|
{"getcounts", sys_getcounts, 1},
|
1995-08-29 06:18:14 -03:00
|
|
|
#endif
|
1997-01-24 00:07:45 -04:00
|
|
|
#ifdef DYNAMIC_EXECUTION_PROFILE
|
|
|
|
{"getdxp", _Py_GetDXProfile, 1},
|
|
|
|
#endif
|
1996-05-23 19:45:41 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
1996-05-24 17:48:31 -03:00
|
|
|
{"getobjects", _Py_GetObjects, 1},
|
2000-06-20 05:12:48 -03:00
|
|
|
{"gettotalrefcount", sys_gettotalrefcount, 1},
|
1995-08-29 06:18:14 -03:00
|
|
|
#endif
|
2000-03-30 20:38:29 -04:00
|
|
|
{"getrefcount", sys_getrefcount, 1, getrefcount_doc},
|
2000-08-31 16:23:01 -03:00
|
|
|
{"getrecursionlimit", sys_getrecursionlimit, 1,
|
|
|
|
getrecursionlimit_doc},
|
2000-12-06 17:47:46 -04:00
|
|
|
{"_getframe", sys_getframe, 1, getframe_doc},
|
1992-09-03 17:25:30 -03:00
|
|
|
#ifdef USE_MALLOPT
|
2000-03-30 20:38:29 -04:00
|
|
|
{"mdebug", sys_mdebug, 1},
|
1992-09-03 17:25:30 -03:00
|
|
|
#endif
|
2000-08-31 16:23:01 -03:00
|
|
|
{"setdefaultencoding", sys_setdefaultencoding, 1,
|
|
|
|
setdefaultencoding_doc},
|
|
|
|
{"setcheckinterval", sys_setcheckinterval, 1,
|
|
|
|
setcheckinterval_doc},
|
1998-06-27 16:43:25 -03:00
|
|
|
{"setprofile", sys_setprofile, 0, setprofile_doc},
|
2000-08-31 16:23:01 -03:00
|
|
|
{"setrecursionlimit", sys_setrecursionlimit, 1,
|
|
|
|
setrecursionlimit_doc},
|
1998-06-27 16:43:25 -03:00
|
|
|
{"settrace", sys_settrace, 0, settrace_doc},
|
1990-12-20 11:06:42 -04:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
list_builtin_module_names(void)
|
1993-01-26 09:33:44 -04:00
|
|
|
{
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *list = PyList_New(0);
|
1993-01-26 09:33:44 -04:00
|
|
|
int i;
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
1997-11-04 13:04:34 -04:00
|
|
|
for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
|
1997-05-05 17:56:21 -03:00
|
|
|
PyObject *name = PyString_FromString(
|
1997-11-04 13:04:34 -04:00
|
|
|
PyImport_Inittab[i].name);
|
1993-01-26 09:33:44 -04:00
|
|
|
if (name == NULL)
|
|
|
|
break;
|
1997-04-29 15:33:38 -03:00
|
|
|
PyList_Append(list, name);
|
|
|
|
Py_DECREF(name);
|
1993-01-26 09:33:44 -04:00
|
|
|
}
|
1997-04-29 15:33:38 -03:00
|
|
|
if (PyList_Sort(list) != 0) {
|
|
|
|
Py_DECREF(list);
|
1994-08-30 05:27:36 -03:00
|
|
|
list = NULL;
|
|
|
|
}
|
1997-01-06 18:55:54 -04:00
|
|
|
if (list) {
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *v = PyList_AsTuple(list);
|
|
|
|
Py_DECREF(list);
|
1997-01-06 18:55:54 -04:00
|
|
|
list = v;
|
|
|
|
}
|
1993-01-26 09:33:44 -04:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2000-12-15 18:02:05 -04:00
|
|
|
static PyObject *warnoptions = NULL;
|
|
|
|
|
|
|
|
void
|
|
|
|
PySys_ResetWarnOptions(void)
|
|
|
|
{
|
|
|
|
if (warnoptions == NULL || !PyList_Check(warnoptions))
|
|
|
|
return;
|
|
|
|
PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PySys_AddWarnOption(char *s)
|
|
|
|
{
|
|
|
|
PyObject *str;
|
|
|
|
|
|
|
|
if (warnoptions == NULL || !PyList_Check(warnoptions)) {
|
|
|
|
Py_XDECREF(warnoptions);
|
|
|
|
warnoptions = PyList_New(0);
|
|
|
|
if (warnoptions == NULL)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
str = PyString_FromString(s);
|
|
|
|
if (str != NULL) {
|
|
|
|
PyList_Append(warnoptions, str);
|
|
|
|
Py_DECREF(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-08-06 00:34:39 -03:00
|
|
|
/* XXX This doc string is too long to be a single string literal in VC++ 5.0.
|
|
|
|
Two literals concatenated works just fine. If you have a K&R compiler
|
|
|
|
or other abomination that however *does* understand longer strings,
|
|
|
|
get rid of the !!! comment in the middle and the quotes that surround it. */
|
1998-06-27 16:43:25 -03:00
|
|
|
static char sys_doc[] =
|
|
|
|
"This module provides access to some objects used or maintained by the\n\
|
|
|
|
interpreter and to functions that interact strongly with the interpreter.\n\
|
|
|
|
\n\
|
|
|
|
Dynamic objects:\n\
|
|
|
|
\n\
|
|
|
|
argv -- command line arguments; argv[0] is the script pathname if known\n\
|
|
|
|
path -- module search path; path[0] is the script directory, else ''\n\
|
|
|
|
modules -- dictionary of loaded modules\n\
|
2001-03-22 22:46:52 -04:00
|
|
|
\n\
|
|
|
|
displayhook -- called to show results in an interactive session\n\
|
|
|
|
excepthook -- called to handle any uncaught exception other than SystemExit\n\
|
|
|
|
To customize printing in an interactive session or to install a custom\n\
|
|
|
|
top-level exception handler, assign other functions to replace these.\n\
|
|
|
|
\n\
|
|
|
|
exitfunc -- if sys.exitfunc exists, this routine is called when Python exits\n\
|
|
|
|
Assigning to sys.exitfunc is deprecated; use the atexit module instead.\n\
|
1998-06-27 16:43:25 -03:00
|
|
|
\n\
|
|
|
|
stdin -- standard input file object; used by raw_input() and input()\n\
|
|
|
|
stdout -- standard output file object; used by the print statement\n\
|
|
|
|
stderr -- standard error object; used for error messages\n\
|
2001-03-22 22:46:52 -04:00
|
|
|
By assigning other file objects (or objects that behave like files)\n\
|
|
|
|
to these, it is possible to redirect all of the interpreter's I/O.\n\
|
1998-06-27 16:43:25 -03:00
|
|
|
\n\
|
|
|
|
last_type -- type of last uncaught exception\n\
|
|
|
|
last_value -- value of last uncaught exception\n\
|
|
|
|
last_traceback -- traceback of last uncaught exception\n\
|
|
|
|
These three are only available in an interactive session after a\n\
|
|
|
|
traceback has been printed.\n\
|
|
|
|
\n\
|
|
|
|
exc_type -- type of exception currently being handled\n\
|
|
|
|
exc_value -- value of exception currently being handled\n\
|
|
|
|
exc_traceback -- traceback of exception currently being handled\n\
|
|
|
|
The function exc_info() should be used instead of these three,\n\
|
|
|
|
because it is thread-safe.\n\
|
1999-01-14 15:07:00 -04:00
|
|
|
"
|
|
|
|
#ifndef MS_WIN16
|
2001-03-22 22:46:52 -04:00
|
|
|
/* concatenating string here */
|
1999-01-14 15:07:00 -04:00
|
|
|
"\n\
|
1998-06-27 16:43:25 -03:00
|
|
|
Static objects:\n\
|
|
|
|
\n\
|
|
|
|
maxint -- the largest supported integer (the smallest is -maxint-1)\n\
|
|
|
|
builtin_module_names -- tuple of module names built into this intepreter\n\
|
2000-04-13 12:29:10 -03:00
|
|
|
version -- the version of this interpreter as a string\n\
|
|
|
|
version_info -- version information as a tuple\n\
|
|
|
|
hexversion -- version information encoded as a single integer\n\
|
1998-06-27 16:43:25 -03:00
|
|
|
copyright -- copyright notice pertaining to this interpreter\n\
|
|
|
|
platform -- platform identifier\n\
|
|
|
|
executable -- pathname of this Python interpreter\n\
|
|
|
|
prefix -- prefix used to find the Python library\n\
|
|
|
|
exec_prefix -- prefix used to find the machine-specific Python library\n\
|
2001-03-22 22:46:52 -04:00
|
|
|
"
|
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
/* concatenating string here */
|
|
|
|
"dllhandle -- [Windows only] integer handle of the Python DLL\n\
|
1998-06-27 16:43:25 -03:00
|
|
|
winver -- [Windows only] version number of the Python DLL\n\
|
2001-03-22 22:46:52 -04:00
|
|
|
"
|
|
|
|
#endif /* MS_WINDOWS */
|
|
|
|
"__stdin__ -- the original stdin; don't touch!\n\
|
|
|
|
__stdout__ -- the original stdout; don't touch!\n\
|
|
|
|
__stderr__ -- the original stderr; don't touch!\n\
|
|
|
|
__displayhook__ -- the original displayhook; don't touch!\n\
|
|
|
|
__excepthook__ -- the original excepthook; don't touch!\n\
|
1998-06-27 16:43:25 -03:00
|
|
|
\n\
|
|
|
|
Functions:\n\
|
|
|
|
\n\
|
2001-01-11 01:41:27 -04:00
|
|
|
displayhook() -- print an object to the screen, and save it in __builtin__._\n\
|
2001-03-22 22:46:52 -04:00
|
|
|
excepthook() -- print an exception and its traceback to sys.stderr\n\
|
1998-06-27 16:43:25 -03:00
|
|
|
exc_info() -- return thread-safe information about the current exception\n\
|
|
|
|
exit() -- exit the interpreter by raising SystemExit\n\
|
|
|
|
getrefcount() -- return the reference count for an object (plus one :-)\n\
|
2000-08-31 16:23:01 -03:00
|
|
|
getrecursionlimit() -- return the max recursion depth for the interpreter\n\
|
1998-06-27 16:43:25 -03:00
|
|
|
setcheckinterval() -- control how often the interpreter checks for events\n\
|
|
|
|
setprofile() -- set the global profiling function\n\
|
2000-08-31 16:23:01 -03:00
|
|
|
setrecursionlimit() -- set the max recursion depth for the interpreter\n\
|
1998-06-27 16:43:25 -03:00
|
|
|
settrace() -- set the global debug tracing function\n\
|
2000-08-14 17:59:57 -03:00
|
|
|
"
|
2001-03-22 22:46:52 -04:00
|
|
|
#endif /* MS_WIN16 */
|
2000-08-14 17:59:57 -03:00
|
|
|
/* end of sys_doc */ ;
|
1998-06-27 16:43:25 -03:00
|
|
|
|
1997-08-02 00:10:38 -03:00
|
|
|
PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
_PySys_Init(void)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-08-02 00:10:38 -03:00
|
|
|
PyObject *m, *v, *sysdict;
|
|
|
|
PyObject *sysin, *sysout, *syserr;
|
2000-04-13 17:03:20 -03:00
|
|
|
char *s;
|
1997-08-02 00:10:38 -03:00
|
|
|
|
1998-06-27 16:43:25 -03:00
|
|
|
m = Py_InitModule3("sys", sys_methods, sys_doc);
|
1997-04-29 15:33:38 -03:00
|
|
|
sysdict = PyModule_GetDict(m);
|
1997-08-02 00:10:38 -03:00
|
|
|
|
|
|
|
sysin = PyFile_FromFile(stdin, "<stdin>", "r", NULL);
|
|
|
|
sysout = PyFile_FromFile(stdout, "<stdout>", "w", NULL);
|
|
|
|
syserr = PyFile_FromFile(stderr, "<stderr>", "w", NULL);
|
1997-04-29 15:33:38 -03:00
|
|
|
if (PyErr_Occurred())
|
1997-08-02 00:10:38 -03:00
|
|
|
return NULL;
|
1997-04-29 15:33:38 -03:00
|
|
|
PyDict_SetItemString(sysdict, "stdin", sysin);
|
|
|
|
PyDict_SetItemString(sysdict, "stdout", sysout);
|
|
|
|
PyDict_SetItemString(sysdict, "stderr", syserr);
|
1998-02-19 16:53:06 -04:00
|
|
|
/* Make backup copies for cleanup */
|
|
|
|
PyDict_SetItemString(sysdict, "__stdin__", sysin);
|
|
|
|
PyDict_SetItemString(sysdict, "__stdout__", sysout);
|
|
|
|
PyDict_SetItemString(sysdict, "__stderr__", syserr);
|
2001-03-22 22:46:52 -04:00
|
|
|
PyDict_SetItemString(sysdict, "__displayhook__",
|
|
|
|
PyDict_GetItemString(sysdict, "displayhook"));
|
|
|
|
PyDict_SetItemString(sysdict, "__excepthook__",
|
|
|
|
PyDict_GetItemString(sysdict, "excepthook"));
|
1997-08-02 00:10:38 -03:00
|
|
|
Py_XDECREF(sysin);
|
|
|
|
Py_XDECREF(sysout);
|
|
|
|
Py_XDECREF(syserr);
|
1997-04-29 15:33:38 -03:00
|
|
|
PyDict_SetItemString(sysdict, "version",
|
|
|
|
v = PyString_FromString(Py_GetVersion()));
|
1999-01-27 12:33:19 -04:00
|
|
|
Py_XDECREF(v);
|
1999-01-03 08:55:39 -04:00
|
|
|
PyDict_SetItemString(sysdict, "hexversion",
|
|
|
|
v = PyInt_FromLong(PY_VERSION_HEX));
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_XDECREF(v);
|
2000-04-13 14:44:51 -03:00
|
|
|
/*
|
|
|
|
* These release level checks are mutually exclusive and cover
|
|
|
|
* the field, so don't get too fancy with the pre-processor!
|
|
|
|
*/
|
|
|
|
#if PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_ALPHA
|
2000-04-13 17:03:20 -03:00
|
|
|
s = "alpha";
|
2000-08-31 12:21:11 -03:00
|
|
|
#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_BETA
|
2000-04-13 17:03:20 -03:00
|
|
|
s = "beta";
|
2000-08-31 12:21:11 -03:00
|
|
|
#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_GAMMA
|
2000-04-13 17:03:20 -03:00
|
|
|
s = "candidate";
|
2000-08-31 12:21:11 -03:00
|
|
|
#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
|
2000-04-13 17:03:20 -03:00
|
|
|
s = "final";
|
2000-04-13 14:44:51 -03:00
|
|
|
#endif
|
2000-04-13 12:29:10 -03:00
|
|
|
PyDict_SetItemString(sysdict, "version_info",
|
2000-04-13 17:03:20 -03:00
|
|
|
v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
|
2000-04-13 12:29:10 -03:00
|
|
|
PY_MINOR_VERSION,
|
2000-04-13 17:03:20 -03:00
|
|
|
PY_MICRO_VERSION, s,
|
2000-04-13 14:44:51 -03:00
|
|
|
PY_RELEASE_SERIAL));
|
2000-04-13 12:29:10 -03:00
|
|
|
Py_XDECREF(v);
|
1997-04-29 15:33:38 -03:00
|
|
|
PyDict_SetItemString(sysdict, "copyright",
|
|
|
|
v = PyString_FromString(Py_GetCopyright()));
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyDict_SetItemString(sysdict, "platform",
|
|
|
|
v = PyString_FromString(Py_GetPlatform()));
|
|
|
|
Py_XDECREF(v);
|
1997-05-22 17:41:20 -03:00
|
|
|
PyDict_SetItemString(sysdict, "executable",
|
|
|
|
v = PyString_FromString(Py_GetProgramFullPath()));
|
|
|
|
Py_XDECREF(v);
|
1997-04-29 15:33:38 -03:00
|
|
|
PyDict_SetItemString(sysdict, "prefix",
|
|
|
|
v = PyString_FromString(Py_GetPrefix()));
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyDict_SetItemString(sysdict, "exec_prefix",
|
|
|
|
v = PyString_FromString(Py_GetExecPrefix()));
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyDict_SetItemString(sysdict, "maxint",
|
|
|
|
v = PyInt_FromLong(PyInt_GetMax()));
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyDict_SetItemString(sysdict, "builtin_module_names",
|
1995-01-02 15:04:15 -04:00
|
|
|
v = list_builtin_module_names());
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_XDECREF(v);
|
2000-08-14 12:47:03 -03:00
|
|
|
{
|
|
|
|
/* Assumes that longs are at least 2 bytes long.
|
|
|
|
Should be safe! */
|
|
|
|
unsigned long number = 1;
|
2000-08-15 01:24:43 -03:00
|
|
|
char *value;
|
2000-08-14 12:47:03 -03:00
|
|
|
|
|
|
|
s = (char *) &number;
|
|
|
|
if (s[0] == 0)
|
2000-08-15 01:24:43 -03:00
|
|
|
value = "big";
|
2000-08-14 12:47:03 -03:00
|
|
|
else
|
2000-08-15 01:24:43 -03:00
|
|
|
value = "little";
|
|
|
|
PyDict_SetItemString(sysdict, "byteorder",
|
2000-08-16 20:03:57 -03:00
|
|
|
v = PyString_FromString(value));
|
|
|
|
Py_XDECREF(v);
|
2000-08-14 12:47:03 -03:00
|
|
|
}
|
1996-08-23 15:14:47 -03:00
|
|
|
#ifdef MS_COREDLL
|
1997-04-29 15:33:38 -03:00
|
|
|
PyDict_SetItemString(sysdict, "dllhandle",
|
2000-06-28 19:07:35 -03:00
|
|
|
v = PyLong_FromVoidPtr(PyWin_DLLhModule));
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_XDECREF(v);
|
|
|
|
PyDict_SetItemString(sysdict, "winver",
|
1997-09-29 20:34:23 -03:00
|
|
|
v = PyString_FromString(PyWin_DLLVersionString));
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_XDECREF(v);
|
1996-04-08 23:37:57 -03:00
|
|
|
#endif
|
2000-12-15 18:02:05 -04:00
|
|
|
if (warnoptions == NULL) {
|
|
|
|
warnoptions = PyList_New(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Py_INCREF(warnoptions);
|
|
|
|
}
|
|
|
|
if (warnoptions != NULL) {
|
2001-01-13 18:06:05 -04:00
|
|
|
PyDict_SetItemString(sysdict, "warnoptions", warnoptions);
|
2000-12-15 18:02:05 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
if (PyErr_Occurred())
|
1997-08-02 00:10:38 -03:00
|
|
|
return NULL;
|
|
|
|
return m;
|
1990-11-18 13:41:40 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
makepathobject(char *path, int delim)
|
1990-11-18 13:41:40 -04:00
|
|
|
{
|
1990-12-20 11:06:42 -04:00
|
|
|
int i, n;
|
|
|
|
char *p;
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *v, *w;
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
n = 1;
|
|
|
|
p = path;
|
|
|
|
while ((p = strchr(p, delim)) != NULL) {
|
|
|
|
n++;
|
|
|
|
p++;
|
|
|
|
}
|
1997-04-29 15:33:38 -03:00
|
|
|
v = PyList_New(n);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
p = strchr(path, delim);
|
|
|
|
if (p == NULL)
|
|
|
|
p = strchr(path, '\0'); /* End of string */
|
1997-04-29 15:33:38 -03:00
|
|
|
w = PyString_FromStringAndSize(path, (int) (p - path));
|
1990-12-20 11:06:42 -04:00
|
|
|
if (w == NULL) {
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_DECREF(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
1990-11-18 13:41:40 -04:00
|
|
|
}
|
1997-04-29 15:33:38 -03:00
|
|
|
PyList_SetItem(v, i, w);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
path = p+1;
|
1990-11-18 13:41:40 -04:00
|
|
|
}
|
1990-12-20 11:06:42 -04:00
|
|
|
return v;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
PySys_SetPath(char *path)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *v;
|
1990-12-20 11:06:42 -04:00
|
|
|
if ((v = makepathobject(path, DELIM)) == NULL)
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_FatalError("can't create sys.path");
|
|
|
|
if (PySys_SetObject("path", v) != 0)
|
|
|
|
Py_FatalError("can't assign sys.path");
|
|
|
|
Py_DECREF(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
|
1997-04-29 15:33:38 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
makeargvobject(int argc, char **argv)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *av;
|
1992-01-14 14:42:53 -04:00
|
|
|
if (argc <= 0 || argv == NULL) {
|
|
|
|
/* Ensure at least one (empty) argument is seen */
|
|
|
|
static char *empty_argv[1] = {""};
|
|
|
|
argv = empty_argv;
|
|
|
|
argc = 1;
|
|
|
|
}
|
1997-04-29 15:33:38 -03:00
|
|
|
av = PyList_New(argc);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (av != NULL) {
|
1990-11-18 13:41:40 -04:00
|
|
|
int i;
|
1990-12-20 11:06:42 -04:00
|
|
|
for (i = 0; i < argc; i++) {
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *v = PyString_FromString(argv[i]);
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL) {
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_DECREF(av);
|
1990-12-20 11:06:42 -04:00
|
|
|
av = NULL;
|
|
|
|
break;
|
1990-11-18 13:41:40 -04:00
|
|
|
}
|
1997-04-29 15:33:38 -03:00
|
|
|
PyList_SetItem(av, i, v);
|
1990-11-18 13:41:40 -04:00
|
|
|
}
|
|
|
|
}
|
1990-12-20 11:06:42 -04:00
|
|
|
return av;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-07-22 15:47:25 -03:00
|
|
|
PySys_SetArgv(int argc, char **argv)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *av = makeargvobject(argc, argv);
|
|
|
|
PyObject *path = PySys_GetObject("path");
|
1990-12-20 11:06:42 -04:00
|
|
|
if (av == NULL)
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_FatalError("no mem for sys.argv");
|
|
|
|
if (PySys_SetObject("argv", av) != 0)
|
|
|
|
Py_FatalError("can't assign sys.argv");
|
1996-07-30 17:35:50 -03:00
|
|
|
if (path != NULL) {
|
1997-04-25 12:38:31 -03:00
|
|
|
char *argv0 = argv[0];
|
1996-07-30 17:35:50 -03:00
|
|
|
char *p = NULL;
|
1996-09-10 11:44:21 -03:00
|
|
|
int n = 0;
|
1997-04-29 15:33:38 -03:00
|
|
|
PyObject *a;
|
1997-04-25 12:38:31 -03:00
|
|
|
#ifdef HAVE_READLINK
|
|
|
|
char link[MAXPATHLEN+1];
|
|
|
|
char argv0copy[2*MAXPATHLEN+1];
|
|
|
|
int nr = 0;
|
|
|
|
if (argc > 0 && argv0 != NULL)
|
|
|
|
nr = readlink(argv0, link, MAXPATHLEN);
|
|
|
|
if (nr > 0) {
|
|
|
|
/* It's a symlink */
|
|
|
|
link[nr] = '\0';
|
|
|
|
if (link[0] == SEP)
|
|
|
|
argv0 = link; /* Link to absolute path */
|
|
|
|
else if (strchr(link, SEP) == NULL)
|
|
|
|
; /* Link without path */
|
|
|
|
else {
|
|
|
|
/* Must join(dirname(argv0), link) */
|
|
|
|
char *q = strrchr(argv0, SEP);
|
|
|
|
if (q == NULL)
|
|
|
|
argv0 = link; /* argv0 without path */
|
|
|
|
else {
|
|
|
|
/* Must make a copy */
|
|
|
|
strcpy(argv0copy, argv0);
|
|
|
|
q = strrchr(argv0copy, SEP);
|
|
|
|
strcpy(q+1, link);
|
|
|
|
argv0 = argv0copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_READLINK */
|
1996-09-10 11:44:21 -03:00
|
|
|
#if SEP == '\\' /* Special case for MS filename syntax */
|
1997-04-25 12:38:31 -03:00
|
|
|
if (argc > 0 && argv0 != NULL) {
|
1996-09-10 11:44:21 -03:00
|
|
|
char *q;
|
1997-04-25 12:38:31 -03:00
|
|
|
p = strrchr(argv0, SEP);
|
1996-09-10 11:44:21 -03:00
|
|
|
/* Test for alternate separator */
|
1997-04-25 12:38:31 -03:00
|
|
|
q = strrchr(p ? p : argv0, '/');
|
1996-09-10 11:44:21 -03:00
|
|
|
if (q != NULL)
|
|
|
|
p = q;
|
|
|
|
if (p != NULL) {
|
1997-04-25 12:38:31 -03:00
|
|
|
n = p + 1 - argv0;
|
1996-09-10 11:44:21 -03:00
|
|
|
if (n > 1 && p[-1] != ':')
|
|
|
|
n--; /* Drop trailing separator */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* All other filename syntaxes */
|
1997-04-25 12:38:31 -03:00
|
|
|
if (argc > 0 && argv0 != NULL)
|
|
|
|
p = strrchr(argv0, SEP);
|
1996-09-10 11:44:21 -03:00
|
|
|
if (p != NULL) {
|
2001-04-10 19:07:43 -03:00
|
|
|
#ifndef RISCOS
|
1997-04-25 12:38:31 -03:00
|
|
|
n = p + 1 - argv0;
|
2001-04-10 19:07:43 -03:00
|
|
|
#else /* don't include trailing separator */
|
|
|
|
n = p - argv0;
|
|
|
|
#endif /* RISCOS */
|
1996-09-10 11:44:21 -03:00
|
|
|
#if SEP == '/' /* Special case for Unix filename syntax */
|
|
|
|
if (n > 1)
|
|
|
|
n--; /* Drop trailing separator */
|
|
|
|
#endif /* Unix */
|
|
|
|
}
|
|
|
|
#endif /* All others */
|
1997-04-29 15:33:38 -03:00
|
|
|
a = PyString_FromStringAndSize(argv0, n);
|
1996-07-30 17:35:50 -03:00
|
|
|
if (a == NULL)
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_FatalError("no mem for sys.path insertion");
|
|
|
|
if (PyList_Insert(path, 0, a) < 0)
|
|
|
|
Py_FatalError("sys.path.insert(0) failed");
|
|
|
|
Py_DECREF(a);
|
1996-07-23 22:31:37 -03:00
|
|
|
}
|
1997-04-29 15:33:38 -03:00
|
|
|
Py_DECREF(av);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1998-05-12 11:59:24 -03:00
|
|
|
|
|
|
|
|
|
|
|
/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
|
|
|
|
Adapted from code submitted by Just van Rossum.
|
|
|
|
|
|
|
|
PySys_WriteStdout(format, ...)
|
|
|
|
PySys_WriteStderr(format, ...)
|
|
|
|
|
|
|
|
The first function writes to sys.stdout; the second to sys.stderr. When
|
|
|
|
there is a problem, they write to the real (C level) stdout or stderr;
|
1998-10-12 15:22:10 -03:00
|
|
|
no exceptions are raised.
|
1998-05-12 11:59:24 -03:00
|
|
|
|
|
|
|
Both take a printf-style format string as their first argument followed
|
|
|
|
by a variable length argument list determined by the format string.
|
|
|
|
|
|
|
|
*** WARNING ***
|
|
|
|
|
|
|
|
The format should limit the total size of the formatted output string to
|
|
|
|
1000 bytes. In particular, this means that no unrestricted "%s" formats
|
|
|
|
should occur; these should be limited using "%.<N>s where <N> is a
|
|
|
|
decimal number calculated so that <N> plus the maximum size of other
|
|
|
|
formatted text does not exceed 1000 bytes. Also watch out for "%f",
|
|
|
|
which can print hundreds of digits for very large numbers.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2000-07-22 15:47:25 -03:00
|
|
|
mywrite(char *name, FILE *fp, const char *format, va_list va)
|
1998-05-12 11:59:24 -03:00
|
|
|
{
|
|
|
|
PyObject *file;
|
1998-10-12 15:22:10 -03:00
|
|
|
PyObject *error_type, *error_value, *error_traceback;
|
1998-05-12 11:59:24 -03:00
|
|
|
|
1998-10-12 15:22:10 -03:00
|
|
|
PyErr_Fetch(&error_type, &error_value, &error_traceback);
|
1998-05-12 11:59:24 -03:00
|
|
|
file = PySys_GetObject(name);
|
|
|
|
if (file == NULL || PyFile_AsFile(file) == fp)
|
|
|
|
vfprintf(fp, format, va);
|
|
|
|
else {
|
|
|
|
char buffer[1001];
|
1998-10-12 15:22:10 -03:00
|
|
|
if (vsprintf(buffer, format, va) >= sizeof(buffer))
|
|
|
|
Py_FatalError("PySys_WriteStdout/err: buffer overrun");
|
1998-05-12 11:59:24 -03:00
|
|
|
if (PyFile_WriteString(buffer, file) != 0) {
|
|
|
|
PyErr_Clear();
|
|
|
|
fputs(buffer, fp);
|
|
|
|
}
|
|
|
|
}
|
1998-10-12 15:22:10 -03:00
|
|
|
PyErr_Restore(error_type, error_value, error_traceback);
|
1998-05-12 11:59:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PySys_WriteStdout(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
mywrite("stdout", stdout, format, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PySys_WriteStderr(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
mywrite("stderr", stderr, format, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|