blob: db84f73c7744f39fcd5cdc19172d6f1234d5ca1c [file] [log] [blame]
Steve Dower312cef72016-10-03 09:04:58 -07001
2/* Testing module for multi-phase initialization of extension modules (PEP 489)
3 */
4
5#include "Python.h"
6
7#ifdef MS_WINDOWS
8
9#include "..\modules\_io\_iomodule.h"
10
11#define WIN32_LEAN_AND_MEAN
12#include <windows.h>
13#include <fcntl.h>
14
15 /* The full definition is in iomodule. We reproduce
Segev Finer5e437fb2021-04-24 01:00:27 +030016 enough here to get the fd, which is all we want. */
Steve Dower312cef72016-10-03 09:04:58 -070017typedef struct {
18 PyObject_HEAD
Segev Finer5e437fb2021-04-24 01:00:27 +030019 int fd;
Steve Dower312cef72016-10-03 09:04:58 -070020} winconsoleio;
21
22
23static int execfunc(PyObject *m)
24{
25 return 0;
26}
27
28PyModuleDef_Slot testconsole_slots[] = {
29 {Py_mod_exec, execfunc},
30 {0, NULL},
31};
32
33/*[clinic input]
34module _testconsole
35
36_testconsole.write_input
37 file: object
38 s: PyBytesObject
39
40Writes UTF-16-LE encoded bytes to the console as if typed by a user.
41[clinic start generated code]*/
42
43static PyObject *
44_testconsole_write_input_impl(PyObject *module, PyObject *file,
45 PyBytesObject *s)
46/*[clinic end generated code: output=48f9563db34aedb3 input=4c774f2d05770bc6]*/
47{
48 INPUT_RECORD *rec = NULL;
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +030049
Steve Dower312cef72016-10-03 09:04:58 -070050 if (!PyWindowsConsoleIO_Check(file)) {
51 PyErr_SetString(PyExc_TypeError, "expected raw console object");
52 return NULL;
53 }
54
55 const wchar_t *p = (const wchar_t *)PyBytes_AS_STRING(s);
56 DWORD size = (DWORD)PyBytes_GET_SIZE(s) / sizeof(wchar_t);
57
Andy Lester7668a8b2020-03-24 23:26:44 -050058 rec = (INPUT_RECORD*)PyMem_Calloc(size, sizeof(INPUT_RECORD));
Steve Dower312cef72016-10-03 09:04:58 -070059 if (!rec)
60 goto error;
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +030061
Steve Dower312cef72016-10-03 09:04:58 -070062 INPUT_RECORD *prec = rec;
63 for (DWORD i = 0; i < size; ++i, ++p, ++prec) {
64 prec->EventType = KEY_EVENT;
65 prec->Event.KeyEvent.bKeyDown = TRUE;
Terry Jan Reedy31c98282020-09-12 01:51:52 -040066 prec->Event.KeyEvent.wRepeatCount = 1;
Steve Dower312cef72016-10-03 09:04:58 -070067 prec->Event.KeyEvent.uChar.UnicodeChar = *p;
68 }
69
Segev Finer5e437fb2021-04-24 01:00:27 +030070 HANDLE hInput = _Py_get_osfhandle(((winconsoleio*)file)->fd);
71 if (hInput == INVALID_HANDLE_VALUE)
72 goto error;
73
Steve Dower312cef72016-10-03 09:04:58 -070074 DWORD total = 0;
75 while (total < size) {
76 DWORD wrote;
77 if (!WriteConsoleInputW(hInput, &rec[total], (size - total), &wrote)) {
78 PyErr_SetFromWindowsErr(0);
79 goto error;
80 }
81 total += wrote;
82 }
83
84 PyMem_Free((void*)rec);
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +030085
Steve Dower312cef72016-10-03 09:04:58 -070086 Py_RETURN_NONE;
87error:
88 if (rec)
89 PyMem_Free((void*)rec);
90 return NULL;
91}
92
93/*[clinic input]
94_testconsole.read_output
95 file: object
96
97Reads a str from the console as written to stdout.
98[clinic start generated code]*/
99
100static PyObject *
101_testconsole_read_output_impl(PyObject *module, PyObject *file)
102/*[clinic end generated code: output=876310d81a73e6d2 input=b3521f64b1b558e3]*/
103{
104 Py_RETURN_NONE;
105}
106
107#include "clinic\_testconsole.c.h"
108
109PyMethodDef testconsole_methods[] = {
110 _TESTCONSOLE_WRITE_INPUT_METHODDEF
111 _TESTCONSOLE_READ_OUTPUT_METHODDEF
112 {NULL, NULL}
113};
114
115static PyModuleDef testconsole_def = {
116 PyModuleDef_HEAD_INIT, /* m_base */
117 "_testconsole", /* m_name */
118 PyDoc_STR("Test module for the Windows console"), /* m_doc */
119 0, /* m_size */
120 testconsole_methods, /* m_methods */
121 testconsole_slots, /* m_slots */
122 NULL, /* m_traverse */
123 NULL, /* m_clear */
124 NULL, /* m_free */
125};
126
127PyMODINIT_FUNC
128PyInit__testconsole(PyObject *spec)
129{
130 return PyModuleDef_Init(&testconsole_def);
131}
132
133#endif /* MS_WINDOWS */