Skip to content

Commit

Permalink
Rename PythonErrorNumber to PythonErrno (#1869)
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp authored Jan 10, 2025
1 parent c02f027 commit 9082851
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 64 deletions.
104 changes: 52 additions & 52 deletions Src/IronPython.Modules/_socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void __init__(CodeContext/*!*/ context, int family = DefaultAddressFamily
socket = HandleToSocket(handle);
if (socket is null) {
throw PythonOps.OSError(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? PythonErrorNumber.WSAENOTSOCK : PythonErrorNumber.EBADF,
? PythonErrno.WSAENOTSOCK : PythonErrno.EBADF,
"Bad file descriptor");
}
} else {
Expand Down Expand Up @@ -310,7 +310,7 @@ public int connect_ex([NotNone] PythonTuple address) {
} catch (SocketException ex) {
return !ClrModule.IsMono ? ex.NativeErrorCode : MapMonoSocketErrorToErrno(ex.SocketErrorCode);
}
return PythonErrorNumber.ENOERROR;
return PythonErrno.ENOERROR;
}

public long detach() {
Expand Down Expand Up @@ -1768,15 +1768,15 @@ internal static Exception MakeException(CodeContext/*!*/ context, Exception exce
// The following SocketErrors have no defined mapping to errno, so a generic errno is used instead
case SocketError.ProcessLimit:
return PythonExceptions.CreateThrowable(error,
!RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PythonErrorNumber.EPROCLIM : PythonErrorNumber.ENOTSUP,
!RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PythonErrno.EPROCLIM : PythonErrno.ENOTSUP,
"Too many processes");
case SocketError.NotInitialized:
case SocketError.SystemNotReady:
case SocketError.VersionNotSupported:
case SocketError.TypeNotFound:
return PythonExceptions.CreateThrowable(error, PythonErrorNumber.ENOTSUP, $"Socket error: {se.SocketErrorCode}");
return PythonExceptions.CreateThrowable(error, PythonErrno.ENOTSUP, $"Socket error: {se.SocketErrorCode}");
case SocketError.SocketError:
return PythonExceptions.CreateThrowable(error, PythonErrorNumber.EIO, $"Unknown socket error");
return PythonExceptions.CreateThrowable(error, PythonErrno.EIO, $"Unknown socket error");

// For the rest, NativeErrorCode provides the errno (except on Mono)
default:
Expand All @@ -1787,7 +1787,7 @@ internal static Exception MakeException(CodeContext/*!*/ context, Exception exce
return PythonExceptions.CreateThrowable(error, se.NativeErrorCode, se.Message);
}
} else if (exception is ObjectDisposedException) {
return PythonExceptions.CreateThrowable(error, PythonErrorNumber.EBADF, "Socket is closed");
return PythonExceptions.CreateThrowable(error, PythonErrno.EBADF, "Socket is closed");
} else if (exception is InvalidOperationException or ArgumentException) {
return MakeException(context, new SocketException((int)SocketError.InvalidArgument));
} else {
Expand Down Expand Up @@ -1856,56 +1856,56 @@ private static Exception MakeGaiException(CodeContext context, int eaiCode) {
private static int MapMonoSocketErrorToErrno(SocketError serror) {
monoSocketErrorToNativeError ??= new Dictionary<SocketError, int>(45)
{
{ SocketError.AccessDenied, PythonErrorNumber.EACCES}, // could also have been EPERM
{ SocketError.AddressAlreadyInUse, PythonErrorNumber.EADDRINUSE },
{ SocketError.AddressNotAvailable, PythonErrorNumber.EADDRNOTAVAIL },
{ SocketError.AddressFamilyNotSupported, PythonErrorNumber.EAFNOSUPPORT },
{ SocketError.AlreadyInProgress, PythonErrorNumber.EALREADY },
{ SocketError.ConnectionAborted, PythonErrorNumber.ECONNABORTED },
{ SocketError.ConnectionRefused, PythonErrorNumber.ECONNREFUSED },
{ SocketError.ConnectionReset, PythonErrorNumber.ECONNRESET },
{ SocketError.DestinationAddressRequired, PythonErrorNumber.EDESTADDRREQ },
{ SocketError.Disconnecting, PythonErrorNumber.ESHUTDOWN },
{ SocketError.Fault, PythonErrorNumber.EFAULT },
{ SocketError.HostDown, PythonErrorNumber.EHOSTDOWN },
{ SocketError.HostNotFound, PythonErrorNumber.ENOENT },
{ SocketError.HostUnreachable, PythonErrorNumber.EHOSTUNREACH },
{ SocketError.InProgress, PythonErrorNumber.EINPROGRESS },
{ SocketError.Interrupted, PythonErrorNumber.EINTR },
{ SocketError.InvalidArgument, PythonErrorNumber.EINVAL },
{ SocketError.IOPending, PythonErrorNumber.EINPROGRESS },
{ SocketError.IsConnected, PythonErrorNumber.EISCONN },
{ SocketError.MessageSize, PythonErrorNumber.EMSGSIZE },
{ SocketError.NetworkDown, PythonErrorNumber.ENETDOWN },
{ SocketError.NetworkReset, PythonErrorNumber.ENETRESET },
{ SocketError.NetworkUnreachable, PythonErrorNumber.ENETUNREACH },
{ SocketError.NoBufferSpaceAvailable, PythonErrorNumber.ENOBUFS },
{ SocketError.NoData, PythonErrorNumber.ENODATA },
{ SocketError.NotConnected, PythonErrorNumber.ENOTCONN },
{ SocketError.NotInitialized, PythonErrorNumber.ENOTSUP },
{ SocketError.NotSocket, PythonErrorNumber.ENOTSOCK },
{ SocketError.OperationAborted, PythonErrorNumber.ECANCELED },
{ SocketError.OperationNotSupported, PythonErrorNumber.ENOTSUP },
{ SocketError.ProcessLimit, !RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PythonErrorNumber.EPROCLIM : PythonErrorNumber.ENOTSUP },
{ SocketError.ProtocolFamilyNotSupported, PythonErrorNumber.EPFNOSUPPORT },
{ SocketError.ProtocolNotSupported, PythonErrorNumber.EPROTONOSUPPORT },
{ SocketError.ProtocolOption, PythonErrorNumber.ENOPROTOOPT },
{ SocketError.ProtocolType, PythonErrorNumber.EPROTOTYPE },
{ SocketError.Shutdown, PythonErrorNumber.EPIPE },
{ SocketError.SocketNotSupported, PythonErrorNumber.ESOCKTNOSUPPORT },
{ SocketError.Success, PythonErrorNumber.ENOERROR },
{ SocketError.SystemNotReady, PythonErrorNumber.ENOTSUP }, // or EAGAIN
{ SocketError.TimedOut, PythonErrorNumber.ETIMEDOUT },
{ SocketError.TooManyOpenSockets, PythonErrorNumber.ENFILE }, // could also have been EMFILE
{ SocketError.TryAgain, PythonErrorNumber.EAGAIN }, // not a perfect mapping, but better than nothing
{ SocketError.TypeNotFound, PythonErrorNumber.ENOTSOCK },
{ SocketError.VersionNotSupported, PythonErrorNumber.EPROTONOSUPPORT },
{ SocketError.WouldBlock, PythonErrorNumber.EWOULDBLOCK }, // on Linux/OSX, same as EAGAIN
{ SocketError.AccessDenied, PythonErrno.EACCES}, // could also have been EPERM
{ SocketError.AddressAlreadyInUse, PythonErrno.EADDRINUSE },
{ SocketError.AddressNotAvailable, PythonErrno.EADDRNOTAVAIL },
{ SocketError.AddressFamilyNotSupported, PythonErrno.EAFNOSUPPORT },
{ SocketError.AlreadyInProgress, PythonErrno.EALREADY },
{ SocketError.ConnectionAborted, PythonErrno.ECONNABORTED },
{ SocketError.ConnectionRefused, PythonErrno.ECONNREFUSED },
{ SocketError.ConnectionReset, PythonErrno.ECONNRESET },
{ SocketError.DestinationAddressRequired, PythonErrno.EDESTADDRREQ },
{ SocketError.Disconnecting, PythonErrno.ESHUTDOWN },
{ SocketError.Fault, PythonErrno.EFAULT },
{ SocketError.HostDown, PythonErrno.EHOSTDOWN },
{ SocketError.HostNotFound, PythonErrno.ENOENT },
{ SocketError.HostUnreachable, PythonErrno.EHOSTUNREACH },
{ SocketError.InProgress, PythonErrno.EINPROGRESS },
{ SocketError.Interrupted, PythonErrno.EINTR },
{ SocketError.InvalidArgument, PythonErrno.EINVAL },
{ SocketError.IOPending, PythonErrno.EINPROGRESS },
{ SocketError.IsConnected, PythonErrno.EISCONN },
{ SocketError.MessageSize, PythonErrno.EMSGSIZE },
{ SocketError.NetworkDown, PythonErrno.ENETDOWN },
{ SocketError.NetworkReset, PythonErrno.ENETRESET },
{ SocketError.NetworkUnreachable, PythonErrno.ENETUNREACH },
{ SocketError.NoBufferSpaceAvailable, PythonErrno.ENOBUFS },
{ SocketError.NoData, PythonErrno.ENODATA },
{ SocketError.NotConnected, PythonErrno.ENOTCONN },
{ SocketError.NotInitialized, PythonErrno.ENOTSUP },
{ SocketError.NotSocket, PythonErrno.ENOTSOCK },
{ SocketError.OperationAborted, PythonErrno.ECANCELED },
{ SocketError.OperationNotSupported, PythonErrno.ENOTSUP },
{ SocketError.ProcessLimit, !RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? PythonErrno.EPROCLIM : PythonErrno.ENOTSUP },
{ SocketError.ProtocolFamilyNotSupported, PythonErrno.EPFNOSUPPORT },
{ SocketError.ProtocolNotSupported, PythonErrno.EPROTONOSUPPORT },
{ SocketError.ProtocolOption, PythonErrno.ENOPROTOOPT },
{ SocketError.ProtocolType, PythonErrno.EPROTOTYPE },
{ SocketError.Shutdown, PythonErrno.EPIPE },
{ SocketError.SocketNotSupported, PythonErrno.ESOCKTNOSUPPORT },
{ SocketError.Success, PythonErrno.ENOERROR },
{ SocketError.SystemNotReady, PythonErrno.ENOTSUP }, // or EAGAIN
{ SocketError.TimedOut, PythonErrno.ETIMEDOUT },
{ SocketError.TooManyOpenSockets, PythonErrno.ENFILE }, // could also have been EMFILE
{ SocketError.TryAgain, PythonErrno.EAGAIN }, // not a perfect mapping, but better than nothing
{ SocketError.TypeNotFound, PythonErrno.ENOTSOCK },
{ SocketError.VersionNotSupported, PythonErrno.EPROTONOSUPPORT },
{ SocketError.WouldBlock, PythonErrno.EWOULDBLOCK }, // on Linux/OSX, same as EAGAIN
};
if (monoSocketErrorToNativeError.TryGetValue(serror, out int errno)) {
return errno;
} else {
return PythonErrorNumber.EIO;
return PythonErrno.EIO;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython.Modules/errno.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

using IronPython.Runtime;

[assembly: PythonModule("errno", typeof(IronPython.Modules.PythonErrorNumber))]
[assembly: PythonModule("errno", typeof(IronPython.Modules.PythonErrno))]
namespace IronPython.Modules {
public static class PythonErrorNumber {
public static class PythonErrno {
public const string __doc__ = "Provides a list of common error numbers. These numbers are frequently reported in various exceptions.";

internal const int ENOERROR = 0;
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython.Modules/mmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public MmapUnix(CodeContext/*!*/ context, int fileno, long length, int flags = M
private static MemoryMappedFileAccess ToMmapFileAccess(int flags, int prot, int access) {
if (access == ACCESS_DEFAULT) {
if ((flags & (MAP_PRIVATE | MAP_SHARED)) == 0) {
throw PythonOps.OSError(PythonErrorNumber.EINVAL, "Invalid argument");
throw PythonOps.OSError(PythonErrno.EINVAL, "Invalid argument");
}
if ((prot & PROT_WRITE) != 0) {
prot |= PROT_READ;
Expand Down Expand Up @@ -830,7 +830,7 @@ public void resize(long newsize) {
// resize on Posix platforms
try {
if (_handle.IsInvalid) {
throw PythonOps.OSError(PythonErrorNumber.EBADF, "Bad file descriptor");
throw PythonOps.OSError(PythonErrno.EBADF, "Bad file descriptor");
}
_view.Flush();
_view.Dispose();
Expand Down
16 changes: 8 additions & 8 deletions Src/IronPython.Modules/nt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public static int dup2(CodeContext/*!*/ context, int fd, int fd2) {
}

if (!fileManager.ValidateFdRange(fd2)) {
throw PythonOps.OSError(PythonErrorNumber.EBADF, "Bad file descriptor");
throw PythonOps.OSError(PythonErrno.EBADF, "Bad file descriptor");
}

if (fileManager.TryGetStreams(fd2, out _)) {
Expand Down Expand Up @@ -496,7 +496,7 @@ public static object fstat(CodeContext/*!*/ context, int fd) {
if (streams.IsStandardIOStream()) return new stat_result(0x1000);
if (StatStream(streams.ReadStream) is not null and var res) return res;
}
return LightExceptions.Throw(PythonOps.OSError(PythonErrorNumber.EBADF, "Bad file descriptor"));
return LightExceptions.Throw(PythonOps.OSError(PythonErrno.EBADF, "Bad file descriptor"));

static object? StatStream(Stream stream) {
if (stream is FileStream fs) return lstat(fs.Name, new Dictionary<string, object>(1));
Expand All @@ -516,7 +516,7 @@ public static void fsync(CodeContext context, int fd) {
try {
streams.Flush();
} catch (IOException) {
throw PythonOps.OSError(PythonErrorNumber.EBADF, "Bad file descriptor");
throw PythonOps.OSError(PythonErrno.EBADF, "Bad file descriptor");
}
}

Expand Down Expand Up @@ -1039,13 +1039,13 @@ public static void putenv([NotNone] string name, [NotNone] string value) {

public static Bytes read(CodeContext/*!*/ context, int fd, int buffersize) {
if (buffersize < 0) {
throw PythonOps.OSError(PythonErrorNumber.EINVAL, "Invalid argument");
throw PythonOps.OSError(PythonErrno.EINVAL, "Invalid argument");
}

try {
PythonContext pythonContext = context.LanguageContext;
var streams = pythonContext.FileManager.GetStreams(fd);
if (!streams.ReadStream.CanRead) throw PythonOps.OSError(PythonErrorNumber.EBADF, "Bad file descriptor");
if (!streams.ReadStream.CanRead) throw PythonOps.OSError(PythonErrno.EBADF, "Bad file descriptor");

return Bytes.Make(streams.Read(buffersize));
} catch (Exception e) {
Expand Down Expand Up @@ -1942,7 +1942,7 @@ public static PythonTuple waitpid(int pid, int options) {
Process? process;
lock (_processToIdMapping) {
if (!_processToIdMapping.TryGetValue(pid, out process)) {
throw GetOsError(PythonErrorNumber.ECHILD);
throw GetOsError(PythonErrno.ECHILD);
}
}

Expand All @@ -1964,7 +1964,7 @@ public static int write(CodeContext/*!*/ context, int fd, [NotNone] IBufferProto
using var buffer = data.GetBuffer();
PythonContext pythonContext = context.LanguageContext;
var streams = pythonContext.FileManager.GetStreams(fd);
if (!streams.WriteStream.CanWrite) throw PythonOps.OSError(PythonErrorNumber.EBADF, "Bad file descriptor");
if (!streams.WriteStream.CanWrite) throw PythonOps.OSError(PythonErrno.EBADF, "Bad file descriptor");

return streams.Write(buffer);
} catch (Exception e) {
Expand Down Expand Up @@ -2411,7 +2411,7 @@ private static Exception DirectoryExistsError(string? filename) {
return GetWin32Error(PythonExceptions._OSError.ERROR_ALREADY_EXISTS, filename);
}
#endif
return GetOsError(PythonErrorNumber.EEXIST, filename);
return GetOsError(PythonErrno.EEXIST, filename);
}

#if FEATURE_NATIVE
Expand Down

0 comments on commit 9082851

Please sign in to comment.