summaryrefslogtreecommitdiffstats
path: root/Python/sysmodule.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1990-12-20 15:06:42 (GMT)
committerGuido van Rossum <guido@python.org>1990-12-20 15:06:42 (GMT)
commit3f5da24ea304e674a9abbdcffc4d671e32aa70f1 (patch)
treee932e31cb9381f40b7c87c377638216c043b5cfc /Python/sysmodule.c
parent226d79eb4a776dd54c9e4544b17deaf928bcef3a (diff)
downloadcpython-3f5da24ea304e674a9abbdcffc4d671e32aa70f1.zip
cpython-3f5da24ea304e674a9abbdcffc4d671e32aa70f1.tar.gz
cpython-3f5da24ea304e674a9abbdcffc4d671e32aa70f1.tar.bz2
"Compiling" version
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r--Python/sysmodule.c199
1 files changed, 99 insertions, 100 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index f276fb4..5cc1d96 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -3,31 +3,31 @@
/*
Various bits of information used by the interpreter are collected in
module 'sys'.
+Function member:
+- exit(sts): call (C, POSIX) exit(sts)
Data members:
- stdin, stdout, stderr: standard file objects
-- ps1, ps2: primary and secondary prompts (strings)
-- path: module search path (list of strings)
- modules: the table of modules (dictionary)
-Function members:
-- exit(sts): call exit()
+- path: module search path (list of strings)
+- argv: script arguments (list of strings)
+- ps1, ps2: optional primary and secondary prompts (strings)
*/
-#include <stdio.h>
+#include "allobjects.h"
-#include "PROTO.h"
-#include "object.h"
-#include "stringobject.h"
-#include "listobject.h"
-#include "dictobject.h"
-#include "fileobject.h"
-#include "moduleobject.h"
#include "sysmodule.h"
-#include "node.h" /* For context.h */
-#include "context.h" /* For import.h */
#include "import.h"
-#include "methodobject.h"
#include "modsupport.h"
-#include "errors.h"
+
+/* Define delimiter used in $PYTHONPATH */
+
+#ifdef THINK_C
+#define DELIM ' '
+#endif
+
+#ifndef DELIM
+#define DELIM ':'
+#endif
static object *sysdict;
@@ -64,34 +64,6 @@ sysset(name, v)
}
static object *
-makeargv(argc, argv)
- int argc;
- char **argv;
-{
- int i;
- object *av, *v;
- if (argc < 0 || argv == NULL)
- argc = 0;
- av = newlistobject(argc);
- if (av != NULL) {
- for (i = 0; i < argc; i++) {
- v = newstringobject(argv[i]);
- if (v == NULL) {
- DECREF(av);
- av = NULL;
- break;
- }
- setlistitem(av, i, v);
- }
- }
- if (av == NULL)
- fatal("no mem for sys.argv");
- return av;
-}
-
-/* sys.exit method */
-
-static object *
sys_exit(self, args)
object *self;
object *args;
@@ -104,88 +76,115 @@ sys_exit(self, args)
/* NOTREACHED */
}
+static struct methodlist sys_methods[] = {
+ {"exit", sys_exit},
+ {NULL, NULL} /* sentinel */
+};
+
static object *sysin, *sysout, *syserr;
void
-initsys(argc, argv)
- int argc;
- char **argv;
+initsys()
{
- object *v;
- object *exit;
- if ((sysdict = newdictobject()) == NULL)
- fatal("can't create sys dict");
+ object *m = initmodule("sys", sys_methods);
+ sysdict = getmoduledict(m);
+ INCREF(sysdict);
/* NB keep an extra ref to the std files to avoid closing them
when the user deletes them */
+ /* XXX File objects should have a "don't close" flag instead */
sysin = newopenfileobject(stdin, "<stdin>", "r");
sysout = newopenfileobject(stdout, "<stdout>", "w");
syserr = newopenfileobject(stderr, "<stderr>", "w");
- v = makeargv(argc, argv);
- exit = newmethodobject("exit", sys_exit, (object *)NULL);
if (err_occurred())
- fatal("can't create sys.* objects");
+ fatal("can't create sys.std* file objects");
dictinsert(sysdict, "stdin", sysin);
dictinsert(sysdict, "stdout", sysout);
dictinsert(sysdict, "stderr", syserr);
- dictinsert(sysdict, "argv", v);
- dictinsert(sysdict, "exit", exit);
+ dictinsert(sysdict, "modules", get_modules());
if (err_occurred())
fatal("can't insert sys.* objects in sys dict");
- DECREF(exit);
- DECREF(v);
- /* The other symbols are added elsewhere */
-
- /* Only now can we initialize the import stuff, after which
- we can turn ourselves into a module */
- initimport();
- if ((v = new_module("sys")) == NULL)
- fatal("can't create sys module");
- if (setmoduledict(v, sysdict) != 0)
- fatal("can't assign sys dict to sys module");
- DECREF(v);
}
-static void
-cleardict(d)
- object *d;
+static object *
+makepathobject(path, delim)
+ char *path;
+ int delim;
{
- int i;
- for (i = getdictsize(d); --i >= 0; ) {
- char *k;
- k = getdictkey(d, i);
- if (k != NULL) {
- (void) dictremove(d, k);
+ int i, n;
+ char *p;
+ object *v, *w;
+
+ n = 1;
+ p = path;
+ while ((p = strchr(p, delim)) != NULL) {
+ n++;
+ p++;
+ }
+ v = newlistobject(n);
+ if (v == NULL)
+ return NULL;
+ for (i = 0; ; i++) {
+ p = strchr(path, delim);
+ if (p == NULL)
+ p = strchr(path, '\0'); /* End of string */
+ w = newsizedstringobject(path, (int) (p - path));
+ if (w == NULL) {
+ DECREF(v);
+ return NULL;
}
+ setlistitem(v, i, w);
+ if (*p == '\0')
+ break;
+ path = p+1;
}
+ return v;
}
void
-closesys()
+setpythonpath(path)
+ char *path;
{
- object *modules;
- modules = sysget("modules");
- if (modules != NULL && is_dictobject(modules)) {
+ object *v;
+ if ((v = makepathobject(path, DELIM)) == NULL)
+ fatal("can't create sys.path");
+ if (sysset("path", v) != 0)
+ fatal("can't assign sys.path");
+ DECREF(v);
+}
+
+static object *
+makeargvobject(argc, argv)
+ int argc;
+ char **argv;
+{
+ object *av;
+ if (argc < 0 || argv == NULL)
+ argc = 0;
+ av = newlistobject(argc);
+ if (av != NULL) {
int i;
- /* Explicitly erase all modules; this is the safest way
- to get rid of at least *some* circular dependencies */
- INCREF(modules);
- for (i = getdictsize(modules); --i >= 0; ) {
- char *k;
- k = getdictkey(modules, i);
- if (k != NULL) {
- object *m;
- m = dictlookup(modules, k);
- if (m != NULL && is_moduleobject(m)) {
- object *d;
- d = getmoduledict(m);
- if (d != NULL && is_dictobject(d)) {
- cleardict(d);
- }
- }
+ for (i = 0; i < argc; i++) {
+ object *v = newstringobject(argv[i]);
+ if (v == NULL) {
+ DECREF(av);
+ av = NULL;
+ break;
}
+ setlistitem(av, i, v);
}
- cleardict(modules);
- DECREF(modules);
}
- DECREF(sysdict);
+ return av;
+}
+
+void
+setpythonargv(argc, argv)
+ int argc;
+ char **argv;
+{
+ object *av = makeargvobject(argc, argv);
+ if (av == NULL)
+ fatal("no mem for sys.argv");
+ if (sysset("argv", av) != 0)
+ fatal("can't assign sys.argv");
+ DECREF(av);
}