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 bytes. All
319 other mode characters will be ignored. Mode 'b' will be assumed if it is
320 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=7a3eed6bbe998fd9]*/
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 Py_UNBLOCK_THREADS
677 if (!newbuf) {
678 sig = -1;
679 PyErr_NoMemory();
680 break;
681 }
682 buf = newbuf;
683 /* Only advance by n and not BUFSIZ in this case */
684 off += n;
685 continue;
686 }
687
688 off += BUFSIZ;
689 }
690
691 Py_END_ALLOW_THREADS
692
693 if (sig)
694 goto error;
695 if (err) {
696 PyErr_SetFromWindowsErr(err);
697 goto error;
698 }
699
700 if (*readlen > 0 && buf[0] == L'\x1a') {
701 PyMem_Free(buf);
702 buf = (wchar_t *)PyMem_Malloc(sizeof(wchar_t));
703 if (!buf) {
704 PyErr_NoMemory();
705 goto error;
706 }
707 buf[0] = L'\0';
708 *readlen = 0;
709 }
710
711 return buf;
712
713 error:
714 if (buf)
715 PyMem_Free(buf);
716 return NULL;
717 }
718
719
720 static Py_ssize_t
721 readinto(_PyIO_State *state, winconsoleio *self, char *buf, Py_ssize_t len)
722 {
723 if (self->fd == -1) {
724 err_closed();
725 return -1;
726 }
727 if (!self->readable) {
728 err_mode(state, "reading");
729 return -1;
730 }
731 if (len == 0)
732 return 0;
733 if (len > BUFMAX) {
734 PyErr_Format(PyExc_ValueError, "cannot read more than %d bytes", BUFMAX);
735 return -1;
736 }
737
738 HANDLE handle = _Py_get_osfhandle(self->fd);
739 if (handle == INVALID_HANDLE_VALUE)
740 return -1;
741
742 /* Each character may take up to 4 bytes in the final buffer.
743 This is highly conservative, but necessary to avoid
744 failure for any given Unicode input (e.g. \U0010ffff).
745 If the caller requests fewer than 4 bytes, we buffer one
746 character.
747 */
748 DWORD wlen = (DWORD)(len / 4);
749 if (wlen == 0) {
750 wlen = 1;
751 }
752
753 DWORD read_len = _copyfrombuf(self, buf, (DWORD)len);
754 if (read_len) {
755 buf = &buf[read_len];
756 len -= read_len;
757 wlen -= 1;
758 }
759 if (len == read_len || wlen == 0)
760 return read_len;
761
762 DWORD n;
763 wchar_t *wbuf = read_console_w(handle, wlen, &n);
764 if (wbuf == NULL)
765 return -1;
766 if (n == 0) {
767 PyMem_Free(wbuf);
768 return read_len;
769 }
770
771 int err = 0;
772 DWORD u8n = 0;
773
774 Py_BEGIN_ALLOW_THREADS
775 if (len < 4) {
776 if (WideCharToMultiByte(CP_UTF8, 0, wbuf, n,
777 self->buf, sizeof(self->buf) / sizeof(self->buf[0]),
778 NULL, NULL))
779 u8n = _copyfrombuf(self, buf, (DWORD)len);
780 } else {
781 u8n = WideCharToMultiByte(CP_UTF8, 0, wbuf, n,
782 buf, (DWORD)len, NULL, NULL);
783 }
784
785 if (u8n) {
786 read_len += u8n;
787 u8n = 0;
788 } else {
789 err = GetLastError();
790 if (err == ERROR_INSUFFICIENT_BUFFER) {
791 /* Calculate the needed buffer for a more useful error, as this
792 means our "/ 4" logic above is insufficient for some input.
793 */
794 u8n = WideCharToMultiByte(CP_UTF8, 0, wbuf, n,
795 NULL, 0, NULL, NULL);
796 }
797 }
798 Py_END_ALLOW_THREADS
799
800 PyMem_Free(wbuf);
801
802 if (u8n) {
803 PyErr_Format(PyExc_SystemError,
804 "Buffer had room for %zd bytes but %u bytes required",
805 len, u8n);
806 return -1;
807 }
808 if (err) {
809 PyErr_SetFromWindowsErr(err);
810 return -1;
811 }
812
813 return read_len;
814 }
815
816 /*[clinic input]
817 _io._WindowsConsoleIO.readinto
818 cls: defining_class
819 buffer: Py_buffer(accept={rwbuffer})
820 /
821
822 Same as RawIOBase.readinto().
823 [clinic start generated code]*/
824
825 static PyObject *
826 _io__WindowsConsoleIO_readinto_impl(winconsoleio *self, PyTypeObject *cls,
827 Py_buffer *buffer)
828 /*[clinic end generated code: output=96717c74f6204b79 input=4b0627c3b1645f78]*/
829 {
830 _PyIO_State *state = get_io_state_by_cls(cls);
831 Py_ssize_t len = readinto(state, self, buffer->buf, buffer->len);
832 if (len < 0)
833 return NULL;
834
835 return PyLong_FromSsize_t(len);
836 }
837
838 static DWORD
839 new_buffersize(winconsoleio *self, DWORD currentsize)
840 {
841 DWORD addend;
842
843 /* Expand the buffer by an amount proportional to the current size,
844 giving us amortized linear-time behavior. For bigger sizes, use a
845 less-than-double growth factor to avoid excessive allocation. */
846 if (currentsize > 65536)
847 addend = currentsize >> 3;
848 else
849 addend = 256 + currentsize;
850 if (addend < SMALLCHUNK)
851 /* Avoid tiny read() calls. */
852 addend = SMALLCHUNK;
853 return addend + currentsize;
854 }
855
856 /*[clinic input]
857 _io._WindowsConsoleIO.readall
858
859 Read all data from the console, returned as bytes.
860
861 Return an empty bytes object at EOF.
862 [clinic start generated code]*/
863
864 static PyObject *
865 _io__WindowsConsoleIO_readall_impl(winconsoleio *self)
866 /*[clinic end generated code: output=e6d312c684f6e23b input=4024d649a1006e69]*/
867 {
868 wchar_t *buf;
869 DWORD bufsize, n, len = 0;
870 PyObject *bytes;
871 DWORD bytes_size, rn;
872 HANDLE handle;
873
874 if (self->fd == -1)
875 return err_closed();
876
877 handle = _Py_get_osfhandle(self->fd);
878 if (handle == INVALID_HANDLE_VALUE)
879 return NULL;
880
881 bufsize = BUFSIZ;
882
883 buf = (wchar_t*)PyMem_Malloc((bufsize + 1) * sizeof(wchar_t));
884 if (buf == NULL) {
885 PyErr_NoMemory();
886 return NULL;
887 }
888
889 while (1) {
890 wchar_t *subbuf;
891
892 if (len >= (Py_ssize_t)bufsize) {
893 DWORD newsize = new_buffersize(self, len);
894 if (newsize > BUFMAX)
895 break;
896 if (newsize < bufsize) {
897 PyErr_SetString(PyExc_OverflowError,
898 "unbounded read returned more bytes "
899 "than a Python bytes object can hold");
900 PyMem_Free(buf);
901 return NULL;
902 }
903 bufsize = newsize;
904
905 wchar_t *tmp = PyMem_Realloc(buf,
906 (bufsize + 1) * sizeof(wchar_t));
907 if (tmp == NULL) {
908 PyMem_Free(buf);
909 PyErr_NoMemory();
910 return NULL;
911 }
912 buf = tmp;
913 }
914
915 subbuf = read_console_w(handle, bufsize - len, &n);
916
917 if (subbuf == NULL) {
918 PyMem_Free(buf);
919 return NULL;
920 }
921
922 if (n > 0)
923 wcsncpy_s(&buf[len], bufsize - len + 1, subbuf, n);
924
925 PyMem_Free(subbuf);
926
927 /* when the read is empty we break */
928 if (n == 0)
929 break;
930
931 len += n;
932 }
933
934 if (len == 0 && _buflen(self) == 0) {
935 /* when the result starts with ^Z we return an empty buffer */
936 PyMem_Free(buf);
937 return PyBytes_FromStringAndSize(NULL, 0);
938 }
939
940 if (len) {
941 Py_BEGIN_ALLOW_THREADS
942 bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len,
943 NULL, 0, NULL, NULL);
944 Py_END_ALLOW_THREADS
945
946 if (!bytes_size) {
947 DWORD err = GetLastError();
948 PyMem_Free(buf);
949 return PyErr_SetFromWindowsErr(err);
950 }
951 } else {
952 bytes_size = 0;
953 }
954
955 bytes_size += _buflen(self);
956 bytes = PyBytes_FromStringAndSize(NULL, bytes_size);
957 rn = _copyfrombuf(self, PyBytes_AS_STRING(bytes), bytes_size);
958
959 if (len) {
960 Py_BEGIN_ALLOW_THREADS
961 bytes_size = WideCharToMultiByte(CP_UTF8, 0, buf, len,
962 &PyBytes_AS_STRING(bytes)[rn], bytes_size - rn, NULL, NULL);
963 Py_END_ALLOW_THREADS
964
965 if (!bytes_size) {
966 DWORD err = GetLastError();
967 PyMem_Free(buf);
968 Py_CLEAR(bytes);
969 return PyErr_SetFromWindowsErr(err);
970 }
971
972 /* add back the number of preserved bytes */
973 bytes_size += rn;
974 }
975
976 PyMem_Free(buf);
977 if (bytes_size < (size_t)PyBytes_GET_SIZE(bytes)) {
978 if (_PyBytes_Resize(&bytes, n * sizeof(wchar_t)) < 0) {
979 Py_CLEAR(bytes);
980 return NULL;
981 }
982 }
983 return bytes;
984 }
985
986 /*[clinic input]
987 _io._WindowsConsoleIO.read
988 cls: defining_class
989 size: Py_ssize_t(accept={int, NoneType}) = -1
990 /
991
992 Read at most size bytes, returned as bytes.
993
994 Only makes one system call when size is a positive integer,
995 so less data may be returned than requested.
996 Return an empty bytes object at EOF.
997 [clinic start generated code]*/
998
999 static PyObject *
1000 _io__WindowsConsoleIO_read_impl(winconsoleio *self, PyTypeObject *cls,
1001 Py_ssize_t size)
1002 /*[clinic end generated code: output=7e569a586537c0ae input=a14570a5da273365]*/
1003 {
1004 PyObject *bytes;
1005 Py_ssize_t bytes_size;
1006
1007 if (self->fd == -1)
1008 return err_closed();
1009 if (!self->readable) {
1010 _PyIO_State *state = get_io_state_by_cls(cls);
1011 return err_mode(state, "reading");
1012 }
1013
1014 if (size < 0)
1015 return _io__WindowsConsoleIO_readall_impl(self);
1016 if (size > BUFMAX) {
1017 PyErr_Format(PyExc_ValueError, "cannot read more than %d bytes", BUFMAX);
1018 return NULL;
1019 }
1020
1021 bytes = PyBytes_FromStringAndSize(NULL, size);
1022 if (bytes == NULL)
1023 return NULL;
1024
1025 _PyIO_State *state = get_io_state_by_cls(cls);
1026 bytes_size = readinto(state, self, PyBytes_AS_STRING(bytes),
1027 PyBytes_GET_SIZE(bytes));
1028 if (bytes_size < 0) {
1029 Py_CLEAR(bytes);
1030 return NULL;
1031 }
1032
1033 if (bytes_size < PyBytes_GET_SIZE(bytes)) {
1034 if (_PyBytes_Resize(&bytes, bytes_size) < 0) {
1035 Py_CLEAR(bytes);
1036 return NULL;
1037 }
1038 }
1039
1040 return bytes;
1041 }
1042
1043 /*[clinic input]
1044 _io._WindowsConsoleIO.write
1045 cls: defining_class
1046 b: Py_buffer
1047 /
1048
1049 Write buffer b to file, return number of bytes written.
1050
1051 Only makes one system call, so not all of the data may be written.
1052 The number of bytes actually written is returned.
1053 [clinic start generated code]*/
1054
1055 static PyObject *
1056 _io__WindowsConsoleIO_write_impl(winconsoleio *self, PyTypeObject *cls,
1057 Py_buffer *b)
1058 /*[clinic end generated code: output=e8019f480243cb29 input=10ac37c19339dfbe]*/
1059 {
1060 BOOL res = TRUE;
1061 wchar_t *wbuf;
1062 DWORD len, wlen, n = 0;
1063 HANDLE handle;
1064
1065 if (self->fd == -1)
1066 return err_closed();
1067 if (!self->writable) {
1068 _PyIO_State *state = get_io_state_by_cls(cls);
1069 return err_mode(state, "writing");
1070 }
1071
1072 handle = _Py_get_osfhandle(self->fd);
1073 if (handle == INVALID_HANDLE_VALUE)
1074 return NULL;
1075
1076 if (!b->len) {
1077 return PyLong_FromLong(0);
1078 }
1079 if (b->len > BUFMAX)
1080 len = BUFMAX;
1081 else
1082 len = (DWORD)b->len;
1083
1084 Py_BEGIN_ALLOW_THREADS
1085 /* issue11395 there is an unspecified upper bound on how many bytes
1086 can be written at once. We cap at 32k - the caller will have to
1087 handle partial writes.
1088 Since we don't know how many input bytes are being ignored, we
1089 have to reduce and recalculate. */
1090 const DWORD max_wlen = 32766 / sizeof(wchar_t);
1091 /* UTF-8 to wchar ratio is at most 3:1. */
1092 len = Py_MIN(len, max_wlen * 3);
1093 while (1) {
1094 /* Fix for github issues gh-110913 and gh-82052. */
1095 len = _find_last_utf8_boundary(b->buf, len);
1096 wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, NULL, 0);
1097 if (wlen <= max_wlen) {
1098 break;
1099 }
1100 len /= 2;
1101 }
1102 Py_END_ALLOW_THREADS
1103
1104 if (!wlen) {
1105 return PyLong_FromLong(0);
1106 }
1107
1108 wbuf = (wchar_t*)PyMem_Malloc(wlen * sizeof(wchar_t));
1109 if (!wbuf) {
1110 PyErr_NoMemory();
1111 return NULL;
1112 }
1113
1114 Py_BEGIN_ALLOW_THREADS
1115 wlen = MultiByteToWideChar(CP_UTF8, 0, b->buf, len, wbuf, wlen);
1116 if (wlen) {
1117 res = WriteConsoleW(handle, wbuf, wlen, &n, NULL);
1118 #ifdef Py_DEBUG
1119 if (res) {
1120 #else
1121 if (res && n < wlen) {
1122 #endif
1123 /* Wrote fewer characters than expected, which means our
1124 * len value may be wrong. So recalculate it from the
1125 * characters that were written.
1126 */
1127 len = _wchar_to_utf8_count(b->buf, len, n);
1128 }
1129 } else
1130 res = 0;
1131 Py_END_ALLOW_THREADS
1132
1133 if (!res) {
1134 DWORD err = GetLastError();
1135 PyMem_Free(wbuf);
1136 return PyErr_SetFromWindowsErr(err);
1137 }
1138
1139 PyMem_Free(wbuf);
1140 return PyLong_FromSsize_t(len);
1141 }
1142
1143 static PyObject *
1144 winconsoleio_repr(PyObject *op)
1145 {
1146 winconsoleio *self = winconsoleio_CAST(op);
1147 const char *type_name = Py_TYPE(self)->tp_name;
1148
1149 if (self->fd == -1) {
1150 return PyUnicode_FromFormat("<%.100s [closed]>", type_name);
1151 }
1152
1153 if (self->readable) {
1154 return PyUnicode_FromFormat("<%.100s mode='rb' closefd=%s>",
1155 type_name,
1156 self->closefd ? "True" : "False");
1157 }
1158 if (self->writable) {
1159 return PyUnicode_FromFormat("<%.100s mode='wb' closefd=%s>",
1160 type_name,
1161 self->closefd ? "True" : "False");
1162 }
1163
1164 PyErr_SetString(PyExc_SystemError, "_WindowsConsoleIO has invalid mode");
1165 return NULL;
1166 }
1167
1168 /*[clinic input]
1169 _io._WindowsConsoleIO.isatty
1170
1171 Always True.
1172 [clinic start generated code]*/
1173
1174 static PyObject *
1175 _io__WindowsConsoleIO_isatty_impl(winconsoleio *self)
1176 /*[clinic end generated code: output=9eac09d287c11bd7 input=9b91591dbe356f86]*/
1177 {
1178 if (self->fd == -1)
1179 return err_closed();
1180
1181 Py_RETURN_TRUE;
1182 }
1183
1184 #define clinic_state() (find_io_state_by_def(Py_TYPE(self)))
1185 #include "clinic/winconsoleio.c.h"
1186 #undef clinic_state
1187
1188 static PyMethodDef winconsoleio_methods[] = {
1189 _IO__WINDOWSCONSOLEIO_READ_METHODDEF
1190 _IO__WINDOWSCONSOLEIO_READALL_METHODDEF
1191 _IO__WINDOWSCONSOLEIO_READINTO_METHODDEF
1192 _IO__WINDOWSCONSOLEIO_WRITE_METHODDEF
1193 _IO__WINDOWSCONSOLEIO_CLOSE_METHODDEF
1194 _IO__WINDOWSCONSOLEIO_READABLE_METHODDEF
1195 _IO__WINDOWSCONSOLEIO_WRITABLE_METHODDEF
1196 _IO__WINDOWSCONSOLEIO_FILENO_METHODDEF
1197 _IO__WINDOWSCONSOLEIO_ISATTY_METHODDEF
1198 {"_isatty_open_only", _io__WindowsConsoleIO_isatty, METH_NOARGS},
1199 {NULL, NULL} /* sentinel */
1200 };
1201
1202 /* 'closed' and 'mode' are attributes for compatibility with FileIO. */
1203
1204 static PyObject *
1205 get_closed(PyObject *op, void *Py_UNUSED(closure))
1206 {
1207 winconsoleio *self = winconsoleio_CAST(op);
1208 return PyBool_FromLong((long)(self->fd == -1));
1209 }
1210
1211 static PyObject *
1212 get_closefd(PyObject *op, void *Py_UNUSED(closure))
1213 {
1214 winconsoleio *self = winconsoleio_CAST(op);
1215 return PyBool_FromLong((long)(self->closefd));
1216 }
1217
1218 static PyObject *
1219 get_mode(PyObject *op, void *Py_UNUSED(closure))
1220 {
1221 winconsoleio *self = winconsoleio_CAST(op);
1222 return PyUnicode_FromString(self->readable ? "rb" : "wb");
1223 }
1224
1225 static PyGetSetDef winconsoleio_getsetlist[] = {
1226 {"closed", get_closed, NULL, "True if the file is closed"},
1227 {"closefd", get_closefd, NULL,
1228 "True if the file descriptor will be closed by close()."},
1229 {"mode", get_mode, NULL, "String giving the file mode"},
1230 {NULL},
1231 };
1232
1233 static PyMemberDef winconsoleio_members[] = {
1234 {"_blksize", Py_T_UINT, offsetof(winconsoleio, blksize), 0},
1235 {"_finalizing", Py_T_BOOL, offsetof(winconsoleio, finalizing), 0},
1236 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(winconsoleio, weakreflist), Py_READONLY},
1237 {"__dictoffset__", Py_T_PYSSIZET, offsetof(winconsoleio, dict), Py_READONLY},
1238 {NULL}
1239 };
1240
1241 static PyType_Slot winconsoleio_slots[] = {
1242 {Py_tp_dealloc, winconsoleio_dealloc},
1243 {Py_tp_repr, winconsoleio_repr},
1244 {Py_tp_getattro, PyObject_GenericGetAttr},
1245 {Py_tp_doc, (void *)_io__WindowsConsoleIO___init____doc__},
1246 {Py_tp_traverse, winconsoleio_traverse},
1247 {Py_tp_clear, winconsoleio_clear},
1248 {Py_tp_methods, winconsoleio_methods},
1249 {Py_tp_members, winconsoleio_members},
1250 {Py_tp_getset, winconsoleio_getsetlist},
1251 {Py_tp_init, _io__WindowsConsoleIO___init__},
1252 {Py_tp_new, winconsoleio_new},
1253 {0, NULL},
1254 };
1255
1256 PyType_Spec winconsoleio_spec = {
1257 .name = "_io._WindowsConsoleIO",
1258 .basicsize = sizeof(winconsoleio),
1259 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1260 Py_TPFLAGS_IMMUTABLETYPE),
1261 .slots = winconsoleio_slots,
1262 };
1263
1264 #endif /* HAVE_WINDOWS_CONSOLE_IO */