@@ -417,7 +417,7 @@ public Bytes recv(int bufsize, int flags = 0) {
417417 try {
418418 bytesRead = _socket . Receive ( buffer , bufsize , ( SocketFlags ) flags ) ;
419419 } catch ( Exception e ) {
420- throw MakeRecvException ( e , SocketError . NotConnected ) ;
420+ throw MakeException ( _context , e ) ;
421421 }
422422
423423 var bytes = new byte [ bytesRead ] ;
@@ -453,7 +453,7 @@ public int recv_into([NotNone] IBufferProtocol buffer, int nbytes = 0, int flags
453453 try {
454454 bytesRead = _socket . Receive ( byteBuffer , nbytes , ( SocketFlags ) flags ) ;
455455 } catch ( Exception e ) {
456- throw MakeRecvException ( e , SocketError . NotConnected ) ;
456+ throw MakeException ( _context , e ) ;
457457 }
458458
459459 byteBuffer . AsSpan ( 0 , bytesRead ) . CopyTo ( span ) ;
@@ -490,7 +490,7 @@ public PythonTuple recvfrom(int bufsize, int flags = 0) {
490490 try {
491491 bytesRead = _socket . ReceiveFrom ( buffer , bufsize , ( SocketFlags ) flags , ref remoteEP ) ;
492492 } catch ( Exception e ) {
493- throw MakeRecvException ( e , SocketError . InvalidArgument ) ;
493+ throw MakeException ( _context , e ) ;
494494 }
495495
496496 var bytes = new byte [ bytesRead ] ;
@@ -523,7 +523,7 @@ public PythonTuple recvfrom_into([NotNone] IBufferProtocol buffer, int nbytes =
523523 try {
524524 bytesRead = _socket . ReceiveFrom ( byteBuffer , nbytes , ( SocketFlags ) flags , ref remoteEP ) ;
525525 } catch ( Exception e ) {
526- throw MakeRecvException ( e , SocketError . InvalidArgument ) ;
526+ throw MakeException ( _context , e ) ;
527527 }
528528
529529 byteBuffer . AsSpan ( 0 , bytesRead ) . CopyTo ( span ) ;
@@ -555,18 +555,6 @@ private static int byteBufferSize(string funcName, int nbytes, int bufLength, in
555555 }
556556 }
557557
558- private Exception MakeRecvException ( Exception e , SocketError errorCode = SocketError . InvalidArgument ) {
559- if ( e is ObjectDisposedException ) return MakeException ( _context , e ) ;
560-
561- // on the socket recv throw a special socket error code when SendTimeout is zero
562- if ( _socket . SendTimeout == 0 ) {
563- var s = new SocketException ( ( int ) errorCode ) ;
564- return PythonExceptions . CreateThrowable ( error , ( int ) errorCode , s . Message ) ;
565- } else {
566- return MakeException ( _context , e ) ;
567- }
568- }
569-
570558 [ Documentation ( "send(string[, flags]) -> bytes_sent\n \n "
571559 + "Send data to the remote socket. The socket must be connected to a remote\n "
572560 + "socket (by calling either connect() or accept(). Returns the number of bytes\n "
@@ -734,8 +722,9 @@ public void settimeout(object? timeout) {
734722 _socket . SendTimeout = ( int ) ( seconds * MillisecondsPerSecond ) ;
735723 _timeout = ( int ) ( seconds * MillisecondsPerSecond ) ;
736724 }
737- } finally {
738725 _socket . ReceiveTimeout = _socket . SendTimeout ;
726+ } catch ( ObjectDisposedException ex ) {
727+ throw MakeException ( _context , ex ) ;
739728 }
740729 }
741730
@@ -1595,7 +1584,6 @@ public static void setdefaulttimeout(CodeContext/*!*/ context, object? timeout)
15951584 public const int EAI_SERVICE = ( int ) SocketError . TypeNotFound ;
15961585 public const int EAI_SOCKTYPE = ( int ) SocketError . SocketNotSupported ;
15971586 public const int EAI_SYSTEM = ( int ) SocketError . SocketError ; // TODO: not on windows?
1598- public const int EBADF = ( int ) 0x9 ; // TODO: not in _socket in CPython, can we remove it?
15991587 public const int INADDR_ALLHOSTS_GROUP = unchecked ( ( int ) 0xe0000001 ) ;
16001588 public const int INADDR_ANY = ( int ) 0x00000000 ;
16011589 public const int INADDR_BROADCAST = unchecked ( ( int ) 0xFFFFFFFF ) ;
@@ -1717,17 +1705,19 @@ public static void setdefaulttimeout(CodeContext/*!*/ context, object? timeout)
17171705 /// </summary>
17181706 internal static Exception MakeException ( CodeContext /*!*/ context , Exception exception ) {
17191707 // !!! this shouldn't just blindly set the type to error (see summary)
1720- if ( exception is SocketException ) {
1721- SocketException se = ( SocketException ) exception ;
1708+ if ( exception is SocketException se ) {
17221709 switch ( se . SocketErrorCode ) {
17231710 case SocketError . NotConnected : // CPython times out when the socket isn't connected.
17241711 case SocketError . TimedOut :
17251712 return PythonExceptions . CreateThrowable ( timeout ( context ) , ( int ) se . SocketErrorCode , se . Message ) ;
1713+ case SocketError . WouldBlock :
1714+ return PythonExceptions . CreateThrowable ( error , se . ErrorCode , se . Message ) ;
17261715 default :
17271716 return PythonExceptions . CreateThrowable ( error , ( int ) se . SocketErrorCode , se . Message ) ;
17281717 }
17291718 } else if ( exception is ObjectDisposedException ) {
1730- return PythonExceptions . CreateThrowable ( error , ( int ) EBADF , "the socket is closed" ) ;
1719+ const int EBADF = 0x9 ;
1720+ return PythonExceptions . CreateThrowable ( error , EBADF , "the socket is closed" ) ;
17311721 } else if ( exception is InvalidOperationException ) {
17321722 return MakeException ( context , new SocketException ( ( int ) SocketError . InvalidArgument ) ) ;
17331723 } else {
0 commit comments