Mercurial > cpython-lru_cache-weakref
comparison Python/sysmodule.c @ 6:fc6fcd7df4f7 legacy-trunk
Initial revision
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Sun, 14 Oct 1990 12:07:46 +0000 |
| parents | |
| children | f6bcf3c1d714 |
comparison
equal
deleted
inserted
replaced
| 5:5570dbb1ce55 | 6:fc6fcd7df4f7 |
|---|---|
| 1 /* System module */ | |
| 2 | |
| 3 /* | |
| 4 Various bits of information used by the interpreter are collected in | |
| 5 module 'sys'. | |
| 6 Data members: | |
| 7 - stdin, stdout, stderr: standard file objects | |
| 8 - ps1, ps2: primary and secondary prompts (strings) | |
| 9 - path: module search path (list of strings) | |
| 10 - modules: the table of modules (dictionary) | |
| 11 Function members: | |
| 12 - exit(sts): call exit() | |
| 13 */ | |
| 14 | |
| 15 #include <stdio.h> | |
| 16 | |
| 17 #include "PROTO.h" | |
| 18 #include "object.h" | |
| 19 #include "stringobject.h" | |
| 20 #include "listobject.h" | |
| 21 #include "dictobject.h" | |
| 22 #include "fileobject.h" | |
| 23 #include "moduleobject.h" | |
| 24 #include "sysmodule.h" | |
| 25 #include "node.h" /* For context.h */ | |
| 26 #include "context.h" /* For import.h */ | |
| 27 #include "import.h" | |
| 28 #include "methodobject.h" | |
| 29 #include "modsupport.h" | |
| 30 #include "errors.h" | |
| 31 | |
| 32 static object *sysdict; | |
| 33 | |
| 34 object * | |
| 35 sysget(name) | |
| 36 char *name; | |
| 37 { | |
| 38 return dictlookup(sysdict, name); | |
| 39 } | |
| 40 | |
| 41 FILE * | |
| 42 sysgetfile(name, def) | |
| 43 char *name; | |
| 44 FILE *def; | |
| 45 { | |
| 46 FILE *fp = NULL; | |
| 47 object *v = sysget(name); | |
| 48 if (v != NULL) | |
| 49 fp = getfilefile(v); | |
| 50 if (fp == NULL) | |
| 51 fp = def; | |
| 52 return fp; | |
| 53 } | |
| 54 | |
| 55 int | |
| 56 sysset(name, v) | |
| 57 char *name; | |
| 58 object *v; | |
| 59 { | |
| 60 if (v == NULL) | |
| 61 return dictremove(sysdict, name); | |
| 62 else | |
| 63 return dictinsert(sysdict, name, v); | |
| 64 } | |
| 65 | |
| 66 static object * | |
| 67 makeargv(argc, argv) | |
| 68 int argc; | |
| 69 char **argv; | |
| 70 { | |
| 71 int i; | |
| 72 object *av, *v; | |
| 73 if (argc < 0 || argv == NULL) | |
| 74 argc = 0; | |
| 75 av = newlistobject(argc); | |
| 76 if (av != NULL) { | |
| 77 for (i = 0; i < argc; i++) { | |
| 78 v = newstringobject(argv[i]); | |
| 79 if (v == NULL) { | |
| 80 DECREF(av); | |
| 81 av = NULL; | |
| 82 break; | |
| 83 } | |
| 84 setlistitem(av, i, v); | |
| 85 } | |
| 86 } | |
| 87 if (av == NULL) | |
| 88 fatal("no mem for sys.argv"); | |
| 89 return av; | |
| 90 } | |
| 91 | |
| 92 /* sys.exit method */ | |
| 93 | |
| 94 static object * | |
| 95 sys_exit(self, args) | |
| 96 object *self; | |
| 97 object *args; | |
| 98 { | |
| 99 int sts; | |
| 100 if (!getintarg(args, &sts)) | |
| 101 return NULL; | |
| 102 goaway(sts); | |
| 103 exit(sts); /* Just in case */ | |
| 104 /* NOTREACHED */ | |
| 105 } | |
| 106 | |
| 107 static object *sysin, *sysout, *syserr; | |
| 108 | |
| 109 void | |
| 110 initsys(argc, argv) | |
| 111 int argc; | |
| 112 char **argv; | |
| 113 { | |
| 114 object *v; | |
| 115 object *exit; | |
| 116 if ((sysdict = newdictobject()) == NULL) | |
| 117 fatal("can't create sys dict"); | |
| 118 /* NB keep an extra ref to the std files to avoid closing them | |
| 119 when the user deletes them */ | |
| 120 sysin = newopenfileobject(stdin, "<stdin>", "r"); | |
| 121 sysout = newopenfileobject(stdout, "<stdout>", "w"); | |
| 122 syserr = newopenfileobject(stderr, "<stderr>", "w"); | |
| 123 v = makeargv(argc, argv); | |
| 124 exit = newmethodobject("exit", sys_exit, (object *)NULL); | |
| 125 if (err_occurred()) | |
| 126 fatal("can't create sys.* objects"); | |
| 127 dictinsert(sysdict, "stdin", sysin); | |
| 128 dictinsert(sysdict, "stdout", sysout); | |
| 129 dictinsert(sysdict, "stderr", syserr); | |
| 130 dictinsert(sysdict, "argv", v); | |
| 131 dictinsert(sysdict, "exit", exit); | |
| 132 if (err_occurred()) | |
| 133 fatal("can't insert sys.* objects in sys dict"); | |
| 134 DECREF(v); | |
| 135 /* The other symbols are added elsewhere */ | |
| 136 | |
| 137 /* Only now can we initialize the import stuff, after which | |
| 138 we can turn ourselves into a module */ | |
| 139 initimport(); | |
| 140 if ((v = new_module("sys")) == NULL) | |
| 141 fatal("can't create sys module"); | |
| 142 if (setmoduledict(v, sysdict) != 0) | |
| 143 fatal("can't assign sys dict to sys module"); | |
| 144 } | |
| 145 | |
| 146 void | |
| 147 closesys() | |
| 148 { | |
| 149 object *mtab; | |
| 150 mtab = sysget("modules"); | |
| 151 if (mtab != NULL && is_dictobject(mtab)) | |
| 152 dictremove(mtab, "sys"); /* Get rid of recursion */ | |
| 153 else | |
| 154 fprintf(stderr, "[module sys not found]\n"); | |
| 155 DECREF(sysdict); | |
| 156 } |
