Skip to content

Instantly share code, notes, and snippets.

@pelavarre
Created August 19, 2023 21:58
Show Gist options
  • Save pelavarre/f7ccde1fc7cc54bd2e4b221412da07d3 to your computer and use it in GitHub Desktop.
Save pelavarre/f7ccde1fc7cc54bd2e4b221412da07d3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
usage: p.py
calling Python 'termios.tcsetattr' via 'tty.setraw' or 'tty.setcbreak'
wrongly changes the Type of 'mode[CC][VMIN]' and 'mode[CC][VTIME]'
to Int, from a Bytes of Length 1
diff'ing the 'termios.tcgetattr' shows this, at my macOS & at my Linux
bizarrely enough, calling 'termios.tcsetattr' to rollback all the 'mode'
spontaneously changes the Type of these two Items again,
back to a Bytes of Length 1 from an Int
Linux evidence @ https://gist.github.com/pelavarre/9883ab005ba1b5e0698aefcc4949acf7
macOS evidence @ https://gist.github.com/pelavarre/907b94cafe60728cfe4579f07d40688f
"""
import datetime as dt
import platform
import sys
import termios # often '.so'
import tty # often '.py'
print(dt.datetime.now())
print(platform.system())
print(termios.__file__)
print(tty.__file__)
fd = sys.stderr.fileno()
for func in (tty.setraw, tty.setcbreak):
func_longname = "{}.{}".format(func.__module__, func.__name__)
print()
print()
print(func_longname)
a = termios.tcgetattr(fd)
b = None
try:
func(fd)
b = termios.tcgetattr(fd)
finally:
when = termios.TCSAFLUSH
attributes = a
termios.tcsetattr(fd, when, attributes)
c = termios.tcgetattr(fd)
assert str(a) == str(c), (a, c)
print()
print("Original vs {!r} samples of 'termios.tcgetattr'".format(func_longname))
print()
print(a)
print()
print(b)
print()
print("Differing Lists inside 'termios.tcgetattr'")
for i, ipair in enumerate(zip(a, b)):
(ai, bi) = ipair
if not isinstance(ai, int):
assert isinstance(ai, list), (ai,)
print()
print("[{}]".format(i))
print(ai)
print(bi)
print()
print("Differing Items inside 'termios.tcgetattr'")
for i, ipair in enumerate(zip(a, b)):
(ai, bi) = ipair
if (type(ai) is not type(bi)) or (ai != bi):
if isinstance(ai, int):
print("[{}] from={} to={}".format(i, hex(ai), hex(bi)))
else:
for j, jp in enumerate(zip(ai, bi)):
(p, r) = jp
if (type(p) is not type(r)) or (p != r):
print("[{}, {}] from={} to={}".format(i, j, p, r))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment