Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 2 | /* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c. |
| 3 | By default, or when stdin is not a tty device, we have a super |
| 4 | simple my_readline function using fgets. |
| 5 | Optionally, we can use the GNU readline library. |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 6 | my_readline() has a different return value from GNU readline(): |
| 7 | - NULL if an interrupt occurred or if an error occurred |
| 8 | - a malloc'ed empty string if EOF was read |
| 9 | - a malloc'ed string ending in \n normally |
| 10 | */ |
| 11 | |
Guido van Rossum | 8efa47b | 1998-08-27 19:43:43 +0000 | [diff] [blame] | 12 | #include "Python.h" |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 13 | #ifdef MS_WINDOWS |
| 14 | #define WIN32_LEAN_AND_MEAN |
| 15 | #include "windows.h" |
| 16 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 17 | |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 18 | |
| 19 | PyThreadState* _PyOS_ReadlineTState; |
| 20 | |
Tim Peters | b7e898a | 2004-07-07 20:42:07 +0000 | [diff] [blame] | 21 | #ifdef WITH_THREAD |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 22 | #include "pythread.h" |
| 23 | static PyThread_type_lock _PyOS_ReadlineLock = NULL; |
| 24 | #endif |
| 25 | |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 26 | int (*PyOS_InputHook)(void) = NULL; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 27 | |
| 28 | /* This function restarts a fgets() after an EINTR error occurred |
| 29 | except if PyOS_InterruptOccurred() returns true. */ |
| 30 | |
| 31 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 32 | my_fgets(char *buf, int len, FILE *fp) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 33 | { |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 34 | #ifdef MS_WINDOWS |
| 35 | HANDLE hInterruptEvent; |
| 36 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | char *p; |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 38 | int err; |
Victor Stinner | 52c950f | 2011-04-09 15:55:44 +0200 | [diff] [blame] | 39 | while (1) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 40 | if (PyOS_InputHook != NULL) |
| 41 | (void)(PyOS_InputHook)(); |
| 42 | errno = 0; |
Victor Stinner | 4f71101 | 2011-05-30 23:46:00 +0200 | [diff] [blame] | 43 | clearerr(fp); |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 44 | p = fgets(buf, len, fp); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 45 | if (p != NULL) |
| 46 | return 0; /* No error */ |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 47 | err = errno; |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 48 | #ifdef MS_WINDOWS |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 49 | /* Ctrl-C anywhere on the line or Ctrl-Z if the only character |
| 50 | on a line will set ERROR_OPERATION_ABORTED. Under normal |
| 51 | circumstances Ctrl-C will also have caused the SIGINT handler |
| 52 | to fire which will have set the event object returned by |
| 53 | _PyOS_SigintEvent. This signal fires in another thread and |
| 54 | is not guaranteed to have occurred before this point in the |
| 55 | code. |
| 56 | |
| 57 | Therefore: check whether the event is set with a small timeout. |
| 58 | If it is, assume this is a Ctrl-C and reset the event. If it |
| 59 | isn't set assume that this is a Ctrl-Z on its own and drop |
| 60 | through to check for EOF. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 62 | if (GetLastError()==ERROR_OPERATION_ABORTED) { |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 63 | hInterruptEvent = _PyOS_SigintEvent(); |
Martin v. Löwis | b26a9b1 | 2013-01-25 14:25:48 +0100 | [diff] [blame] | 64 | switch (WaitForSingleObjectEx(hInterruptEvent, 10, FALSE)) { |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 65 | case WAIT_OBJECT_0: |
| 66 | ResetEvent(hInterruptEvent); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 67 | return 1; /* Interrupt */ |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 68 | case WAIT_FAILED: |
| 69 | return -2; /* Error */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 70 | } |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 71 | } |
Mark Hammond | 2f10cb8 | 2002-07-14 23:12:29 +0000 | [diff] [blame] | 72 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 73 | if (feof(fp)) { |
Victor Stinner | 4755ab0 | 2011-05-10 00:19:53 +0200 | [diff] [blame] | 74 | clearerr(fp); |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 75 | return -1; /* EOF */ |
| 76 | } |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 77 | #ifdef EINTR |
Antoine Pitrou | c345ce1 | 2011-12-16 12:28:32 +0100 | [diff] [blame] | 78 | if (err == EINTR) { |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 79 | int s; |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 80 | #ifdef WITH_THREAD |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 81 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 82 | #endif |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 83 | s = PyErr_CheckSignals(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 84 | #ifdef WITH_THREAD |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 85 | PyEval_SaveThread(); |
Michael W. Hudson | e3afc59 | 2005-04-07 10:11:19 +0000 | [diff] [blame] | 86 | #endif |
Victor Stinner | 52c950f | 2011-04-09 15:55:44 +0200 | [diff] [blame] | 87 | if (s < 0) |
| 88 | return 1; |
Tim Golden | 9175c3d | 2012-06-29 18:39:26 +0100 | [diff] [blame] | 89 | /* try again */ |
Victor Stinner | 52c950f | 2011-04-09 15:55:44 +0200 | [diff] [blame] | 90 | continue; |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 91 | } |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 92 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | if (PyOS_InterruptOccurred()) { |
| 94 | return 1; /* Interrupt */ |
| 95 | } |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 96 | return -2; /* Error */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | } |
Antoine Pitrou | 7f14f0d | 2010-05-09 16:14:21 +0000 | [diff] [blame] | 98 | /* NOTREACHED */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 101 | #ifdef MS_WINDOWS |
| 102 | /* Readline implementation using ReadConsoleW */ |
| 103 | |
| 104 | extern char _get_console_type(HANDLE handle); |
| 105 | |
| 106 | char * |
| 107 | _PyOS_WindowsConsoleReadline(HANDLE hStdIn) |
| 108 | { |
| 109 | static wchar_t wbuf_local[1024 * 16]; |
| 110 | const DWORD chunk_size = 1024; |
| 111 | |
| 112 | DWORD n_read, total_read, wbuflen, u8len; |
| 113 | wchar_t *wbuf; |
| 114 | char *buf = NULL; |
| 115 | int err = 0; |
| 116 | |
| 117 | n_read = 0; |
| 118 | total_read = 0; |
| 119 | wbuf = wbuf_local; |
| 120 | wbuflen = sizeof(wbuf_local) / sizeof(wbuf_local[0]) - 1; |
| 121 | while (1) { |
| 122 | if (!ReadConsoleW(hStdIn, &wbuf[total_read], wbuflen - total_read, &n_read, NULL)) { |
| 123 | err = GetLastError(); |
| 124 | goto exit; |
| 125 | } |
| 126 | if (n_read == 0) { |
| 127 | int s; |
| 128 | err = GetLastError(); |
| 129 | if (err != ERROR_OPERATION_ABORTED) |
| 130 | goto exit; |
| 131 | err = 0; |
| 132 | HANDLE hInterruptEvent = _PyOS_SigintEvent(); |
| 133 | if (WaitForSingleObjectEx(hInterruptEvent, 100, FALSE) |
| 134 | == WAIT_OBJECT_0) { |
| 135 | ResetEvent(hInterruptEvent); |
| 136 | #ifdef WITH_THREAD |
| 137 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
| 138 | #endif |
| 139 | s = PyErr_CheckSignals(); |
| 140 | #ifdef WITH_THREAD |
| 141 | PyEval_SaveThread(); |
| 142 | #endif |
| 143 | if (s < 0) |
| 144 | goto exit; |
| 145 | } |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | total_read += n_read; |
| 150 | if (total_read == 0 || wbuf[total_read - 1] == L'\n') { |
| 151 | break; |
| 152 | } |
| 153 | wbuflen += chunk_size; |
| 154 | if (wbuf == wbuf_local) { |
| 155 | wbuf[total_read] = '\0'; |
| 156 | wbuf = (wchar_t*)PyMem_RawMalloc(wbuflen * sizeof(wchar_t)); |
| 157 | if (wbuf) |
| 158 | wcscpy_s(wbuf, wbuflen, wbuf_local); |
| 159 | } |
| 160 | else |
| 161 | wbuf = (wchar_t*)PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t)); |
| 162 | } |
| 163 | |
| 164 | if (wbuf[0] == '\x1a') { |
| 165 | buf = PyMem_RawMalloc(1); |
| 166 | if (buf) |
| 167 | buf[0] = '\0'; |
| 168 | goto exit; |
| 169 | } |
| 170 | |
| 171 | u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, NULL, 0, NULL, NULL); |
| 172 | buf = PyMem_RawMalloc(u8len + 1); |
| 173 | u8len = WideCharToMultiByte(CP_UTF8, 0, wbuf, total_read, buf, u8len, NULL, NULL); |
| 174 | buf[u8len] = '\0'; |
Benjamin Peterson | e2e792d | 2016-09-19 22:17:16 -0700 | [diff] [blame] | 175 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 176 | exit: |
| 177 | if (wbuf != wbuf_local) |
| 178 | PyMem_RawFree(wbuf); |
| 179 | |
| 180 | if (err) { |
| 181 | #ifdef WITH_THREAD |
| 182 | PyEval_RestoreThread(_PyOS_ReadlineTState); |
| 183 | #endif |
| 184 | PyErr_SetFromWindowsErr(err); |
| 185 | #ifdef WITH_THREAD |
| 186 | PyEval_SaveThread(); |
| 187 | #endif |
| 188 | } |
| 189 | |
| 190 | return buf; |
| 191 | } |
| 192 | |
| 193 | #endif |
| 194 | |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 195 | |
| 196 | /* Readline implementation using fgets() */ |
| 197 | |
| 198 | char * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 199 | PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 200 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | size_t n; |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 202 | char *p, *pr; |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 203 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 204 | #ifdef MS_WINDOWS |
| 205 | if (!Py_LegacyWindowsStdioFlag && sys_stdin == stdin) { |
Steve Dower | 3cd187b | 2016-10-08 12:18:16 -0700 | [diff] [blame] | 206 | HANDLE hStdIn, hStdErr; |
Benjamin Peterson | e2e792d | 2016-09-19 22:17:16 -0700 | [diff] [blame] | 207 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 208 | _Py_BEGIN_SUPPRESS_IPH |
| 209 | hStdIn = (HANDLE)_get_osfhandle(fileno(sys_stdin)); |
Steve Dower | 3cd187b | 2016-10-08 12:18:16 -0700 | [diff] [blame] | 210 | hStdErr = (HANDLE)_get_osfhandle(fileno(stderr)); |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 211 | _Py_END_SUPPRESS_IPH |
Benjamin Peterson | e2e792d | 2016-09-19 22:17:16 -0700 | [diff] [blame] | 212 | |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 213 | if (_get_console_type(hStdIn) == 'r') { |
| 214 | fflush(sys_stdout); |
Steve Dower | 3cd187b | 2016-10-08 12:18:16 -0700 | [diff] [blame] | 215 | if (prompt) { |
| 216 | if (_get_console_type(hStdErr) == 'w') { |
| 217 | wchar_t *wbuf; |
| 218 | int wlen; |
| 219 | wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1, |
| 220 | NULL, 0); |
Steve Dower | 59bd34f | 2016-10-08 12:20:45 -0700 | [diff] [blame^] | 221 | if (wlen && |
Steve Dower | 3cd187b | 2016-10-08 12:18:16 -0700 | [diff] [blame] | 222 | (wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t)))) { |
| 223 | wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1, |
| 224 | wbuf, wlen); |
| 225 | if (wlen) { |
| 226 | DWORD n; |
| 227 | fflush(stderr); |
| 228 | WriteConsoleW(hStdErr, wbuf, wlen, &n, NULL); |
| 229 | } |
| 230 | PyMem_RawFree(wbuf); |
| 231 | } |
| 232 | } else { |
| 233 | fprintf(stderr, "%s", prompt); |
| 234 | fflush(stderr); |
| 235 | } |
| 236 | } |
Steve Dower | 3929499 | 2016-08-30 21:22:36 -0700 | [diff] [blame] | 237 | clearerr(sys_stdin); |
| 238 | return _PyOS_WindowsConsoleReadline(hStdIn); |
| 239 | } |
| 240 | } |
| 241 | #endif |
| 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | n = 100; |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 244 | p = (char *)PyMem_RawMalloc(n); |
| 245 | if (p == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | return NULL; |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | fflush(sys_stdout); |
| 249 | if (prompt) |
| 250 | fprintf(stderr, "%s", prompt); |
| 251 | fflush(stderr); |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | switch (my_fgets(p, (int)n, sys_stdin)) { |
| 254 | case 0: /* Normal case */ |
| 255 | break; |
| 256 | case 1: /* Interrupt */ |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 257 | PyMem_RawFree(p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | return NULL; |
| 259 | case -1: /* EOF */ |
| 260 | case -2: /* Error */ |
| 261 | default: /* Shouldn't happen */ |
| 262 | *p = '\0'; |
| 263 | break; |
| 264 | } |
| 265 | n = strlen(p); |
| 266 | while (n > 0 && p[n-1] != '\n') { |
| 267 | size_t incr = n+2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | if (incr > INT_MAX) { |
Victor Stinner | c548660 | 2013-10-19 02:40:16 +0200 | [diff] [blame] | 269 | PyMem_RawFree(p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | PyErr_SetString(PyExc_OverflowError, "input line too long"); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 271 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | } |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 273 | pr = (char *)PyMem_RawRealloc(p, n + incr); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 274 | if (pr == NULL) { |
Victor Stinner | c548660 | 2013-10-19 02:40:16 +0200 | [diff] [blame] | 275 | PyMem_RawFree(p); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 276 | PyErr_NoMemory(); |
| 277 | return NULL; |
| 278 | } |
| 279 | p = pr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | if (my_fgets(p+n, (int)incr, sys_stdin) != 0) |
| 281 | break; |
| 282 | n += strlen(p+n); |
| 283 | } |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 284 | pr = (char *)PyMem_RawRealloc(p, n+1); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 285 | if (pr == NULL) { |
Victor Stinner | c548660 | 2013-10-19 02:40:16 +0200 | [diff] [blame] | 286 | PyMem_RawFree(p); |
Christian Heimes | 9ae513c | 2013-08-06 15:59:16 +0200 | [diff] [blame] | 287 | PyErr_NoMemory(); |
| 288 | return NULL; |
| 289 | } |
| 290 | return pr; |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
| 294 | /* By initializing this function pointer, systems embedding Python can |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 295 | override the readline function. |
| 296 | |
| 297 | Note: Python expects in return a buffer allocated with PyMem_Malloc. */ |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 298 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 299 | char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, const char *); |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 300 | |
| 301 | |
| 302 | /* Interface used by tokenizer.c and bltinmodule.c */ |
| 303 | |
| 304 | char * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 305 | PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) |
Guido van Rossum | fbd64c8 | 1997-02-18 21:53:32 +0000 | [diff] [blame] | 306 | { |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 307 | char *rv, *res; |
| 308 | size_t len; |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | if (_PyOS_ReadlineTState == PyThreadState_GET()) { |
| 311 | PyErr_SetString(PyExc_RuntimeError, |
| 312 | "can't re-enter readline"); |
| 313 | return NULL; |
| 314 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | |
| 317 | if (PyOS_ReadlineFunctionPointer == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | PyOS_ReadlineFunctionPointer = PyOS_StdioReadline; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 320 | |
Tim Peters | b7e898a | 2004-07-07 20:42:07 +0000 | [diff] [blame] | 321 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | if (_PyOS_ReadlineLock == NULL) { |
| 323 | _PyOS_ReadlineLock = PyThread_allocate_lock(); |
| 324 | } |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 325 | #endif |
| 326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | _PyOS_ReadlineTState = PyThreadState_GET(); |
| 328 | Py_BEGIN_ALLOW_THREADS |
| 329 | #ifdef WITH_THREAD |
| 330 | PyThread_acquire_lock(_PyOS_ReadlineLock, 1); |
| 331 | #endif |
Michael W. Hudson | 30ea2f2 | 2004-07-07 17:44:12 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | /* This is needed to handle the unlikely case that the |
| 334 | * interpreter is in interactive mode *and* stdin/out are not |
| 335 | * a tty. This can happen, for example if python is run like |
| 336 | * this: python -i < test1.py |
| 337 | */ |
| 338 | if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout))) |
| 339 | rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt); |
| 340 | else |
| 341 | rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, |
| 342 | prompt); |
| 343 | Py_END_ALLOW_THREADS |
| 344 | |
| 345 | #ifdef WITH_THREAD |
| 346 | PyThread_release_lock(_PyOS_ReadlineLock); |
| 347 | #endif |
| 348 | |
| 349 | _PyOS_ReadlineTState = NULL; |
| 350 | |
Victor Stinner | 2fe9bac | 2013-10-10 16:18:20 +0200 | [diff] [blame] | 351 | if (rv == NULL) |
| 352 | return NULL; |
| 353 | |
| 354 | len = strlen(rv) + 1; |
| 355 | res = PyMem_Malloc(len); |
| 356 | if (res != NULL) |
| 357 | memcpy(res, rv, len); |
| 358 | PyMem_RawFree(rv); |
| 359 | |
| 360 | return res; |
Guido van Rossum | 6fa6343 | 1993-12-24 10:36:57 +0000 | [diff] [blame] | 361 | } |