Changeset 388 for python/vendor/current/Parser/myreadline.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Parser/myreadline.c
r2 r388 40 40 my_fgets(char *buf, int len, FILE *fp) 41 41 { 42 char *p; 43 for (;;) { 44 if (PyOS_InputHook != NULL) 45 (void)(PyOS_InputHook)(); 46 errno = 0; 47 p = fgets(buf, len, fp); 48 if (p != NULL) 49 return 0; /* No error */ 42 char *p; 50 43 #ifdef MS_WINDOWS 51 /* In the case of a Ctrl+C or some other external event 52 interrupting the operation: 53 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32 54 error code (and feof() returns TRUE). 55 Win9x: Ctrl+C seems to have no effect on fgets() returning 56 early - the signal handler is called, but the fgets() 57 only returns "normally" (ie, when Enter hit or feof()) 58 */ 59 if (GetLastError()==ERROR_OPERATION_ABORTED) { 60 /* Signals come asynchronously, so we sleep a brief 61 moment before checking if the handler has been 62 triggered (we cant just return 1 before the 63 signal handler has been called, as the later 64 signal may be treated as a separate interrupt). 65 */ 66 Sleep(1); 67 if (PyOS_InterruptOccurred()) { 68 return 1; /* Interrupt */ 69 } 70 /* Either the sleep wasn't long enough (need a 71 short loop retrying?) or not interrupted at all 72 (in which case we should revisit the whole thing!) 73 Logging some warning would be nice. assert is not 74 viable as under the debugger, the various dialogs 75 mean the condition is not true. 76 */ 77 } 44 int i; 45 #endif 46 47 while (1) { 48 if (PyOS_InputHook != NULL) 49 (void)(PyOS_InputHook)(); 50 errno = 0; 51 clearerr(fp); 52 p = fgets(buf, len, fp); 53 if (p != NULL) 54 return 0; /* No error */ 55 #ifdef MS_WINDOWS 56 /* Ctrl-C anywhere on the line or Ctrl-Z if the only character 57 on a line will set ERROR_OPERATION_ABORTED. Under normal 58 circumstances Ctrl-C will also have caused the SIGINT handler 59 to fire. This signal fires in another thread and is not 60 guaranteed to have occurred before this point in the code. 61 62 Therefore: check in a small loop to see if the trigger has 63 fired, in which case assume this is a Ctrl-C event. If it 64 hasn't fired within 10ms assume that this is a Ctrl-Z on its 65 own or that the signal isn't going to fire for some other 66 reason and drop through to check for EOF. 67 */ 68 if (GetLastError()==ERROR_OPERATION_ABORTED) { 69 for (i = 0; i < 10; i++) { 70 if (PyOS_InterruptOccurred()) 71 return 1; 72 Sleep(1); 73 } 74 } 78 75 #endif /* MS_WINDOWS */ 79 if (feof(fp)) { 80 return -1; /* EOF */ 81 } 76 if (feof(fp)) { 77 clearerr(fp); 78 return -1; /* EOF */ 79 } 82 80 #ifdef EINTR 83 if (errno == EINTR) { 84 int s; 85 #ifdef WITH_THREAD 86 PyEval_RestoreThread(_PyOS_ReadlineTState); 87 #endif 88 s = PyErr_CheckSignals(); 89 #ifdef WITH_THREAD 90 PyEval_SaveThread(); 91 #endif 92 if (s < 0) { 93 return 1; 94 } 95 } 96 #endif 97 if (PyOS_InterruptOccurred()) { 98 return 1; /* Interrupt */ 99 } 100 return -2; /* Error */ 101 } 102 /* NOTREACHED */ 81 if (errno == EINTR) { 82 int s; 83 #ifdef WITH_THREAD 84 PyEval_RestoreThread(_PyOS_ReadlineTState); 85 #endif 86 s = PyErr_CheckSignals(); 87 #ifdef WITH_THREAD 88 PyEval_SaveThread(); 89 #endif 90 if (s < 0) 91 return 1; 92 /* try again */ 93 continue; 94 } 95 #endif 96 if (PyOS_InterruptOccurred()) { 97 return 1; /* Interrupt */ 98 } 99 return -2; /* Error */ 100 } 101 /* NOTREACHED */ 103 102 } 104 103 … … 109 108 PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) 110 109 { 111 112 113 114 115 116 110 size_t n; 111 char *p; 112 n = 100; 113 if ((p = (char *)PyMem_MALLOC(n)) == NULL) 114 return NULL; 115 fflush(sys_stdout); 117 116 #ifndef RISCOS 118 119 117 if (prompt) 118 fprintf(stderr, "%s", prompt); 120 119 #else 121 122 123 124 125 126 127 #endif 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 120 if (prompt) { 121 if(Py_RISCOSWimpFlag) 122 fprintf(stderr, "\x0cr%s\x0c", prompt); 123 else 124 fprintf(stderr, "%s", prompt); 125 } 126 #endif 127 fflush(stderr); 128 switch (my_fgets(p, (int)n, sys_stdin)) { 129 case 0: /* Normal case */ 130 break; 131 case 1: /* Interrupt */ 132 PyMem_FREE(p); 133 return NULL; 134 case -1: /* EOF */ 135 case -2: /* Error */ 136 default: /* Shouldn't happen */ 137 *p = '\0'; 138 break; 139 } 140 n = strlen(p); 141 while (n > 0 && p[n-1] != '\n') { 142 size_t incr = n+2; 143 p = (char *)PyMem_REALLOC(p, n + incr); 144 if (p == NULL) 145 return NULL; 146 if (incr > INT_MAX) { 147 PyErr_SetString(PyExc_OverflowError, "input line too long"); 148 } 149 if (my_fgets(p+n, (int)incr, sys_stdin) != 0) 150 break; 151 n += strlen(p+n); 152 } 153 return (char *)PyMem_REALLOC(p, n+1); 155 154 } 156 155 … … 169 168 PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) 170 169 { 171 172 173 174 175 176 177 178 179 180 170 char *rv; 171 172 if (_PyOS_ReadlineTState == PyThreadState_GET()) { 173 PyErr_SetString(PyExc_RuntimeError, 174 "can't re-enter readline"); 175 return NULL; 176 } 177 178 179 if (PyOS_ReadlineFunctionPointer == NULL) { 181 180 #ifdef __VMS 182 181 PyOS_ReadlineFunctionPointer = vms__StdioReadline; 183 182 #else 184 185 #endif 186 187 188 #ifdef WITH_THREAD 189 190 _PyOS_ReadlineLock = PyThread_allocate_lock(); 191 192 #endif 193 194 195 196 #ifdef WITH_THREAD 197 198 #endif 199 200 201 202 203 204 205 206 207 208 209 210 211 212 #ifdef WITH_THREAD 213 214 #endif 215 216 217 218 183 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; 184 #endif 185 } 186 187 #ifdef WITH_THREAD 188 if (_PyOS_ReadlineLock == NULL) { 189 _PyOS_ReadlineLock = PyThread_allocate_lock(); 190 } 191 #endif 192 193 _PyOS_ReadlineTState = PyThreadState_GET(); 194 Py_BEGIN_ALLOW_THREADS 195 #ifdef WITH_THREAD 196 PyThread_acquire_lock(_PyOS_ReadlineLock, 1); 197 #endif 198 199 /* This is needed to handle the unlikely case that the 200 * interpreter is in interactive mode *and* stdin/out are not 201 * a tty. This can happen, for example if python is run like 202 * this: python -i < test1.py 203 */ 204 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout))) 205 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt); 206 else 207 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, 208 prompt); 209 Py_END_ALLOW_THREADS 210 211 #ifdef WITH_THREAD 212 PyThread_release_lock(_PyOS_ReadlineLock); 213 #endif 214 215 _PyOS_ReadlineTState = NULL; 216 217 return rv; 219 218 }
Note:
See TracChangeset
for help on using the changeset viewer.