Skip to content

Commit

Permalink
pythongh-119770: Make termios ioctl() constants positive
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed May 31, 2024
1 parent 9621d36 commit 4630dfa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Lib/test/test_termios.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ def test_constants(self):
self.assertLess(termios.VTIME, termios.NCCS)
self.assertLess(termios.VMIN, termios.NCCS)

def test_ioctl_constants(self):
# gh-119770: ioctl() constants must be positive
for name in dir(termios):
if not name.startswith('TIO'):
continue
value = getattr(termios, name)
with self.subTest(name=name):
self.assertGreaterEqual(value, 0)

def test_exception(self):
self.assertTrue(issubclass(termios.error, Exception))
self.assertFalse(issubclass(termios.error, OSError))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make :mod:`termios` ``ioctl()`` constants positive. Patch by Victor Stinner.
18 changes: 15 additions & 3 deletions Modules/termios.c
Original file line number Diff line number Diff line change
Expand Up @@ -1352,9 +1352,21 @@ termios_exec(PyObject *mod)
}

while (constant->name != NULL) {
if (PyModule_AddIntConstant(
mod, constant->name, constant->value) < 0) {
return -1;
if (strncmp(constant->name, "TIO", 3) == 0) {
// gh-119770: Convert value to unsigned int for ioctl() constants,
// constants can be negative on macOS whereas ioctl() expects an
// unsigned long 'request'.
unsigned int value = constant->value & UINT_MAX;
if (PyModule_Add(mod, constant->name,
PyLong_FromUnsignedLong(value)) < 0) {
return -1;
}
}
else {
if (PyModule_AddIntConstant(
mod, constant->name, constant->value) < 0) {
return -1;
}
}
++constant;
}
Expand Down

0 comments on commit 4630dfa

Please sign in to comment.