Steve Dower | 312cef7 | 2016-10-03 09:04:58 -0700 | [diff] [blame] | 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 handle, which is all we want. */ |
| 17 | typedef struct { |
| 18 | PyObject_HEAD |
| 19 | HANDLE handle; |
| 20 | } winconsoleio; |
| 21 | |
| 22 | |
| 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 * |
| 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 Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 49 | |
Steve Dower | 312cef7 | 2016-10-03 09:04:58 -0700 | [diff] [blame] | 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); |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 62 | |
Steve Dower | 312cef7 | 2016-10-03 09:04:58 -0700 | [diff] [blame] | 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); |
Serhiy Storchaka | 13ad3b7 | 2017-09-14 09:38:36 +0300 | [diff] [blame] | 83 | |
Steve Dower | 312cef7 | 2016-10-03 09:04:58 -0700 | [diff] [blame] | 84 | Py_RETURN_NONE; |
| 85 | error: |
| 86 | if (rec) |
| 87 | PyMem_Free((void*)rec); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | /*[clinic input] |
| 92 | _testconsole.read_output |
| 93 | file: object |
| 94 | |
| 95 | Reads a str from the console as written to stdout. |
| 96 | [clinic start generated code]*/ |
| 97 | |
| 98 | static 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 | |
| 107 | PyMethodDef testconsole_methods[] = { |
| 108 | _TESTCONSOLE_WRITE_INPUT_METHODDEF |
| 109 | _TESTCONSOLE_READ_OUTPUT_METHODDEF |
| 110 | {NULL, NULL} |
| 111 | }; |
| 112 | |
| 113 | static 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 | |
| 125 | PyMODINIT_FUNC |
| 126 | PyInit__testconsole(PyObject *spec) |
| 127 | { |
| 128 | return PyModuleDef_Init(&testconsole_def); |
| 129 | } |
| 130 | |
| 131 | #endif /* MS_WINDOWS */ |