1
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
16 enough here to get the fd, which is all we want. */
17 typedef struct {
18 PyObject_HEAD
19 int fd;
20 } winconsoleio;
21
22
execfunc(PyObject * m)23 static int execfunc(PyObject *m)
24 {
25 return 0;
26 }
27
28 PyModuleDef_Slot testconsole_slots[] = {
29 {Py_mod_exec, execfunc},
30 {0, NULL},
31 };
32
33 /*[clinic input]
34 module _testconsole
35
36 _testconsole.write_input
37 file: object
38 s: PyBytesObject
39
40 Writes UTF-16-LE encoded bytes to the console as if typed by a user.
41 [clinic start generated code]*/
42
43 static PyObject *
_testconsole_write_input_impl(PyObject * module,PyObject * file,PyBytesObject * s)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;
49
50 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
58 rec = (INPUT_RECORD*)PyMem_Calloc(size, sizeof(INPUT_RECORD));
59 if (!rec)
60 goto error;
61
62 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;
66 prec->Event.KeyEvent.wRepeatCount = 1;
67 prec->Event.KeyEvent.uChar.UnicodeChar = *p;
68 }
69
70 HANDLE hInput = _Py_get_osfhandle(((winconsoleio*)file)->fd);
71 if (hInput == INVALID_HANDLE_VALUE)
72 goto error;
73
74 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);
85
86 Py_RETURN_NONE;
87 error:
88 if (rec)
89 PyMem_Free((void*)rec);
90 return NULL;
91 }
92
93 /*[clinic input]
94 _testconsole.read_output
95 file: object
96
97 Reads a str from the console as written to stdout.
98 [clinic start generated code]*/
99
100 static PyObject *
_testconsole_read_output_impl(PyObject * module,PyObject * file)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
109 PyMethodDef testconsole_methods[] = {
110 _TESTCONSOLE_WRITE_INPUT_METHODDEF
111 _TESTCONSOLE_READ_OUTPUT_METHODDEF
112 {NULL, NULL}
113 };
114
115 static 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
127 PyMODINIT_FUNC
PyInit__testconsole(PyObject * spec)128 PyInit__testconsole(PyObject *spec)
129 {
130 return PyModuleDef_Init(&testconsole_def);
131 }
132
133 #endif /* MS_WINDOWS */
134