From 2732c7fb3342372827c9fc920ce7ab3a73d72222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lozier?= Date: Mon, 12 Dec 2022 19:49:40 -0500 Subject: [PATCH 1/2] Fix some assertion errors --- Src/IronPython.Modules/select.cs | 23 +++++---------------- Src/IronPython/Runtime/Operations/IntOps.cs | 2 +- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/Src/IronPython.Modules/select.cs b/Src/IronPython.Modules/select.cs index 5b302eae0..f5f8c2ed3 100644 --- a/Src/IronPython.Modules/select.cs +++ b/Src/IronPython.Modules/select.cs @@ -1,13 +1,13 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. + #if FEATURE_SYNC_SOCKETS using System; using System.Collections; using System.Collections.Generic; -using System.Runtime.InteropServices; -using Microsoft.Scripting; +using System.Net.Sockets; using IronPython.Runtime; using IronPython.Runtime.Exceptions; @@ -15,10 +15,6 @@ using IronPython.Runtime.Types; using Microsoft.Scripting.Runtime; -using System.Runtime.CompilerServices; -using Microsoft.Scripting.Utils; - -using System.Net.Sockets; [assembly: PythonModule("select", typeof(IronPython.Modules.PythonSelect))] namespace IronPython.Modules { @@ -67,9 +63,9 @@ public static PythonTuple select(CodeContext/*!*/ context, object iwtd, object o try { Socket.Select(readerList, writerList, errorList, timeoutMicroseconds); } catch (ArgumentNullException) { - throw MakeException(context, SocketExceptionToTuple(new SocketException((int)SocketError.InvalidArgument))); + throw PythonSocket.MakeException(context, new SocketException((int)SocketError.InvalidArgument)); } catch (SocketException e) { - throw MakeException(context, SocketExceptionToTuple(e)); + throw PythonSocket.MakeException(context, e); } // Convert back to what the user originally passed in @@ -80,14 +76,6 @@ public static PythonTuple select(CodeContext/*!*/ context, object iwtd, object o return PythonTuple.MakeTuple(readerList, writerList, errorList); } - private static PythonTuple SocketExceptionToTuple(SocketException e) { - return PythonTuple.MakeTuple(e.ErrorCode, e.Message); - } - - private static Exception MakeException(CodeContext/*!*/ context, object value) { - return PythonExceptions.CreateThrowable((PythonType)context.LanguageContext.GetModuleState("selecterror"), value); - } - /// /// Process a sequence of objects that are compatible with ObjectToSocket(). Return two /// things as out params: an in-order List of sockets that correspond to the original @@ -137,8 +125,7 @@ private static Socket ObjectToSocket(CodeContext context, object obj) { } socket = PythonSocket.socket.HandleToSocket(handle); if (socket == null) { - SocketException e = new SocketException((int)SocketError.NotSocket); - throw PythonExceptions.CreateThrowable((PythonType)context.LanguageContext.GetModuleState("selecterror"), PythonTuple.MakeTuple(e.ErrorCode, e.Message)); + throw PythonSocket.MakeException(context, new SocketException((int)SocketError.NotSocket)); } return socket; } diff --git a/Src/IronPython/Runtime/Operations/IntOps.cs b/Src/IronPython/Runtime/Operations/IntOps.cs index e05c0ca97..5edf33255 100644 --- a/Src/IronPython/Runtime/Operations/IntOps.cs +++ b/Src/IronPython/Runtime/Operations/IntOps.cs @@ -288,7 +288,7 @@ public static string __format__(CodeContext/*!*/ context, int self, [NotNone] st throw PythonOps.ValueError("Unknown format code '{0}' for object of type 'int'", spec.TypeRepr); } - Debug.Assert(digits[0] != '-'); + Debug.Assert(spec.Type == 'c' || digits[0] != '-'); return spec.AlignNumericText(digits, self == 0, self > 0); } From edd12bb61606a88be7ae31e91351e26bc6149f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lozier?= Date: Tue, 13 Dec 2022 21:48:29 -0500 Subject: [PATCH 2/2] Add tests --- Tests/test_regressions.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Tests/test_regressions.py b/Tests/test_regressions.py index c3408a8e0..63256dd7d 100644 --- a/Tests/test_regressions.py +++ b/Tests/test_regressions.py @@ -1683,4 +1683,18 @@ def d01(a=1): pass exec('def f(' + ','.join('a{0}'.format(i) for i in range(size)) + '): pass', d) d["f"](*range(size)) # just make sure this runs successfully + def test_ipy3_gh1614(self): + # https://github.com/IronLanguages/ironpython3/issues/1614 + + # this was causing an assertion error in DEBUG builds + self.assertEqual('{:c}'.format(0x2d), '-') + + def test_ipy3_gh1615(self): + # https://github.com/IronLanguages/ironpython3/issues/1615 + + # this was causing an assertion error in DEBUG builds + import select + with self.assertRaises(OSError): + select.select([], [], []) + run_test(__name__)