blob: 1c93679e43542269422556bebc1d8b0a5b987fbf [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;
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_Malloc(sizeof(INPUT_RECORD) * size);
59 if (!rec)
60 goto error;
61 memset(rec, 0, sizeof(INPUT_RECORD) * size);
62
63 INPUT_RECORD *prec = rec;
64 for (DWORD i = 0; i < size; ++i, ++p, ++prec) {
65 prec->EventType = KEY_EVENT;
66 prec->Event.KeyEvent.bKeyDown = TRUE;
67 prec->Event.KeyEvent.wRepeatCount = 10;
68 prec->Event.KeyEvent.uChar.UnicodeChar = *p;
69 }
70
71 HANDLE hInput = ((winconsoleio*)file)->handle;
72 DWORD total = 0;
73 while (total < size) {
74 DWORD wrote;
75 if (!WriteConsoleInputW(hInput, &rec[total], (size - total), &wrote)) {
76 PyErr_SetFromWindowsErr(0);
77 goto error;
78 }
79 total += wrote;
80 }
81
82 PyMem_Free((void*)rec);
83
84 Py_RETURN_NONE;
85error:
86 if (rec)
87 PyMem_Free((void*)rec);
88 return NULL;
89}
90
91/*[clinic input]
92_testconsole.read_output
93 file: object
94
95Reads a str from the console as written to stdout.
96[clinic start generated code]*/
97
98static PyObject *
99_testconsole_read_output_impl(PyObject *module, PyObject *file)
100/*[clinic end generated code: output=876310d81a73e6d2 input=b3521f64b1b558e3]*/
101{
102 Py_RETURN_NONE;
103}
104
105#include "clinic\_testconsole.c.h"
106
107PyMethodDef testconsole_methods[] = {
108 _TESTCONSOLE_WRITE_INPUT_METHODDEF
109 _TESTCONSOLE_READ_OUTPUT_METHODDEF
110 {NULL, NULL}
111};
112
113static PyModuleDef testconsole_def = {
114 PyModuleDef_HEAD_INIT, /* m_base */
115 "_testconsole", /* m_name */
116 PyDoc_STR("Test module for the Windows console"), /* m_doc */
117 0, /* m_size */
118 testconsole_methods, /* m_methods */
119 testconsole_slots, /* m_slots */
120 NULL, /* m_traverse */
121 NULL, /* m_clear */
122 NULL, /* m_free */
123};
124
125PyMODINIT_FUNC
126PyInit__testconsole(PyObject *spec)
127{
128 return PyModuleDef_Init(&testconsole_def);
129}
130
131#endif /* MS_WINDOWS */