8000 gh-119770: Make termios ioctl() constants positive · vstinner/cpython@4630dfa · GitHub
[go: up one dir, main page]

Skip to content

Commit 4630dfa

Browse files
committed
pythongh-119770: Make termios ioctl() constants positive
1 parent 9621d36 commit 4630dfa

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

Lib/test/test_termios.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ def test_constants(self):
211211
self.assertLess(termios.VTIME, termios.NCCS)
212212
self.assertLess(termios.VMIN, termios.NCCS)
213213

214+
def test_ioctl_constants(self):
215+
# gh-119770: ioctl() constants must be positive
216+
for name in dir(termios):
217+
if not name.startswith('TIO'):
218+
continue
219+
value = getattr(termios, name)
220+
with self.subTest(name=name):
221+
self.assertGreaterEqual(value, 0)
222+
214223
def test_exception(self):
215224
self.assertTrue(issubclass(termios.error, Exception))
216225
self.assertFalse(issubclass(termios.error, OSError))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :mod:`termios` ``ioctl()`` constants positive. Patch by Victor Stinner.

Modules/termios.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,9 +1352,21 @@ termios_exec(PyObject *mod)
13521352
}
13531353

13541354
while (constant->name != NULL) {
1355-
if (PyModule_AddIntConstant(
1356-
mod, constant->name, constant->value) < 0) {
1357-
return -1;
1355+
if (strncmp(constant->name, "TIO", 3) == 0) {
1356+
// gh-119770: Convert value to unsigned int for ioctl() constants,
1357+
// constants can be negative on macOS whereas ioctl() expects an
1358+
// unsigned long 'request'.
1359+
unsigned int value = constant->value & UINT_MAX;
1360+
if (PyModule_Add(mod, constant->name,
1361+
PyLong_FromUnsignedLong(value)) < 0) {
1362+
return -1;
1363+
}
1364+
}
1365+
else {
1366+
if (PyModule_AddIntConstant(
1367+
mod, constant->name, constant->value) < 0) {
1368+
return -1;
1369+
}
13581370
}
13591371
++constant;
13601372
}

0 commit comments

Comments
 (0)
0