Skip to content

Commit aa20681

Browse files
committed
Enable nullable on some modules
1 parent 82873fc commit aa20681

24 files changed

+115
-118
lines changed

src/core/IronPython.Modules/SimpleSignalState.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using IronPython.Runtime;
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#nullable enable
66

77
#if FEATURE_PROCESS
88

9+
using System;
10+
11+
using IronPython.Runtime;
12+
913
namespace IronPython.Modules {
1014
public static partial class PythonSignal {
1115
internal class SimpleSignalState : PythonSignalState {
@@ -14,9 +18,9 @@ public SimpleSignalState(PythonContext pc)
1418
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
1519
}
1620

17-
private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) {
21+
private void Console_CancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
1822
int pySignal;
19-
switch(e.SpecialKey) {
23+
switch (e.SpecialKey) {
2024
case ConsoleSpecialKey.ControlC:
2125
pySignal = SIGINT;
2226
break;
@@ -28,7 +32,7 @@ private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) {
2832
default:
2933
throw new InvalidOperationException("unreachable");
3034
}
31-
35+
3236
lock (PySignalToPyHandler) {
3337
if (PySignalToPyHandler[pySignal].GetType() == typeof(int)) {
3438
int tempId = (int)PySignalToPyHandler[pySignal];

src/core/IronPython.Modules/_bisect.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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.
44

5+
#nullable enable
6+
57
using System;
68
using System.Collections;
79
using System.Reflection;
@@ -29,9 +31,9 @@ common approach.
2931

3032
#region Private Implementation Details
3133

32-
private static int InternalBisectLeft(CodeContext/*!*/ context, PythonList list, object item, int lo, int hi) {
34+
private static int InternalBisectLeft(CodeContext/*!*/ context, PythonList list, object? item, int lo, int hi) {
3335
int mid;
34-
object litem;
36+
object? litem;
3537

3638
if (lo < 0) {
3739
throw PythonOps.ValueError("lo must be non-negative");
@@ -52,7 +54,7 @@ private static int InternalBisectLeft(CodeContext/*!*/ context, PythonList list,
5254
return lo;
5355
}
5456

55-
private static int InternalBisectLeft(CodeContext/*!*/ context, object list, object item, int lo, int hi) {
57+
private static int InternalBisectLeft(CodeContext/*!*/ context, object? list, object? item, int lo, int hi) {
5658
int mid;
5759
object litem;
5860

@@ -63,6 +65,7 @@ private static int InternalBisectLeft(CodeContext/*!*/ context, object list, obj
6365
if (hi == -1) {
6466
hi = PythonOps.Length(list);
6567
}
68+
6669
IComparer comparer = context.LanguageContext.GetLtComparer(GetComparisonType(context, list));
6770
while (lo < hi) {
6871
mid = (int)(((long)lo + hi) / 2);
@@ -75,8 +78,8 @@ private static int InternalBisectLeft(CodeContext/*!*/ context, object list, obj
7578
return lo;
7679
}
7780

78-
private static int InternalBisectRight(CodeContext/*!*/ context, PythonList list, object item, int lo, int hi) {
79-
object litem;
81+
private static int InternalBisectRight(CodeContext/*!*/ context, PythonList list, object? item, int lo, int hi) {
82+
object? litem;
8083
int mid;
8184

8285
if (lo < 0) {
@@ -98,7 +101,7 @@ private static int InternalBisectRight(CodeContext/*!*/ context, PythonList list
98101
return lo;
99102
}
100103

101-
private static int InternalBisectRight(CodeContext/*!*/ context, object list, object item, int lo, int hi) {
104+
private static int InternalBisectRight(CodeContext/*!*/ context, object list, object? item, int lo, int hi) {
102105
object litem;
103106
int mid;
104107

@@ -122,7 +125,7 @@ private static int InternalBisectRight(CodeContext/*!*/ context, object list, ob
122125
return lo;
123126
}
124127

125-
private static Type GetComparisonType(CodeContext/*!*/ context, object a) {
128+
private static Type GetComparisonType(CodeContext/*!*/ context, object? a) {
126129
if (PythonOps.Length(a) > 0) {
127130
// use the 1st index to determine the type - we're assuming lists are
128131
// homogeneous
@@ -183,7 +186,7 @@ before the leftmost x already there.
183186
Optional args lo (default 0) and hi (default len(a)) bound the
184187
slice of a to be searched.
185188
")]
186-
public static object bisect_left(CodeContext/*!*/ context, object a, object x, int lo = 0, int hi = -1) {
189+
public static object bisect_left(CodeContext/*!*/ context, object? a, object? x, int lo = 0, int hi = -1) {
187190
if (a is PythonList l && l.GetType() == typeof(PythonList)) {
188191
return InternalBisectLeft(context, l, x, lo, hi);
189192
}
@@ -201,7 +204,7 @@ public static object bisect_left(CodeContext/*!*/ context, object a, object x, i
201204
Optional args lo (default 0) and hi (default len(a)) bound the
202205
slice of a to be searched.
203206
")]
204-
public static void InsortRight(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
207+
public static void InsortRight(CodeContext/*!*/ context, object a, object x, int lo = 0, int hi = -1) {
205208
if (a is PythonList l && l.GetType() == typeof(PythonList)) {
206209
l.Insert(InternalBisectRight(context, l, x, lo, hi), x);
207210
return;
@@ -227,7 +230,7 @@ public static void InsortRight(CodeContext/*!*/ context, object a, object x, int
227230
Optional args lo (default 0) and hi (default len(a)) bound the
228231
slice of a to be searched.
229232
")]
230-
public static void insort_left(CodeContext/*!*/ context, object a, object x, int lo=0, int hi=-1) {
233+
public static void insort_left(CodeContext/*!*/ context, object? a, object? x, int lo = 0, int hi = -1) {
231234
if (a is PythonList l && l.GetType() == typeof(PythonList)) {
232235
l.Insert(InternalBisectLeft(context, l, x, lo, hi), x);
233236
return;

src/core/IronPython.Modules/_heapq.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
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.
44

5+
#nullable enable
6+
57
using System;
68
using System.Collections;
7-
using System.Collections.Generic;
8-
using System.Text;
9-
using Microsoft.Scripting.Runtime;
9+
1010
using IronPython.Runtime;
11-
using IronPython.Runtime.Binding;
1211
using IronPython.Runtime.Operations;
1312
using IronPython.Runtime.Types;
1413

14+
using Microsoft.Scripting.Runtime;
15+
1516
[assembly: PythonModule("_heapq", typeof(IronPython.Modules.PythonHeapq))]
1617
namespace IronPython.Modules {
1718
public static class PythonHeapq {
@@ -21,14 +22,14 @@ public static class PythonHeapq {
2122
#region public API
2223

2324
[Documentation("Transform list into a heap, in-place, in O(len(heap)) time.")]
24-
public static void heapify(CodeContext/*!*/ context, PythonList list) {
25+
public static void heapify(CodeContext/*!*/ context, [NotNone] PythonList list) {
2526
lock (list) {
2627
DoHeapify(context, list);
2728
}
2829
}
2930

3031
[Documentation("Pop the smallest item off the heap, maintaining the heap invariant.")]
31-
public static object heappop(CodeContext/*!*/ context, PythonList list) {
32+
public static object? heappop(CodeContext/*!*/ context, [NotNone] PythonList list) {
3233
lock (list) {
3334
int last = list._size - 1;
3435
if (last < 0) {
@@ -42,7 +43,7 @@ public static object heappop(CodeContext/*!*/ context, PythonList list) {
4243
}
4344

4445
[Documentation("Push item onto heap, maintaining the heap invariant.")]
45-
public static void heappush(CodeContext/*!*/ context, PythonList list, object item) {
46+
public static void heappush(CodeContext/*!*/ context, [NotNone] PythonList list, object? item) {
4647
lock (list) {
4748
list.AddNoLock(item);
4849
SiftUp(context, list, list._size - 1);
@@ -53,7 +54,7 @@ public static void heappush(CodeContext/*!*/ context, PythonList list, object it
5354
+ "from the heap. The combined action runs more efficiently than\n"
5455
+ "heappush() followed by a separate call to heappop()."
5556
)]
56-
public static object heappushpop(CodeContext/*!*/ context, PythonList list, object item) {
57+
public static object? heappushpop(CodeContext/*!*/ context, [NotNone] PythonList list, object? item) {
5758
lock (list) {
5859
return DoPushPop(context, list, item);
5960
}
@@ -67,9 +68,9 @@ public static object heappushpop(CodeContext/*!*/ context, PythonList list, obje
6768
+ " if item > heap[0]:\n"
6869
+ " item = heapreplace(heap, item)\n"
6970
)]
70-
public static object heapreplace(CodeContext/*!*/ context, PythonList list, object item) {
71+
public static object? heapreplace(CodeContext/*!*/ context, [NotNone] PythonList list, object? item) {
7172
lock (list) {
72-
object ret = list._data[0];
73+
object? ret = list._data[0];
7374
list._data[0] = item;
7475
SiftDown(context, list, 0, list._size - 1);
7576
return ret;
@@ -80,7 +81,7 @@ public static object heapreplace(CodeContext/*!*/ context, PythonList list, obje
8081
[Documentation("Find the n largest elements in a dataset.\n\n"
8182
+ "Equivalent to: sorted(iterable, reverse=True)[:n]\n"
8283
)]
83-
public static PythonList nlargest(CodeContext/*!*/ context, int n, object iterable) {
84+
public static PythonList nlargest(CodeContext/*!*/ context, int n, object? iterable) {
8485
if (n <= 0) {
8586
return new PythonList();
8687
}
@@ -113,7 +114,7 @@ public static PythonList nlargest(CodeContext/*!*/ context, int n, object iterab
113114
[Documentation("Find the n smallest elements in a dataset.\n\n"
114115
+ "Equivalent to: sorted(iterable)[:n]\n"
115116
)]
116-
public static PythonList nsmallest(CodeContext/*!*/ context, int n, object iterable) {
117+
public static PythonList nsmallest(CodeContext/*!*/ context, int n, object? iterable) {
117118
if (n <= 0) {
118119
return new PythonList();
119120
}
@@ -146,7 +147,7 @@ public static PythonList nsmallest(CodeContext/*!*/ context, int n, object itera
146147

147148
#region private implementation details (NOTE: thread-unsafe)
148149

149-
private static bool IsLessThan(CodeContext/*!*/ context, object x, object y) {
150+
private static bool IsLessThan(CodeContext/*!*/ context, object? x, object? y) {
150151
object ret;
151152
if (PythonTypeOps.TryInvokeBinaryOperator(context, x, y, "__lt__", out ret) &&
152153
!Object.ReferenceEquals(ret, NotImplementedType.Value)) {
@@ -206,8 +207,8 @@ private static void DoHeapifyMax(CodeContext/*!*/ context, PythonList list) {
206207
}
207208
}
208209

209-
private static object DoPushPop(CodeContext/*!*/ context, PythonList heap, object item) {
210-
object first;
210+
private static object? DoPushPop(CodeContext/*!*/ context, PythonList heap, object? item) {
211+
object? first;
211212
if (heap._size == 0 || !IsLessThan(context, first = heap._data[0], item)) {
212213
return item;
213214
}
@@ -216,8 +217,8 @@ private static object DoPushPop(CodeContext/*!*/ context, PythonList heap, objec
216217
return first;
217218
}
218219

219-
private static object DoPushPopMax(CodeContext/*!*/ context, PythonList heap, object item) {
220-
object first;
220+
private static object? DoPushPopMax(CodeContext/*!*/ context, PythonList heap, object? item) {
221+
object? first;
221222
if (heap._size == 0 || !IsLessThan(context, item, first = heap._data[0])) {
222223
return item;
223224
}

src/core/IronPython.Modules/_multiprocessing.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
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.
44

5+
#nullable enable
6+
57
using System;
68
using System.Net.Sockets;
79
using System.Runtime.InteropServices;
810
using System.Runtime.Versioning;
911

10-
using Microsoft.Win32.SafeHandles;
11-
1212
using IronPython.Runtime;
1313

1414
[assembly: PythonModule("_multiprocessing", typeof(IronPython.Modules.MultiProcessing))]
1515
namespace IronPython.Modules {
1616
public static class MultiProcessing {
1717
// TODO: implement SemLock and sem_unlink
1818

19-
public static object flags { get; set; } = new PythonDictionary();
19+
public static object? flags { get; set; } = new PythonDictionary();
2020

2121
[DllImport("ws2_32.dll", ExactSpelling = true, SetLastError = true)]
2222
private static extern SocketError closesocket([In] IntPtr socketHandle);
2323

2424
[SupportedOSPlatform("windows"), PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
25-
public static object closesocket(int handle) {
25+
public static object? closesocket(int handle) {
2626
var error = closesocket(new IntPtr(handle));
2727
// TODO: raise error
2828
return null;

src/core/IronPython.Modules/_opcode.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@
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.
44

5-
using System;
6-
using System.Collections;
7-
using System.Collections.Generic;
8-
using System.Diagnostics;
9-
using System.Numerics;
10-
using System.Text;
11-
12-
using Microsoft.Scripting.Utils;
5+
#nullable enable
136

147
using IronPython.Runtime;
15-
using IronPython.Runtime.Exceptions;
168
using IronPython.Runtime.Operations;
17-
using IronPython.Runtime.Types;
189

1910
[assembly: PythonModule("_opcode", typeof(IronPython.Modules.PythonOpcode))]
2011
namespace IronPython.Modules {
@@ -156,7 +147,7 @@ public static class PythonOpcode {
156147
private const int FVS_MASK = 0x4;
157148
private const int FVS_HAVE_SPEC = 0x4;
158149

159-
public static int stack_effect(CodeContext context, int opcode, object oparg=null) {
150+
public static int stack_effect(CodeContext context, int opcode, object? oparg = null) {
160151
int ioparg = 0;
161152

162153
if (opcode >= HAVE_ARGUMENT) {

src/core/IronPython.Modules/_overlapped.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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.
44

5+
#nullable enable
6+
57
using System;
68
using System.Numerics;
79
using System.Runtime.InteropServices;

src/core/IronPython.Modules/_random.RandomGen.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
// Copyright(c) .NET Foundation and Contributors
2-
//
3-
// Permission is hereby granted, free of charge, to any person obtaining a copy
4-
// of this software and associated documentation files (the "Software"), to deal
5-
// in the Software without restriction, including without limitation the rights
6-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7-
// copies of the Software, and to permit persons to whom the Software is
8-
// furnished to do so, subject to the following conditions:
9-
//
10-
// The above copyright notice and this permission notice shall be included in all
11-
// copies or substantial portions of the Software.
12-
//
13-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19-
// SOFTWARE.
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
205

216
using System;
227

src/core/IronPython.Modules/_random.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
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.
44

5+
#nullable enable
6+
57
using System;
8+
using System.Diagnostics.CodeAnalysis;
69
using System.Linq;
7-
using System.Numerics;
810

911
using IronPython.Runtime;
1012
using IronPython.Runtime.Operations;
13+
1114
using Microsoft.Scripting.Utils;
1215

1316
[assembly: PythonModule("_random", typeof(IronPython.Modules.PythonRandom))]
@@ -19,7 +22,7 @@ public static partial class PythonRandom {
1922
public class Random {
2023
private RandomGen _rnd;
2124

22-
public Random(object seed = null) {
25+
public Random(object? seed = null) {
2326
this.seed(seed);
2427
}
2528

@@ -57,7 +60,8 @@ public object random() {
5760
}
5861
}
5962

60-
public void seed(object s = null) {
63+
[MemberNotNull(nameof(_rnd))]
64+
public void seed(object? s = null) {
6165
int newSeed;
6266
switch (s) {
6367
case null:
@@ -79,7 +83,7 @@ public void seed(object s = null) {
7983
}
8084
}
8185

82-
public void setstate(PythonTuple state) {
86+
public void setstate([NotNone] PythonTuple state) {
8387
if (state.Count != 58) throw PythonOps.ValueError("state vector is the wrong size");
8488
var stateArray = state._data.Cast<int>().ToArray();
8589
lock (this) {

0 commit comments

Comments
 (0)