Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-85283: Build _testconsole extension with limited C API #117125

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ Build Changes
* The ``errno``, ``fcntl``, ``grp``, ``md5``, ``pwd``, ``resource``,
``termios``, ``winsound``,
``_ctypes_test``, ``_multiprocessing.posixshmem``, ``_scproxy``, ``_stat``,
``_statistics``, ``_testimportmultiple`` and ``_uuid``
``_statistics``, ``_testconsole``, ``_testimportmultiple`` and ``_uuid``
C extensions are now built with the
:ref:`limited C API <limited-c-api>`.
(Contributed by Victor Stinner in :gh:`85283`.)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
The ``fcntl``, ``grp``, ``pwd``, ``termios`` and ``_statistics`` C extensions are now
built with the :ref:`limited C API <limited-c-api>`. Patch by Victor Stinner.
The ``fcntl``, ``grp``, ``pwd``, ``termios``, ``_statistics`` and
``_testconsole`` C extensions are now built with the :ref:`limited C API
<limited-c-api>`. Patch by Victor Stinner.
35 changes: 20 additions & 15 deletions PC/_testconsole.c
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
99 changes: 19 additions & 80 deletions PC/clinic/_testconsole.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading