Mercurial > cpython-lru_cache-weakref
annotate Python/pythonmain.c @ 74:bc244df20251 legacy-trunk
Add extra DECREF.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Sun, 18 Nov 1990 17:36:26 +0000 |
| parents | a36c348a8e1a |
| children | de34f4a4d353 |
| 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? */ | |
| 47 | |
| 48 initsys(argc-1, argv+1); | |
| 49 inittime(); | |
| 50 initmath(); | |
| 51 | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
52 setpythonpath(getpythonpath()); |
| 6 | 53 |
| 54 initrun(); | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
55 initcalls(); |
| 6 | 56 |
| 57 if (!isatty(fileno(fp))) { | |
| 58 ret = runfile(fp, file_input, (char *)NULL, (char *)NULL); | |
| 59 } | |
| 60 else { | |
| 61 sysset("ps1", newstringobject(">>> ")); | |
| 62 sysset("ps2", newstringobject("... ")); | |
| 63 for (;;) { | |
| 64 object *v = sysget("ps1"), *w = sysget("ps2"); | |
| 65 char *ps1 = NULL, *ps2 = NULL; | |
| 66 if (v != NULL && is_stringobject(v)) { | |
| 67 INCREF(v); | |
| 68 ps1 = getstringvalue(v); | |
| 69 } | |
| 70 else | |
| 71 v = NULL; | |
| 72 if (w != NULL && is_stringobject(w)) { | |
| 73 INCREF(w); | |
| 74 ps2 = getstringvalue(w); | |
| 75 } | |
| 76 else | |
| 77 w = NULL; | |
| 78 ret = runfile(fp, single_input, ps1, ps2); | |
| 79 if (v != NULL) | |
| 80 DECREF(v); | |
| 81 if (w != NULL) | |
| 82 DECREF(w); | |
| 83 if (ret == E_EOF || ret == E_NOMEM) | |
| 84 break; | |
| 85 } | |
| 86 } | |
| 87 goaway(ret == E_DONE || ret == E_EOF ? 0 : 1); | |
| 88 /*NOTREACHED*/ | |
| 89 } | |
| 90 | |
| 91 goaway(sts) | |
| 92 int sts; | |
| 93 { | |
| 94 #ifdef THINK_C | |
| 95 if (sts == 0) | |
| 96 Click_On(0); | |
| 97 #endif | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
98 closerun(); |
|
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
99 donecalls(); |
| 6 | 100 exit(sts); |
| 101 /*NOTREACHED*/ | |
| 102 } | |
| 103 | |
| 104 /* Parse input from a file and execute it */ | |
| 105 | |
| 106 static int | |
| 107 runfile(fp, start, ps1, ps2) | |
| 108 FILE *fp; | |
| 109 int start; | |
| 110 char *ps1, *ps2; | |
| 111 { | |
| 112 node *n; | |
| 113 int ret; | |
| 114 ret = parsefile(fp, &gram, start, ps1, ps2, &n); | |
| 115 if (ret != E_DONE) | |
| 116 return ret; | |
| 117 return execute(n) == 0 ? E_DONE : E_ERROR; | |
| 118 } | |
| 119 | |
| 120 /* Ask a yes/no question */ | |
| 121 | |
| 122 int | |
| 123 askyesno(prompt) | |
| 124 char *prompt; | |
| 125 { | |
| 126 char buf[256]; | |
| 127 | |
| 128 printf("%s [ny] ", prompt); | |
| 129 if (fgets(buf, sizeof buf, stdin) == NULL) | |
| 130 return 0; | |
| 131 return buf[0] == 'y' || buf[0] == 'Y'; | |
| 132 } | |
| 133 | |
|
70
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
134 #ifdef THINK_C |
|
a36c348a8e1a
Moved configuration-dependent initializations (etc.) to config_*.c
Guido van Rossum <guido@python.org>
parents:
16
diff
changeset
|
135 |
| 6 | 136 /* Check for file descriptor connected to interactive device. |
| 137 Pretend that stdin is always interactive, other files never. */ | |
| 138 | |
| 139 int | |
| 140 isatty(fd) | |
| 141 int fd; | |
| 142 { | |
| 143 return fd == fileno(stdin); | |
| 144 } | |
| 145 | |
| 146 #endif | |
| 147 | |
| 148 /* WISH LIST | |
| 149 | |
| 150 - improved per-module error handling; different use of errno | |
| 151 - possible new types: | |
| 152 - iterator (for range, keys, ...) | |
| 153 - improve interpreter error handling, e.g., true tracebacks | |
| 154 - release parse trees when no longer needed (make them objects?) | |
| 155 - faster parser (for long modules) | |
| 156 - save precompiled modules on file? | |
| 157 - fork threads, locking | |
| 158 - allow syntax extensions | |
| 159 */ |
