Skip to content

Commit 2e71874

Browse files
committed
mass enable nullable types
1 parent 11edcc3 commit 2e71874

22 files changed

+22
-37
lines changed

src/runtime/BorrowedReference.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public BorrowedReference(IntPtr pointer)
3030
=> a.pointer == b.pointer;
3131
public static bool operator !=(BorrowedReference a, BorrowedReference b)
3232
=> a.pointer != b.pointer;
33-
public static bool operator ==(BorrowedReference reference, NullOnly @null)
33+
public static bool operator ==(BorrowedReference reference, NullOnly? @null)
3434
=> reference.IsNull;
35-
public static bool operator !=(BorrowedReference reference, NullOnly @null)
35+
public static bool operator !=(BorrowedReference reference, NullOnly? @null)
3636
=> !reference.IsNull;
37-
public static bool operator ==(NullOnly @null, BorrowedReference reference)
37+
public static bool operator ==(NullOnly? @null, BorrowedReference reference)
3838
=> reference.IsNull;
39-
public static bool operator !=(NullOnly @null, BorrowedReference reference)
39+
public static bool operator !=(NullOnly? @null, BorrowedReference reference)
4040
=> !reference.IsNull;
4141

4242
public override bool Equals(object obj) {

src/runtime/Python.Runtime.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<LangVersion>10.0</LangVersion>
66
<RootNamespace>Python.Runtime</RootNamespace>
77
<AssemblyName>Python.Runtime</AssemblyName>
8+
<Nullable>enable</Nullable>
89

910
<PackageId>pythonnet</PackageId>
1011
<PackageLicenseFile>LICENSE</PackageLicenseFile>

src/runtime/PythonReferenceComparer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System.Collections.Generic;
32

43
namespace Python.Runtime

src/runtime/TypeSpec.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Collections.Generic;
43
using System.Linq;

src/runtime/Util.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Collections.Generic;
43
using System.Diagnostics;

src/runtime/arrayobject.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Collections;
43
using System.Collections.Generic;

src/runtime/classbase.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#nullable enable
21
using System;
32
using System.Collections;
43
using System.Collections.Generic;
54
using System.Diagnostics;
65
using System.Linq;
76
using System.Reflection;
8-
using System.Runtime.InteropServices;
97

108
namespace Python.Runtime
119
{

src/runtime/classderived.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using System;
32
using System.Collections.Generic;
43
using System.ComponentModel;
@@ -56,7 +55,7 @@ internal ClassDerivedObject(Type tp) : base(tp)
5655
var cls = (ClassDerivedObject)GetManagedObject(tp)!;
5756

5857
// call the managed constructor
59-
object obj = cls.binder.InvokeRaw(null, args, kw);
58+
object? obj = cls.binder.InvokeRaw(null, args, kw);
6059
if (obj == null)
6160
{
6261
return default;

src/runtime/classobject.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System.Linq;
21
using System;
2+
using System.Linq;
33
using System.Reflection;
44

55
namespace Python.Runtime
@@ -43,16 +43,15 @@ internal NewReference GetDocString()
4343
}
4444
str += t.ToString();
4545
}
46-
return NewReference.DangerousFromPointer(Runtime.PyString_FromString(str));
46+
return Runtime.PyString_FromString(str);
4747
}
4848

4949

5050
/// <summary>
5151
/// Implements __new__ for reflected classes and value types.
5252
/// </summary>
53-
public static IntPtr tp_new(IntPtr tpRaw, IntPtr args, IntPtr kw)
53+
public static NewReference tp_new(BorrowedReference tp, BorrowedReference args, BorrowedReference kw)
5454
{
55-
var tp = new BorrowedReference(tpRaw);
5655
var self = GetManagedObject(tp) as ClassObject;
5756

5857
// Sanity check: this ensures a graceful error if someone does
@@ -77,32 +76,32 @@ public static IntPtr tp_new(IntPtr tpRaw, IntPtr args, IntPtr kw)
7776
if (Runtime.PyTuple_Size(args) != 1)
7877
{
7978
Exceptions.SetError(Exceptions.TypeError, "no constructors match given arguments");
80-
return IntPtr.Zero;
79+
return default;
8180
}
8281

83-
IntPtr op = Runtime.PyTuple_GetItem(args, 0);
82+
BorrowedReference op = Runtime.PyTuple_GetItem(args, 0);
8483
object result;
8584

8685
if (!Converter.ToManaged(op, type, out result, true))
8786
{
88-
return IntPtr.Zero;
87+
return default;
8988
}
9089

91-
return CLRObject.GetReference(result, tp).DangerousMoveToPointerOrNull();
90+
return CLRObject.GetReference(result, tp);
9291
}
9392

9493
if (type.IsAbstract)
9594
{
9695
Exceptions.SetError(Exceptions.TypeError, "cannot instantiate abstract class");
97-
return IntPtr.Zero;
96+
return default;
9897
}
9998

10099
if (type.IsEnum)
101100
{
102-
return NewEnum(type, new BorrowedReference(args), tp).DangerousMoveToPointerOrNull();
101+
return NewEnum(type, args, tp);
103102
}
104103

105-
object obj = self.binder.InvokeRaw(IntPtr.Zero, args, kw);
104+
object obj = self.binder.InvokeRaw(null, args, kw);
106105
if (obj == null)
107106
{
108107
return IntPtr.Zero;
@@ -154,7 +153,7 @@ private static NewReference NewEnum(Type type, BorrowedReference args, BorrowedR
154153
/// both to implement the Array[int] syntax for creating arrays and
155154
/// to support generic name overload resolution using [].
156155
/// </summary>
157-
public override IntPtr type_subscript(IntPtr idx)
156+
public override NewReference type_subscript(BorrowedReference idx)
158157
{
159158
if (!type.Valid)
160159
{

src/runtime/constructorbinder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal ConstructorBinder(Type containingType)
2929
/// object - the reason is that only the caller knows the correct
3030
/// Python type to use when wrapping the result (may be a subclass).
3131
/// </summary>
32-
internal object InvokeRaw(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
32+
internal object? InvokeRaw(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
3333
{
3434
return InvokeRaw(inst, args, kw, null);
3535
}
@@ -49,7 +49,7 @@ internal object InvokeRaw(BorrowedReference inst, BorrowedReference args, Borrow
4949
/// Binding binding = this.Bind(inst, args, kw, info);
5050
/// to take advantage of Bind()'s ability to use a single MethodBase (CI or MI).
5151
/// </remarks>
52-
internal object InvokeRaw(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info)
52+
internal object? InvokeRaw(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase? info)
5353
{
5454
if (!_containingType.Valid)
5555
{

0 commit comments

Comments
 (0)