Skip to content

Commit 9035d36

Browse files
committed
Simplify error creation
1 parent fcc8db8 commit 9035d36

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

Src/IronPython.Modules/_overlapped.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
using System;
66
using System.Numerics;
77
using System.Runtime.InteropServices;
8+
using System.Runtime.Versioning;
89

910
using IronPython.Runtime;
1011

1112
[assembly: PythonModule("_overlapped", typeof(IronPython.Modules.PythonOverlapped), PlatformsAttribute.PlatformFamily.Windows)]
1213
namespace IronPython.Modules {
14+
15+
[SupportedOSPlatform("windows")]
1316
public static class PythonOverlapped {
1417
public const int ERROR_NETNAME_DELETED = 64;
1518
public const int ERROR_SEM_TIMEOUT = 121;

Src/IronPython.Modules/_winapi.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
using System;
1010
using System.Numerics;
1111
using System.Runtime.InteropServices;
12+
using System.Runtime.Versioning;
1213
using System.Text;
1314

1415
using IronPython.Runtime;
1516
using IronPython.Runtime.Operations;
1617

1718
[assembly: PythonModule("_winapi", typeof(IronPython.Modules.PythonWinApi), PlatformsAttribute.PlatformFamily.Windows)]
1819
namespace IronPython.Modules {
20+
[SupportedOSPlatform("windows")]
1921
public static class PythonWinApi {
2022
#region Public API
2123

24+
[SupportedOSPlatform("windows")]
2225
public static object? ConnectNamedPipe(BigInteger handle, bool overlapped = false) {
2326
if (overlapped) throw new NotImplementedException();
2427

Src/IronPython.Modules/nt.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,8 +2318,13 @@ private static Exception GetOsError(int error, string? filename = null, string?
23182318

23192319
#if FEATURE_NATIVE || FEATURE_CTYPES
23202320

2321+
[SupportedOSPlatform("windows")]
2322+
internal static Exception GetLastWin32Error(string? filename = null, string? filename2 = null)
2323+
=> GetWin32Error(Marshal.GetLastWin32Error(), filename, filename2);
2324+
23212325
// Gets an error message for a Win32 error code.
2322-
internal static string GetMessage(int errorCode) {
2326+
[SupportedOSPlatform("windows")]
2327+
private static string GetWin32ErrorMessage(int errorCode) {
23232328
string msg = new Win32Exception(errorCode).Message;
23242329
// error codes: https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes
23252330
if (errorCode is not (< 0 or >= 8200 or 34 or 106 or 317 or 718)) {
@@ -2332,11 +2337,9 @@ internal static string GetMessage(int errorCode) {
23322337
return msg.TrimEnd('\r', '\n', '.');
23332338
}
23342339

2335-
internal static Exception GetLastWin32Error(string? filename = null, string? filename2 = null)
2336-
=> GetWin32Error(Marshal.GetLastWin32Error(), filename, filename2);
2337-
2340+
[SupportedOSPlatform("windows")]
23382341
private static Exception GetWin32Error(int error, string? filename = null, string? filename2 = null) {
2339-
var msg = GetMessage(error);
2342+
var msg = GetWin32ErrorMessage(error);
23402343
return PythonOps.OSError(0, msg, filename, error, filename2);
23412344
}
23422345

Src/IronPython.Modules/posix.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using System;
88
using System.IO;
9+
using System.Runtime.InteropServices;
910
using System.Runtime.Versioning;
1011
using System.Text;
1112

@@ -28,7 +29,8 @@ public static partial class PythonNT {
2829
[SupportedOSPlatform("linux")]
2930
[SupportedOSPlatform("osx")]
3031
private static Exception GetLastUnixError(string? filename = null, string? filename2 = null)
31-
=> GetOsError(NativeConvert.FromErrno(Syscall.GetLastError()), filename, filename2);
32+
// On POSIX, GetLastWin32Error returns the errno value, same as GetLastPInvokeError
33+
=> GetOsError(Marshal.GetLastWin32Error(), filename, filename2);
3234

3335

3436
[SupportedOSPlatform("linux")]
@@ -41,7 +43,7 @@ private static int strerror_r(int code, StringBuilder buffer)
4143
[SupportedOSPlatform("linux")]
4244
[SupportedOSPlatform("osx")]
4345
private static Tuple<int, Stream, int, Stream> CreatePipeStreamsUnix() {
44-
Mono.Unix.UnixPipes pipes = Mono.Unix.UnixPipes.CreatePipes();
46+
UnixPipes pipes = UnixPipes.CreatePipes();
4547
return Tuple.Create<int, Stream, int, Stream>(pipes.Reading.Handle, pipes.Reading, pipes.Writing.Handle, pipes.Writing);
4648
}
4749
#endif
@@ -169,7 +171,7 @@ internal static void ftruncateUnix(int fd, long length) {
169171
Errno errno;
170172
do {
171173
result = Syscall.ftruncate(fd, length);
172-
} while (Mono.Unix.UnixMarshal.ShouldRetrySyscall(result, out errno));
174+
} while (UnixMarshal.ShouldRetrySyscall(result, out errno));
173175

174176
if (errno != 0)
175177
throw GetOsError(NativeConvert.FromErrno(errno));

0 commit comments

Comments
 (0)