0
0
mirror of https://github.com/python/cpython.git synced 2024-11-21 12:59:38 +01:00

gh-115538: Emit warning when use bool as fd in _io.WindowsConsoleIO (GH-116925)

This commit is contained in:
AN Long 2024-03-18 19:48:50 +08:00 committed by GitHub
parent 43c9d6196a
commit cd2ed91780
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 0 deletions

View File

@ -43,6 +43,9 @@ class WindowsConsoleIOTests(unittest.TestCase):
self.assertEqual(0, f.fileno())
f.close() # multiple close should not crash
f.close()
with self.assertWarns(RuntimeWarning):
with ConIO(False):
pass
try:
f = ConIO(1, 'w')
@ -55,6 +58,9 @@ class WindowsConsoleIOTests(unittest.TestCase):
self.assertEqual(1, f.fileno())
f.close()
f.close()
with self.assertWarns(RuntimeWarning):
with ConIO(False):
pass
try:
f = ConIO(2, 'w')

View File

@ -0,0 +1,2 @@
:class:`_io.WindowsConsoleIO` now emit a warning if a boolean value is
passed as a filedescriptor argument.

View File

@ -298,6 +298,13 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
self->fd = -1;
}
if (PyBool_Check(nameobj)) {
if (PyErr_WarnEx(PyExc_RuntimeWarning,
"bool is used as a file descriptor", 1))
{
return -1;
}
}
fd = PyLong_AsInt(nameobj);
if (fd < 0) {
if (!PyErr_Occurred()) {