Skip to content

Commit d8b570a

Browse files
authored
Use OS-specific values for file open flags (#1709)
* Use platform-specific values for O_flags * Rename generate_errno.py to generate_os_codes.py * Remove optional flags
1 parent 275cc39 commit d8b570a

4 files changed

Lines changed: 222 additions & 63 deletions

File tree

Src/IronPython.Modules/errno.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class PythonErrorNumber {
1818
#region Generated Errno Codes
1919

2020
// *** BEGIN GENERATED CODE ***
21-
// generated by function: generate_errno_codes from: generate_errno.py
21+
// generated by function: generate_errno_codes from: generate_os_codes.py
2222

2323

2424
public static int EPERM => 1;
@@ -539,7 +539,7 @@ public static void PerformModuleReload(PythonContext/*!*/ context, PythonDiction
539539
#region Generated Errno Names
540540

541541
// *** BEGIN GENERATED CODE ***
542-
// generated by function: generate_errno_names from: generate_errno.py
542+
// generated by function: generate_errno_names from: generate_os_codes.py
543543

544544
// names defined on all platforms
545545
errorcode[E2BIG] = "E2BIG";

Src/IronPython.Modules/nt.cs

Lines changed: 108 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -858,15 +858,6 @@ public static object open(CodeContext/*!*/ context, [NotNone] string path, int f
858858
fs = new FileStream(path, fileMode, access, FileShare.ReadWrite, DefaultBufferSize, options);
859859
}
860860

861-
string mode2;
862-
if (fs.CanRead && fs.CanWrite) mode2 = "w+";
863-
else if (fs.CanWrite) mode2 = "w";
864-
else mode2 = "r";
865-
866-
if ((flags & O_BINARY) != 0) {
867-
mode2 += "b";
868-
}
869-
870861
return context.LanguageContext.FileManager.AddToStrongMapping(new PythonIOModule.FileIO(context, fs) { name = path });
871862
} catch (Exception e) {
872863
throw ToPythonException(e, path);
@@ -883,14 +874,17 @@ public static object open(CodeContext context, object? path, int flags, [ParamDi
883874

884875
private static FileOptions FileOptionsFromFlags(int flag) {
885876
FileOptions res = FileOptions.None;
886-
if ((flag & O_TEMPORARY) != 0) {
887-
res |= FileOptions.DeleteOnClose;
888-
}
889-
if ((flag & O_RANDOM) != 0) {
890-
res |= FileOptions.RandomAccess;
891-
}
892-
if ((flag & O_SEQUENTIAL) != 0) {
893-
res |= FileOptions.SequentialScan;
877+
878+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
879+
if ((flag & O_TEMPORARY) != 0) {
880+
res |= FileOptions.DeleteOnClose;
881+
}
882+
if ((flag & O_RANDOM) != 0) {
883+
res |= FileOptions.RandomAccess;
884+
}
885+
if ((flag & O_SEQUENTIAL) != 0) {
886+
res |= FileOptions.SequentialScan;
887+
}
894888
}
895889

896890
return res;
@@ -1851,25 +1845,108 @@ public static void kill(CodeContext/*!*/ context, int pid, int sig) {
18511845

18521846
#endif
18531847

1854-
public const int O_APPEND = 0x8;
1855-
public const int O_CREAT = 0x100;
1856-
public const int O_TRUNC = 0x200;
1848+
#region Generated O_Flags
18571849

1858-
public const int O_EXCL = 0x400;
1859-
public const int O_NOINHERIT = 0x80;
1850+
// *** BEGIN GENERATED CODE ***
1851+
// generated by function: generate_all_O_flags from: generate_os_codes.py
18601852

1861-
public const int O_RANDOM = 0x10;
1862-
public const int O_SEQUENTIAL = 0x20;
18631853

1864-
public const int O_SHORT_LIVED = 0x1000;
1865-
public const int O_TEMPORARY = 0x40;
1854+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
1855+
[SupportedOSPlatform("linux")]
1856+
[SupportedOSPlatform("macos")]
1857+
public static int O_ACCMODE => 0x3;
1858+
1859+
public static int O_APPEND => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0x8 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x8 : 0x400;
18661860

1867-
public const int O_WRONLY = 0x1;
1868-
public const int O_RDONLY = 0x0;
1869-
public const int O_RDWR = 0x2;
1861+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
1862+
[SupportedOSPlatform("windows")]
1863+
public static int O_BINARY => 0x8000;
1864+
1865+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
1866+
[SupportedOSPlatform("linux")]
1867+
[SupportedOSPlatform("macos")]
1868+
public static int O_CLOEXEC => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x1000000 : 0x80000;
1869+
1870+
public static int O_CREAT => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0x100 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x200 : 0x40;
1871+
1872+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
1873+
[SupportedOSPlatform("linux")]
1874+
[SupportedOSPlatform("macos")]
1875+
public static int O_DSYNC => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x400000 : 0x1000;
1876+
1877+
public static int O_EXCL => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0x400 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x800 : 0x80;
1878+
1879+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows, PlatformID.Unix)]
1880+
[SupportedOSPlatform("macos")]
1881+
public static int O_EXEC => 0x40000000;
1882+
1883+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows, PlatformID.MacOSX)]
1884+
[SupportedOSPlatform("linux")]
1885+
public static int O_LARGEFILE => 0x0;
1886+
1887+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
1888+
[SupportedOSPlatform("linux")]
1889+
[SupportedOSPlatform("macos")]
1890+
public static int O_NDELAY => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x4 : 0x800;
1891+
1892+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
1893+
[SupportedOSPlatform("linux")]
1894+
[SupportedOSPlatform("macos")]
1895+
public static int O_NOCTTY => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x20000 : 0x100;
18701896

1871-
public const int O_BINARY = 0x8000;
1872-
public const int O_TEXT = 0x4000;
1897+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
1898+
[SupportedOSPlatform("windows")]
1899+
public static int O_NOINHERIT => 0x80;
1900+
1901+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
1902+
[SupportedOSPlatform("linux")]
1903+
[SupportedOSPlatform("macos")]
1904+
public static int O_NONBLOCK => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x4 : 0x800;
1905+
1906+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
1907+
[SupportedOSPlatform("windows")]
1908+
public static int O_RANDOM => 0x10;
1909+
1910+
public static int O_RDONLY => 0x0;
1911+
1912+
public static int O_RDWR => 0x2;
1913+
1914+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows, PlatformID.MacOSX)]
1915+
[SupportedOSPlatform("linux")]
1916+
public static int O_RSYNC => 0x101000;
1917+
1918+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows, PlatformID.Unix)]
1919+
[SupportedOSPlatform("macos")]
1920+
public static int O_SEARCH => 0x40100000;
1921+
1922+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
1923+
[SupportedOSPlatform("windows")]
1924+
public static int O_SEQUENTIAL => 0x20;
1925+
1926+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
1927+
[SupportedOSPlatform("windows")]
1928+
public static int O_SHORT_LIVED => 0x1000;
1929+
1930+
[PythonHidden(PlatformsAttribute.PlatformFamily.Windows)]
1931+
[SupportedOSPlatform("linux")]
1932+
[SupportedOSPlatform("macos")]
1933+
public static int O_SYNC => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x80 : 0x101000;
1934+
1935+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
1936+
[SupportedOSPlatform("windows")]
1937+
public static int O_TEMPORARY => 0x40;
1938+
1939+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
1940+
[SupportedOSPlatform("windows")]
1941+
public static int O_TEXT => 0x4000;
1942+
1943+
public static int O_TRUNC => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0x200 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x400 : 0x200;
1944+
1945+
public static int O_WRONLY => 0x1;
1946+
1947+
// *** END GENERATED CODE ***
1948+
1949+
#endregion
18731950

18741951
public const int P_WAIT = 0;
18751952
public const int P_NOWAIT = 1;

Src/IronPython/Modules/_io.cs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,31 @@ public static partial class PythonIOModule {
3131
private static readonly object _blockingIOErrorKey = new object();
3232
private static readonly object _unsupportedOperationKey = new object();
3333

34-
private const int O_RDONLY = 0x0000;
35-
private const int O_WRONLY = 0x0001;
36-
private const int O_RDWR = 0x0002;
34+
// Values of the O_flags below has to be identical with flags defined in PythonNT
3735

38-
private const int O_APPEND = 0x0008;
39-
private const int O_CREAT = 0x0100;
40-
private const int O_TRUNC = 0x0200;
41-
private const int O_EXCL = 0x0400;
36+
#region Generated Common O_Flags
37+
38+
// *** BEGIN GENERATED CODE ***
39+
// generated by function: generate_common_O_flags from: generate_os_codes.py
40+
41+
42+
private static int O_RDONLY => 0x0;
43+
44+
private static int O_WRONLY => 0x1;
45+
46+
private static int O_RDWR => 0x2;
47+
48+
private static int O_APPEND => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0x8 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x8 : 0x400;
49+
50+
private static int O_CREAT => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0x100 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x200 : 0x40;
51+
52+
private static int O_TRUNC => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0x200 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x400 : 0x200;
53+
54+
private static int O_EXCL => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 0x400 : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 0x800 : 0x80;
55+
56+
// *** END GENERATED CODE ***
57+
58+
#endregion
4259

4360
[SpecialName]
4461
public static void PerformModuleReload(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
Lines changed: 88 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# See the LICENSE file in the project root for more information.
44

55
from generate import generate
6+
from collections import OrderedDict
67

7-
num_systems = 3
8-
linux_idx, darwin_idx, windows_idx = range(num_systems)
8+
systems = ['linux', 'macos', 'windows']
9+
linux_idx, darwin_idx, windows_idx = range(len(systems))
910

1011
# python3 -c 'import errno;print(dict(sorted(errno.errorcode.items())))'
1112
errorcode_linux = {1: 'EPERM', 2: 'ENOENT', 3: 'ESRCH', 4: 'EINTR', 5: 'EIO', 6: 'ENXIO', 7: 'E2BIG', 8: 'ENOEXEC', 9: 'EBADF', 10: 'ECHILD', 11: 'EAGAIN', 12: 'ENOMEM', 13: 'EACCES', 14: 'EFAULT', 15: 'ENOTBLK', 16: 'EBUSY', 17: 'EEXIST', 18: 'EXDEV', 19: 'ENODEV', 20: 'ENOTDIR', 21: 'EISDIR', 22: 'EINVAL', 23: 'ENFILE', 24: 'EMFILE', 25: 'ENOTTY', 26: 'ETXTBSY', 27: 'EFBIG', 28: 'ENOSPC', 29: 'ESPIPE', 30: 'EROFS', 31: 'EMLINK', 32: 'EPIPE', 33: 'EDOM', 34: 'ERANGE', 35: 'EDEADLOCK', 36: 'ENAMETOOLONG', 37: 'ENOLCK', 38: 'ENOSYS', 39: 'ENOTEMPTY', 40: 'ELOOP', 42: 'ENOMSG', 43: 'EIDRM', 44: 'ECHRNG', 45: 'EL2NSYNC', 46: 'EL3HLT', 47: 'EL3RST', 48: 'ELNRNG', 49: 'EUNATCH', 50: 'ENOCSI', 51: 'EL2HLT', 52: 'EBADE', 53: 'EBADR', 54: 'EXFULL', 55: 'ENOANO', 56: 'EBADRQC', 57: 'EBADSLT', 59: 'EBFONT', 60: 'ENOSTR', 61: 'ENODATA', 62: 'ETIME', 63: 'ENOSR', 64: 'ENONET', 65: 'ENOPKG', 66: 'EREMOTE', 67: 'ENOLINK', 68: 'EADV', 69: 'ESRMNT', 70: 'ECOMM', 71: 'EPROTO', 72: 'EMULTIHOP', 73: 'EDOTDOT', 74: 'EBADMSG', 75: 'EOVERFLOW', 76: 'ENOTUNIQ', 77: 'EBADFD', 78: 'EREMCHG', 79: 'ELIBACC', 80: 'ELIBBAD', 81: 'ELIBSCN', 82: 'ELIBMAX', 83: 'ELIBEXEC', 84: 'EILSEQ', 85: 'ERESTART', 86: 'ESTRPIPE', 87: 'EUSERS', 88: 'ENOTSOCK', 89: 'EDESTADDRREQ', 90: 'EMSGSIZE', 91: 'EPROTOTYPE', 92: 'ENOPROTOOPT', 93: 'EPROTONOSUPPORT', 94: 'ESOCKTNOSUPPORT', 95: 'ENOTSUP', 96: 'EPFNOSUPPORT', 97: 'EAFNOSUPPORT', 98: 'EADDRINUSE', 99: 'EADDRNOTAVAIL', 100: 'ENETDOWN', 101: 'ENETUNREACH', 102: 'ENETRESET', 103: 'ECONNABORTED', 104: 'ECONNRESET', 105: 'ENOBUFS', 106: 'EISCONN', 107: 'ENOTCONN', 108: 'ESHUTDOWN', 109: 'ETOOMANYREFS', 110: 'ETIMEDOUT', 111: 'ECONNREFUSED', 112: 'EHOSTDOWN', 113: 'EHOSTUNREACH', 114: 'EALREADY', 115: 'EINPROGRESS', 116: 'ESTALE', 117: 'EUCLEAN', 118: 'ENOTNAM', 119: 'ENAVAIL', 120: 'EISNAM', 121: 'EREMOTEIO', 122: 'EDQUOT', 123: 'ENOMEDIUM', 124: 'EMEDIUMTYPE', 125: 'ECANCELED', 126: 'ENOKEY', 127: 'EKEYEXPIRED', 128: 'EKEYREVOKED', 129: 'EKEYREJECTED', 130: 'EOWNERDEAD', 131: 'ENOTRECOVERABLE', 132: 'ERFKILL'}
@@ -15,10 +16,10 @@
1516
darwin_aliases = {'EWOULDBLOCK': 'EAGAIN'}
1617
aliases = {**linux_aliases, **darwin_aliases}
1718

18-
def set_value(errorval, name, value, index):
19-
if name not in errorval:
20-
errorval[name] = [None] * num_systems
21-
errorval[name][index] = value
19+
def set_value(codeval, name, value, index):
20+
if name not in codeval:
21+
codeval[name] = [None] * len(systems)
22+
codeval[name][index] = value
2223

2324
def collect_errornames():
2425
errorval = {}
@@ -38,7 +39,7 @@ def collect_errornames():
3839

3940
# set aliases
4041
for alias, target in aliases.items():
41-
for idx in range(num_systems):
42+
for idx in range(len(systems)):
4243
if errorval[target][idx] and not errorval[alias][idx]:
4344
errorval[alias][idx] = errorval[target][idx]
4445

@@ -57,7 +58,7 @@ def priority(error):
5758
for code in codes:
5859
res *= 100000
5960
res += code if code else 0
60-
return res + shifts * 100000 ** (num_systems - 1) // 10
61+
return res + shifts * 100000 ** (len(systems) - 1) // 10
6162

6263
names = sorted(errorvalues, key = priority)
6364
for name in names:
@@ -77,29 +78,29 @@ def priority(error):
7778
if hidden_on:
7879
cw.write(f"[PythonHidden({', '.join(hidden_on)})]")
7980

80-
value = windows_code_expr(codes)
81+
value = windows_code_expr(codes, str)
8182
cw.write(f"public static int {name} => {value};")
8283

83-
def windows_code_expr(codes):
84-
if codes[windows_idx]:
85-
if not any(codes[:windows_idx]) or all(map(lambda x: x == codes[windows_idx], codes[:windows_idx])):
86-
return str(codes[windows_idx])
84+
def windows_code_expr(codes, fmt):
85+
if codes[windows_idx] is not None:
86+
if all(c is None for c in codes[:windows_idx]) or all(x == codes[windows_idx] for x in codes[:windows_idx]):
87+
return fmt(codes[windows_idx])
8788
else:
88-
return f"RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? {codes[windows_idx]} : {darwin_code_expr(codes)}"
89+
return f"RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? {fmt(codes[windows_idx])} : {darwin_code_expr(codes, fmt)}"
8990
else:
90-
return darwin_code_expr(codes)
91+
return darwin_code_expr(codes, fmt)
9192

92-
def darwin_code_expr(codes):
93-
if codes[darwin_idx]:
94-
if not any(codes[:darwin_idx]) or all(map(lambda x: x == codes[darwin_idx], codes[:darwin_idx])):
95-
return str(codes[darwin_idx])
93+
def darwin_code_expr(codes, fmt):
94+
if codes[darwin_idx] is not None:
95+
if all(c is None for c in codes[:darwin_idx]) or all(x == codes[darwin_idx] for x in codes[:darwin_idx]):
96+
return fmt(codes[darwin_idx])
9697
else:
97-
return f"RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? {codes[darwin_idx]} : {linux_code_expr(codes)}"
98+
return f"RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? {fmt(codes[darwin_idx])} : {linux_code_expr(codes, fmt)}"
9899
else:
99-
return linux_code_expr(codes)
100+
return linux_code_expr(codes, fmt)
100101

101-
def linux_code_expr(codes):
102-
return str(codes[linux_idx])
102+
def linux_code_expr(codes, fmt):
103+
return fmt(codes[linux_idx])
103104

104105
def generate_errno_names(cw):
105106
def is_windows_alias(name):
@@ -142,10 +143,74 @@ def is_windows_alias(name):
142143
cw.write(f'errorcode[{name}] = "{name}";')
143144
cw.exit_block()
144145

146+
147+
# python3 -c 'import os;print(dict(sorted((s, getattr(os, s)) for s in dir(os) if s.startswith("O_"))))'
148+
O_flags_linux = {'O_ACCMODE': 3, 'O_APPEND': 1024, 'O_ASYNC': 8192, 'O_CLOEXEC': 524288, 'O_CREAT': 64, 'O_DIRECT': 16384, 'O_DIRECTORY': 65536, 'O_DSYNC': 4096, 'O_EXCL': 128, 'O_FSYNC': 1052672, 'O_LARGEFILE': 0, 'O_NDELAY': 2048, 'O_NOATIME': 262144, 'O_NOCTTY': 256, 'O_NOFOLLOW': 131072, 'O_NONBLOCK': 2048, 'O_PATH': 2097152, 'O_RDONLY': 0, 'O_RDWR': 2, 'O_RSYNC': 1052672, 'O_SYNC': 1052672, 'O_TMPFILE': 4259840, 'O_TRUNC': 512, 'O_WRONLY': 1}
149+
O_flags_darwin = {'O_ACCMODE': 3, 'O_APPEND': 8, 'O_ASYNC': 64, 'O_CLOEXEC': 16777216, 'O_CREAT': 512, 'O_DIRECTORY': 1048576, 'O_DSYNC': 4194304, 'O_EXCL': 2048, 'O_EXEC': 1073741824, 'O_EXLOCK': 32, 'O_NDELAY': 4, 'O_NOCTTY': 131072, 'O_NOFOLLOW': 256, 'O_NONBLOCK': 4, 'O_RDONLY': 0, 'O_RDWR': 2, 'O_SEARCH': 1074790400, 'O_SHLOCK': 16, 'O_SYNC': 128, 'O_TRUNC': 1024, 'O_WRONLY': 1}
150+
O_flags_windows = {'O_APPEND': 8, 'O_BINARY': 32768, 'O_CREAT': 256, 'O_EXCL': 1024, 'O_NOINHERIT': 128, 'O_RANDOM': 16, 'O_RDONLY': 0, 'O_RDWR': 2, 'O_SEQUENTIAL': 32, 'O_SHORT_LIVED': 4096, 'O_TEMPORARY': 64, 'O_TEXT': 16384, 'O_TRUNC': 512, 'O_WRONLY': 1}
151+
152+
O_flags_optional = {'O_ASYNC', 'O_DIRECT', 'O_DIRECTORY', 'O_NOFOLLOW', 'O_NOATIME', 'O_PATH', 'O_TMPFILE', 'O_SHLOCK', 'O_EXLOCK'}
153+
O_flags_python3_10 = {'O_EVTONLY', 'O_FSYNC', 'O_SYMLINK', 'O_NOFOLLOW_ANY'}
154+
155+
def collect_codes():
156+
codeval = {}
157+
for name in O_flags_linux:
158+
set_value(codeval, name, O_flags_linux[name], linux_idx)
159+
for name in O_flags_darwin:
160+
set_value(codeval, name, O_flags_darwin[name], darwin_idx)
161+
for name in O_flags_windows:
162+
set_value(codeval, name, O_flags_windows[name], windows_idx)
163+
for name in O_flags_optional | O_flags_python3_10:
164+
codeval.pop(name, None)
165+
return OrderedDict(sorted(codeval.items()))
166+
167+
O_flagvalues = collect_codes()
168+
169+
def generate_O_flags(cw, flagvalues, access):
170+
for name in flagvalues.keys():
171+
codes = flagvalues[name]
172+
hidden_on = []
173+
supported_on = set(systems)
174+
cw.writeline()
175+
if codes[windows_idx] is None:
176+
hidden_on = ["PlatformsAttribute.PlatformFamily.Windows"]
177+
supported_on.discard(systems[windows_idx])
178+
if codes[linux_idx] is None and codes[darwin_idx] is None:
179+
assert not hidden_on, "Cannot hide on both Unix and Windows"
180+
hidden_on = ["PlatformsAttribute.PlatformFamily.Unix"]
181+
supported_on.discard(systems[linux_idx])
182+
supported_on.discard(systems[darwin_idx])
183+
else:
184+
if codes[linux_idx] is None:
185+
hidden_on += ["PlatformID.Unix"]
186+
supported_on.discard(systems[linux_idx])
187+
if codes[darwin_idx] is None:
188+
hidden_on += ["PlatformID.MacOSX"]
189+
supported_on.discard(systems[darwin_idx])
190+
if hidden_on:
191+
cw.write(f"[PythonHidden({', '.join(hidden_on)})]")
192+
if len(supported_on) != len(systems):
193+
for s in sorted(supported_on):
194+
cw.write(f'[SupportedOSPlatform("{s}")]')
195+
196+
value = windows_code_expr(codes, fmt=hex)
197+
cw.write(f"{access} static int {name} => {value};")
198+
199+
def generate_all_O_flags(cw):
200+
generate_O_flags(cw, O_flagvalues, 'public')
201+
202+
common_O_flags = ['O_RDONLY', 'O_WRONLY', 'O_RDWR', 'O_APPEND', 'O_CREAT', 'O_TRUNC', 'O_EXCL']
203+
204+
def generate_common_O_flags(cw):
205+
generate_O_flags(cw, OrderedDict((f, O_flagvalues[f]) for f in common_O_flags), 'private')
206+
207+
145208
def main():
146209
return generate(
147210
("Errno Codes", generate_errno_codes),
148211
("Errno Names", generate_errno_names),
212+
("O_Flags", generate_all_O_flags),
213+
("Common O_Flags", generate_common_O_flags),
149214
)
150215

151216
if __name__ == "__main__":

0 commit comments

Comments
 (0)