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.
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
14 #define WIN32_LEAN_AND_MEAN
16 #endif /* MS_WINDOWS */
19 extern char* vms__StdioReadline(FILE *sys_stdin
, FILE *sys_stdout
, char *prompt
);
23 PyThreadState
* _PyOS_ReadlineTState
;
27 static PyThread_type_lock _PyOS_ReadlineLock
= NULL
;
30 int (*PyOS_InputHook
)(void) = NULL
;
32 /* This function restarts a fgets() after an EINTR error occurred
33 except if PyOS_InterruptOccurred() returns true. */
36 my_fgets(char *buf
, int len
, FILE *fp
)
40 if (PyOS_InputHook
!= NULL
)
41 (void)(PyOS_InputHook
)();
43 p
= fgets(buf
, len
, fp
);
45 return 0; /* No error */
47 /* In the case of a Ctrl+C or some other external event
48 interrupting the operation:
49 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
50 error code (and feof() returns TRUE).
51 Win9x: Ctrl+C seems to have no effect on fgets() returning
52 early - the signal handler is called, but the fgets()
53 only returns "normally" (ie, when Enter hit or feof())
55 if (GetLastError()==ERROR_OPERATION_ABORTED
) {
56 /* Signals come asynchronously, so we sleep a brief
57 moment before checking if the handler has been
58 triggered (we cant just return 1 before the
59 signal handler has been called, as the later
60 signal may be treated as a separate interrupt).
63 if (PyOS_InterruptOccurred()) {
64 return 1; /* Interrupt */
66 /* Either the sleep wasn't long enough (need a
67 short loop retrying?) or not interrupted at all
68 (in which case we should revisit the whole thing!)
69 Logging some warning would be nice. assert is not
70 viable as under the debugger, the various dialogs
71 mean the condition is not true.
74 #endif /* MS_WINDOWS */
82 PyEval_RestoreThread(_PyOS_ReadlineTState
);
84 s
= PyErr_CheckSignals();
93 if (PyOS_InterruptOccurred()) {
94 return 1; /* Interrupt */
96 return -2; /* Error */
102 /* Readline implementation using fgets() */
105 PyOS_StdioReadline(FILE *sys_stdin
, FILE *sys_stdout
, char *prompt
)
110 if ((p
= (char *)PyMem_MALLOC(n
)) == NULL
)
114 fprintf(stderr
, "%s", prompt
);
116 switch (my_fgets(p
, (int)n
, sys_stdin
)) {
117 case 0: /* Normal case */
119 case 1: /* Interrupt */
124 default: /* Shouldn't happen */
129 while (n
> 0 && p
[n
-1] != '\n') {
131 p
= (char *)PyMem_REALLOC(p
, n
+ incr
);
134 if (incr
> INT_MAX
) {
135 PyErr_SetString(PyExc_OverflowError
, "input line too long");
137 if (my_fgets(p
+n
, (int)incr
, sys_stdin
) != 0)
141 return (char *)PyMem_REALLOC(p
, n
+1);
145 /* By initializing this function pointer, systems embedding Python can
146 override the readline function.
148 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
150 char *(*PyOS_ReadlineFunctionPointer
)(FILE *, FILE *, char *);
153 /* Interface used by tokenizer.c and bltinmodule.c */
156 PyOS_Readline(FILE *sys_stdin
, FILE *sys_stdout
, char *prompt
)
160 if (_PyOS_ReadlineTState
== PyThreadState_GET()) {
161 PyErr_SetString(PyExc_RuntimeError
,
162 "can't re-enter readline");
167 if (PyOS_ReadlineFunctionPointer
== NULL
) {
169 PyOS_ReadlineFunctionPointer
= vms__StdioReadline
;
171 PyOS_ReadlineFunctionPointer
= PyOS_StdioReadline
;
176 if (_PyOS_ReadlineLock
== NULL
) {
177 _PyOS_ReadlineLock
= PyThread_allocate_lock();
181 _PyOS_ReadlineTState
= PyThreadState_GET();
182 Py_BEGIN_ALLOW_THREADS
184 PyThread_acquire_lock(_PyOS_ReadlineLock
, 1);
187 /* This is needed to handle the unlikely case that the
188 * interpreter is in interactive mode *and* stdin/out are not
189 * a tty. This can happen, for example if python is run like
190 * this: python -i < test1.py
192 if (!isatty (fileno (sys_stdin
)) || !isatty (fileno (sys_stdout
)))
193 rv
= PyOS_StdioReadline (sys_stdin
, sys_stdout
, prompt
);
195 rv
= (*PyOS_ReadlineFunctionPointer
)(sys_stdin
, sys_stdout
,
200 PyThread_release_lock(_PyOS_ReadlineLock
);
203 _PyOS_ReadlineTState
= NULL
;