@@ -136,14 +136,27 @@ public void __init__(CodeContext/*!*/ context, int family = DefaultAddressFamily
136136 throw MakeException ( context , new SocketException ( ( int ) SocketError . ProtocolNotSupported ) ) ;
137137 }
138138
139- Socket ? socket ;
139+ Socket ? socket = null ;
140140 if ( fileno is socket sock ) {
141141 socket = sock . _socket ;
142142 _hostName = sock . _hostName ;
143143 // we now own the lifetime of the socket
144144 GC . SuppressFinalize ( sock ) ;
145- } else if ( fileno != null && ( socket = HandleToSocket ( ( long ) fileno ) ) != null ) {
146- // nothing to do here
145+ } else if ( fileno != null ) {
146+ if ( ! PythonOps . TryToIndex ( fileno , out object ? handleObj ) ) {
147+ throw PythonOps . TypeErrorForUnIndexableObject ( fileno ) ;
148+ }
149+ long handle = Converter . ConvertToInt64 ( handleObj ) ;
150+ // Windows reserves only INVALID_SOCKET (~0) as an invalid handle
151+ if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? handle == - 1 : handle < 0 ) {
152+ throw PythonOps . ValueError ( "negative file descriptor" ) ;
153+ }
154+ socket = HandleToSocket ( handle ) ;
155+ if ( socket is null ) {
156+ throw PythonOps . OSError ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows )
157+ ? PythonErrorNumber . WSAENOTSOCK : PythonErrorNumber . EBADF ,
158+ "Bad file descriptor" ) ;
159+ }
147160 } else {
148161 try {
149162 socket = new Socket ( addressFamily , socketType , protocolType ) ;
@@ -413,7 +426,7 @@ public Bytes recv(int bufsize, int flags = 0) {
413426 try {
414427 bytesRead = _socket . Receive ( buffer , bufsize , ( SocketFlags ) flags ) ;
415428 } catch ( Exception e ) {
416- throw MakeRecvException ( e , SocketError . NotConnected ) ;
429+ throw MakeException ( _context , e ) ;
417430 }
418431
419432 var bytes = new byte [ bytesRead ] ;
@@ -449,7 +462,7 @@ public int recv_into([NotNone] IBufferProtocol buffer, int nbytes = 0, int flags
449462 try {
450463 bytesRead = _socket . Receive ( byteBuffer , nbytes , ( SocketFlags ) flags ) ;
451464 } catch ( Exception e ) {
452- throw MakeRecvException ( e , SocketError . NotConnected ) ;
465+ throw MakeException ( _context , e ) ;
453466 }
454467
455468 byteBuffer . AsSpan ( 0 , bytesRead ) . CopyTo ( span ) ;
@@ -486,7 +499,7 @@ public PythonTuple recvfrom(int bufsize, int flags = 0) {
486499 try {
487500 bytesRead = _socket . ReceiveFrom ( buffer , bufsize , ( SocketFlags ) flags , ref remoteEP ) ;
488501 } catch ( Exception e ) {
489- throw MakeRecvException ( e , SocketError . InvalidArgument ) ;
502+ throw MakeException ( _context , e ) ;
490503 }
491504
492505 var bytes = new byte [ bytesRead ] ;
@@ -519,7 +532,7 @@ public PythonTuple recvfrom_into([NotNone] IBufferProtocol buffer, int nbytes =
519532 try {
520533 bytesRead = _socket . ReceiveFrom ( byteBuffer , nbytes , ( SocketFlags ) flags , ref remoteEP ) ;
521534 } catch ( Exception e ) {
522- throw MakeRecvException ( e , SocketError . InvalidArgument ) ;
535+ throw MakeException ( _context , e ) ;
523536 }
524537
525538 byteBuffer . AsSpan ( 0 , bytesRead ) . CopyTo ( span ) ;
@@ -551,18 +564,6 @@ private static int byteBufferSize(string funcName, int nbytes, int bufLength, in
551564 }
552565 }
553566
554- private Exception MakeRecvException ( Exception e , SocketError errorCode = SocketError . InvalidArgument ) {
555- if ( e is ObjectDisposedException ) return MakeException ( _context , e ) ;
556-
557- // on the socket recv throw a special socket error code when SendTimeout is zero
558- if ( _socket . SendTimeout == 0 ) {
559- var s = new SocketException ( ( int ) errorCode ) ;
560- return PythonExceptions . CreateThrowable ( error , s . ErrorCode , s . Message ) ;
561- } else {
562- return MakeException ( _context , e ) ;
563- }
564- }
565-
566567 [ Documentation ( "send(string[, flags]) -> bytes_sent\n \n "
567568 + "Send data to the remote socket. The socket must be connected to a remote\n "
568569 + "socket (by calling either connect() or accept(). Returns the number of bytes\n "
@@ -1787,7 +1788,7 @@ internal static Exception MakeException(CodeContext/*!*/ context, Exception exce
17871788 }
17881789 } else if ( exception is ObjectDisposedException ) {
17891790 return PythonExceptions . CreateThrowable ( error , PythonErrorNumber . EBADF , "Socket is closed" ) ;
1790- } else if ( exception is InvalidOperationException ) {
1791+ } else if ( exception is InvalidOperationException or ArgumentException ) {
17911792 return MakeException ( context , new SocketException ( ( int ) SocketError . InvalidArgument ) ) ;
17921793 } else {
17931794 return exception ;
0 commit comments