annotate Parser/myreadline.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents c4c5eeb07739
children e38335d4df91 5fcb0e0be89e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
2 /* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c.
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
3 By default, or when stdin is not a tty device, we have a super
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
4 simple my_readline function using fgets.
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
5 Optionally, we can use the GNU readline library.
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 my_readline() has a different return value from GNU readline():
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 - NULL if an interrupt occurred or if an error occurred
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 - a malloc'ed empty string if EOF was read
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 - a malloc'ed string ending in \n normally
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 */
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
9056
05181e104420 We now assume that PyOS_Readline() is called with the interpreter lock
Guido van Rossum <guido@python.org>
parents: 5973
diff changeset
12 #include "Python.h"
24234
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
13 #ifdef MS_WINDOWS
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
14 #define WIN32_LEAN_AND_MEAN
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
15 #include "windows.h"
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
16 #endif /* MS_WINDOWS */
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25924
diff changeset
18 #ifdef __VMS
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25924
diff changeset
19 extern char* vms__StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt);
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25924
diff changeset
20 #endif
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25924
diff changeset
21
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
22
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
23 PyThreadState* _PyOS_ReadlineTState;
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
24
32527
0da660e05e69 "#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD
Tim Peters <tim.peters@gmail.com>
parents: 32526
diff changeset
25 #ifdef WITH_THREAD
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
26 #include "pythread.h"
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
27 static PyThread_type_lock _PyOS_ReadlineLock = NULL;
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
28 #endif
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
29
13659
e78153e179fb Mass ANSIfication.
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
30 int (*PyOS_InputHook)(void) = NULL;
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
31
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
32 #ifdef RISCOS
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
33 int Py_RISCOSWimpFlag;
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
34 #endif
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
35
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
36 /* This function restarts a fgets() after an EINTR error occurred
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
37 except if PyOS_InterruptOccurred() returns true. */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
38
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
39 static int
13659
e78153e179fb Mass ANSIfication.
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
40 my_fgets(char *buf, int len, FILE *fp)
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
41 {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
42 char *p;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
43 for (;;) {
5973
e77847e1158e Renamed Py_input_hook to PyOS_InputHook.
Guido van Rossum <guido@python.org>
parents: 5948
diff changeset
44 if (PyOS_InputHook != NULL)
e77847e1158e Renamed Py_input_hook to PyOS_InputHook.
Guido van Rossum <guido@python.org>
parents: 5948
diff changeset
45 (void)(PyOS_InputHook)();
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
46 errno = 0;
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
47 p = fgets(buf, len, fp);
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
48 if (p != NULL)
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
49 return 0; /* No error */
24234
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
50 #ifdef MS_WINDOWS
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
51 /* In the case of a Ctrl+C or some other external event
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
52 interrupting the operation:
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
53 Win2k/NT: ERROR_OPERATION_ABORTED is the most recent Win32
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
54 error code (and feof() returns TRUE).
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
55 Win9x: Ctrl+C seems to have no effect on fgets() returning
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
56 early - the signal handler is called, but the fgets()
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
57 only returns "normally" (ie, when Enter hit or feof())
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
58 */
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
59 if (GetLastError()==ERROR_OPERATION_ABORTED) {
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
60 /* Signals come asynchronously, so we sleep a brief
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
61 moment before checking if the handler has been
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
62 triggered (we cant just return 1 before the
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
63 signal handler has been called, as the later
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
64 signal may be treated as a separate interrupt).
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
65 */
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
66 Sleep(1);
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
67 if (PyOS_InterruptOccurred()) {
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
68 return 1; /* Interrupt */
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
69 }
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
70 /* Either the sleep wasn't long enough (need a
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
71 short loop retrying?) or not interrupted at all
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
72 (in which case we should revisit the whole thing!)
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
73 Logging some warning would be nice. assert is not
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
74 viable as under the debugger, the various dialogs
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
75 mean the condition is not true.
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
76 */
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
77 }
c8917b59c065 Fix bug 439992 - [win32] KeyboardInterrupt Not Caught.
Mark Hammond <mhammond@skippinet.com.au>
parents: 16947
diff changeset
78 #endif /* MS_WINDOWS */
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
79 if (feof(fp)) {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
80 return -1; /* EOF */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
81 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
82 #ifdef EINTR
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
83 if (errno == EINTR) {
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
84 int s;
34878
b996747c08b6 Backport my recent raw_input() vs no threads build vs SIGINT argh:
Michael W. Hudson <mwh@python.net>
parents: 32549
diff changeset
85 #ifdef WITH_THREAD
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
86 PyEval_RestoreThread(_PyOS_ReadlineTState);
34878
b996747c08b6 Backport my recent raw_input() vs no threads build vs SIGINT argh:
Michael W. Hudson <mwh@python.net>
parents: 32549
diff changeset
87 #endif
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
88 s = PyErr_CheckSignals();
34878
b996747c08b6 Backport my recent raw_input() vs no threads build vs SIGINT argh:
Michael W. Hudson <mwh@python.net>
parents: 32549
diff changeset
89 #ifdef WITH_THREAD
32549
be1a12d0b895 PyThreadState_Swap(NULL) didn't do what I thought it did. Fixes
Michael W. Hudson <mwh@python.net>
parents: 32527
diff changeset
90 PyEval_SaveThread();
34878
b996747c08b6 Backport my recent raw_input() vs no threads build vs SIGINT argh:
Michael W. Hudson <mwh@python.net>
parents: 32549
diff changeset
91 #endif
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
92 if (s < 0) {
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
93 return 1;
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
94 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
95 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
96 #endif
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
97 if (PyOS_InterruptOccurred()) {
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
98 return 1; /* Interrupt */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
99 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
100 return -2; /* Error */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
101 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
102 /* NOTREACHED */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
103 }
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
104
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
105
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
106 /* Readline implementation using fgets() */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
107
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
108 char *
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
109 PyOS_StdioReadline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
110 {
13093
cbe02b8aee07 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
111 size_t n;
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
112 char *p;
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 n = 100;
37600
c4c5eeb07739 As discussed on python-dev, really fix the PyMem_*/PyObject_* memory API
Neal Norwitz <nnorwitz@gmail.com>
parents: 37585
diff changeset
114 if ((p = (char *)PyMem_MALLOC(n)) == NULL)
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 return NULL;
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
116 fflush(sys_stdout);
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
117 #ifndef RISCOS
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if (prompt)
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 fprintf(stderr, "%s", prompt);
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
120 #else
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
121 if (prompt) {
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
122 if(Py_RISCOSWimpFlag)
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
123 fprintf(stderr, "\x0cr%s\x0c", prompt);
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
124 else
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
125 fprintf(stderr, "%s", prompt);
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
126 }
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 14869
diff changeset
127 #endif
3332
95643c0b0c0e add sigrelse() call for SunOS 4.1; add some fflush() calls
Guido van Rossum <guido@python.org>
parents: 2060
diff changeset
128 fflush(stderr);
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
129 switch (my_fgets(p, (int)n, sys_stdin)) {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
130 case 0: /* Normal case */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
131 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
132 case 1: /* Interrupt */
37600
c4c5eeb07739 As discussed on python-dev, really fix the PyMem_*/PyObject_* memory API
Neal Norwitz <nnorwitz@gmail.com>
parents: 37585
diff changeset
133 PyMem_FREE(p);
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 return NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
135 case -1: /* EOF */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
136 case -2: /* Error */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
137 default: /* Shouldn't happen */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
138 *p = '\0';
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1452
diff changeset
139 break;
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 }
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 n = strlen(p);
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 while (n > 0 && p[n-1] != '\n') {
13093
cbe02b8aee07 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
143 size_t incr = n+2;
37600
c4c5eeb07739 As discussed on python-dev, really fix the PyMem_*/PyObject_* memory API
Neal Norwitz <nnorwitz@gmail.com>
parents: 37585
diff changeset
144 p = (char *)PyMem_REALLOC(p, n + incr);
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 if (p == NULL)
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 return NULL;
13093
cbe02b8aee07 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
147 if (incr > INT_MAX) {
cbe02b8aee07 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
148 PyErr_SetString(PyExc_OverflowError, "input line too long");
cbe02b8aee07 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
149 }
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
150 if (my_fgets(p+n, (int)incr, sys_stdin) != 0)
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 break;
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 n += strlen(p+n);
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 }
37600
c4c5eeb07739 As discussed on python-dev, really fix the PyMem_*/PyObject_* memory API
Neal Norwitz <nnorwitz@gmail.com>
parents: 37585
diff changeset
154 return (char *)PyMem_REALLOC(p, n+1);
1452
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 }
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
156
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
157
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
158 /* By initializing this function pointer, systems embedding Python can
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 9064
diff changeset
159 override the readline function.
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 9064
diff changeset
160
37600
c4c5eeb07739 As discussed on python-dev, really fix the PyMem_*/PyObject_* memory API
Neal Norwitz <nnorwitz@gmail.com>
parents: 37585
diff changeset
161 Note: Python expects in return a buffer allocated with PyMem_Malloc. */
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
162
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
163 char *(*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, char *);
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
164
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
165
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
166 /* Interface used by tokenizer.c and bltinmodule.c */
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
167
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
168 char *
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
169 PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
170 {
9064
0781a4999051 The previous fix was still broken; the Py_END_ALLOW_THREADS macro was
Guido van Rossum <guido@python.org>
parents: 9056
diff changeset
171 char *rv;
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
172
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
173 if (_PyOS_ReadlineTState == PyThreadState_GET()) {
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
174 PyErr_SetString(PyExc_RuntimeError,
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
175 "can't re-enter readline");
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
176 return NULL;
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
177 }
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
178
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
179
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
180 if (PyOS_ReadlineFunctionPointer == NULL) {
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25924
diff changeset
181 #ifdef __VMS
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25924
diff changeset
182 PyOS_ReadlineFunctionPointer = vms__StdioReadline;
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25924
diff changeset
183 #else
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
184 PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 25924
diff changeset
185 #endif
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
186 }
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
187
32527
0da660e05e69 "#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD
Tim Peters <tim.peters@gmail.com>
parents: 32526
diff changeset
188 #ifdef WITH_THREAD
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
189 if (_PyOS_ReadlineLock == NULL) {
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
190 _PyOS_ReadlineLock = PyThread_allocate_lock();
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
191 }
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
192 #endif
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
193
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
194 _PyOS_ReadlineTState = PyThreadState_GET();
9056
05181e104420 We now assume that PyOS_Readline() is called with the interpreter lock
Guido van Rossum <guido@python.org>
parents: 5973
diff changeset
195 Py_BEGIN_ALLOW_THREADS
32527
0da660e05e69 "#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD
Tim Peters <tim.peters@gmail.com>
parents: 32526
diff changeset
196 #ifdef WITH_THREAD
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
197 PyThread_acquire_lock(_PyOS_ReadlineLock, 1);
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
198 #endif
25924
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
199
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
200 /* This is needed to handle the unlikely case that the
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
201 * interpreter is in interactive mode *and* stdin/out are not
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
202 * a tty. This can happen, for example if python is run like
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
203 * this: python -i < test1.py
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
204 */
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
205 if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
206 rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
207 else
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
208 rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
dc4a0336a2a3 Patch #512981: Update readline input stream on sys.stdin/out change.
Martin v. Löwis <martin@v.loewis.de>
parents: 24234
diff changeset
209 prompt);
9056
05181e104420 We now assume that PyOS_Readline() is called with the interpreter lock
Guido van Rossum <guido@python.org>
parents: 5973
diff changeset
210 Py_END_ALLOW_THREADS
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
211
32527
0da660e05e69 "#if WITH_THREAD" is incorrect; must be #ifdef instead; WITH_THREAD
Tim Peters <tim.peters@gmail.com>
parents: 32526
diff changeset
212 #ifdef WITH_THREAD
32526
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
213 PyThread_release_lock(_PyOS_ReadlineLock);
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
214 #endif
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
215
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
216 _PyOS_ReadlineTState = NULL;
78bd25b42d46 This closes patch:
Michael W. Hudson <mwh@python.net>
parents: 30968
diff changeset
217
9064
0781a4999051 The previous fix was still broken; the Py_END_ALLOW_THREADS macro was
Guido van Rossum <guido@python.org>
parents: 9056
diff changeset
218 return rv;
5134
def03d3b8ddb Restructured quite a bit, hopefully Lee Busby will find this useful.
Guido van Rossum <guido@python.org>
parents: 4577
diff changeset
219 }