Skip to content

Commit eb0c485

Browse files
gh-101819: Remove _PyWindowsConsoleIO_Type from the Windows DLL (GH-101904)
Automerge-Triggered-By: GH:erlend-aasland
1 parent c776624 commit eb0c485

8 files changed

+29
-14
lines changed

Include/internal/pycore_global_objects_fini_generated.h

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct _Py_global_strings {
6363
STRUCT_FOR_ID(True)
6464
STRUCT_FOR_ID(WarningMessage)
6565
STRUCT_FOR_ID(_)
66+
STRUCT_FOR_ID(_WindowsConsoleIO)
6667
STRUCT_FOR_ID(__IOBase_closed)
6768
STRUCT_FOR_ID(__abc_tpflags__)
6869
STRUCT_FOR_ID(__abs__)
@@ -238,6 +239,7 @@ struct _Py_global_strings {
238239
STRUCT_FOR_ID(_get_sourcefile)
239240
STRUCT_FOR_ID(_handle_fromlist)
240241
STRUCT_FOR_ID(_initializing)
242+
STRUCT_FOR_ID(_io)
241243
STRUCT_FOR_ID(_is_text_encoding)
242244
STRUCT_FOR_ID(_length_)
243245
STRUCT_FOR_ID(_limbo)

Include/internal/pycore_runtime_init_generated.h

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_io/_iomodule.h

-4
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ extern PyTypeObject PyBufferedRandom_Type;
2121
extern PyTypeObject PyTextIOWrapper_Type;
2222
extern PyTypeObject PyIncrementalNewlineDecoder_Type;
2323

24-
#ifndef Py_LIMITED_API
2524
#ifdef MS_WINDOWS
2625
extern PyTypeObject PyWindowsConsoleIO_Type;
27-
PyAPI_DATA(PyObject *) _PyWindowsConsoleIO_Type;
28-
#define PyWindowsConsoleIO_Check(op) (PyObject_TypeCheck((op), (PyTypeObject*)_PyWindowsConsoleIO_Type))
2926
#endif /* MS_WINDOWS */
30-
#endif /* Py_LIMITED_API */
3127

3228
/* These functions are used as METH_NOARGS methods, are normally called
3329
* with args=NULL, and return a new reference.

Modules/_io/winconsoleio.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
260260
int fd_is_own = 0;
261261
HANDLE handle = NULL;
262262

263-
assert(PyWindowsConsoleIO_Check(self));
263+
assert(PyObject_TypeCheck(self, (PyTypeObject *)&PyWindowsConsoleIO_Type));
264264
if (self->fd >= 0) {
265265
if (self->closefd) {
266266
/* Have to close the existing file first. */
@@ -1174,6 +1174,4 @@ PyTypeObject PyWindowsConsoleIO_Type = {
11741174
0, /* tp_finalize */
11751175
};
11761176

1177-
PyObject * _PyWindowsConsoleIO_Type = (PyObject*)&PyWindowsConsoleIO_Type;
1178-
11791177
#endif /* MS_WINDOWS */

PC/_testconsole.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#ifdef MS_WINDOWS
1111

1212
#include "pycore_fileutils.h" // _Py_get_osfhandle()
13-
#include "..\modules\_io\_iomodule.h"
13+
#include "pycore_runtime.h" // _Py_ID()
1414

1515
#define WIN32_LEAN_AND_MEAN
1616
#include <windows.h>
@@ -51,7 +51,14 @@ _testconsole_write_input_impl(PyObject *module, PyObject *file,
5151
{
5252
INPUT_RECORD *rec = NULL;
5353

54-
if (!PyWindowsConsoleIO_Check(file)) {
54+
PyTypeObject *winconsoleio_type = (PyTypeObject *)_PyImport_GetModuleAttr(
55+
&_Py_ID(_io), &_Py_ID(_WindowsConsoleIO));
56+
if (winconsoleio_type == NULL) {
57+
return NULL;
58+
}
59+
int is_subclass = PyObject_TypeCheck(file, winconsoleio_type);
60+
Py_DECREF(winconsoleio_type);
61+
if (!is_subclass) {
5562
PyErr_SetString(PyExc_TypeError, "expected raw console object");
5663
return NULL;
5764
}

Python/pylifecycle.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ extern void _PyIO_Fini(void);
5454

5555
#ifdef MS_WINDOWS
5656
# undef BYTE
57-
58-
extern PyTypeObject PyWindowsConsoleIO_Type;
59-
# define PyWindowsConsoleIO_Check(op) \
60-
(PyObject_TypeCheck((op), &PyWindowsConsoleIO_Type))
6157
#endif
6258

6359
#define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
@@ -2358,8 +2354,16 @@ create_stdio(const PyConfig *config, PyObject* io,
23582354

23592355
#ifdef MS_WINDOWS
23602356
/* Windows console IO is always UTF-8 encoded */
2361-
if (PyWindowsConsoleIO_Check(raw))
2357+
PyTypeObject *winconsoleio_type = (PyTypeObject *)_PyImport_GetModuleAttr(
2358+
&_Py_ID(_io), &_Py_ID(_WindowsConsoleIO));
2359+
if (winconsoleio_type == NULL) {
2360+
goto error;
2361+
}
2362+
int is_subclass = PyObject_TypeCheck(raw, winconsoleio_type);
2363+
Py_DECREF(winconsoleio_type);
2364+
if (is_subclass) {
23622365
encoding = L"utf-8";
2366+
}
23632367
#endif
23642368

23652369
text = PyUnicode_FromString(name);

0 commit comments

Comments
 (0)