Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb84c48

Browse files
committed
getting rid of a few minor warnings and compile errors
1 parent cf606a2 commit bb84c48

File tree

10 files changed

+53
-54
lines changed

10 files changed

+53
-54
lines changed

src/runtime/Codecs/EnumPyIntCodec.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public sealed class EnumPyIntCodec : IPyObjectEncoder, IPyObjectDecoder
1010
public bool CanDecode(PyType objectType, Type targetType)
1111
{
1212
return targetType.IsEnum
13-
&& objectType.IsSubclass(new BorrowedReference(Runtime.PyLongType));
13+
&& objectType.IsSubclass(Runtime.PyLongType);
1414
}
1515

1616
public bool CanEncode(Type type)
1717
{
1818
return type == typeof(object) || type == typeof(ValueType) || type.IsEnum;
1919
}
2020

21-
public bool TryDecode<T>(PyObject pyObj, out T value)
21+
public bool TryDecode<T>(PyObject pyObj, out T? value)
2222
{
2323
value = default;
2424
if (!typeof(T).IsEnum) return false;
@@ -27,7 +27,7 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
2727

2828
if (!PyInt.IsIntType(pyObj)) return false;
2929

30-
object result;
30+
object? result;
3131
try
3232
{
3333
result = pyObj.AsManagedObject(etype);
@@ -46,7 +46,7 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
4646
return false;
4747
}
4848

49-
public PyObject TryEncode(object value)
49+
public PyObject? TryEncode(object value)
5050
{
5151
if (value is null) return null;
5252

src/runtime/DefaultBaseTypeProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases)
2121
static BorrowedReference GetBaseType(Type type)
2222
{
2323
if (type == typeof(Exception))
24-
return new BorrowedReference(Exceptions.Exception);
24+
return Exceptions.Exception;
2525

2626
return type.BaseType is not null
2727
? ClassManager.GetClass(type.BaseType).ObjectReference
28-
: new BorrowedReference(Runtime.PyBaseObjectType);
28+
: Runtime.PyBaseObjectType;
2929
}
3030

3131
DefaultBaseTypeProvider(){}

src/runtime/Mixins/CollectionMixinsProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public IEnumerable<PyType> GetBaseTypes(Type type, IList<PyType> existingBases)
7070

7171
if (type.IsInterface && type.BaseType is null)
7272
{
73-
newBases.RemoveAll(@base => @base.Handle == Runtime.PyBaseObjectType);
73+
newBases.RemoveAll(@base => PythonReferenceComparer.Instance.Equals(@base, Runtime.PyBaseObjectType));
7474
}
7575

7676
return newBases;

src/runtime/ReferenceExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ static class ReferenceExtensions
99
/// </summary>
1010
[Pure]
1111
public static bool IsNone(this in NewReference reference)
12-
=> reference.DangerousGetAddress() == Runtime.PyNone;
12+
=> reference.BorrowNullable() == Runtime.PyNone;
1313
/// <summary>
1414
/// Checks if the reference points to Python object <c>None</c>.
1515
/// </summary>
1616
[Pure]
1717
public static bool IsNone(this BorrowedReference reference)
18-
=> reference.DangerousGetAddress() == Runtime.PyNone;
18+
=> reference == Runtime.PyNone;
1919
}
2020
}

src/runtime/classderived.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ private static void AddVirtualMethod(MethodInfo method, Type baseType, TypeBuild
349349
Type[] parameterTypes = (from param in parameters select param.ParameterType).ToArray();
350350

351351
// If the method isn't abstract create a method for calling the original method
352-
string baseMethodName = null;
352+
string? baseMethodName = null;
353353
if (!method.IsAbstract)
354354
{
355355
baseMethodName = "_" + baseType.Name + "__" + method.Name;
@@ -678,7 +678,7 @@ public static T InvokeMethod<T>(IPythonDerivedType obj, string methodName, strin
678678

679679
PyObject py_result = method.Invoke(pyargs);
680680
disposeList.Add(py_result);
681-
return (T)py_result.AsManagedObject(typeof(T));
681+
return py_result.As<T>();
682682
}
683683
}
684684
}
@@ -781,7 +781,7 @@ public static T InvokeGetProperty<T>(IPythonDerivedType obj, string propertyName
781781
using var pyself = new PyObject(self.ObjectReference);
782782
using (PyObject pyvalue = pyself.GetAttr(propertyName))
783783
{
784-
return (T)pyvalue.AsManagedObject(typeof(T));
784+
return pyvalue.As<T>();
785785
}
786786
}
787787
finally

src/runtime/constructorbinder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ internal ConstructorBinder(Type containingType)
5353
{
5454
if (!_containingType.Valid)
5555
{
56-
return Exceptions.RaiseTypeError(_containingType.DeletedMessage);
56+
Exceptions.RaiseTypeError(_containingType.DeletedMessage);
57+
return null;
5758
}
5859
object result;
5960
Type tp = _containingType.Value;
@@ -83,7 +84,7 @@ internal ConstructorBinder(Type containingType)
8384
return result;
8485
}
8586

86-
Binding binding = Bind(inst, args, kw, info);
87+
Binding? binding = Bind(inst, args, kw, info);
8788

8889
if (binding == null)
8990
{
@@ -94,9 +95,8 @@ internal ConstructorBinder(Type containingType)
9495
// if there is a default constructor and, if so, assume that
9596
// any extra args are intended for the subclass' __init__.
9697

97-
IntPtr eargs = Runtime.PyTuple_New(0);
98-
binding = Bind(inst, eargs, IntPtr.Zero);
99-
Runtime.XDecref(eargs);
98+
using var eargs = Runtime.PyTuple_New(0);
99+
binding = Bind(inst, eargs.BorrowOrThrow(), kw: null);
100100

101101
if (binding == null)
102102
{

src/runtime/loader.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using System.Diagnostics;
21
using System;
3-
using System.Runtime.InteropServices;
42
using System.Text;
5-
using System.Threading;
63

74
namespace Python.Runtime
85
{
@@ -13,7 +10,6 @@ static class Loader
1310
{
1411
public unsafe static int Initialize(IntPtr data, int size)
1512
{
16-
IntPtr gs = IntPtr.Zero;
1713
try
1814
{
1915
var dllPath = Encoding.UTF8.GetString((byte*)data.ToPointer(), size);
@@ -27,11 +23,18 @@ public unsafe static int Initialize(IntPtr data, int size)
2723
PythonDLL = null;
2824
}
2925

30-
gs = PyGILState_Ensure();
26+
var gs = PyGILState_Ensure();
3127

32-
// Console.WriteLine("Startup thread");
33-
PythonEngine.InitExt();
34-
// Console.WriteLine("Startup finished");
28+
try
29+
{
30+
// Console.WriteLine("Startup thread");
31+
PythonEngine.InitExt();
32+
// Console.WriteLine("Startup finished");
33+
}
34+
finally
35+
{
36+
PyGILState_Release(gs);
37+
}
3538
}
3639
catch (Exception exc)
3740
{
@@ -40,27 +43,27 @@ public unsafe static int Initialize(IntPtr data, int size)
4043
);
4144
return 1;
4245
}
43-
finally
44-
{
45-
if (gs != IntPtr.Zero)
46-
{
47-
PyGILState_Release(gs);
48-
}
49-
}
46+
5047
return 0;
5148
}
5249

5350
public unsafe static int Shutdown(IntPtr data, int size)
5451
{
55-
IntPtr gs = IntPtr.Zero;
5652
try
5753
{
5854
var command = Encoding.UTF8.GetString((byte*)data.ToPointer(), size);
5955

6056
if (command == "full_shutdown")
6157
{
62-
gs = PyGILState_Ensure();
63-
PythonEngine.Shutdown();
58+
var gs = PyGILState_Ensure();
59+
try
60+
{
61+
PythonEngine.Shutdown();
62+
}
63+
finally
64+
{
65+
PyGILState_Release(gs);
66+
}
6467
}
6568
}
6669
catch (Exception exc)
@@ -70,13 +73,7 @@ public unsafe static int Shutdown(IntPtr data, int size)
7073
);
7174
return 1;
7275
}
73-
finally
74-
{
75-
if (gs != IntPtr.Zero)
76-
{
77-
PyGILState_Release(gs);
78-
}
79-
}
76+
8077
return 0;
8178
}
8279
}

src/runtime/pythonexception.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private static Exception FromPyErr(BorrowedReference typeRef, BorrowedReference
174174
return pyErr;
175175
}
176176

177-
if (PyObjectConversions.TryDecode(valRef, typeRef, typeof(Exception), out object decoded)
177+
if (PyObjectConversions.TryDecode(valRef, typeRef, typeof(Exception), out object? decoded)
178178
&& decoded is Exception decodedException)
179179
{
180180
return decodedException;
@@ -199,7 +199,7 @@ private static Exception FromPyErr(BorrowedReference typeRef, BorrowedReference
199199
using var pyErrType = Runtime.InteropModule.GetAttr("PyErr");
200200
using var pyErrInfo = pyErrType.Invoke(new PyTuple(), errorDict);
201201
if (PyObjectConversions.TryDecode(pyErrInfo.Reference, pyErrType.Reference,
202-
typeof(Exception), out object decoded) && decoded is Exception decodedPyErrInfo)
202+
typeof(Exception), out object? decoded) && decoded is Exception decodedPyErrInfo)
203203
{
204204
return decodedPyErrInfo;
205205
}
@@ -394,11 +394,11 @@ public PythonException Clone()
394394
=> new PythonException(type: Type, value: Value, traceback: Traceback,
395395
Message, InnerException);
396396

397-
internal bool Is(IntPtr type)
397+
internal bool Is(BorrowedReference type)
398398
{
399399
return Runtime.PyErr_GivenExceptionMatches(
400400
given: (Value ?? Type).Reference,
401-
typeOrTypes: new BorrowedReference(type)) != 0;
401+
typeOrTypes: type) != 0;
402402
}
403403

404404
private static void CheckRuntimeIsRunning()

src/runtime/runtime.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
176176

177177
private static void InitPyMembers()
178178
{
179-
using (var builtinsOwned = PyImport_Import(new BorrowedReference(PyIdentifier.builtins)))
179+
using (var builtinsOwned = PyImport_Import(PyIdentifier.builtins))
180180
{
181181
var builtins = builtinsOwned.Borrow();
182182
SetPyMember(out PyNotImplemented, PyObject_GetAttrString(builtins, "NotImplemented").StealNullable());
@@ -403,10 +403,11 @@ private static void SetPyMember(out PyObject obj, StolenReference value)
403403
_pyRefs.Add(obj);
404404
}
405405

406-
private static void SetPyMemberTypeOf(out PyObject obj, PyObject value)
406+
private static void SetPyMemberTypeOf(out PyType obj, PyObject value)
407407
{
408408
var type = PyObject_Type(value);
409-
SetPyMember(out obj, type.StealNullable());
409+
obj = new PyType(type.StealOrThrow(), prevalidated: true);
410+
_pyRefs.Add(obj);
410411
}
411412

412413
private static void SetPyMemberTypeOf(out PyObject obj, StolenReference value)
@@ -513,8 +514,8 @@ private static void MoveClrInstancesOnwershipToPython()
513514
internal static PyObject PyDictType;
514515
internal static PyObject PyLongType;
515516
internal static PyObject PyFloatType;
516-
internal static PyObject PyBoolType;
517-
internal static PyObject PyNoneType;
517+
internal static PyType PyBoolType;
518+
internal static PyType PyNoneType;
518519
internal static PyType PyTypeType;
519520

520521
internal static int* Py_NoSiteFlag;

src/runtime/slots/mp_length.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Python.Runtime.Slots
99
{
1010
internal static class mp_length_slot
1111
{
12-
private static MethodInfo _lengthMethod;
12+
private static MethodInfo? _lengthMethod;
1313
public static MethodInfo Method
1414
{
1515
get
@@ -22,7 +22,7 @@ public static MethodInfo Method
2222
nameof(mp_length_slot.mp_length),
2323
BindingFlags.Static | BindingFlags.NonPublic);
2424
Debug.Assert(_lengthMethod != null);
25-
return _lengthMethod;
25+
return _lengthMethod!;
2626
}
2727
}
2828

@@ -47,12 +47,13 @@ public static bool CanAssign(Type clrType)
4747
/// Implements __len__ for classes that implement ICollection
4848
/// (this includes any IList implementer or Array subclass)
4949
/// </summary>
50-
private static int mp_length(IntPtr ob)
50+
private static nint mp_length(BorrowedReference ob)
5151
{
5252
var co = ManagedType.GetManagedObject(ob) as CLRObject;
5353
if (co == null)
5454
{
5555
Exceptions.RaiseTypeError("invalid object");
56+
return -1;
5657
}
5758

5859
// first look for ICollection implementation directly

0 commit comments

Comments
 (0)