Skip to content

Commit bc1f309

Browse files
committed
termios baud rates
1 parent f533735 commit bc1f309

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/core/IronPython.Modules/termios.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static class PythonTermios {
3030

3131
#region termios IO Control Codes (TIOC*)
3232

33-
public static int TIOCGWINSZ => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 1074295912 : 21523;
33+
public static int TIOCGWINSZ => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x40087468 : 0x5413;
3434

3535

3636
#endregion
@@ -137,10 +137,27 @@ public static class PythonTermios {
137137
public static int TCIOFF => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 3 : 2; // transmit STOP character
138138
public static int TCION => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 4 : 3; // transmit START character
139139

140-
#endregion
141-
142-
143-
#region Public API
140+
// baud rates
141+
public static int B0 => 0;
142+
public static int B50 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 50 : 1;
143+
public static int B75 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 75 : 2;
144+
public static int B110 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 110 : 3;
145+
public static int B134 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 134 : 4;
146+
public static int B150 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 150 : 5;
147+
public static int B200 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 200 : 6;
148+
public static int B300 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 300 : 7;
149+
public static int B600 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 600 : 8;
150+
public static int B1200 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 1200 : 9;
151+
public static int B1800 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 1800 : 10;
152+
public static int B2400 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 2400 : 11;
153+
public static int B4800 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 4800 : 12;
154+
public static int B9600 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 9600 : 13;
155+
public static int B19200 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 19200 : 14;
156+
public static int B38400 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 38400 : 15;
157+
public static int B57600 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 57600 : 0x1001;
158+
public static int B115200 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 115200 : 0x1002;
159+
public static int B230400 => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 230400 : 0x1003;
160+
// higher baud rates are not defined on macOS
144161

145162
public static object tcgetattr(CodeContext context, int fd) {
146163
if (fd < 0) throw PythonOps.ValueError("file descriptor cannot be a negative integer ({0})", fd);
@@ -258,8 +275,8 @@ public static void tcsetattr(CodeContext context, object? file, int when, [NotNo
258275
private static uint _lflag => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ?
259276
ECHOKE | ECHOE | ECHOK | ECHO | ECHOCTL | ISIG | ICANON | IEXTEN | PENDIN
260277
: ECHOKE | ECHOE | ECHOK | ECHO | ECHOCTL | ISIG | ICANON | IEXTEN;
261-
private static int _ispeed => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 38400 : /* B38400 */ 15;
262-
private static int _ospeed => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 38400 : /* B38400 */ 15;
278+
private static int _ispeed => B38400;
279+
private static int _ospeed => B38400;
263280

264281
private static readonly byte[] macos__specialChars = [
265282
(byte)0x04, // VEOF ^D

tests/suite/modules/io_related/test_termios.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def test_cc(self):
9999
cc_values = [4, 11, 16, 2, 14, 3, 12, 0, 1, 10, 8, 9, 15, 13, 6, 5, 7, 32]
100100
self.verify_flags(cc, cc_values)
101101

102+
def test_baud_rates(self):
103+
rates = ["B0", "B50", "B75", "B110", "B134", "B150", "B200", "B300", "B600", "B1200", "B1800", "B2400", "B4800", "B9600", "B19200", "B38400", "B57600", "B115200", "B230400"]
104+
if is_osx:
105+
rates_values = [0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400]
106+
elif is_linux:
107+
rates_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 4097, 4098, 4099]
108+
self.verify_flags(rates, rates_values)
109+
102110

103111
if __name__ == "__main__":
104112
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)