ยปCore Development>Code coverage>PC/_testconsole.c

Python code coverage for PC/_testconsole.c

#countcontent
1n/a
2n/a/* Testing module for multi-phase initialization of extension modules (PEP 489)
3n/a */
4n/a
5n/a#include "Python.h"
6n/a
7n/a#ifdef MS_WINDOWS
8n/a
9n/a#include "..\modules\_io\_iomodule.h"
10n/a
11n/a#define WIN32_LEAN_AND_MEAN
12n/a#include <windows.h>
13n/a#include <fcntl.h>
14n/a
15n/a /* The full definition is in iomodule. We reproduce
16n/a enough here to get the handle, which is all we want. */
17n/atypedef struct {
18n/a PyObject_HEAD
19n/a HANDLE handle;
20n/a} winconsoleio;
21n/a
22n/a
23n/astatic int execfunc(PyObject *m)
24n/a{
25n/a return 0;
26n/a}
27n/a
28n/aPyModuleDef_Slot testconsole_slots[] = {
29n/a {Py_mod_exec, execfunc},
30n/a {0, NULL},
31n/a};
32n/a
33n/a/*[clinic input]
34n/amodule _testconsole
35n/a
36n/a_testconsole.write_input
37n/a file: object
38n/a s: PyBytesObject
39n/a
40n/aWrites UTF-16-LE encoded bytes to the console as if typed by a user.
41n/a[clinic start generated code]*/
42n/a
43n/astatic PyObject *
44n/a_testconsole_write_input_impl(PyObject *module, PyObject *file,
45n/a PyBytesObject *s)
46n/a/*[clinic end generated code: output=48f9563db34aedb3 input=4c774f2d05770bc6]*/
47n/a{
48n/a INPUT_RECORD *rec = NULL;
49n/a
50n/a if (!PyWindowsConsoleIO_Check(file)) {
51n/a PyErr_SetString(PyExc_TypeError, "expected raw console object");
52n/a return NULL;
53n/a }
54n/a
55n/a const wchar_t *p = (const wchar_t *)PyBytes_AS_STRING(s);
56n/a DWORD size = (DWORD)PyBytes_GET_SIZE(s) / sizeof(wchar_t);
57n/a
58n/a rec = (INPUT_RECORD*)PyMem_Malloc(sizeof(INPUT_RECORD) * size);
59n/a if (!rec)
60n/a goto error;
61n/a memset(rec, 0, sizeof(INPUT_RECORD) * size);
62n/a
63n/a INPUT_RECORD *prec = rec;
64n/a for (DWORD i = 0; i < size; ++i, ++p, ++prec) {
65n/a prec->EventType = KEY_EVENT;
66n/a prec->Event.KeyEvent.bKeyDown = TRUE;
67n/a prec->Event.KeyEvent.wRepeatCount = 10;
68n/a prec->Event.KeyEvent.uChar.UnicodeChar = *p;
69n/a }
70n/a
71n/a HANDLE hInput = ((winconsoleio*)file)->handle;
72n/a DWORD total = 0;
73n/a while (total < size) {
74n/a DWORD wrote;
75n/a if (!WriteConsoleInputW(hInput, &rec[total], (size - total), &wrote)) {
76n/a PyErr_SetFromWindowsErr(0);
77n/a goto error;
78n/a }
79n/a total += wrote;
80n/a }
81n/a
82n/a PyMem_Free((void*)rec);
83n/a
84n/a Py_RETURN_NONE;
85n/aerror:
86n/a if (rec)
87n/a PyMem_Free((void*)rec);
88n/a return NULL;
89n/a}
90n/a
91n/a/*[clinic input]
92n/a_testconsole.read_output
93n/a file: object
94n/a
95n/aReads a str from the console as written to stdout.
96n/a[clinic start generated code]*/
97n/a
98n/astatic PyObject *
99n/a_testconsole_read_output_impl(PyObject *module, PyObject *file)
100n/a/*[clinic end generated code: output=876310d81a73e6d2 input=b3521f64b1b558e3]*/
101n/a{
102n/a Py_RETURN_NONE;
103n/a}
104n/a
105n/a#include "clinic\_testconsole.c.h"
106n/a
107n/aPyMethodDef testconsole_methods[] = {
108n/a _TESTCONSOLE_WRITE_INPUT_METHODDEF
109n/a _TESTCONSOLE_READ_OUTPUT_METHODDEF
110n/a {NULL, NULL}
111n/a};
112n/a
113n/astatic PyModuleDef testconsole_def = {
114n/a PyModuleDef_HEAD_INIT, /* m_base */
115n/a "_testconsole", /* m_name */
116n/a PyDoc_STR("Test module for the Windows console"), /* m_doc */
117n/a 0, /* m_size */
118n/a testconsole_methods, /* m_methods */
119n/a testconsole_slots, /* m_slots */
120n/a NULL, /* m_traverse */
121n/a NULL, /* m_clear */
122n/a NULL, /* m_free */
123n/a};
124n/a
125n/aPyMODINIT_FUNC
126n/aPyInit__testconsole(PyObject *spec)
127n/a{
128n/a return PyModuleDef_Init(&testconsole_def);
129n/a}
130n/a
131n/a#endif /* MS_WINDOWS */