1
2 /* Readline interface for the tokenizer 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.
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
12 #include "Python.h"
13 #include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
14 #include "pycore_pystate.h" // _PyThreadState_GET()
15 #ifdef MS_WINDOWS
16 # ifndef WIN32_LEAN_AND_MEAN
17 # define WIN32_LEAN_AND_MEAN
18 # endif
19 # include "windows.h"
20 #endif /* MS_WINDOWS */
21
22 #ifdef HAVE_UNISTD_H
23 # include <unistd.h> // isatty()
24 #endif
25
26
27 // Export the symbol since it's used by the readline shared extension
28 PyAPI_DATA(PyThreadState*) _PyOS_ReadlineTState;
29 PyThreadState *_PyOS_ReadlineTState = NULL;
30
31 static PyMutex _PyOS_ReadlineLock;
32
33 int (*PyOS_InputHook)(void) = NULL;
34
35 /* This function restarts a fgets() after an EINTR error occurred
36 except if _PyOS_InterruptOccurred() returns true. */
37
38 static int
my_fgets(PyThreadState * tstate,char * buf,int len,FILE * fp)39 my_fgets(PyThreadState* tstate, char *buf, int len, FILE *fp)
40 {
41 #ifdef MS_WINDOWS
42 HANDLE handle;
43 _Py_BEGIN_SUPPRESS_IPH
44 handle = (HANDLE)_get_osfhandle(fileno(fp));
45 _Py_END_SUPPRESS_IPH
46
47 /* bpo-40826: fgets(fp) does crash if fileno(fp) is closed */
48 if (handle == INVALID_HANDLE_VALUE) {
49 return -1; /* EOF */
50 }
51 #endif
52
53 while (1) {
54 if (PyOS_InputHook != NULL &&
55 // GH-104668: See PyOS_ReadlineFunctionPointer's comment below...
56 _Py_IsMainInterpreter(tstate->interp))
57 {
58 (void)(PyOS_InputHook)();
59 }
60
61 errno = 0;
62 clearerr(fp);
63 char *p = fgets(buf, len, fp);
64 if (p != NULL) {
65 return 0; /* No error */
66 }
67 int err = errno;
68
69 #ifdef MS_WINDOWS
70 /* Ctrl-C anywhere on the line or Ctrl-Z if the only character
71 on a line will set ERROR_OPERATION_ABORTED. Under normal
72 circumstances Ctrl-C will also have caused the SIGINT handler
73 to fire which will have set the event object returned by
74 _PyOS_SigintEvent. This signal fires in another thread and
75 is not guaranteed to have occurred before this point in the
76 code.
77
78 Therefore: check whether the event is set with a small timeout.
79 If it is, assume this is a Ctrl-C and reset the event. If it
80 isn't set assume that this is a Ctrl-Z on its own and drop
81 through to check for EOF.
82 */
83 if (GetLastError()==ERROR_OPERATION_ABORTED) {
84 HANDLE hInterruptEvent = _PyOS_SigintEvent();
85 switch (WaitForSingleObjectEx(hInterruptEvent, 10, FALSE)) {
86 case WAIT_OBJECT_0:
87 ResetEvent(hInterruptEvent);
88 return 1; /* Interrupt */
89 case WAIT_FAILED:
90 return -2; /* Error */
91 }
92 }
93 #endif /* MS_WINDOWS */
94
95 if (feof(fp)) {
96 clearerr(fp);
97 return -1; /* EOF */
98 }
99
100 #ifdef EINTR
101 if (err == EINTR) {
102 PyEval_RestoreThread(tstate);
103 int s = PyErr_CheckSignals();
104 PyEval_SaveThread();
105
106 if (s < 0) {
107 return 1;
108 }
109 /* try again */
110 continue;
111 }
112 #endif
113
114 if (_PyOS_InterruptOccurred(tstate)) {
115 return 1; /* Interrupt */
116 }
117 return -2; /* Error */
118 }
119 /* NOTREACHED */
120 }
121
122 #ifdef HAVE_WINDOWS_CONSOLE_IO
123 /* Readline implementation using ReadConsoleW */
124
125 extern char _get_console_type(HANDLE handle);
126
127 char *
_PyOS_WindowsConsoleReadline(PyThreadState * tstate,HANDLE hStdIn)128 _PyOS_WindowsConsoleReadline(PyThreadState *tstate, HANDLE hStdIn)
129 {
130 static wchar_t wbuf_local[1024 * 16];
131 const DWORD chunk_size = 1024;
132
133 DWORD n_read, total_read, wbuflen, u8len;
134 wchar_t *wbuf;
135 char *buf = NULL;
136 int err = 0;
137
138 n_read = (DWORD)-1;
139 total_read = 0;
140 wbuf = wbuf_local;
141 wbuflen = sizeof(wbuf_local) / sizeof(wbuf_local[0]) - 1;
142 while (1) {
143 if (PyOS_InputHook != NULL &&
144 // GH-104668: See PyOS_ReadlineFunctionPointer's comment below...
145 _Py_IsMainInterpreter(tstate->interp))
146 {
147 (void)(PyOS_InputHook)();
148 }
149 if (!ReadConsoleW(hStdIn, &wbuf[total_read], wbuflen - total_read, &n_read, NULL)) {
150 err = GetLastError();
151 goto exit;
152 }
153 if (n_read == (DWORD)-1 && (err = GetLastError()) == ERROR_OPERATION_ABORTED) {
154 break;
155 }
156 if (n_read == 0) {
157 int s;
158 err = GetLastError();
159 if (err != ERROR_OPERATION_ABORTED)
160 goto exit;
161 err = 0;
162 HANDLE hInterruptEvent = _PyOS_SigintEvent();
163 if (WaitForSingleObjectEx(hInterruptEvent, 100, FALSE)
164 == WAIT_OBJECT_0) {
165 ResetEvent(hInterruptEvent);
166 PyEval_RestoreThread(tstate);
167 s = PyErr_CheckSignals();
168 PyEval_SaveThread();
169 if (s < 0) {
170 goto exit;
171 }
172 }
173 break;
174 }
175
176 total_read += n_read;
177 if (total_read == 0 || wbuf[total_read - 1] == L'\n') {
178 break;
179 }
180 wbuflen += chunk_size;
181 if (wbuf == wbuf_local) {
182 wbuf[total_read] = '\0';
183 wbuf = (wchar_t*)PyMem_RawMalloc(wbuflen * sizeof(wchar_t));
184 if (wbuf) {
185 wcscpy_s(wbuf, wbuflen, wbuf_local);
186 }
187 else {
188 PyEval_RestoreThread(tstate);
189 PyErr_NoMemory();
190 PyEval_SaveThread();
191 goto exit;
192 }
193 }
194 else {
195 wchar_t *tmp = PyMem_RawRealloc(wbuf, wbuflen * sizeof(wchar_t));
196 if (tmp == NULL) {
197 PyEval_RestoreThread(tstate);
198 PyErr_NoMemory();
199 PyEval_SaveThread();
200 goto exit;
201 }
202 wbuf = tmp;
203 }
204 }
205
206 if (wbuf[0] == '\x1a') {
207 buf = PyMem_RawMalloc(1);
208 if (buf) {
209 buf[0] = '\0';
210 }
211 else {
212 PyEval_RestoreThread(tstate);
213 PyErr_NoMemory();
214 PyEval_SaveThread();
215 }
216 goto exit;
217 }
218
219 u8len = WideCharToMultiByte(CP_UTF8, 0,
220 wbuf, total_read,
221 NULL, 0,
222 NULL, NULL);
223 buf = PyMem_RawMalloc(u8len + 1);
224 if (buf == NULL) {
225 PyEval_RestoreThread(tstate);
226 PyErr_NoMemory();
227 PyEval_SaveThread();
228 goto exit;
229 }
230
231 u8len = WideCharToMultiByte(CP_UTF8, 0,
232 wbuf, total_read,
233 buf, u8len,
234 NULL, NULL);
235 buf[u8len] = '\0';
236
237 exit:
238 if (wbuf != wbuf_local) {
239 PyMem_RawFree(wbuf);
240 }
241
242 if (err) {
243 PyEval_RestoreThread(tstate);
244 PyErr_SetFromWindowsErr(err);
245 PyEval_SaveThread();
246 }
247 return buf;
248 }
249
250 #endif /* HAVE_WINDOWS_CONSOLE_IO */
251
252
253 /* Readline implementation using fgets() */
254
255 char *
PyOS_StdioReadline(FILE * sys_stdin,FILE * sys_stdout,const char * prompt)256 PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
257 {
258 size_t n;
259 char *p, *pr;
260 PyThreadState *tstate = _PyOS_ReadlineTState;
261 assert(tstate != NULL);
262
263 #ifdef HAVE_WINDOWS_CONSOLE_IO
264 const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
265 if (!config->legacy_windows_stdio && sys_stdin == stdin) {
266 HANDLE hStdIn, hStdErr;
267
268 hStdIn = _Py_get_osfhandle_noraise(fileno(sys_stdin));
269 hStdErr = _Py_get_osfhandle_noraise(fileno(stderr));
270
271 if (_get_console_type(hStdIn) == 'r') {
272 fflush(sys_stdout);
273 if (prompt) {
274 if (_get_console_type(hStdErr) == 'w') {
275 wchar_t *wbuf;
276 int wlen;
277 wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
278 NULL, 0);
279 if (wlen) {
280 wbuf = PyMem_RawMalloc(wlen * sizeof(wchar_t));
281 if (wbuf == NULL) {
282 PyEval_RestoreThread(tstate);
283 PyErr_NoMemory();
284 PyEval_SaveThread();
285 return NULL;
286 }
287 wlen = MultiByteToWideChar(CP_UTF8, 0, prompt, -1,
288 wbuf, wlen);
289 if (wlen) {
290 DWORD n;
291 fflush(stderr);
292 /* wlen includes null terminator, so subtract 1 */
293 WriteConsoleW(hStdErr, wbuf, wlen - 1, &n, NULL);
294 }
295 PyMem_RawFree(wbuf);
296 }
297 } else {
298 fprintf(stderr, "%s", prompt);
299 fflush(stderr);
300 }
301 }
302 clearerr(sys_stdin);
303 return _PyOS_WindowsConsoleReadline(tstate, hStdIn);
304 }
305 }
306 #endif
307
308 fflush(sys_stdout);
309 if (prompt) {
310 fprintf(stderr, "%s", prompt);
311 }
312 fflush(stderr);
313
314 n = 0;
315 p = NULL;
316 do {
317 size_t incr = (n > 0) ? n + 2 : 100;
318 if (incr > INT_MAX) {
319 PyMem_RawFree(p);
320 PyEval_RestoreThread(tstate);
321 PyErr_SetString(PyExc_OverflowError, "input line too long");
322 PyEval_SaveThread();
323 return NULL;
324 }
325 pr = (char *)PyMem_RawRealloc(p, n + incr);
326 if (pr == NULL) {
327 PyMem_RawFree(p);
328 PyEval_RestoreThread(tstate);
329 PyErr_NoMemory();
330 PyEval_SaveThread();
331 return NULL;
332 }
333 p = pr;
334 int err = my_fgets(tstate, p + n, (int)incr, sys_stdin);
335 if (err == 1) {
336 // Interrupt
337 PyMem_RawFree(p);
338 return NULL;
339 } else if (err != 0) {
340 // EOF or error
341 p[n] = '\0';
342 break;
343 }
344 n += strlen(p + n);
345 } while (p[n-1] != '\n');
346
347 pr = (char *)PyMem_RawRealloc(p, n+1);
348 if (pr == NULL) {
349 PyMem_RawFree(p);
350 PyEval_RestoreThread(tstate);
351 PyErr_NoMemory();
352 PyEval_SaveThread();
353 return NULL;
354 }
355 return pr;
356 }
357
358
359 /* By initializing this function pointer, systems embedding Python can
360 override the readline function.
361
362 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
363
364 char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, const char *) = NULL;
365
366
367 /* Interface used by file_tokenizer.c and bltinmodule.c */
368
369 char *
PyOS_Readline(FILE * sys_stdin,FILE * sys_stdout,const char * prompt)370 PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt)
371 {
372 char *rv, *res;
373 size_t len;
374
375 PyThreadState *tstate = _PyThreadState_GET();
376 if (_Py_atomic_load_ptr_relaxed(&_PyOS_ReadlineTState) == tstate) {
377 PyErr_SetString(PyExc_RuntimeError,
378 "can't re-enter readline");
379 return NULL;
380 }
381
382 // GH-123321: We need to acquire the lock before setting
383 // _PyOS_ReadlineTState, otherwise the variable may be nullified by a
384 // different thread.
385 Py_BEGIN_ALLOW_THREADS
386 PyMutex_Lock(&_PyOS_ReadlineLock);
387 _Py_atomic_store_ptr_relaxed(&_PyOS_ReadlineTState, tstate);
388 if (PyOS_ReadlineFunctionPointer == NULL) {
389 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
390 }
391
392 /* This is needed to handle the unlikely case that the
393 * interpreter is in interactive mode *and* stdin/out are not
394 * a tty. This can happen, for example if python is run like
395 * this: python -i < test1.py
396 */
397 if (!isatty(fileno(sys_stdin)) || !isatty(fileno(sys_stdout)) ||
398 // GH-104668: Don't call global callbacks like PyOS_InputHook or
399 // PyOS_ReadlineFunctionPointer from subinterpreters, since it seems
400 // like there's no good way for users (like readline and tkinter) to
401 // avoid using global state to manage them. Plus, we generally don't
402 // want to cause trouble for libraries that don't know/care about
403 // subinterpreter support. If libraries really need better APIs that
404 // work per-interpreter and have ways to access module state, we can
405 // certainly add them later (but for now we'll cross our fingers and
406 // hope that nobody actually cares):
407 !_Py_IsMainInterpreter(tstate->interp))
408 {
409 rv = PyOS_StdioReadline(sys_stdin, sys_stdout, prompt);
410 }
411 else {
412 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout, prompt);
413 }
414
415 // gh-123321: Must set the variable and then release the lock before
416 // taking the GIL. Otherwise a deadlock or segfault may occur.
417 _Py_atomic_store_ptr_relaxed(&_PyOS_ReadlineTState, NULL);
418 PyMutex_Unlock(&_PyOS_ReadlineLock);
419 Py_END_ALLOW_THREADS
420
421 if (rv == NULL)
422 return NULL;
423
424 len = strlen(rv) + 1;
425 res = PyMem_Malloc(len);
426 if (res != NULL) {
427 memcpy(res, rv, len);
428 }
429 else {
430 PyErr_NoMemory();
431 }
432 PyMem_RawFree(rv);
433
434 return res;
435 }
436