Mercurial > cpython-lru_cache-weakref
annotate Python/pythonmain.c @ 81:de34f4a4d353 legacy-trunk
Some extra DECREFs.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Sun, 18 Nov 1990 17:40:32 +0000 |
| parents | a36c348a8e1a |
| children | a6934380c6e7 |
| rev | line source |
|---|---|
| 6 | 1 /* Python interpreter main program */ |
| 2 | |
| 3 #include <stdio.h> | |
| 4 #include <ctype.h> | |
| 5 #include "string.h" | |
| 6 | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
7 extern char *getpythonpath(); |
| 6 | 8 |
| 9 #include "PROTO.h" | |
| 10 #include "grammar.h" | |
| 11 #include "node.h" | |
| 12 #include "parsetok.h" | |
| 13 #include "graminit.h" | |
| 14 #include "errcode.h" | |
| 15 #include "object.h" | |
| 16 #include "stringobject.h" | |
| 17 #include "sysmodule.h" | |
| 18 | |
| 19 extern grammar gram; /* From graminit.c */ | |
| 20 | |
| 21 int debugging; | |
| 22 | |
| 23 main(argc, argv) | |
| 24 int argc; | |
| 25 char **argv; | |
| 26 { | |
| 27 char *filename = NULL; | |
| 28 FILE *fp = stdin; | |
| 29 int ret; | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
30 |
|
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
31 initargs(&argc, &argv); |
| 6 | 32 |
| 33 initintr(); /* For intrcheck() */ | |
| 34 | |
| 35 if (argc > 1 && strcmp(argv[1], "-") != 0) | |
| 36 filename = argv[1]; | |
| 37 | |
| 38 if (filename != NULL) { | |
| 39 if ((fp = fopen(filename, "r")) == NULL) { | |
| 40 fprintf(stderr, "python: can't open file '%s'\n", | |
| 41 filename); | |
| 42 exit(2); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 /* XXX what is the ideal initialization order? */ | |
| 81 | 47 /* XXX exceptions are initialized by initrun but this |
| 48 may be too late */ | |
| 6 | 49 |
| 50 initsys(argc-1, argv+1); | |
| 51 inittime(); | |
| 52 initmath(); | |
| 53 | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
54 setpythonpath(getpythonpath()); |
| 6 | 55 |
| 56 initrun(); | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
57 initcalls(); |
| 6 | 58 |
| 59 if (!isatty(fileno(fp))) { | |
| 60 ret = runfile(fp, file_input, (char *)NULL, (char *)NULL); | |
| 61 } | |
| 62 else { | |
| 81 | 63 object *v, *w; |
| 64 sysset("ps1", v = newstringobject(">>> ")); | |
| 65 sysset("ps2", w = newstringobject("... ")); | |
| 66 DECREF(v); | |
| 67 DECREF(w); | |
| 6 | 68 for (;;) { |
| 69 char *ps1 = NULL, *ps2 = NULL; | |
| 81 | 70 v = sysget("ps1"); |
| 71 w = sysget("ps2"); | |
| 6 | 72 if (v != NULL && is_stringobject(v)) { |
| 73 INCREF(v); | |
| 74 ps1 = getstringvalue(v); | |
| 75 } | |
| 76 else | |
| 77 v = NULL; | |
| 78 if (w != NULL && is_stringobject(w)) { | |
| 79 INCREF(w); | |
| 80 ps2 = getstringvalue(w); | |
| 81 } | |
| 82 else | |
| 83 w = NULL; | |
| 84 ret = runfile(fp, single_input, ps1, ps2); | |
| 85 if (v != NULL) | |
| 86 DECREF(v); | |
| 87 if (w != NULL) | |
| 88 DECREF(w); | |
| 89 if (ret == E_EOF || ret == E_NOMEM) | |
| 90 break; | |
| 91 } | |
| 92 } | |
| 93 goaway(ret == E_DONE || ret == E_EOF ? 0 : 1); | |
| 94 /*NOTREACHED*/ | |
| 95 } | |
| 96 | |
| 97 goaway(sts) | |
| 98 int sts; | |
| 99 { | |
| 100 #ifdef THINK_C | |
| 101 if (sts == 0) | |
| 102 Click_On(0); | |
| 103 #endif | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
104 closerun(); |
|
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
105 donecalls(); |
| 6 | 106 exit(sts); |
| 107 /*NOTREACHED*/ | |
| 108 } | |
| 109 | |
| 110 /* Parse input from a file and execute it */ | |
| 111 | |
| 112 static int | |
| 113 runfile(fp, start, ps1, ps2) | |
| 114 FILE *fp; | |
| 115 int start; | |
| 116 char *ps1, *ps2; | |
| 117 { | |
| 118 node *n; | |
| 119 int ret; | |
| 120 ret = parsefile(fp, &gram, start, ps1, ps2, &n); | |
| 121 if (ret != E_DONE) | |
| 122 return ret; | |
| 123 return execute(n) == 0 ? E_DONE : E_ERROR; | |
| 124 } | |
| 125 | |
| 126 /* Ask a yes/no question */ | |
| 127 | |
| 128 int | |
| 129 askyesno(prompt) | |
| 130 char *prompt; | |
| 131 { | |
| 132 char buf[256]; | |
| 133 | |
| 134 printf("%s [ny] ", prompt); | |
| 135 if (fgets(buf, sizeof buf, stdin) == NULL) | |
| 136 return 0; | |
| 137 return buf[0] == 'y' || buf[0] == 'Y'; | |
| 138 } | |
| 139 | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
140 #ifdef THINK_C |
|
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
141 |
| 6 | 142 /* Check for file descriptor connected to interactive device. |
| 143 Pretend that stdin is always interactive, other files never. */ | |
| 144 | |
| 145 int | |
| 146 isatty(fd) | |
| 147 int fd; | |
| 148 { | |
| 149 return fd == fileno(stdin); | |
| 150 } | |
| 151 | |
| 152 #endif | |
| 153 | |
| 154 /* WISH LIST | |
| 155 | |
| 156 - improved per-module error handling; different use of errno | |
| 157 - possible new types: | |
| 158 - iterator (for range, keys, ...) | |
| 159 - improve interpreter error handling, e.g., true tracebacks | |
| 160 - release parse trees when no longer needed (make them objects?) | |
| 161 - faster parser (for long modules) | |
| 162 - save precompiled modules on file? | |
| 163 - fork threads, locking | |
| 164 - allow syntax extensions | |
| 165 */ |
