Skip to content

Commit ba425c8

Browse files
authored
Fix some assertion errors (#1619)
* Fix some assertion errors * Add tests
1 parent 1196691 commit ba425c8

3 files changed

Lines changed: 20 additions & 19 deletions

File tree

Src/IronPython.Modules/select.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
4+
45
#if FEATURE_SYNC_SOCKETS
56

67
using System;
78
using System.Collections;
89
using System.Collections.Generic;
9-
using System.Runtime.InteropServices;
10-
using Microsoft.Scripting;
10+
using System.Net.Sockets;
1111

1212
using IronPython.Runtime;
1313
using IronPython.Runtime.Exceptions;
1414
using IronPython.Runtime.Operations;
1515
using IronPython.Runtime.Types;
1616

1717
using Microsoft.Scripting.Runtime;
18-
using System.Runtime.CompilerServices;
19-
using Microsoft.Scripting.Utils;
20-
21-
using System.Net.Sockets;
2218

2319
[assembly: PythonModule("select", typeof(IronPython.Modules.PythonSelect))]
2420
namespace IronPython.Modules {
@@ -67,9 +63,9 @@ public static PythonTuple select(CodeContext/*!*/ context, object iwtd, object o
6763
try {
6864
Socket.Select(readerList, writerList, errorList, timeoutMicroseconds);
6965
} catch (ArgumentNullException) {
70-
throw MakeException(context, SocketExceptionToTuple(new SocketException((int)SocketError.InvalidArgument)));
66+
throw PythonSocket.MakeException(context, new SocketException((int)SocketError.InvalidArgument));
7167
} catch (SocketException e) {
72-
throw MakeException(context, SocketExceptionToTuple(e));
68+
throw PythonSocket.MakeException(context, e);
7369
}
7470

7571
// Convert back to what the user originally passed in
@@ -80,14 +76,6 @@ public static PythonTuple select(CodeContext/*!*/ context, object iwtd, object o
8076
return PythonTuple.MakeTuple(readerList, writerList, errorList);
8177
}
8278

83-
private static PythonTuple SocketExceptionToTuple(SocketException e) {
84-
return PythonTuple.MakeTuple(e.ErrorCode, e.Message);
85-
}
86-
87-
private static Exception MakeException(CodeContext/*!*/ context, object value) {
88-
return PythonExceptions.CreateThrowable((PythonType)context.LanguageContext.GetModuleState("selecterror"), value);
89-
}
90-
9179
/// <summary>
9280
/// Process a sequence of objects that are compatible with ObjectToSocket(). Return two
9381
/// 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) {
137125
}
138126
socket = PythonSocket.socket.HandleToSocket(handle);
139127
if (socket == null) {
140-
SocketException e = new SocketException((int)SocketError.NotSocket);
141-
throw PythonExceptions.CreateThrowable((PythonType)context.LanguageContext.GetModuleState("selecterror"), PythonTuple.MakeTuple(e.ErrorCode, e.Message));
128+
throw PythonSocket.MakeException(context, new SocketException((int)SocketError.NotSocket));
142129
}
143130
return socket;
144131
}

Src/IronPython/Runtime/Operations/IntOps.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public static string __format__(CodeContext/*!*/ context, int self, [NotNone] st
288288
throw PythonOps.ValueError("Unknown format code '{0}' for object of type 'int'", spec.TypeRepr);
289289
}
290290

291-
Debug.Assert(digits[0] != '-');
291+
Debug.Assert(spec.Type == 'c' || digits[0] != '-');
292292

293293
return spec.AlignNumericText(digits, self == 0, self > 0);
294294
}

Tests/test_regressions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,4 +1683,18 @@ def d01(a=1): pass
16831683
exec('def f(' + ','.join('a{0}'.format(i) for i in range(size)) + '): pass', d)
16841684
d["f"](*range(size)) # just make sure this runs successfully
16851685

1686+
def test_ipy3_gh1614(self):
1687+
# https://github.com/IronLanguages/ironpython3/issues/1614
1688+
1689+
# this was causing an assertion error in DEBUG builds
1690+
self.assertEqual('{:c}'.format(0x2d), '-')
1691+
1692+
def test_ipy3_gh1615(self):
1693+
# https://github.com/IronLanguages/ironpython3/issues/1615
1694+
1695+
# this was causing an assertion error in DEBUG builds
1696+
import select
1697+
with self.assertRaises(OSError):
1698+
select.select([], [], [])
1699+
16861700
run_test(__name__)

0 commit comments

Comments
 (0)