summaryrefslogtreecommitdiffstats
path: root/PC/_testconsole.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-03-21 16:45:43 (GMT)
committerGitHub <noreply@github.com>2024-03-21 16:45:43 (GMT)
commitabdd1f938f08e536864532b2071f144515ecc88b (patch)
treee17a96a927762c4364996c2fe1ee47a18d08bd10 /PC/_testconsole.c
parent8bea6c411d65cd987616b4ecdb86373e4f21f1c6 (diff)
downloadcpython-abdd1f938f08e536864532b2071f144515ecc88b.zip
cpython-abdd1f938f08e536864532b2071f144515ecc88b.tar.gz
cpython-abdd1f938f08e536864532b2071f144515ecc88b.tar.bz2
gh-85283: Build _testconsole extension with limited C API (#117125)
Diffstat (limited to 'PC/_testconsole.c')
-rw-r--r--PC/_testconsole.c35
1 files changed, 20 insertions, 15 deletions
diff --git a/PC/_testconsole.c b/PC/_testconsole.c
index 1dc0d23..f1ace00 100644
--- a/PC/_testconsole.c
+++ b/PC/_testconsole.c
@@ -1,17 +1,16 @@
/* Testing module for multi-phase initialization of extension modules (PEP 489)
*/
-#ifndef Py_BUILD_CORE_BUILTIN
-# define Py_BUILD_CORE_MODULE 1
+// Need limited C API version 3.12 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
+#include "pyconfig.h" // Py_GIL_DISABLED
+#ifndef Py_GIL_DISABLED
+# define Py_LIMITED_API 0x030c0000
#endif
#include "Python.h"
#ifdef MS_WINDOWS
-#include "pycore_fileutils.h" // _Py_get_osfhandle()
-#include "pycore_runtime.h" // _Py_ID()
-
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <fcntl.h>
@@ -57,20 +56,24 @@ module _testconsole
_testconsole.write_input
file: object
- s: PyBytesObject
+ s: Py_buffer
Writes UTF-16-LE encoded bytes to the console as if typed by a user.
[clinic start generated code]*/
static PyObject *
-_testconsole_write_input_impl(PyObject *module, PyObject *file,
- PyBytesObject *s)
-/*[clinic end generated code: output=48f9563db34aedb3 input=4c774f2d05770bc6]*/
+_testconsole_write_input_impl(PyObject *module, PyObject *file, Py_buffer *s)
+/*[clinic end generated code: output=58631a8985426ad3 input=68062f1bb2e52206]*/
{
INPUT_RECORD *rec = NULL;
- PyTypeObject *winconsoleio_type = (PyTypeObject *)_PyImport_GetModuleAttr(
- &_Py_ID(_io), &_Py_ID(_WindowsConsoleIO));
+ PyObject *mod = PyImport_ImportModule("_io");
+ if (mod == NULL) {
+ return NULL;
+ }
+
+ PyTypeObject *winconsoleio_type = (PyTypeObject *)PyObject_GetAttrString(mod, "_WindowsConsoleIO");
+ Py_DECREF(mod);
if (winconsoleio_type == NULL) {
return NULL;
}
@@ -81,8 +84,8 @@ _testconsole_write_input_impl(PyObject *module, PyObject *file,
return NULL;
}
- const wchar_t *p = (const wchar_t *)PyBytes_AS_STRING(s);
- DWORD size = (DWORD)PyBytes_GET_SIZE(s) / sizeof(wchar_t);
+ const wchar_t *p = (const wchar_t *)s->buf;
+ DWORD size = (DWORD)s->len / sizeof(wchar_t);
rec = (INPUT_RECORD*)PyMem_Calloc(size, sizeof(INPUT_RECORD));
if (!rec)
@@ -96,9 +99,11 @@ _testconsole_write_input_impl(PyObject *module, PyObject *file,
prec->Event.KeyEvent.uChar.UnicodeChar = *p;
}
- HANDLE hInput = _Py_get_osfhandle(((winconsoleio*)file)->fd);
- if (hInput == INVALID_HANDLE_VALUE)
+ HANDLE hInput = (HANDLE)_get_osfhandle(((winconsoleio*)file)->fd);
+ if (hInput == INVALID_HANDLE_VALUE) {
+ PyErr_SetFromErrno(PyExc_OSError);
goto error;
+ }
DWORD total = 0;
while (total < size) {