Debian Codegraph @

Modules/_io/winconsoleio.c

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