11/* Testing module for multi-phase initialization of extension modules (PEP 489)
22 */
33
4- #ifndef Py_BUILD_CORE_BUILTIN
5- # define Py_BUILD_CORE_MODULE 1
4+ // Need limited C API version 3.12 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
5+ #include "pyconfig.h" // Py_GIL_DISABLED
6+ #ifndef Py_GIL_DISABLED
7+ # define Py_LIMITED_API 0x030c0000
68#endif
79
810#include "Python.h"
911
1012#ifdef MS_WINDOWS
1113
12- #include "pycore_fileutils.h" // _Py_get_osfhandle()
13- #include "pycore_runtime.h" // _Py_ID()
14-
1514#define WIN32_LEAN_AND_MEAN
1615#include <windows.h>
1716#include <fcntl.h>
@@ -57,20 +56,24 @@ module _testconsole
5756
5857_testconsole.write_input
5958 file: object
60- s: PyBytesObject
59+ s: Py_buffer
6160
6261Writes UTF-16-LE encoded bytes to the console as if typed by a user.
6362[clinic start generated code]*/
6463
6564static PyObject *
66- _testconsole_write_input_impl (PyObject * module , PyObject * file ,
67- PyBytesObject * s )
68- /*[clinic end generated code: output=48f9563db34aedb3 input=4c774f2d05770bc6]*/
65+ _testconsole_write_input_impl (PyObject * module , PyObject * file , Py_buffer * s )
66+ /*[clinic end generated code: output=58631a8985426ad3 input=68062f1bb2e52206]*/
6967{
7068 INPUT_RECORD * rec = NULL ;
7169
72- PyTypeObject * winconsoleio_type = (PyTypeObject * )_PyImport_GetModuleAttr (
73- & _Py_ID (_io ), & _Py_ID (_WindowsConsoleIO ));
70+ PyObject * mod = PyImport_ImportModule ("_io" );
71+ if (mod == NULL ) {
72+ return NULL ;
73+ }
74+
75+ PyTypeObject * winconsoleio_type = (PyTypeObject * )PyObject_GetAttrString (mod , "_WindowsConsoleIO" );
76+ Py_DECREF (mod );
7477 if (winconsoleio_type == NULL ) {
7578 return NULL ;
7679 }
@@ -81,8 +84,8 @@ _testconsole_write_input_impl(PyObject *module, PyObject *file,
8184 return NULL ;
8285 }
8386
84- const wchar_t * p = (const wchar_t * )PyBytes_AS_STRING ( s ) ;
85- DWORD size = (DWORD )PyBytes_GET_SIZE ( s ) / sizeof (wchar_t );
87+ const wchar_t * p = (const wchar_t * )s -> buf ;
88+ DWORD size = (DWORD )s -> len / sizeof (wchar_t );
8689
8790 rec = (INPUT_RECORD * )PyMem_Calloc (size , sizeof (INPUT_RECORD ));
8891 if (!rec )
@@ -96,9 +99,11 @@ _testconsole_write_input_impl(PyObject *module, PyObject *file,
9699 prec -> Event .KeyEvent .uChar .UnicodeChar = * p ;
97100 }
98101
99- HANDLE hInput = _Py_get_osfhandle (((winconsoleio * )file )-> fd );
100- if (hInput == INVALID_HANDLE_VALUE )
102+ HANDLE hInput = (HANDLE )_get_osfhandle (((winconsoleio * )file )-> fd );
103+ if (hInput == INVALID_HANDLE_VALUE ) {
104+ PyErr_SetFromErrno (PyExc_OSError );
101105 goto error ;
106+ }
102107
103108 DWORD total = 0 ;
104109 while (total < size ) {
0 commit comments