blob: f6fcfcf1964b8ada0514e1ecce153855acd161f5 [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
16 enough here to get the handle, which is all we want. */
17typedef struct {
18 PyObject_HEAD
19 HANDLE handle;
20} 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;
66 prec->Event.KeyEvent.wRepeatCount = 10;
67 prec->Event.KeyEvent.uChar.UnicodeChar = *p;
68 }
69
70 HANDLE hInput = ((winconsoleio*)file)->handle;
71 DWORD total = 0;
72 while (total < size) {
73 DWORD wrote;
74 if (!WriteConsoleInputW(hInput, &rec[total], (size - total), &wrote)) {
75 PyErr_SetFromWindowsErr(0);
76 goto error;
77 }
78 total += wrote;
79 }
80
81 PyMem_Free((void*)rec);
Serhiy Storchaka13ad3b72017-09-14 09:38:36 +030082
Steve Dower312cef72016-10-03 09:04:58 -070083 Py_RETURN_NONE;
84error:
85 if (rec)
86 PyMem_Free((void*)rec);
87 return NULL;
88}
89
90/*[clinic input]
91_testconsole.read_output
92 file: object
93
94Reads a str from the console as written to stdout.
95[clinic start generated code]*/
96
97static PyObject *
98_testconsole_read_output_impl(PyObject *module, PyObject *file)
99/*[clinic end generated code: output=876310d81a73e6d2 input=b3521f64b1b558e3]*/
100{
101 Py_RETURN_NONE;
102}
103
104#include "clinic\_testconsole.c.h"
105
106PyMethodDef testconsole_methods[] = {
107 _TESTCONSOLE_WRITE_INPUT_METHODDEF
108 _TESTCONSOLE_READ_OUTPUT_METHODDEF
109 {NULL, NULL}
110};
111
112static PyModuleDef testconsole_def = {
113 PyModuleDef_HEAD_INIT, /* m_base */
114 "_testconsole", /* m_name */
115 PyDoc_STR("Test module for the Windows console"), /* m_doc */
116 0, /* m_size */
117 testconsole_methods, /* m_methods */
118 testconsole_slots, /* m_slots */
119 NULL, /* m_traverse */
120 NULL, /* m_clear */
121 NULL, /* m_free */
122};
123
124PyMODINIT_FUNC
125PyInit__testconsole(PyObject *spec)
126{
127 return PyModuleDef_Init(&testconsole_def);
128}
129
130#endif /* MS_WINDOWS */