1 | n/a | /* |
---|
2 | n/a | An implementation of Windows console I/O |
---|
3 | n/a | |
---|
4 | n/a | Classes defined here: _WindowsConsoleIO |
---|
5 | n/a | |
---|
6 | n/a | Written by Steve Dower |
---|
7 | n/a | */ |
---|
8 | n/a | |
---|
9 | n/a | #define PY_SSIZE_T_CLEAN |
---|
10 | n/a | #include "Python.h" |
---|
11 | n/a | |
---|
12 | n/a | #ifdef MS_WINDOWS |
---|
13 | n/a | |
---|
14 | n/a | #include "structmember.h" |
---|
15 | n/a | #ifdef HAVE_SYS_TYPES_H |
---|
16 | n/a | #include <sys/types.h> |
---|
17 | n/a | #endif |
---|
18 | n/a | #ifdef HAVE_SYS_STAT_H |
---|
19 | n/a | #include <sys/stat.h> |
---|
20 | n/a | #endif |
---|
21 | n/a | #include <stddef.h> /* For offsetof */ |
---|
22 | n/a | |
---|
23 | n/a | #define WIN32_LEAN_AND_MEAN |
---|
24 | n/a | #include <windows.h> |
---|
25 | n/a | #include <fcntl.h> |
---|
26 | n/a | |
---|
27 | n/a | #include "_iomodule.h" |
---|
28 | n/a | |
---|
29 | n/a | /* BUFSIZ determines how many characters can be typed at the console |
---|
30 | n/a | before it starts blocking. */ |
---|
31 | n/a | #if BUFSIZ < (16*1024) |
---|
32 | n/a | #define SMALLCHUNK (2*1024) |
---|
33 | n/a | #elif (BUFSIZ >= (2 << 25)) |
---|
34 | n/a | #error "unreasonable BUFSIZ > 64MB defined" |
---|
35 | n/a | #else |
---|
36 | n/a | #define SMALLCHUNK BUFSIZ |
---|
37 | n/a | #endif |
---|
38 | n/a | |
---|
39 | n/a | /* BUFMAX determines how many bytes can be read in one go. */ |
---|
40 | n/a | #define BUFMAX (32*1024*1024) |
---|
41 | n/a | |
---|
42 | n/a | /* SMALLBUF determines how many utf-8 characters will be |
---|
43 | n/a | buffered within the stream, in order to support reads |
---|
44 | n/a | of less than one character */ |
---|
45 | n/a | #define SMALLBUF 4 |
---|
46 | n/a | |
---|
47 | n/a | char _get_console_type(HANDLE handle) { |
---|
48 | n/a | DWORD mode, peek_count; |
---|
49 | n/a | |
---|
50 | n/a | if (handle == INVALID_HANDLE_VALUE) |
---|
51 | n/a | return '\0'; |
---|
52 | n/a | |
---|
53 | n/a | if (!GetConsoleMode(handle, &mode)) |
---|
54 | n/a | return '\0'; |
---|
55 | n/a | |
---|
56 | n/a | /* Peek at the handle to see whether it is an input or output handle */ |
---|
57 | n/a | if (GetNumberOfConsoleInputEvents(handle, &peek_count)) |
---|
58 | n/a | return 'r'; |
---|
59 | n/a | return 'w'; |
---|
60 | n/a | } |
---|
61 | n/a | |
---|
62 | n/a | char _PyIO_get_console_type(PyObject *path_or_fd) { |
---|
63 | n/a | int fd = PyLong_AsLong(path_or_fd); |
---|
64 | n/a | PyErr_Clear(); |
---|
65 | n/a | if (fd >= 0) { |
---|
66 | n/a | HANDLE handle; |
---|
67 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
68 | n/a | handle = (HANDLE)_get_osfhandle(fd); |
---|
69 | n/a | _Py_END_SUPPRESS_IPH |
---|
70 | n/a | if (handle == INVALID_HANDLE_VALUE) |
---|
71 | n/a | return '\0'; |
---|
72 | n/a | return _get_console_type(handle); |
---|
73 | n/a | } |
---|
74 | n/a | |
---|
75 | n/a | PyObject *decoded; |
---|
76 | n/a | wchar_t *decoded_wstr; |
---|
77 | n/a | |
---|
78 | n/a | if (!PyUnicode_FSDecoder(path_or_fd, &decoded)) { |
---|
79 | n/a | PyErr_Clear(); |
---|
80 | n/a | return '\0'; |
---|
81 | n/a | } |
---|
82 | n/a | decoded_wstr = PyUnicode_AsWideCharString(decoded, NULL); |
---|
83 | n/a | Py_CLEAR(decoded); |
---|
84 | n/a | if (!decoded_wstr) { |
---|
85 | n/a | PyErr_Clear(); |
---|
86 | n/a | return '\0'; |
---|
87 | n/a | } |
---|
88 | n/a | |
---|
89 | n/a | char m = '\0'; |
---|
90 | n/a | if (!_wcsicmp(decoded_wstr, L"CONIN$")) { |
---|
91 | n/a | m = 'r'; |
---|
92 | n/a | } else if (!_wcsicmp(decoded_wstr, L"CONOUT$")) { |
---|
93 | n/a | m = 'w'; |
---|
94 | n/a | } else if (!_wcsicmp(decoded_wstr, L"CON")) { |
---|
95 | n/a | m = 'x'; |
---|
96 | n/a | } |
---|
97 | n/a | if (m) { |
---|
98 | n/a | PyMem_Free(decoded_wstr); |
---|
99 | n/a | return m; |
---|
100 | n/a | } |
---|
101 | n/a | |
---|
102 | n/a | DWORD length; |
---|
103 | n/a | wchar_t name_buf[MAX_PATH], *pname_buf = name_buf; |
---|
104 | n/a | |
---|
105 | n/a | length = GetFullPathNameW(decoded_wstr, MAX_PATH, pname_buf, NULL); |
---|
106 | n/a | if (length > MAX_PATH) { |
---|
107 | n/a | pname_buf = PyMem_New(wchar_t, length); |
---|
108 | n/a | if (pname_buf) |
---|
109 | n/a | length = GetFullPathNameW(decoded_wstr, length, pname_buf, NULL); |
---|
110 | n/a | else |
---|
111 | n/a | length = 0; |
---|
112 | n/a | } |
---|
113 | n/a | PyMem_Free(decoded_wstr); |
---|
114 | n/a | |
---|
115 | n/a | if (length) { |
---|
116 | n/a | wchar_t *name = pname_buf; |
---|
117 | n/a | if (length >= 4 && name[3] == L'\\' && |
---|
118 | n/a | (name[2] == L'.' || name[2] == L'?') && |
---|
119 | n/a | name[1] == L'\\' && name[0] == L'\\') { |
---|
120 | n/a | name += 4; |
---|
121 | n/a | } |
---|
122 | n/a | if (!_wcsicmp(name, L"CONIN$")) { |
---|
123 | n/a | m = 'r'; |
---|
124 | n/a | } else if (!_wcsicmp(name, L"CONOUT$")) { |
---|
125 | n/a | m = 'w'; |
---|
126 | n/a | } else if (!_wcsicmp(name, L"CON")) { |
---|
127 | n/a | m = 'x'; |
---|
128 | n/a | } |
---|
129 | n/a | } |
---|
130 | n/a | |
---|
131 | n/a | if (pname_buf != name_buf) |
---|
132 | n/a | PyMem_Free(pname_buf); |
---|
133 | n/a | return m; |
---|
134 | n/a | } |
---|
135 | n/a | |
---|
136 | n/a | |
---|
137 | n/a | /*[clinic input] |
---|
138 | n/a | module _io |
---|
139 | n/a | class _io._WindowsConsoleIO "winconsoleio *" "&PyWindowsConsoleIO_Type" |
---|
140 | n/a | [clinic start generated code]*/ |
---|
141 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=e897fdc1fba4e131]*/ |
---|
142 | n/a | |
---|
143 | n/a | /*[python input] |
---|
144 | n/a | class io_ssize_t_converter(CConverter): |
---|
145 | n/a | type = 'Py_ssize_t' |
---|
146 | n/a | converter = '_PyIO_ConvertSsize_t' |
---|
147 | n/a | [python start generated code]*/ |
---|
148 | n/a | /*[python end generated code: output=da39a3ee5e6b4b0d input=d0a811d3cbfd1b33]*/ |
---|
149 | n/a | |
---|
150 | n/a | typedef struct { |
---|
151 | n/a | PyObject_HEAD |
---|
152 | n/a | HANDLE handle; |
---|
153 | n/a | int fd; |
---|
154 | n/a | unsigned int created : 1; |
---|
155 | n/a | unsigned int readable : 1; |
---|
156 | n/a | unsigned int writable : 1; |
---|
157 | n/a | unsigned int closehandle : 1; |
---|
158 | n/a | char finalizing; |
---|
159 | n/a | unsigned int blksize; |
---|
160 | n/a | PyObject *weakreflist; |
---|
161 | n/a | PyObject *dict; |
---|
162 | n/a | char buf[SMALLBUF]; |
---|
163 | n/a | wchar_t wbuf; |
---|
164 | n/a | } winconsoleio; |
---|
165 | n/a | |
---|
166 | n/a | PyTypeObject PyWindowsConsoleIO_Type; |
---|
167 | n/a | |
---|
168 | n/a | _Py_IDENTIFIER(name); |
---|
169 | n/a | |
---|
170 | n/a | int |
---|
171 | n/a | _PyWindowsConsoleIO_closed(PyObject *self) |
---|
172 | n/a | { |
---|
173 | n/a | return ((winconsoleio *)self)->handle == INVALID_HANDLE_VALUE; |
---|
174 | n/a | } |
---|
175 | n/a | |
---|
176 | n/a | |
---|
177 | n/a | /* Returns 0 on success, -1 with exception set on failure. */ |
---|
178 | n/a | static int |
---|
179 | n/a | internal_close(winconsoleio *self) |
---|
180 | n/a | { |
---|
181 | n/a | if (self->handle != INVALID_HANDLE_VALUE) { |
---|
182 | n/a | if (self->closehandle) { |
---|
183 | n/a | if (self->fd >= 0) { |
---|
184 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
185 | n/a | close(self->fd); |
---|
186 | n/a | _Py_END_SUPPRESS_IPH |
---|
187 | n/a | } |
---|
188 | n/a | CloseHandle(self->handle); |
---|
189 | n/a | } |
---|
190 | n/a | self->handle = INVALID_HANDLE_VALUE; |
---|
191 | n/a | self->fd = -1; |
---|
192 | n/a | } |
---|
193 | n/a | return 0; |
---|
194 | n/a | } |
---|
195 | n/a | |
---|
196 | n/a | /*[clinic input] |
---|
197 | n/a | _io._WindowsConsoleIO.close |
---|
198 | n/a | |
---|
199 | n/a | Close the handle. |
---|
200 | n/a | |
---|
201 | n/a | A closed handle cannot be used for further I/O operations. close() may be |
---|
202 | n/a | called more than once without error. |
---|
203 | n/a | [clinic start generated code]*/ |
---|
204 | n/a | |
---|
205 | n/a | static PyObject * |
---|
206 | n/a | _io__WindowsConsoleIO_close_impl(winconsoleio *self) |
---|
207 | n/a | /*[clinic end generated code: output=27ef95b66c29057b input=185617e349ae4c7b]*/ |
---|
208 | n/a | { |
---|
209 | n/a | PyObject *res; |
---|
210 | n/a | PyObject *exc, *val, *tb; |
---|
211 | n/a | int rc; |
---|
212 | n/a | _Py_IDENTIFIER(close); |
---|
213 | n/a | res = _PyObject_CallMethodIdObjArgs((PyObject*)&PyRawIOBase_Type, |
---|
214 | n/a | &PyId_close, self, NULL); |
---|
215 | n/a | if (!self->closehandle) { |
---|
216 | n/a | self->handle = INVALID_HANDLE_VALUE; |
---|
217 | n/a | return res; |
---|
218 | n/a | } |
---|
219 | n/a | if (res == NULL) |
---|
220 | n/a | PyErr_Fetch(&exc, &val, &tb); |
---|
221 | n/a | rc = internal_close(self); |
---|
222 | n/a | if (res == NULL) |
---|
223 | n/a | _PyErr_ChainExceptions(exc, val, tb); |
---|
224 | n/a | if (rc < 0) |
---|
225 | n/a | Py_CLEAR(res); |
---|
226 | n/a | return res; |
---|
227 | n/a | } |
---|
228 | n/a | |
---|
229 | n/a | static PyObject * |
---|
230 | n/a | winconsoleio_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
231 | n/a | { |
---|
232 | n/a | winconsoleio *self; |
---|
233 | n/a | |
---|
234 | n/a | assert(type != NULL && type->tp_alloc != NULL); |
---|
235 | n/a | |
---|
236 | n/a | self = (winconsoleio *) type->tp_alloc(type, 0); |
---|
237 | n/a | if (self != NULL) { |
---|
238 | n/a | self->handle = INVALID_HANDLE_VALUE; |
---|
239 | n/a | self->fd = -1; |
---|
240 | n/a | self->created = 0; |
---|
241 | n/a | self->readable = 0; |
---|
242 | n/a | self->writable = 0; |
---|
243 | n/a | self->closehandle = 0; |
---|
244 | n/a | self->blksize = 0; |
---|
245 | n/a | self->weakreflist = NULL; |
---|
246 | n/a | } |
---|
247 | n/a | |
---|
248 | n/a | return (PyObject *) self; |
---|
249 | n/a | } |
---|
250 | n/a | |
---|
251 | n/a | /*[clinic input] |
---|
252 | n/a | _io._WindowsConsoleIO.__init__ |
---|
253 | n/a | file as nameobj: object |
---|
254 | n/a | mode: str = "r" |
---|
255 | n/a | closefd: int(c_default="1") = True |
---|
256 | n/a | opener: object = None |
---|
257 | n/a | |
---|
258 | n/a | Open a console buffer by file descriptor. |
---|
259 | n/a | |
---|
260 | n/a | The mode can be 'rb' (default), or 'wb' for reading or writing bytes. All |
---|
261 | n/a | other mode characters will be ignored. Mode 'b' will be assumed if it is |
---|
262 | n/a | omitted. The *opener* parameter is always ignored. |
---|
263 | n/a | [clinic start generated code]*/ |
---|
264 | n/a | |
---|
265 | n/a | static int |
---|
266 | n/a | _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj, |
---|
267 | n/a | const char *mode, int closefd, |
---|
268 | n/a | PyObject *opener) |
---|
269 | n/a | /*[clinic end generated code: output=3fd9cbcdd8d95429 input=61be39633a86f5d7]*/ |
---|
270 | n/a | { |
---|
271 | n/a | const char *s; |
---|
272 | n/a | wchar_t *name = NULL; |
---|
273 | n/a | char console_type = '\0'; |
---|
274 | n/a | int ret = 0; |
---|
275 | n/a | int rwa = 0; |
---|
276 | n/a | int fd = -1; |
---|
277 | n/a | int fd_is_own = 0; |
---|
278 | n/a | |
---|
279 | n/a | assert(PyWindowsConsoleIO_Check(self)); |
---|
280 | n/a | if (self->handle >= 0) { |
---|
281 | n/a | if (self->closehandle) { |
---|
282 | n/a | /* Have to close the existing file first. */ |
---|
283 | n/a | if (internal_close(self) < 0) |
---|
284 | n/a | return -1; |
---|
285 | n/a | } |
---|
286 | n/a | else |
---|
287 | n/a | self->handle = INVALID_HANDLE_VALUE; |
---|
288 | n/a | } |
---|
289 | n/a | |
---|
290 | n/a | if (PyFloat_Check(nameobj)) { |
---|
291 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
292 | n/a | "integer argument expected, got float"); |
---|
293 | n/a | return -1; |
---|
294 | n/a | } |
---|
295 | n/a | |
---|
296 | n/a | fd = _PyLong_AsInt(nameobj); |
---|
297 | n/a | if (fd < 0) { |
---|
298 | n/a | if (!PyErr_Occurred()) { |
---|
299 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
300 | n/a | "negative file descriptor"); |
---|
301 | n/a | return -1; |
---|
302 | n/a | } |
---|
303 | n/a | PyErr_Clear(); |
---|
304 | n/a | } |
---|
305 | n/a | self->fd = fd; |
---|
306 | n/a | |
---|
307 | n/a | if (fd < 0) { |
---|
308 | n/a | PyObject *decodedname = Py_None; |
---|
309 | n/a | Py_INCREF(decodedname); |
---|
310 | n/a | |
---|
311 | n/a | int d = PyUnicode_FSDecoder(nameobj, (void*)&decodedname); |
---|
312 | n/a | if (!d) |
---|
313 | n/a | return -1; |
---|
314 | n/a | |
---|
315 | n/a | Py_ssize_t length; |
---|
316 | n/a | name = PyUnicode_AsWideCharString(decodedname, &length); |
---|
317 | n/a | console_type = _PyIO_get_console_type(decodedname); |
---|
318 | n/a | Py_CLEAR(decodedname); |
---|
319 | n/a | if (name == NULL) |
---|
320 | n/a | return -1; |
---|
321 | n/a | |
---|
322 | n/a | if (wcslen(name) != length) { |
---|
323 | n/a | PyMem_Free(name); |
---|
324 | n/a | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
---|
325 | n/a | return -1; |
---|
326 | n/a | } |
---|
327 | n/a | } |
---|
328 | n/a | |
---|
329 | n/a | s = mode; |
---|
330 | n/a | while (*s) { |
---|
331 | n/a | switch (*s++) { |
---|
332 | n/a | case '+': |
---|
333 | n/a | case 'a': |
---|
334 | n/a | case 'b': |
---|
335 | n/a | case 'x': |
---|
336 | n/a | break; |
---|
337 | n/a | case 'r': |
---|
338 | n/a | if (rwa) |
---|
339 | n/a | goto bad_mode; |
---|
340 | n/a | rwa = 1; |
---|
341 | n/a | self->readable = 1; |
---|
342 | n/a | if (console_type == 'x') |
---|
343 | n/a | console_type = 'r'; |
---|
344 | n/a | break; |
---|
345 | n/a | case 'w': |
---|
346 | n/a | if (rwa) |
---|
347 | n/a | goto bad_mode; |
---|
348 | n/a | rwa = 1; |
---|
349 | n/a | self->writable = 1; |
---|
350 | n/a | if (console_type == 'x') |
---|
351 | n/a | console_type = 'w'; |
---|
352 | n/a | break; |
---|
353 | n/a | default: |
---|
354 | n/a | PyErr_Format(PyExc_ValueError, |
---|
355 | n/a | "invalid mode: %.200s", mode); |
---|
356 | n/a | goto error; |
---|
357 | n/a | } |
---|
358 | n/a | } |
---|
359 | n/a | |
---|
360 | n/a | if (!rwa) |
---|
361 | n/a | goto bad_mode; |
---|
362 | n/a | |
---|
363 | n/a | if (fd >= 0) { |
---|
364 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
365 | n/a | self->handle = (HANDLE)_get_osfhandle(fd); |
---|
366 | n/a | _Py_END_SUPPRESS_IPH |
---|
367 | n/a | self->closehandle = 0; |
---|
368 | n/a | } else { |
---|
369 | n/a | DWORD access = GENERIC_READ; |
---|
370 | n/a | |
---|
371 | n/a | self->closehandle = 1; |
---|
372 | n/a | if (!closefd) { |
---|
373 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
374 | n/a | "Cannot use closefd=False with file name"); |
---|
375 | n/a | goto error; |
---|
376 | n/a | } |
---|
377 | n/a | |
---|
378 | n/a | if (self->writable) |
---|
379 | n/a | access = GENERIC_WRITE; |
---|
380 | n/a | |
---|
381 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
382 | n/a | /* Attempt to open for read/write initially, then fall back |
---|
383 | n/a | on the specific access. This is required for modern names |
---|
384 | n/a | CONIN$ and CONOUT$, which allow reading/writing state as |
---|
385 | n/a | well as reading/writing content. */ |
---|
386 | n/a | self->handle = CreateFileW(name, GENERIC_READ | GENERIC_WRITE, |
---|
387 | n/a | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
---|
388 | n/a | if (self->handle == INVALID_HANDLE_VALUE) |
---|
389 | n/a | self->handle = CreateFileW(name, access, |
---|
390 | n/a | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
---|
391 | n/a | Py_END_ALLOW_THREADS |
---|
392 | n/a | |
---|
393 | n/a | if (self->handle == INVALID_HANDLE_VALUE) { |
---|
394 | n/a | PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, GetLastError(), nameobj); |
---|
395 | n/a | goto error; |
---|
396 | n/a | } |
---|
397 | n/a | } |
---|
398 | n/a | |
---|
399 | n/a | if (console_type == '\0') |
---|
400 | n/a | console_type = _get_console_type(self->handle); |
---|
401 | n/a | |
---|
402 | n/a | if (self->writable && console_type != 'w') { |
---|
403 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
404 | n/a | "Cannot open console input buffer for writing"); |
---|
405 | n/a | goto error; |
---|
406 | n/a | } |
---|
407 | n/a | if (self->readable && console_type != 'r') { |
---|
408 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
409 | n/a | "Cannot open console output buffer for reading"); |
---|
410 | n/a | goto error; |
---|
411 | n/a | } |
---|
412 | n/a | |
---|
413 | n/a | self->blksize = DEFAULT_BUFFER_SIZE; |
---|
414 | n/a | memset(self->buf, 0, 4); |
---|
415 | n/a | |
---|
416 | n/a | if (_PyObject_SetAttrId((PyObject *)self, &PyId_name, nameobj) < 0) |
---|
417 | n/a | goto error; |
---|
418 | n/a | |
---|
419 | n/a | goto done; |
---|
420 | n/a | |
---|
421 | n/a | bad_mode: |
---|
422 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
423 | n/a | "Must have exactly one of read or write mode"); |
---|
424 | n/a | error: |
---|
425 | n/a | ret = -1; |
---|
426 | n/a | internal_close(self); |
---|
427 | n/a | |
---|
428 | n/a | done: |
---|
429 | n/a | if (name) |
---|
430 | n/a | PyMem_Free(name); |
---|
431 | n/a | return ret; |
---|
432 | n/a | } |
---|
433 | n/a | |
---|
434 | n/a | static int |
---|
435 | n/a | winconsoleio_traverse(winconsoleio *self, visitproc visit, void *arg) |
---|
436 | n/a | { |
---|
437 | n/a | Py_VISIT(self->dict); |
---|
438 | n/a | return 0; |
---|
439 | n/a | } |
---|
440 | n/a | |
---|
441 | n/a | static int |
---|
442 | n/a | winconsoleio_clear(winconsoleio *self) |
---|
443 | n/a | { |
---|
444 | n/a | Py_CLEAR(self->dict); |
---|
445 | n/a | return 0; |
---|
446 | n/a | } |
---|
447 | n/a | |
---|
448 | n/a | static void |
---|
449 | n/a | winconsoleio_dealloc(winconsoleio *self) |
---|
450 | n/a | { |
---|
451 | n/a | self->finalizing = 1; |
---|
452 | n/a | if (_PyIOBase_finalize((PyObject *) self) < 0) |
---|
453 | n/a | return; |
---|
454 | n/a | _PyObject_GC_UNTRACK(self); |
---|
455 | n/a | if (self->weakreflist != NULL) |
---|
456 | n/a | PyObject_ClearWeakRefs((PyObject *) self); |
---|
457 | n/a | Py_CLEAR(self->dict); |
---|
458 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
---|
459 | n/a | } |
---|
460 | n/a | |
---|
461 | n/a | static PyObject * |
---|
462 | n/a | err_closed(void) |
---|
463 | n/a | { |
---|
464 | n/a | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
---|
465 | n/a | return NULL; |
---|
466 | n/a | } |
---|
467 | n/a | |
---|
468 | n/a | static PyObject * |
---|
469 | n/a | err_mode(const char *action) |
---|
470 | n/a | { |
---|
471 | n/a | _PyIO_State *state = IO_STATE(); |
---|
472 | n/a | if (state != NULL) |
---|
473 | n/a | PyErr_Format(state->unsupported_operation, |
---|
474 | n/a | "Console buffer does not support %s", action); |
---|
475 | n/a | return NULL; |
---|
476 | n/a | } |
---|
477 | n/a | |
---|
478 | n/a | /*[clinic input] |
---|
479 | n/a | _io._WindowsConsoleIO.fileno |
---|
480 | n/a | |
---|
481 | n/a | Return the underlying file descriptor (an integer). |
---|
482 | n/a | |
---|
483 | n/a | fileno is only set when a file descriptor is used to open |
---|
484 | n/a | one of the standard streams. |
---|
485 | n/a | |
---|
486 | n/a | [clinic start generated code]*/ |
---|
487 | n/a | |
---|
488 | n/a | static PyObject * |
---|
489 | n/a | _io__WindowsConsoleIO_fileno_impl(winconsoleio *self) |
---|
490 | n/a | /*[clinic end generated code: output=006fa74ce3b5cfbf input=079adc330ddaabe6]*/ |
---|
491 | n/a | { |
---|
492 | n/a | if (self->fd < 0 && self->handle != INVALID_HANDLE_VALUE) { |
---|
493 | n/a | _Py_BEGIN_SUPPRESS_IPH |
---|
494 | n/a | if (self->writable) |
---|
495 | n/a | self->fd = _open_osfhandle((intptr_t)self->handle, _O_WRONLY | _O_BINARY); |
---|
496 | n/a | else |
---|
497 | n/a | self->fd = _open_osfhandle((intptr_t)self->handle, _O_RDONLY | _O_BINARY); |
---|
498 | n/a | _Py_END_SUPPRESS_IPH |
---|
499 | n/a | } |
---|
500 | n/a | if (self->fd < 0) |
---|
501 | n/a | return err_mode("fileno"); |
---|
502 | n/a | return PyLong_FromLong(self->fd); |
---|
503 | n/a | } |
---|
504 | n/a | |
---|
505 | n/a | /*[clinic input] |
---|
506 | n/a | _io._WindowsConsoleIO.readable |
---|
507 | n/a | |
---|
508 | n/a | True if console is an input buffer. |
---|
509 | n/a | [clinic start generated code]*/ |
---|
510 | n/a | |
---|
511 | n/a | static PyObject * |
---|
512 | n/a | _io__WindowsConsoleIO_readable_impl(winconsoleio *self) |
---|
513 | n/a | /*[clinic end generated code: output=daf9cef2743becf0 input=6be9defb5302daae]*/ |
---|
514 | n/a | { |
---|
515 | n/a | if (self->handle == INVALID_HANDLE_VALUE) |
---|
516 | n/a | return err_closed(); |
---|
517 | n/a | return PyBool_FromLong((long) self->readable); |
---|
518 | n/a | } |
---|
519 | n/a | |
---|
520 | n/a | /*[clinic input] |
---|
521 | n/a | _io._WindowsConsoleIO.writable |
---|
522 | n/a | |
---|
523 | n/a | True if console is an output buffer. |
---|
524 | n/a | [clinic start generated code]*/ |
---|
525 | n/a | |
---|
526 | n/a | static PyObject * |
---|
527 | n/a | _io__WindowsConsoleIO_writable_impl(winconsoleio *self) |
---|
528 | n/a | /*[clinic end generated code: output=e0a2ad7eae5abf67 input=cefbd8abc24df6a0]*/ |
---|
529 | n/a | { |
---|
530 | n/a | if (self->handle == INVALID_HANDLE_VALUE) |
---|
531 | n/a | return err_closed(); |
---|
532 | n/a | return PyBool_FromLong((long) self->writable); |
---|
533 | n/a | } |
---|
534 | n/a | |
---|
535 | n/a | static DWORD |
---|
536 | n/a | _buflen(winconsoleio *self) |
---|
537 | n/a | { |
---|
538 | n/a | for (DWORD i = 0; i < SMALLBUF; ++i) { |
---|
539 | n/a | if (!self->buf[i]) |
---|
540 | n/a | return i; |
---|
541 | n/a | } |
---|
542 | n/a | return SMALLBUF; |
---|
543 | n/a | } |
---|
544 | n/a | |
---|
545 | n/a | static DWORD |
---|
546 | n/a | _copyfrombuf(winconsoleio *self, char *buf, DWORD len) |
---|
547 | n/a | { |
---|
548 | n/a | DWORD n = 0; |
---|
549 | n/a | |
---|
550 | n/a | while (self->buf[0] && len--) { |
---|
551 | n/a | buf[n++] = self->buf[0]; |
---|
552 | n/a | for (int i = 1; i < SMALLBUF; ++i) |
---|
553 | n/a | self->buf[i - 1] = self->buf[i]; |
---|
554 | n/a | self->buf[SMALLBUF - 1] = 0; |
---|
555 | n/a | } |
---|
556 | n/a | |
---|
557 | n/a | return n; |
---|
558 | n/a | } |
---|
559 | n/a | |
---|
560 | n/a | static wchar_t * |
---|
561 | n/a | read_console_w(HANDLE handle, DWORD maxlen, DWORD *readlen) { |
---|
562 | n/a | int err = 0, sig = 0; |
---|
563 | n/a | |
---|
564 | n/a | wchar_t *buf = (wchar_t*)PyMem_Malloc(maxlen * sizeof(wchar_t)); |
---|
565 | n/a | if (!buf) |
---|
566 | n/a | goto error; |
---|
567 | n/a | |
---|
568 | n/a | *readlen = 0; |
---|
569 | n/a | |
---|
570 | n/a | //DebugBreak(); |
---|
571 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
572 | n/a | DWORD off = 0; |
---|
573 | n/a | while (off < maxlen) { |
---|
574 | n/a | DWORD n, len = min(maxlen - off, BUFSIZ); |
---|
575 | n/a | SetLastError(0); |
---|
576 | n/a | BOOL res = ReadConsoleW(handle, &buf[off], len, &n, NULL); |
---|
577 | n/a | |
---|
578 | n/a | if (!res) { |
---|
579 | n/a | err = GetLastError(); |
---|
580 | n/a | break; |
---|
581 | n/a | } |
---|
582 | n/a | if (n == 0) { |
---|
583 | n/a | err = GetLastError(); |
---|
584 | n/a | if (err != ERROR_OPERATION_ABORTED) |
---|
585 | n/a | break; |
---|
586 | n/a | err = 0; |
---|
587 | n/a | HANDLE hInterruptEvent = _PyOS_SigintEvent(); |
---|
588 | n/a | if (WaitForSingleObjectEx(hInterruptEvent, 100, FALSE) |
---|
589 | n/a | == WAIT_OBJECT_0) { |
---|
590 | n/a | ResetEvent(hInterruptEvent); |
---|
591 | n/a | Py_BLOCK_THREADS |
---|
592 | n/a | sig = PyErr_CheckSignals(); |
---|
593 | n/a | Py_UNBLOCK_THREADS |
---|
594 | n/a | if (sig < 0) |
---|
595 | n/a | break; |
---|
596 | n/a | } |
---|
597 | n/a | } |
---|
598 | n/a | *readlen += n; |
---|
599 | n/a | |
---|
600 | n/a | /* If we didn't read a full buffer that time, don't try |
---|
601 | n/a | again or we will block a second time. */ |
---|
602 | n/a | if (n < len) |
---|
603 | n/a | break; |
---|
604 | n/a | /* If the buffer ended with a newline, break out */ |
---|
605 | n/a | if (buf[*readlen - 1] == '\n') |
---|
606 | n/a | break; |
---|
607 | n/a | /* If the buffer ends with a high surrogate, expand the |
---|
608 | n/a | buffer and read an extra character. */ |
---|
609 | n/a | WORD char_type; |
---|
610 | n/a | if (off + BUFSIZ >= maxlen && |
---|
611 | n/a | GetStringTypeW(CT_CTYPE3, &buf[*readlen - 1], 1, &char_type) && |
---|
612 | n/a | char_type == C3_HIGHSURROGATE) { |
---|
613 | n/a | wchar_t *newbuf; |
---|
614 | n/a | maxlen += 1; |
---|
615 | n/a | Py_BLOCK_THREADS |
---|
616 | n/a | newbuf = (wchar_t*)PyMem_Realloc(buf, maxlen * sizeof(wchar_t)); |
---|
617 | n/a | Py_UNBLOCK_THREADS |
---|
618 | n/a | if (!newbuf) { |
---|
619 | n/a | sig = -1; |
---|
620 | n/a | break; |
---|
621 | n/a | } |
---|
622 | n/a | buf = newbuf; |
---|
623 | n/a | /* Only advance by n and not BUFSIZ in this case */ |
---|
624 | n/a | off += n; |
---|
625 | n/a | continue; |
---|
626 | n/a | } |
---|
627 | n/a | |
---|
628 | n/a | off += BUFSIZ; |
---|
629 | n/a | } |
---|
630 | n/a | |
---|
631 | n/a | Py_END_ALLOW_THREADS |
---|
632 | n/a | |
---|
633 | n/a | if (sig) |
---|
634 | n/a | goto error; |
---|
635 | n/a | if (err) { |
---|
636 | n/a | PyErr_SetFromWindowsErr(err); |
---|
637 | n/a | goto error; |
---|
638 | n/a | } |
---|
639 | n/a | |
---|
640 | n/a | if (*readlen > 0 && buf[0] == L'\x1a') { |
---|
641 | n/a | PyMem_Free(buf); |
---|
642 | n/a | buf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t)); |
---|
643 | n/a | if (!buf) |
---|
644 | n/a | goto error; |
---|
645 | n/a | buf[0] = L'\0'; |
---|
646 | n/a | *readlen = 0; |
---|
647 | n/a | } |
---|
648 | n/a | |
---|
649 | n/a | return buf; |
---|
650 | n/a | |
---|
651 | n/a | error: |
---|
652 | n/a | if (buf) |
---|
653 | n/a | PyMem_Free(buf); |
---|
654 | n/a | return NULL; |
---|
655 | n/a | } |
---|
656 | n/a | |
---|
657 | n/a | |
---|
658 | n/a | static Py_ssize_t |
---|
659 | n/a | readinto(winconsoleio *self, char *buf, Py_ssize_t len) |
---|
660 | n/a | { |
---|
661 | n/a | if (self->handle == INVALID_HANDLE_VALUE) { |
---|
662 | n/a | err_closed(); |
---|
663 | n/a | return -1; |
---|
664 | n/a | } |
---|
665 | n/a | if (!self->readable) { |
---|
666 | n/a | err_mode("reading"); |
---|
667 | n/a | return -1; |
---|
668 | n/a | } |
---|
669 | n/a | if (len == 0) |
---|
670 | n/a | return 0; |
---|
671 | n/a | if (len > BUFMAX) { |
---|
672 | n/a | PyErr_Format(PyExc_ValueError, "cannot read more than %d bytes", BUFMAX); |
---|
673 | n/a | return -1; |
---|
674 | n/a | } |
---|
675 | n/a | |
---|
676 | n/a | /* Each character may take up to 4 bytes in the final buffer. |
---|
677 | n/a | This is highly conservative, but necessary to avoid |
---|
678 | n/a | failure for any given Unicode input (e.g. \U0010ffff). |
---|
679 | n/a | If the caller requests fewer than 4 bytes, we buffer one |
---|
680 | n/a | character. |
---|
681 | n/a | */ |
---|
682 | n/a | DWORD wlen = (DWORD)(len / 4); |
---|
683 | n/a | if (wlen == 0) { |
---|
684 | n/a | wlen = 1; |
---|
685 | n/a | } |
---|
686 | n/a | |
---|
687 | n/a | DWORD read_len = _copyfrombuf(self, buf, (DWORD)len); |
---|
688 | n/a | if (read_len) { |
---|
689 | n/a | buf = &buf[read_len]; |
---|
690 | n/a | len -= read_len; |
---|
691 | n/a | wlen -= 1; |
---|
692 | n/a | } |
---|
693 | n/a | if (len == read_len || wlen == 0) |
---|
694 | n/a | return read_len; |
---|
695 | n/a | |
---|
696 | n/a | DWORD n; |
---|
697 | n/a | wchar_t *wbuf = read_console_w(self->handle, wlen, &n); |
---|
698 | n/a | if (wbuf == NULL) |
---|
699 | n/a | return -1; |
---|
700 | n/a | if (n == 0) { |
---|
701 | n/a | PyMem_Free(wbuf); |
---|
702 | n/a | return read_len; |
---|
703 | n/a | } |
---|
704 | n/a | |
---|
705 | n/a | int err = 0; |
---|
706 | n/a | DWORD u8n = 0; |
---|
707 | n/a | |
---|
708 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
709 | n/a | if (len < 4) { |
---|
710 | n/a | if (WideCharToMultiByte(CP_UTF8, 0, wbuf, n, |
---|
711 | n/a | self->buf, sizeof(self->buf) / sizeof(self->buf[0]), |
---|
712 | n/a | NULL, NULL)) |
---|
713 | n/a | u8n = _copyfrombuf(self, buf, (DWORD)len); |
---|
714 | n/a | } else { |
---|
715 | n/a | u8n = WideCharToMultiByte(CP_UTF8, 0, wbuf, n, |
---|
716 | n/a | buf, (DWORD)len, NULL, NULL); |
---|
717 | n/a | } |
---|
718 | n/a | |
---|
719 | n/a | if (u8n) { |
---|
720 | n/a | read_len += u8n; |
---|
721 | n/a | u8n = 0; |
---|
722 | n/a | } else { |
---|
723 | n/a | err = GetLastError(); |
---|
724 | n/a | if (err == ERROR_INSUFFICIENT_BUFFER) { |
---|
725 | n/a | /* Calculate the needed buffer for a more useful error, as this |
---|
726 | n/a | means our "/ 4" logic above is insufficient for some input. |
---|
727 | n/a | */ |
---|
728 | n/a | u8n = WideCharToMultiByte(CP_UTF8, 0, wbuf, n, |
---|
729 | n/a | NULL, 0, NULL, NULL); |
---|
730 | n/a | } |
---|
731 | n/a | } |
---|
732 | n/a | Py_END_ALLOW_THREADS |
---|
733 | n/a | |
---|
734 | n/a | PyMem_Free(wbuf); |
---|
735 | n/a | |
---|
736 | n/a | if (u8n) { |
---|
737 | n/a | PyErr_Format(PyExc_SystemError, |
---|
738 | n/a | "Buffer had room for %d bytes but %d bytes required", |
---|
739 | n/a | len, u8n); |
---|
740 | n/a | return -1; |
---|
741 | n/a | } |
---|
742 | n/a | if (err) { |
---|
743 | n/a | PyErr_SetFromWindowsErr(err); |
---|
744 | n/a | return -1; |
---|
745 | n/a | } |
---|
746 | n/a | |
---|
747 | n/a | return read_len; |
---|
748 | n/a | } |
---|
749 | n/a | |
---|
750 | n/a | /*[clinic input] |
---|
751 | n/a | _io._WindowsConsoleIO.readinto |
---|
752 | n/a | buffer: Py_buffer(accept={rwbuffer}) |
---|
753 | n/a | / |
---|
754 | n/a | |
---|
755 | n/a | Same as RawIOBase.readinto(). |
---|
756 | n/a | [clinic start generated code]*/ |
---|
757 | n/a | |
---|
758 | n/a | static PyObject * |
---|
759 | n/a | _io__WindowsConsoleIO_readinto_impl(winconsoleio *self, Py_buffer *buffer) |
---|
760 | n/a | /*[clinic end generated code: output=66d1bdfa3f20af39 input=4ed68da48a6baffe]*/ |
---|
761 | n/a | { |
---|
762 | n/a | Py_ssize_t len = readinto(self, buffer->buf, buffer->len); |
---|
763 | n/a | if (len < 0) |
---|
764 | n/a | return NULL; |
---|
765 | n/a | |
---|
766 | n/a | return PyLong_FromSsize_t(len); |
---|
767 | n/a | } |
---|
768 | n/a | |
---|
769 | n/a | static DWORD |
---|
770 | n/a | new_buffersize(winconsoleio *self, DWORD currentsize) |
---|
771 | n/a | { |
---|
772 | n/a | DWORD addend; |
---|
773 | n/a | |
---|
774 | n/a | /* Expand the buffer by an amount proportional to the current size, |
---|
775 | n/a | giving us amortized linear-time behavior. For bigger sizes, use a |
---|
776 | n/a | less-than-double growth factor to avoid excessive allocation. */ |
---|
777 | n/a | if (currentsize > 65536) |
---|
778 | n/a | addend = currentsize >> 3; |
---|
779 | n/a | else |
---|
780 | n/a | addend = 256 + currentsize; |
---|
781 | n/a | if (addend < SMALLCHUNK) |
---|
782 | n/a | /* Avoid tiny read() calls. */ |
---|
783 | n/a | addend = SMALLCHUNK; |
---|
784 | n/a | return addend + currentsize; |
---|
785 | n/a | } |
---|
786 | n/a | |
---|
787 | n/a | /*[clinic input] |
---|
788 | n/a | _io._WindowsConsoleIO.readall |
---|
789 | n/a | |
---|
790 | n/a | Read all data from the console, returned as bytes. |
---|
791 | n/a | |
---|
792 | n/a | Return an empty bytes object at EOF. |
---|
793 | n/a | [clinic start generated code]*/ |
---|
794 | n/a | |
---|
795 | n/a | static PyObject * |
---|
796 | n/a | _io__WindowsConsoleIO_readall_impl(winconsoleio *self) |
---|
797 | n/a | /*[clinic end generated code: output=e6d312c684f6e23b input=4024d649a1006e69]*/ |
---|
798 | n/a | { |
---|
799 | n/a | wchar_t *buf; |
---|
800 | n/a | DWORD bufsize, n, len = 0; |
---|
801 | n/a | PyObject *bytes; |
---|
802 | n/a | DWORD bytes_size, rn; |
---|
803 | n/a | |
---|
804 | n/a | if (self->handle == INVALID_HANDLE_VALUE) |
---|
805 | n/a | return err_closed(); |
---|
806 | n/a | |
---|
807 | n/a | bufsize = BUFSIZ; |
---|
808 | n/a | |
---|
809 | n/a | buf = (wchar_t*)PyMem_Malloc((bufsize + 1) * sizeof(wchar_t)); |
---|
810 | n/a | if (buf == NULL) |
---|
811 | n/a | return NULL; |
---|
812 | n/a | |
---|
813 | n/a | while (1) { |
---|
814 | n/a | wchar_t *subbuf; |
---|
815 | n/a | |
---|
816 | n/a | if (len >= (Py_ssize_t)bufsize) { |
---|
817 | n/a | DWORD newsize = new_buffersize(self, len); |
---|
818 | n/a | if (newsize > BUFMAX) |
---|
819 | n/a | break; |
---|
820 | n/a | if (newsize < bufsize) { |
---|
821 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
822 | n/a | "unbounded read returned more bytes " |
---|
823 | n/a | "than a Python bytes object can hold"); |
---|
824 | n/a | PyMem_Free(buf); |
---|
825 | n/a | return NULL; |
---|
826 | n/a | } |
---|
827 | n/a | bufsize = newsize; |
---|
828 | n/a | |
---|
829 | n/a | buf = PyMem_Realloc(buf, (bufsize + 1) * sizeof(wchar_t)); |
---|
830 | n/a | if (!buf) { |
---|
831 | n/a | PyMem_Free(buf); |
---|
832 | n/a | return NULL; |
---|
833 | n/a | } |
---|
834 | n/a | } |
---|
835 | n/a | |
---|
836 | n/a | subbuf = read_console_w(self->handle, bufsize - len, &n); |
---|
837 | n/a | |
---|
838 | n/a | if (subbuf == NULL) { |
---|
839 | n/a | PyMem_Free(buf); |
---|
840 | n/a | return NULL; |
---|
841 | n/a | } |
---|
842 | n/a | |
---|
843 | n/a | if (n > 0) |
---|
844 | n/a | wcsncpy_s(&buf[len], bufsize - len + 1, subbuf, n); |
---|
845 | n/a | |
---|
846 | n/a | PyMem_Free(subbuf); |
---|
847 | n/a | |
---|
848 | n/a | /* when the read is empty we break */ |
---|
849 | n/a | if (n == 0) |
---|
850 | n/a | break; |
---|
851 | n/a | |
---|
852 | n/a | len += n; |
---|
853 | n/a | } |
---|
854 | n/a | |
---|
855 | n/a | if (len == 0 && _buflen(self) == 0) { |
---|
856 | n/a | /* when the result starts with ^Z we return an empty buffer */ |
---|
857 | n/a | PyMem_Free(buf); |
---|
858 | n/a | return PyBytes_FromStringAndSize(NULL, 0); |
---|
859 | n/a | } |
---|
860 | n/a | |
---|
861 | n/a | if (len) { |
---|
862 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
863 | n/a | bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len, |
---|
864 | n/a | NULL, 0, NULL, NULL); |
---|
865 | n/a | Py_END_ALLOW_THREADS |
---|
866 | n/a | |
---|
867 | n/a | if (!bytes_size) { |
---|
868 | n/a | DWORD err = GetLastError(); |
---|
869 | n/a | PyMem_Free(buf); |
---|
870 | n/a | return PyErr_SetFromWindowsErr(err); |
---|
871 | n/a | } |
---|
872 | n/a | } else { |
---|
873 | n/a | bytes_size = 0; |
---|
874 | n/a | } |
---|
875 | n/a | |
---|
876 | n/a | bytes_size += _buflen(self); |
---|
877 | n/a | bytes = PyBytes_FromStringAndSize(NULL, bytes_size); |
---|
878 | n/a | rn = _copyfrombuf(self, PyBytes_AS_STRING(bytes), bytes_size); |
---|
879 | n/a | |
---|
880 | n/a | if (len) { |
---|
881 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
882 | n/a | bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len, |
---|
883 | n/a | &PyBytes_AS_STRING(bytes)[rn], bytes_size - rn, NULL, NULL); |
---|
884 | n/a | Py_END_ALLOW_THREADS |
---|
885 | n/a | |
---|
886 | n/a | if (!bytes_size) { |
---|
887 | n/a | DWORD err = GetLastError(); |
---|
888 | n/a | PyMem_Free(buf); |
---|
889 | n/a | Py_CLEAR(bytes); |
---|
890 | n/a | return PyErr_SetFromWindowsErr(err); |
---|
891 | n/a | } |
---|
892 | n/a | |
---|
893 | n/a | /* add back the number of preserved bytes */ |
---|
894 | n/a | bytes_size += rn; |
---|
895 | n/a | } |
---|
896 | n/a | |
---|
897 | n/a | PyMem_Free(buf); |
---|
898 | n/a | if (bytes_size < (size_t)PyBytes_GET_SIZE(bytes)) { |
---|
899 | n/a | if (_PyBytes_Resize(&bytes, n * sizeof(wchar_t)) < 0) { |
---|
900 | n/a | Py_CLEAR(bytes); |
---|
901 | n/a | return NULL; |
---|
902 | n/a | } |
---|
903 | n/a | } |
---|
904 | n/a | return bytes; |
---|
905 | n/a | } |
---|
906 | n/a | |
---|
907 | n/a | /*[clinic input] |
---|
908 | n/a | _io._WindowsConsoleIO.read |
---|
909 | n/a | size: io_ssize_t = -1 |
---|
910 | n/a | / |
---|
911 | n/a | |
---|
912 | n/a | Read at most size bytes, returned as bytes. |
---|
913 | n/a | |
---|
914 | n/a | Only makes one system call when size is a positive integer, |
---|
915 | n/a | so less data may be returned than requested. |
---|
916 | n/a | Return an empty bytes object at EOF. |
---|
917 | n/a | [clinic start generated code]*/ |
---|
918 | n/a | |
---|
919 | n/a | static PyObject * |
---|
920 | n/a | _io__WindowsConsoleIO_read_impl(winconsoleio *self, Py_ssize_t size) |
---|
921 | n/a | /*[clinic end generated code: output=57df68af9f4b22d0 input=6c56fceec460f1dd]*/ |
---|
922 | n/a | { |
---|
923 | n/a | PyObject *bytes; |
---|
924 | n/a | Py_ssize_t bytes_size; |
---|
925 | n/a | |
---|
926 | n/a | if (self->handle == INVALID_HANDLE_VALUE) |
---|
927 | n/a | return err_closed(); |
---|
928 | n/a | if (!self->readable) |
---|
929 | n/a | return err_mode("reading"); |
---|
930 | n/a | |
---|
931 | n/a | if (size < 0) |
---|
932 | n/a | return _io__WindowsConsoleIO_readall_impl(self); |
---|
933 | n/a | if (size > BUFMAX) { |
---|
934 | n/a | PyErr_Format(PyExc_ValueError, "cannot read more than %d bytes", BUFMAX); |
---|
935 | n/a | return NULL; |
---|
936 | n/a | } |
---|
937 | n/a | |
---|
938 | n/a | bytes = PyBytes_FromStringAndSize(NULL, size); |
---|
939 | n/a | if (bytes == NULL) |
---|
940 | n/a | return NULL; |
---|
941 | n/a | |
---|
942 | n/a | bytes_size = readinto(self, PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
---|
943 | n/a | if (bytes_size < 0) { |
---|
944 | n/a | Py_CLEAR(bytes); |
---|
945 | n/a | return NULL; |
---|
946 | n/a | } |
---|
947 | n/a | |
---|
948 | n/a | if (bytes_size < PyBytes_GET_SIZE(bytes)) { |
---|
949 | n/a | if (_PyBytes_Resize(&bytes, bytes_size) < 0) { |
---|
950 | n/a | Py_CLEAR(bytes); |
---|
951 | n/a | return NULL; |
---|
952 | n/a | } |
---|
953 | n/a | } |
---|
954 | n/a | |
---|
955 | n/a | return bytes; |
---|
956 | n/a | } |
---|
957 | n/a | |
---|
958 | n/a | /*[clinic input] |
---|
959 | n/a | _io._WindowsConsoleIO.write |
---|
960 | n/a | b: Py_buffer |
---|
961 | n/a | / |
---|
962 | n/a | |
---|
963 | n/a | Write buffer b to file, return number of bytes written. |
---|
964 | n/a | |
---|
965 | n/a | Only makes one system call, so not all of the data may be written. |
---|
966 | n/a | The number of bytes actually written is returned. |
---|
967 | n/a | [clinic start generated code]*/ |
---|
968 | n/a | |
---|
969 | n/a | static PyObject * |
---|
970 | n/a | _io__WindowsConsoleIO_write_impl(winconsoleio *self, Py_buffer *b) |
---|
971 | n/a | /*[clinic end generated code: output=775bdb16fbf9137b input=be35fb624f97c941]*/ |
---|
972 | n/a | { |
---|
973 | n/a | BOOL res = TRUE; |
---|
974 | n/a | wchar_t *wbuf; |
---|
975 | n/a | DWORD len, wlen, n = 0; |
---|
976 | n/a | |
---|
977 | n/a | if (self->handle == INVALID_HANDLE_VALUE) |
---|
978 | n/a | return err_closed(); |
---|
979 | n/a | if (!self->writable) |
---|
980 | n/a | return err_mode("writing"); |
---|
981 | n/a | |
---|
982 | n/a | if (b->len > BUFMAX) |
---|
983 | n/a | len = BUFMAX; |
---|
984 | n/a | else |
---|
985 | n/a | len = (DWORD)b->len; |
---|
986 | n/a | |
---|
987 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
988 | n/a | wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0); |
---|
989 | n/a | |
---|
990 | n/a | /* issue11395 there is an unspecified upper bound on how many bytes |
---|
991 | n/a | can be written at once. We cap at 32k - the caller will have to |
---|
992 | n/a | handle partial writes. |
---|
993 | n/a | Since we don't know how many input bytes are being ignored, we |
---|
994 | n/a | have to reduce and recalculate. */ |
---|
995 | n/a | while (wlen > 32766 / sizeof(wchar_t)) { |
---|
996 | n/a | len /= 2; |
---|
997 | n/a | wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0); |
---|
998 | n/a | } |
---|
999 | n/a | Py_END_ALLOW_THREADS |
---|
1000 | n/a | |
---|
1001 | n/a | if (!wlen) |
---|
1002 | n/a | return PyErr_SetFromWindowsErr(0); |
---|
1003 | n/a | |
---|
1004 | n/a | wbuf = (wchar_t*)PyMem_Malloc(wlen * sizeof(wchar_t)); |
---|
1005 | n/a | |
---|
1006 | n/a | Py_BEGIN_ALLOW_THREADS |
---|
1007 | n/a | wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, wbuf, wlen); |
---|
1008 | n/a | if (wlen) { |
---|
1009 | n/a | res = WriteConsoleW(self->handle, wbuf, wlen, &n, NULL); |
---|
1010 | n/a | if (n < wlen) { |
---|
1011 | n/a | /* Wrote fewer characters than expected, which means our |
---|
1012 | n/a | * len value may be wrong. So recalculate it from the |
---|
1013 | n/a | * characters that were written. As this could potentially |
---|
1014 | n/a | * result in a different value, we also validate that value. |
---|
1015 | n/a | */ |
---|
1016 | n/a | len = WideCharToMultiByte(CP_UTF8, 0, wbuf, n, |
---|
1017 | n/a | NULL, 0, NULL, NULL); |
---|
1018 | n/a | if (len) { |
---|
1019 | n/a | wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, |
---|
1020 | n/a | NULL, 0); |
---|
1021 | n/a | assert(wlen == len); |
---|
1022 | n/a | } |
---|
1023 | n/a | } |
---|
1024 | n/a | } else |
---|
1025 | n/a | res = 0; |
---|
1026 | n/a | Py_END_ALLOW_THREADS |
---|
1027 | n/a | |
---|
1028 | n/a | if (!res) { |
---|
1029 | n/a | DWORD err = GetLastError(); |
---|
1030 | n/a | PyMem_Free(wbuf); |
---|
1031 | n/a | return PyErr_SetFromWindowsErr(err); |
---|
1032 | n/a | } |
---|
1033 | n/a | |
---|
1034 | n/a | PyMem_Free(wbuf); |
---|
1035 | n/a | return PyLong_FromSsize_t(len); |
---|
1036 | n/a | } |
---|
1037 | n/a | |
---|
1038 | n/a | static PyObject * |
---|
1039 | n/a | winconsoleio_repr(winconsoleio *self) |
---|
1040 | n/a | { |
---|
1041 | n/a | if (self->handle == INVALID_HANDLE_VALUE) |
---|
1042 | n/a | return PyUnicode_FromFormat("<_io._WindowsConsoleIO [closed]>"); |
---|
1043 | n/a | |
---|
1044 | n/a | if (self->readable) |
---|
1045 | n/a | return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='rb' closefd=%s>", |
---|
1046 | n/a | self->closehandle ? "True" : "False"); |
---|
1047 | n/a | if (self->writable) |
---|
1048 | n/a | return PyUnicode_FromFormat("<_io._WindowsConsoleIO mode='wb' closefd=%s>", |
---|
1049 | n/a | self->closehandle ? "True" : "False"); |
---|
1050 | n/a | |
---|
1051 | n/a | PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode"); |
---|
1052 | n/a | return NULL; |
---|
1053 | n/a | } |
---|
1054 | n/a | |
---|
1055 | n/a | /*[clinic input] |
---|
1056 | n/a | _io._WindowsConsoleIO.isatty |
---|
1057 | n/a | |
---|
1058 | n/a | Always True. |
---|
1059 | n/a | [clinic start generated code]*/ |
---|
1060 | n/a | |
---|
1061 | n/a | static PyObject * |
---|
1062 | n/a | _io__WindowsConsoleIO_isatty_impl(winconsoleio *self) |
---|
1063 | n/a | /*[clinic end generated code: output=9eac09d287c11bd7 input=9b91591dbe356f86]*/ |
---|
1064 | n/a | { |
---|
1065 | n/a | if (self->handle == INVALID_HANDLE_VALUE) |
---|
1066 | n/a | return err_closed(); |
---|
1067 | n/a | |
---|
1068 | n/a | Py_RETURN_TRUE; |
---|
1069 | n/a | } |
---|
1070 | n/a | |
---|
1071 | n/a | static PyObject * |
---|
1072 | n/a | winconsoleio_getstate(winconsoleio *self) |
---|
1073 | n/a | { |
---|
1074 | n/a | PyErr_Format(PyExc_TypeError, |
---|
1075 | n/a | "cannot serialize '%s' object", Py_TYPE(self)->tp_name); |
---|
1076 | n/a | return NULL; |
---|
1077 | n/a | } |
---|
1078 | n/a | |
---|
1079 | n/a | #include "clinic/winconsoleio.c.h" |
---|
1080 | n/a | |
---|
1081 | n/a | static PyMethodDef winconsoleio_methods[] = { |
---|
1082 | n/a | _IO__WINDOWSCONSOLEIO_READ_METHODDEF |
---|
1083 | n/a | _IO__WINDOWSCONSOLEIO_READALL_METHODDEF |
---|
1084 | n/a | _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF |
---|
1085 | n/a | _IO__WINDOWSCONSOLEIO_WRITE_METHODDEF |
---|
1086 | n/a | _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF |
---|
1087 | n/a | _IO__WINDOWSCONSOLEIO_READABLE_METHODDEF |
---|
1088 | n/a | _IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF |
---|
1089 | n/a | _IO__WINDOWSCONSOLEIO_FILENO_METHODDEF |
---|
1090 | n/a | _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF |
---|
1091 | n/a | {"__getstate__", (PyCFunction)winconsoleio_getstate, METH_NOARGS, NULL}, |
---|
1092 | n/a | {NULL, NULL} /* sentinel */ |
---|
1093 | n/a | }; |
---|
1094 | n/a | |
---|
1095 | n/a | /* 'closed' and 'mode' are attributes for compatibility with FileIO. */ |
---|
1096 | n/a | |
---|
1097 | n/a | static PyObject * |
---|
1098 | n/a | get_closed(winconsoleio *self, void *closure) |
---|
1099 | n/a | { |
---|
1100 | n/a | return PyBool_FromLong((long)(self->handle == INVALID_HANDLE_VALUE)); |
---|
1101 | n/a | } |
---|
1102 | n/a | |
---|
1103 | n/a | static PyObject * |
---|
1104 | n/a | get_closefd(winconsoleio *self, void *closure) |
---|
1105 | n/a | { |
---|
1106 | n/a | return PyBool_FromLong((long)(self->closehandle)); |
---|
1107 | n/a | } |
---|
1108 | n/a | |
---|
1109 | n/a | static PyObject * |
---|
1110 | n/a | get_mode(winconsoleio *self, void *closure) |
---|
1111 | n/a | { |
---|
1112 | n/a | return PyUnicode_FromString(self->readable ? "rb" : "wb"); |
---|
1113 | n/a | } |
---|
1114 | n/a | |
---|
1115 | n/a | static PyGetSetDef winconsoleio_getsetlist[] = { |
---|
1116 | n/a | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
---|
1117 | n/a | {"closefd", (getter)get_closefd, NULL, |
---|
1118 | n/a | "True if the file descriptor will be closed by close()."}, |
---|
1119 | n/a | {"mode", (getter)get_mode, NULL, "String giving the file mode"}, |
---|
1120 | n/a | {NULL}, |
---|
1121 | n/a | }; |
---|
1122 | n/a | |
---|
1123 | n/a | static PyMemberDef winconsoleio_members[] = { |
---|
1124 | n/a | {"_blksize", T_UINT, offsetof(winconsoleio, blksize), 0}, |
---|
1125 | n/a | {"_finalizing", T_BOOL, offsetof(winconsoleio, finalizing), 0}, |
---|
1126 | n/a | {NULL} |
---|
1127 | n/a | }; |
---|
1128 | n/a | |
---|
1129 | n/a | PyTypeObject PyWindowsConsoleIO_Type = { |
---|
1130 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
1131 | n/a | "_io._WindowsConsoleIO", |
---|
1132 | n/a | sizeof(winconsoleio), |
---|
1133 | n/a | 0, |
---|
1134 | n/a | (destructor)winconsoleio_dealloc, /* tp_dealloc */ |
---|
1135 | n/a | 0, /* tp_print */ |
---|
1136 | n/a | 0, /* tp_getattr */ |
---|
1137 | n/a | 0, /* tp_setattr */ |
---|
1138 | n/a | 0, /* tp_reserved */ |
---|
1139 | n/a | (reprfunc)winconsoleio_repr, /* tp_repr */ |
---|
1140 | n/a | 0, /* tp_as_number */ |
---|
1141 | n/a | 0, /* tp_as_sequence */ |
---|
1142 | n/a | 0, /* tp_as_mapping */ |
---|
1143 | n/a | 0, /* tp_hash */ |
---|
1144 | n/a | 0, /* tp_call */ |
---|
1145 | n/a | 0, /* tp_str */ |
---|
1146 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
1147 | n/a | 0, /* tp_setattro */ |
---|
1148 | n/a | 0, /* tp_as_buffer */ |
---|
1149 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
---|
1150 | n/a | | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */ |
---|
1151 | n/a | _io__WindowsConsoleIO___init____doc__, /* tp_doc */ |
---|
1152 | n/a | (traverseproc)winconsoleio_traverse, /* tp_traverse */ |
---|
1153 | n/a | (inquiry)winconsoleio_clear, /* tp_clear */ |
---|
1154 | n/a | 0, /* tp_richcompare */ |
---|
1155 | n/a | offsetof(winconsoleio, weakreflist), /* tp_weaklistoffset */ |
---|
1156 | n/a | 0, /* tp_iter */ |
---|
1157 | n/a | 0, /* tp_iternext */ |
---|
1158 | n/a | winconsoleio_methods, /* tp_methods */ |
---|
1159 | n/a | winconsoleio_members, /* tp_members */ |
---|
1160 | n/a | winconsoleio_getsetlist, /* tp_getset */ |
---|
1161 | n/a | 0, /* tp_base */ |
---|
1162 | n/a | 0, /* tp_dict */ |
---|
1163 | n/a | 0, /* tp_descr_get */ |
---|
1164 | n/a | 0, /* tp_descr_set */ |
---|
1165 | n/a | offsetof(winconsoleio, dict), /* tp_dictoffset */ |
---|
1166 | n/a | _io__WindowsConsoleIO___init__, /* tp_init */ |
---|
1167 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
1168 | n/a | winconsoleio_new, /* tp_new */ |
---|
1169 | n/a | PyObject_GC_Del, /* tp_free */ |
---|
1170 | n/a | 0, /* tp_is_gc */ |
---|
1171 | n/a | 0, /* tp_bases */ |
---|
1172 | n/a | 0, /* tp_mro */ |
---|
1173 | n/a | 0, /* tp_cache */ |
---|
1174 | n/a | 0, /* tp_subclasses */ |
---|
1175 | n/a | 0, /* tp_weaklist */ |
---|
1176 | n/a | 0, /* tp_del */ |
---|
1177 | n/a | 0, /* tp_version_tag */ |
---|
1178 | n/a | 0, /* tp_finalize */ |
---|
1179 | n/a | }; |
---|
1180 | n/a | |
---|
1181 | n/a | PyAPI_DATA(PyObject *) _PyWindowsConsoleIO_Type = (PyObject*)&PyWindowsConsoleIO_Type; |
---|
1182 | n/a | |
---|
1183 | n/a | #endif /* MS_WINDOWS */ |
---|