Skip to content

Commit 4793818

Browse files
committed
switched converter extensions and sample codecs to the new style references
1 parent 027e529 commit 4793818

File tree

4 files changed

+33
-34
lines changed

4 files changed

+33
-34
lines changed

src/runtime/Codecs/ListDecoder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private static bool IsList(PyType objectType)
2020
//if (!SequenceDecoder.IsSequence(objectType)) return false;
2121

2222
//returns wheter the type is a list.
23-
return objectType.Handle == Runtime.PyListType;
23+
return PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyListType);
2424
}
2525

2626
public bool CanDecode(PyType objectType, Type targetType)

src/runtime/Codecs/TupleCodecs.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public bool CanEncode(Type type)
1818
&& type.Name.StartsWith(typeof(TTuple).Name + '`');
1919
}
2020

21-
public PyObject TryEncode(object value)
21+
public PyObject? TryEncode(object value)
2222
{
2323
if (value == null) return null;
2424

@@ -27,45 +27,45 @@ public PyObject TryEncode(object value)
2727
if (!this.CanEncode(tupleType)) return null;
2828
if (tupleType == typeof(TTuple)) return new PyTuple();
2929

30-
long fieldCount = tupleType.GetGenericArguments().Length;
31-
var tuple = Runtime.PyTuple_New(fieldCount);
32-
Exceptions.ErrorCheck(tuple);
30+
nint fieldCount = tupleType.GetGenericArguments().Length;
31+
using var tuple = Runtime.PyTuple_New(fieldCount);
32+
PythonException.ThrowIfIsNull(tuple);
3333
int fieldIndex = 0;
3434
foreach (FieldInfo field in tupleType.GetFields())
3535
{
3636
var item = field.GetValue(value);
37-
IntPtr pyItem = Converter.ToPython(item, field.FieldType);
38-
Runtime.PyTuple_SetItem(tuple, fieldIndex, pyItem);
37+
using var pyItem = Converter.ToPython(item, field.FieldType);
38+
Runtime.PyTuple_SetItem(tuple.Borrow(), fieldIndex, pyItem.Steal());
3939
fieldIndex++;
4040
}
41-
return new PyTuple(StolenReference.DangerousFromPointer(tuple));
41+
return new PyTuple(tuple.Steal());
4242
}
4343

4444
public bool CanDecode(PyType objectType, Type targetType)
45-
=> objectType.Handle == Runtime.PyTupleType && this.CanEncode(targetType);
45+
=> objectType == Runtime.PyTupleType && this.CanEncode(targetType);
4646

47-
public bool TryDecode<T>(PyObject pyObj, out T value)
47+
public bool TryDecode<T>(PyObject pyObj, out T? value)
4848
{
4949
if (pyObj == null) throw new ArgumentNullException(nameof(pyObj));
5050

5151
value = default;
5252

53-
if (!Runtime.PyTuple_Check(pyObj.Handle)) return false;
53+
if (!Runtime.PyTuple_Check(pyObj)) return false;
5454

5555
if (typeof(T) == typeof(object))
5656
{
57-
bool converted = Decode(pyObj, out object result);
57+
bool converted = Decode(pyObj, out object? result);
5858
if (converted)
5959
{
60-
value = (T)result;
60+
value = (T?)result;
6161
return true;
6262
}
6363

6464
return false;
6565
}
6666

6767
var itemTypes = typeof(T).GetGenericArguments();
68-
long itemCount = Runtime.PyTuple_Size(pyObj.Handle);
68+
nint itemCount = Runtime.PyTuple_Size(pyObj);
6969
if (itemTypes.Length != itemCount) return false;
7070

7171
if (itemCount == 0)
@@ -74,10 +74,10 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
7474
return true;
7575
}
7676

77-
var elements = new object[itemCount];
77+
var elements = new object?[itemCount];
7878
for (int itemIndex = 0; itemIndex < itemTypes.Length; itemIndex++)
7979
{
80-
IntPtr pyItem = Runtime.PyTuple_GetItem(pyObj.Handle, itemIndex);
80+
BorrowedReference pyItem = Runtime.PyTuple_GetItem(pyObj, itemIndex);
8181
if (!Converter.ToManaged(pyItem, itemTypes[itemIndex], out elements[itemIndex], setError: false))
8282
{
8383
Exceptions.Clear();
@@ -89,20 +89,20 @@ public bool TryDecode<T>(PyObject pyObj, out T value)
8989
return true;
9090
}
9191

92-
static bool Decode(PyObject tuple, out object value)
92+
static bool Decode(PyObject tuple, out object? value)
9393
{
94-
long itemCount = Runtime.PyTuple_Size(tuple.Handle);
94+
long itemCount = Runtime.PyTuple_Size(tuple);
9595
if (itemCount == 0)
9696
{
9797
value = EmptyTuple;
9898
return true;
9999
}
100-
var elements = new object[itemCount];
100+
var elements = new object?[itemCount];
101101
var itemTypes = new Type[itemCount];
102102
value = null;
103103
for (int itemIndex = 0; itemIndex < elements.Length; itemIndex++)
104104
{
105-
var pyItem = Runtime.PyTuple_GetItem(tuple.Handle, itemIndex);
105+
var pyItem = Runtime.PyTuple_GetItem(tuple, itemIndex);
106106
if (!Converter.ToManaged(pyItem, typeof(object), out elements[itemIndex], setError: false))
107107
{
108108
Exceptions.Clear();

src/runtime/converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ static bool DecodableByUser(Type type)
527527
|| typeCode is TypeCode.Object or TypeCode.Decimal or TypeCode.DateTime;
528528
}
529529

530-
internal delegate bool TryConvertFromPythonDelegate(IntPtr pyObj, out object result);
530+
internal delegate bool TryConvertFromPythonDelegate(BorrowedReference pyObj, out object? result);
531531

532532
internal static int ToInt32(BorrowedReference value)
533533
{

src/runtime/converterextensions.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface IPyObjectDecoder
2424
/// <param name="pyObj">Object to decode</param>
2525
/// <param name="value">The variable, that will receive decoding result</param>
2626
/// <returns></returns>
27-
bool TryDecode<T>(PyObject pyObj, out T value);
27+
bool TryDecode<T>(PyObject pyObj, out T? value);
2828
}
2929

3030
/// <summary>
@@ -39,7 +39,7 @@ public interface IPyObjectEncoder
3939
/// <summary>
4040
/// Attempts to encode CLR object <paramref name="value"/> into Python object
4141
/// </summary>
42-
PyObject TryEncode(object value);
42+
PyObject? TryEncode(object value);
4343
}
4444

4545
/// <summary>
@@ -80,7 +80,7 @@ public static void RegisterDecoder(IPyObjectDecoder decoder)
8080
}
8181

8282
#region Encoding
83-
internal static PyObject TryEncode(object obj, Type type)
83+
internal static PyObject? TryEncode(object obj, Type type)
8484
{
8585
if (obj == null) throw new ArgumentNullException(nameof(obj));
8686
if (type == null) throw new ArgumentNullException(nameof(type));
@@ -106,13 +106,12 @@ static IPyObjectEncoder[] GetEncoders(Type type)
106106
#endregion
107107

108108
#region Decoding
109-
static readonly ConcurrentDictionary<TypePair, Converter.TryConvertFromPythonDelegate>
110-
pythonToClr = new ConcurrentDictionary<TypePair, Converter.TryConvertFromPythonDelegate>();
111-
internal static bool TryDecode(BorrowedReference value, BorrowedReference type, Type targetType, out object result)
112-
=> TryDecode(value.DangerousGetAddress(), type.DangerousGetAddress(), targetType, out result);
113-
internal static bool TryDecode(IntPtr pyHandle, IntPtr pyType, Type targetType, out object result)
109+
static readonly ConcurrentDictionary<TypePair, Converter.TryConvertFromPythonDelegate?> pythonToClr = new();
110+
internal static bool TryDecode(BorrowedReference value, BorrowedReference type, Type targetType, out object? result)
111+
=> TryDecode(value, type.DangerousGetAddress(), targetType, out result);
112+
internal static bool TryDecode(BorrowedReference pyHandle, IntPtr pyType, Type targetType, out object? result)
114113
{
115-
if (pyHandle == IntPtr.Zero) throw new ArgumentNullException(nameof(pyHandle));
114+
if (pyHandle == null) throw new ArgumentNullException(nameof(pyHandle));
116115
if (pyType == IntPtr.Zero) throw new ArgumentNullException(nameof(pyType));
117116
if (targetType == null) throw new ArgumentNullException(nameof(targetType));
118117

@@ -122,7 +121,7 @@ internal static bool TryDecode(IntPtr pyHandle, IntPtr pyType, Type targetType,
122121
return decoder.Invoke(pyHandle, out result);
123122
}
124123

125-
static Converter.TryConvertFromPythonDelegate GetDecoder(IntPtr sourceType, Type targetType)
124+
static Converter.TryConvertFromPythonDelegate? GetDecoder(IntPtr sourceType, Type targetType)
126125
{
127126
IPyObjectDecoder decoder;
128127
var sourceTypeRef = new BorrowedReference(sourceType);
@@ -138,10 +137,10 @@ static Converter.TryConvertFromPythonDelegate GetDecoder(IntPtr sourceType, Type
138137

139138
var decode = genericDecode.MakeGenericMethod(targetType);
140139

141-
bool TryDecode(IntPtr pyHandle, out object result)
140+
bool TryDecode(BorrowedReference pyHandle, out object? result)
142141
{
143-
var pyObj = new PyObject(Runtime.SelfIncRef(pyHandle));
144-
var @params = new object[] { pyObj, null };
142+
var pyObj = new PyObject(pyHandle);
143+
var @params = new object?[] { pyObj, null };
145144
bool success = (bool)decode.Invoke(decoder, @params);
146145
if (!success)
147146
{

0 commit comments

Comments
 (0)