Skip to content

Commit bad9298

Browse files
authored
Changes for .NET 7 (#1537)
1 parent dd351ba commit bad9298

4 files changed

Lines changed: 43 additions & 34 deletions

File tree

Src/DLR

Src/IronPython.Modules/_socket.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -716,22 +716,25 @@ public void setblocking(int shouldBlock) {
716716
// NOTE: The above IronPython specific timeout behavior is due to the underlying
717717
// .Net Socket.SendTimeout behavior and is outside of our control.
718718
public void settimeout(object? timeout) {
719-
try {
720-
if (timeout == null) {
721-
_socket.Blocking = true;
722-
_socket.SendTimeout = 0;
723-
} else {
724-
double seconds;
725-
seconds = Converter.ConvertToDouble(timeout);
726-
if (seconds < 0) {
727-
throw PythonOps.ValueError("Timeout value out of range");
728-
}
729-
_socket.Blocking = seconds > 0; // 0 timeout means non-blocking mode
730-
_socket.SendTimeout = (int)(seconds * MillisecondsPerSecond);
731-
_timeout = (int)(seconds * MillisecondsPerSecond);
719+
bool blocking = true;
720+
int timeoutVal = 0;
721+
if (timeout is not null) {
722+
double seconds;
723+
seconds = Converter.ConvertToDouble(timeout);
724+
if (seconds < 0) {
725+
throw PythonOps.ValueError("Timeout value out of range");
732726
}
733-
} finally {
727+
blocking = seconds > 0; // 0 timeout means non-blocking mode
728+
timeoutVal = (int)(seconds * MillisecondsPerSecond);
729+
}
730+
731+
try {
732+
_socket.Blocking = blocking;
733+
_socket.SendTimeout = timeoutVal;
734734
_socket.ReceiveTimeout = _socket.SendTimeout;
735+
_timeout = timeoutVal;
736+
} catch (Exception ex) {
737+
throw MakeException(_context, ex);
735738
}
736739
}
737740

Src/IronPython.Modules/_ssl.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ public void do_handshake() {
451451
}
452452
_sslStream.AuthenticateAsServer(_cert, _certsMode == PythonSsl.CERT_REQUIRED, enabledSslProtocols, false);
453453
} else {
454-
_sslStream.AuthenticateAsClient(server_hostname ?? _socket._hostName, context._cert_store, enabledSslProtocols, false);
454+
_sslStream.AuthenticateAsClient(server_hostname ?? _socket._hostName ?? string.Empty, context._cert_store, enabledSslProtocols, false);
455455
}
456456
} catch (AuthenticationException e) {
457457
((IDisposable)_socket._socket).Dispose();
@@ -479,12 +479,13 @@ TLSv1.1 no no yes no yes no
479479
TLSv1.2 no no yes no no yes
480480
*/
481481

482+
#pragma warning disable CA5397 // Do not use deprecated SslProtocols values
483+
#pragma warning disable CS0618 // Type or member is obsolete
484+
#pragma warning disable SYSLIB0039 // Type or member is obsolete
485+
482486
private static SslProtocols GetProtocolType(int protocol, int options) {
483487
SslProtocols result = SslProtocols.None;
484-
485488
switch (protocol) {
486-
#pragma warning disable CA5397 // Do not use deprecated SslProtocols values
487-
#pragma warning disable CS0618 // Type or member is obsolete
488489
case PythonSsl.PROTOCOL_SSLv2:
489490
result = SslProtocols.Ssl2;
490491
break;
@@ -494,33 +495,31 @@ private static SslProtocols GetProtocolType(int protocol, int options) {
494495
case PythonSsl.PROTOCOL_SSLv23:
495496
result = SslProtocols.Ssl2 | SslProtocols.Ssl3 | SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12;
496497
break;
497-
#pragma warning restore CS0618 // Type or member is obsolete
498498
case PythonSsl.PROTOCOL_TLSv1:
499499
result = SslProtocols.Tls;
500500
break;
501501
case PythonSsl.PROTOCOL_TLSv1_1:
502502
result = SslProtocols.Tls11;
503503
break;
504-
#pragma warning restore CA5397 // Do not use deprecated SslProtocols values
505504
case PythonSsl.PROTOCOL_TLSv1_2:
506505
result = SslProtocols.Tls12;
507506
break;
508507
default:
509508
throw new InvalidOperationException("bad ssl protocol type: " + protocol);
510509
}
511510
// Filter out requested protocol exclusions:
512-
#pragma warning disable CA5397 // Do not use deprecated SslProtocols values
513-
#pragma warning disable CS0618 // Type or member is obsolete
514511
result &= (options & PythonSsl.OP_NO_SSLv3) != 0 ? ~SslProtocols.Ssl3 : ~SslProtocols.None;
515512
result &= (options & PythonSsl.OP_NO_SSLv2) != 0 ? ~SslProtocols.Ssl2 : ~SslProtocols.None;
516-
#pragma warning restore CS0618 // Type or member is obsolete
517513
result &= (options & PythonSsl.OP_NO_TLSv1) != 0 ? ~SslProtocols.Tls : ~SslProtocols.None;
518514
result &= (options & PythonSsl.OP_NO_TLSv1_1) != 0 ? ~SslProtocols.Tls11 : ~SslProtocols.None;
519515
result &= (options & PythonSsl.OP_NO_TLSv1_2) != 0 ? ~SslProtocols.Tls12 : ~SslProtocols.None;
520-
#pragma warning restore CA5397 // Do not use deprecated SslProtocols values
521516
return result;
522517
}
523518

519+
#pragma warning restore SYSLIB0039 // Type or member is obsolete
520+
#pragma warning restore CS0618 // Type or member is obsolete
521+
#pragma warning restore CA5397 // Do not use deprecated SslProtocols values
522+
524523
public PythonTuple cipher() {
525524
if (_sslStream != null && _sslStream.IsAuthenticated) {
526525
return PythonTuple.MakeTuple(
@@ -534,19 +533,23 @@ public PythonTuple cipher() {
534533

535534
public object compression() => null; // TODO
536535

537-
private string ProtocolToPython() {
538-
switch (_sslStream.SslProtocol) {
539536
#pragma warning disable CA5397 // Do not use deprecated SslProtocols values
540537
#pragma warning disable CS0618 // Type or member is obsolete
538+
#pragma warning disable SYSLIB0039 // Type or member is obsolete
539+
540+
private string ProtocolToPython() {
541+
switch (_sslStream.SslProtocol) {
541542
case SslProtocols.Ssl2: return "SSLv2";
542543
case SslProtocols.Ssl3: return "TLSv1/SSLv3";
543-
#pragma warning restore CS0618 // Type or member is obsolete
544544
case SslProtocols.Tls: return "TLSv1";
545-
#pragma warning restore CA5397 // Do not use deprecated SslProtocols values
546545
default: return _sslStream.SslProtocol.ToString();
547546
}
548547
}
549548

549+
#pragma warning restore SYSLIB0039 // Type or member is obsolete
550+
#pragma warning restore CS0618 // Type or member is obsolete
551+
#pragma warning restore CA5397 // Do not use deprecated SslProtocols values
552+
550553
public object peer_certificate(bool binary_form) {
551554
var peerCert = _sslStream?.RemoteCertificate;
552555

Tests/test_ssl_stdlib.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import unittest
1010
import sys
1111

12-
from iptest import is_posix, run_test
12+
from iptest import is_mono, is_osx, is_posix, run_test
1313

1414
import test.test_ssl
1515

@@ -81,11 +81,11 @@ def load_tests(loader, standard_tests, pattern):
8181
else:
8282
suite.addTest(test.test_ssl.NetworkedTests('test_makefile_close'))
8383
suite.addTest(unittest.expectedFailure(test.test_ssl.NetworkedTests('test_non_blocking_connect_ex'))) # OSError: [Errno -2146232800] The operation is not allowed on a non-blocking Socket.
84-
suite.addTest(unittest.expectedFailure(test.test_ssl.NetworkedTests('test_non_blocking_handshake'))) # TypeError: Value cannot be null.
84+
suite.addTest(test.test_ssl.NetworkedTests('test_non_blocking_handshake'))
8585
suite.addTest(test.test_ssl.NetworkedTests('test_timeout_connect_ex'))
8686
suite.addTest(unittest.expectedFailure(test.test_ssl.SSLErrorTests('test_lib_reason'))) # AttributeError: 'SSLContext' object has no attribute 'load_dh_params'
8787
suite.addTest(unittest.expectedFailure(test.test_ssl.SSLErrorTests('test_str'))) # AssertionError: '[Errno 1] foo' != 'foo'
88-
suite.addTest(unittest.expectedFailure(test.test_ssl.SSLErrorTests('test_subclass'))) # TypeError: Value cannot be null.
88+
#suite.addTest(test.test_ssl.SSLErrorTests('test_subclass')) # blocking
8989
#suite.addTest(test.test_ssl.ThreadedTests('test_asyncore_server')) # blocking
9090
#suite.addTest(test.test_ssl.ThreadedTests('test_check_hostname'))
9191
#suite.addTest(test.test_ssl.ThreadedTests('test_compression'))
@@ -99,7 +99,7 @@ def load_tests(loader, standard_tests, pattern):
9999
suite.addTest(unittest.expectedFailure(test.test_ssl.ThreadedTests('test_empty_cert'))) # NotImplementedError: keyfile
100100
#suite.addTest(test.test_ssl.ThreadedTests('test_getpeercert')) # blocking
101101
suite.addTest(test.test_ssl.ThreadedTests('test_getpeercert_enotconn'))
102-
suite.addTest(unittest.expectedFailure(test.test_ssl.ThreadedTests('test_handshake_timeout'))) # TypeError: Value cannot be null.
102+
#suite.addTest(test.test_ssl.ThreadedTests('test_handshake_timeout')) # blocking
103103
suite.addTest(unittest.expectedFailure(test.test_ssl.ThreadedTests('test_malformed_cert'))) # NotImplementedError: keyfile
104104
suite.addTest(unittest.expectedFailure(test.test_ssl.ThreadedTests('test_malformed_key'))) # NotImplementedError: keyfile
105105
suite.addTest(unittest.expectedFailure(test.test_ssl.ThreadedTests('test_nonexisting_cert'))) # NotImplementedError: keyfile
@@ -112,7 +112,10 @@ def load_tests(loader, standard_tests, pattern):
112112
#suite.addTest(test.test_ssl.ThreadedTests('test_protocol_tlsv1_2'))
113113
#suite.addTest(test.test_ssl.ThreadedTests('test_read_write_after_close_raises_valuerror')) # blocking
114114
suite.addTest(unittest.expectedFailure(test.test_ssl.ThreadedTests('test_recv_send'))) # NotImplementedError: keyfile
115-
suite.addTest(unittest.expectedFailure(test.test_ssl.ThreadedTests('test_rude_shutdown'))) # TypeError: Value cannot be null.
115+
if is_mono and is_osx:
116+
suite.addTest(unittest.expectedFailure(test.test_ssl.ThreadedTests('test_rude_shutdown'))) # ValueError: Value does not fall within the expected range.
117+
else:
118+
suite.addTest(test.test_ssl.ThreadedTests('test_rude_shutdown'))
116119
#suite.addTest(test.test_ssl.ThreadedTests('test_selected_npn_protocol'))
117120
#suite.addTest(test.test_ssl.ThreadedTests('test_server_accept'))
118121
suite.addTest(unittest.expectedFailure(test.test_ssl.ThreadedTests('test_sni_callback'))) # AttributeError: 'SSLContext' object has no attribute 'set_servername_callback'

0 commit comments

Comments
 (0)