1991-02-19 12:39:46 +00:00
|
|
|
/***********************************************************
|
1995-01-04 19:12:13 +00:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 14:44:06 +00:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 12:39:46 +00:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 14:44:06 +00:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 12:39:46 +00:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 14:44:06 +00:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
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.
|
1991-02-19 12:39:46 +00:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 12:07:46 +00:00
|
|
|
/* System module */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Various bits of information used by the interpreter are collected in
|
|
|
|
module 'sys'.
|
1990-12-20 15:06:42 +00:00
|
|
|
Function member:
|
1995-03-20 15:09:40 +00:00
|
|
|
- exit(sts): raise SystemExit
|
1990-10-14 12:07:46 +00:00
|
|
|
Data members:
|
|
|
|
- stdin, stdout, stderr: standard file objects
|
|
|
|
- modules: the table of modules (dictionary)
|
1990-12-20 15:06:42 +00: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 12:07:46 +00:00
|
|
|
*/
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
#include "Python.h"
|
1990-10-14 12:07:46 +00:00
|
|
|
|
1992-03-23 18:20:18 +00:00
|
|
|
#include "osdefs.h"
|
1990-12-20 15:06:42 +00:00
|
|
|
|
1997-05-20 15:57:25 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1997-04-25 15:38:31 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
1996-09-11 23:12:24 +00:00
|
|
|
#ifdef MS_COREDLL
|
1996-04-09 02:37:57 +00:00
|
|
|
extern void *PyWin_DLLhModule;
|
1997-09-29 23:34:23 +00:00
|
|
|
/* A string loaded from the DLL at startup: */
|
|
|
|
extern const char *PyWin_DLLVersionString;
|
1996-04-09 02:37:57 +00:00
|
|
|
#endif
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *
|
|
|
|
PySys_GetObject(name)
|
1990-10-14 12:07:46 +00:00
|
|
|
char *name;
|
|
|
|
{
|
1997-08-02 03:10:38 +00:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
|
|
|
PyObject *sd = tstate->interp->sysdict;
|
|
|
|
return PyDict_GetItemString(sd, name);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
1997-04-29 18:33:38 +00:00
|
|
|
PySys_GetFile(name, def)
|
1990-10-14 12:07:46 +00:00
|
|
|
char *name;
|
|
|
|
FILE *def;
|
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *v = PySys_GetObject(name);
|
|
|
|
if (v != NULL && PyFile_Check(v))
|
|
|
|
fp = PyFile_AsFile(v);
|
1990-10-14 12:07:46 +00:00
|
|
|
if (fp == NULL)
|
|
|
|
fp = def;
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1997-04-29 18:33:38 +00:00
|
|
|
PySys_SetObject(name, v)
|
1990-10-14 12:07:46 +00:00
|
|
|
char *name;
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *v;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-08-02 03:10:38 +00:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
|
|
|
PyObject *sd = tstate->interp->sysdict;
|
1992-01-26 18:15:48 +00:00
|
|
|
if (v == NULL) {
|
1997-08-02 03:10:38 +00:00
|
|
|
if (PyDict_GetItemString(sd, name) == NULL)
|
1992-01-26 18:15:48 +00:00
|
|
|
return 0;
|
|
|
|
else
|
1997-08-02 03:10:38 +00:00
|
|
|
return PyDict_DelItemString(sd, name);
|
1992-01-26 18:15:48 +00:00
|
|
|
}
|
1990-10-14 12:07:46 +00:00
|
|
|
else
|
1997-08-02 03:10:38 +00:00
|
|
|
return PyDict_SetItemString(sd, name, v);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-05-05 20:56:21 +00:00
|
|
|
static PyObject *
|
|
|
|
sys_exc_info(self, args)
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
|
|
|
{
|
|
|
|
PyThreadState *tstate;
|
|
|
|
if (!PyArg_Parse(args, ""))
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyObject *
|
1990-10-14 12:07:46 +00:00
|
|
|
sys_exit(self, args)
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1991-12-31 13:15:35 +00:00
|
|
|
/* Raise SystemExit so callers may catch it or clean up. */
|
1997-04-29 18:33:38 +00:00
|
|
|
PyErr_SetObject(PyExc_SystemExit, args);
|
1991-12-31 13:15:35 +00:00
|
|
|
return NULL;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyObject *
|
1992-03-23 18:20:18 +00:00
|
|
|
sys_settrace(self, args)
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-03-23 18:20:18 +00:00
|
|
|
{
|
1997-05-05 20:56:21 +00:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
1997-04-29 18:33:38 +00:00
|
|
|
if (args == Py_None)
|
1992-03-23 18:20:18 +00:00
|
|
|
args = NULL;
|
1992-04-05 14:17:55 +00:00
|
|
|
else
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_XINCREF(args);
|
1997-05-05 20:56:21 +00:00
|
|
|
Py_XDECREF(tstate->sys_tracefunc);
|
|
|
|
tstate->sys_tracefunc = args;
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-03-23 18:20:18 +00:00
|
|
|
}
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyObject *
|
1992-03-23 18:20:18 +00:00
|
|
|
sys_setprofile(self, args)
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-03-23 18:20:18 +00:00
|
|
|
{
|
1997-05-05 20:56:21 +00:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
1997-04-29 18:33:38 +00:00
|
|
|
if (args == Py_None)
|
1992-03-23 18:20:18 +00:00
|
|
|
args = NULL;
|
1992-04-05 14:17:55 +00:00
|
|
|
else
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_XINCREF(args);
|
1997-05-05 20:56:21 +00:00
|
|
|
Py_XDECREF(tstate->sys_profilefunc);
|
|
|
|
tstate->sys_profilefunc = args;
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-03-23 18:20:18 +00:00
|
|
|
}
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyObject *
|
1995-01-09 17:46:13 +00:00
|
|
|
sys_setcheckinterval(self, args)
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-01-09 17:46:13 +00:00
|
|
|
{
|
1997-05-05 20:56:21 +00:00
|
|
|
PyThreadState *tstate = PyThreadState_Get();
|
1997-08-02 03:10:38 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "i", &tstate->interp->checkinterval))
|
1995-01-09 17:46:13 +00:00
|
|
|
return NULL;
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1995-01-09 17:46:13 +00:00
|
|
|
}
|
|
|
|
|
1992-09-03 20:25:30 +00:00
|
|
|
#ifdef USE_MALLOPT
|
|
|
|
/* Link with -lmalloc (or -lmpc) on an SGI */
|
|
|
|
#include <malloc.h>
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyObject *
|
1992-09-03 20:25:30 +00:00
|
|
|
sys_mdebug(self, args)
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1992-09-03 20:25:30 +00:00
|
|
|
{
|
|
|
|
int flag;
|
1997-04-29 18:33:38 +00:00
|
|
|
if (!PyArg_Parse(args, "i", &flag))
|
1992-09-03 20:25:30 +00:00
|
|
|
return NULL;
|
|
|
|
mallopt(M_DEBUG, flag);
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
1992-09-03 20:25:30 +00:00
|
|
|
}
|
|
|
|
#endif /* USE_MALLOPT */
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyObject *
|
1995-08-29 09:18:14 +00:00
|
|
|
sys_getrefcount(self, args)
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *self;
|
|
|
|
PyObject *args;
|
1995-08-29 09:18:14 +00:00
|
|
|
{
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *arg;
|
|
|
|
if (!PyArg_Parse(args, "O", &arg))
|
1995-08-29 09:18:14 +00:00
|
|
|
return NULL;
|
1997-04-29 18:33:38 +00:00
|
|
|
return PyInt_FromLong((long) arg->ob_refcnt);
|
1995-08-29 09:18:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
static PyObject *
|
|
|
|
sys_getcounts(self, args)
|
|
|
|
PyObject *self, *args;
|
|
|
|
{
|
|
|
|
extern PyObject *get_counts Py_PROTO((void));
|
|
|
|
|
|
|
|
if (!PyArg_Parse(args, ""))
|
|
|
|
return NULL;
|
|
|
|
return get_counts();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1996-05-23 22:45:41 +00:00
|
|
|
#ifdef Py_TRACE_REFS
|
1996-05-24 20:48:31 +00:00
|
|
|
/* Defined in objects.c because it uses static globals if that file */
|
|
|
|
extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *));
|
1995-08-29 09:18:14 +00:00
|
|
|
#endif
|
1996-05-24 20:48:31 +00:00
|
|
|
|
1997-01-24 04:07:45 +00:00
|
|
|
#ifdef DYNAMIC_EXECUTION_PROFILE
|
|
|
|
/* Defined in ceval.c because it uses static globals if that file */
|
|
|
|
extern PyObject *_Py_GetDXProfile Py_PROTO((PyObject *, PyObject *));
|
|
|
|
#endif
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyMethodDef sys_methods[] = {
|
1997-01-24 04:07:45 +00:00
|
|
|
/* Might as well keep this in alphabetic order */
|
1997-05-05 20:56:21 +00:00
|
|
|
{"exc_info", sys_exc_info, 0},
|
1995-02-21 21:03:24 +00:00
|
|
|
{"exit", sys_exit, 0},
|
1995-08-29 09:18:14 +00:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
{"getcounts", sys_getcounts, 0},
|
|
|
|
#endif
|
1997-01-24 04:07:45 +00:00
|
|
|
#ifdef DYNAMIC_EXECUTION_PROFILE
|
|
|
|
{"getdxp", _Py_GetDXProfile, 1},
|
|
|
|
#endif
|
1996-05-23 22:45:41 +00:00
|
|
|
#ifdef Py_TRACE_REFS
|
1996-05-24 20:48:31 +00:00
|
|
|
{"getobjects", _Py_GetObjects, 1},
|
1995-08-29 09:18:14 +00:00
|
|
|
#endif
|
1997-01-24 04:07:45 +00:00
|
|
|
{"getrefcount", sys_getrefcount, 0},
|
1992-09-03 20:25:30 +00:00
|
|
|
#ifdef USE_MALLOPT
|
1995-02-21 21:03:24 +00:00
|
|
|
{"mdebug", sys_mdebug, 0},
|
1992-09-03 20:25:30 +00:00
|
|
|
#endif
|
1997-01-24 04:07:45 +00:00
|
|
|
{"setcheckinterval", sys_setcheckinterval, 1},
|
1995-02-21 21:03:24 +00:00
|
|
|
{"setprofile", sys_setprofile, 0},
|
|
|
|
{"settrace", sys_settrace, 0},
|
1990-12-20 15:06:42 +00:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyObject *
|
1993-01-26 13:33:44 +00:00
|
|
|
list_builtin_module_names()
|
|
|
|
{
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *list = PyList_New(0);
|
1993-01-26 13:33:44 +00:00
|
|
|
int i;
|
|
|
|
if (list == NULL)
|
|
|
|
return NULL;
|
1997-04-29 20:42:30 +00:00
|
|
|
for (i = 0; _PyImport_Inittab[i].name != NULL; i++) {
|
1997-05-05 20:56:21 +00:00
|
|
|
PyObject *name = PyString_FromString(
|
|
|
|
_PyImport_Inittab[i].name);
|
1993-01-26 13:33:44 +00:00
|
|
|
if (name == NULL)
|
|
|
|
break;
|
1997-04-29 18:33:38 +00:00
|
|
|
PyList_Append(list, name);
|
|
|
|
Py_DECREF(name);
|
1993-01-26 13:33:44 +00:00
|
|
|
}
|
1997-04-29 18:33:38 +00:00
|
|
|
if (PyList_Sort(list) != 0) {
|
|
|
|
Py_DECREF(list);
|
1994-08-30 08:27:36 +00:00
|
|
|
list = NULL;
|
|
|
|
}
|
1997-01-06 22:55:54 +00:00
|
|
|
if (list) {
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *v = PyList_AsTuple(list);
|
|
|
|
Py_DECREF(list);
|
1997-01-06 22:55:54 +00:00
|
|
|
list = v;
|
|
|
|
}
|
1993-01-26 13:33:44 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
1997-08-02 03:10:38 +00:00
|
|
|
PyObject *
|
|
|
|
_PySys_Init()
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-04-29 18:33:38 +00:00
|
|
|
extern int fclose Py_PROTO((FILE *));
|
1997-08-02 03:10:38 +00:00
|
|
|
PyObject *m, *v, *sysdict;
|
|
|
|
PyObject *sysin, *sysout, *syserr;
|
|
|
|
|
|
|
|
m = Py_InitModule("sys", sys_methods);
|
1997-04-29 18:33:38 +00:00
|
|
|
sysdict = PyModule_GetDict(m);
|
1997-08-02 03:10:38 +00: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 18:33:38 +00:00
|
|
|
if (PyErr_Occurred())
|
1997-08-02 03:10:38 +00:00
|
|
|
return NULL;
|
1997-04-29 18:33:38 +00:00
|
|
|
PyDict_SetItemString(sysdict, "stdin", sysin);
|
|
|
|
PyDict_SetItemString(sysdict, "stdout", sysout);
|
|
|
|
PyDict_SetItemString(sysdict, "stderr", syserr);
|
1997-08-02 03:10:38 +00:00
|
|
|
Py_XDECREF(sysin);
|
|
|
|
Py_XDECREF(sysout);
|
|
|
|
Py_XDECREF(syserr);
|
1997-04-29 18:33:38 +00:00
|
|
|
PyDict_SetItemString(sysdict, "version",
|
|
|
|
v = PyString_FromString(Py_GetVersion()));
|
|
|
|
Py_XDECREF(v);
|
|
|
|
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 20:41:20 +00:00
|
|
|
PyDict_SetItemString(sysdict, "executable",
|
|
|
|
v = PyString_FromString(Py_GetProgramFullPath()));
|
|
|
|
Py_XDECREF(v);
|
1997-04-29 18:33:38 +00: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 19:04:15 +00:00
|
|
|
v = list_builtin_module_names());
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_XDECREF(v);
|
1996-08-23 18:14:47 +00:00
|
|
|
#ifdef MS_COREDLL
|
1997-04-29 18:33:38 +00:00
|
|
|
PyDict_SetItemString(sysdict, "dllhandle",
|
|
|
|
v = PyInt_FromLong((int)PyWin_DLLhModule));
|
|
|
|
Py_XDECREF(v);
|
|
|
|
PyDict_SetItemString(sysdict, "winver",
|
1997-09-29 23:34:23 +00:00
|
|
|
v = PyString_FromString(PyWin_DLLVersionString));
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_XDECREF(v);
|
1996-04-09 02:37:57 +00:00
|
|
|
#endif
|
1997-04-29 18:33:38 +00:00
|
|
|
if (PyErr_Occurred())
|
1997-08-02 03:10:38 +00:00
|
|
|
return NULL;
|
|
|
|
return m;
|
1990-11-18 17:41:40 +00:00
|
|
|
}
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyObject *
|
1990-12-20 15:06:42 +00:00
|
|
|
makepathobject(path, delim)
|
|
|
|
char *path;
|
|
|
|
int delim;
|
1990-11-18 17:41:40 +00:00
|
|
|
{
|
1990-12-20 15:06:42 +00:00
|
|
|
int i, n;
|
|
|
|
char *p;
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *v, *w;
|
1990-12-20 15:06:42 +00:00
|
|
|
|
|
|
|
n = 1;
|
|
|
|
p = path;
|
|
|
|
while ((p = strchr(p, delim)) != NULL) {
|
|
|
|
n++;
|
|
|
|
p++;
|
|
|
|
}
|
1997-04-29 18:33:38 +00:00
|
|
|
v = PyList_New(n);
|
1990-12-20 15:06:42 +00: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 18:33:38 +00:00
|
|
|
w = PyString_FromStringAndSize(path, (int) (p - path));
|
1990-12-20 15:06:42 +00:00
|
|
|
if (w == NULL) {
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_DECREF(v);
|
1990-12-20 15:06:42 +00:00
|
|
|
return NULL;
|
1990-11-18 17:41:40 +00:00
|
|
|
}
|
1997-04-29 18:33:38 +00:00
|
|
|
PyList_SetItem(v, i, w);
|
1990-12-20 15:06:42 +00:00
|
|
|
if (*p == '\0')
|
|
|
|
break;
|
|
|
|
path = p+1;
|
1990-11-18 17:41:40 +00:00
|
|
|
}
|
1990-12-20 15:06:42 +00:00
|
|
|
return v;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-04-29 18:33:38 +00:00
|
|
|
PySys_SetPath(path)
|
1990-12-20 15:06:42 +00:00
|
|
|
char *path;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *v;
|
1990-12-20 15:06:42 +00:00
|
|
|
if ((v = makepathobject(path, DELIM)) == NULL)
|
1997-04-29 18:33:38 +00: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 15:06:42 +00:00
|
|
|
}
|
|
|
|
|
1997-04-29 18:33:38 +00:00
|
|
|
static PyObject *
|
1990-12-20 15:06:42 +00:00
|
|
|
makeargvobject(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *av;
|
1992-01-14 18:42:53 +00: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 18:33:38 +00:00
|
|
|
av = PyList_New(argc);
|
1990-12-20 15:06:42 +00:00
|
|
|
if (av != NULL) {
|
1990-11-18 17:41:40 +00:00
|
|
|
int i;
|
1990-12-20 15:06:42 +00:00
|
|
|
for (i = 0; i < argc; i++) {
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *v = PyString_FromString(argv[i]);
|
1990-12-20 15:06:42 +00:00
|
|
|
if (v == NULL) {
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_DECREF(av);
|
1990-12-20 15:06:42 +00:00
|
|
|
av = NULL;
|
|
|
|
break;
|
1990-11-18 17:41:40 +00:00
|
|
|
}
|
1997-04-29 18:33:38 +00:00
|
|
|
PyList_SetItem(av, i, v);
|
1990-11-18 17:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
1990-12-20 15:06:42 +00:00
|
|
|
return av;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1997-04-29 18:33:38 +00:00
|
|
|
PySys_SetArgv(argc, argv)
|
1990-12-20 15:06:42 +00:00
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *av = makeargvobject(argc, argv);
|
|
|
|
PyObject *path = PySys_GetObject("path");
|
1990-12-20 15:06:42 +00:00
|
|
|
if (av == NULL)
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_FatalError("no mem for sys.argv");
|
|
|
|
if (PySys_SetObject("argv", av) != 0)
|
|
|
|
Py_FatalError("can't assign sys.argv");
|
1996-07-30 20:35:50 +00:00
|
|
|
if (path != NULL) {
|
1997-04-25 15:38:31 +00:00
|
|
|
char *argv0 = argv[0];
|
1996-07-30 20:35:50 +00:00
|
|
|
char *p = NULL;
|
1996-09-10 14:44:21 +00:00
|
|
|
int n = 0;
|
1997-04-29 18:33:38 +00:00
|
|
|
PyObject *a;
|
1997-04-25 15:38:31 +00: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 14:44:21 +00:00
|
|
|
#if SEP == '\\' /* Special case for MS filename syntax */
|
1997-04-25 15:38:31 +00:00
|
|
|
if (argc > 0 && argv0 != NULL) {
|
1996-09-10 14:44:21 +00:00
|
|
|
char *q;
|
1997-04-25 15:38:31 +00:00
|
|
|
p = strrchr(argv0, SEP);
|
1996-09-10 14:44:21 +00:00
|
|
|
/* Test for alternate separator */
|
1997-04-25 15:38:31 +00:00
|
|
|
q = strrchr(p ? p : argv0, '/');
|
1996-09-10 14:44:21 +00:00
|
|
|
if (q != NULL)
|
|
|
|
p = q;
|
|
|
|
if (p != NULL) {
|
1997-04-25 15:38:31 +00:00
|
|
|
n = p + 1 - argv0;
|
1996-09-10 14:44:21 +00:00
|
|
|
if (n > 1 && p[-1] != ':')
|
|
|
|
n--; /* Drop trailing separator */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* All other filename syntaxes */
|
1997-04-25 15:38:31 +00:00
|
|
|
if (argc > 0 && argv0 != NULL)
|
|
|
|
p = strrchr(argv0, SEP);
|
1996-09-10 14:44:21 +00:00
|
|
|
if (p != NULL) {
|
1997-04-25 15:38:31 +00:00
|
|
|
n = p + 1 - argv0;
|
1996-09-10 14:44:21 +00:00
|
|
|
#if SEP == '/' /* Special case for Unix filename syntax */
|
|
|
|
if (n > 1)
|
|
|
|
n--; /* Drop trailing separator */
|
|
|
|
#endif /* Unix */
|
|
|
|
}
|
|
|
|
#endif /* All others */
|
1997-04-29 18:33:38 +00:00
|
|
|
a = PyString_FromStringAndSize(argv0, n);
|
1996-07-30 20:35:50 +00:00
|
|
|
if (a == NULL)
|
1997-04-29 18:33:38 +00: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-24 01:31:37 +00:00
|
|
|
}
|
1997-04-29 18:33:38 +00:00
|
|
|
Py_DECREF(av);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|