| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,7 @@ private static bool IsList(PyType objectType) | |||
| 20 | 20 | //if (!SequenceDecoder.IsSequence(objectType)) return false; | |
| 21 | 21 | ||
| 22 | 22 | //returns wheter the type is a list. | |
| 23 | - return objectType.Handle == Runtime.PyListType; | ||
| 23 | + return PythonReferenceComparer.Instance.Equals(objectType, Runtime.PyListType); | ||
| 24 | 24 | } | |
| 25 | 25 | ||
| 26 | 26 | public bool CanDecode(PyType objectType, Type targetType) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,7 @@ public bool CanEncode(Type type) | |||
| 18 | 18 | && type.Name.StartsWith(typeof(TTuple).Name + '`'); | |
| 19 | 19 | } | |
| 20 | 20 | ||
| 21 | - public PyObject TryEncode(object value) | ||
| 21 | + public PyObject? TryEncode(object value) | ||
| 22 | 22 | { | |
| 23 | 23 | if (value == null) return null; | |
| 24 | 24 | ||
@@ -27,45 +27,45 @@ public PyObject TryEncode(object value) | |||
| 27 | 27 | if (!this.CanEncode(tupleType)) return null; | |
| 28 | 28 | if (tupleType == typeof(TTuple)) return new PyTuple(); | |
| 29 | 29 | ||
| 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); | ||
| 33 | 33 | int fieldIndex = 0; | |
| 34 | 34 | foreach (FieldInfo field in tupleType.GetFields()) | |
| 35 | 35 | { | |
| 36 | 36 | 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()); | ||
| 39 | 39 | fieldIndex++; | |
| 40 | 40 | } | |
| 41 | - return new PyTuple(StolenReference.DangerousFromPointer(tuple)); | ||
| 41 | + return new PyTuple(tuple.Steal()); | ||
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | 44 | public bool CanDecode(PyType objectType, Type targetType) | |
| 45 | - => objectType.Handle == Runtime.PyTupleType && this.CanEncode(targetType); | ||
| 45 | + => objectType == Runtime.PyTupleType && this.CanEncode(targetType); | ||
| 46 | 46 | ||
| 47 | - public bool TryDecode<T>(PyObject pyObj, out T value) | ||
| 47 | + public bool TryDecode<T>(PyObject pyObj, out T? value) | ||
| 48 | 48 | { | |
| 49 | 49 | if (pyObj == null) throw new ArgumentNullException(nameof(pyObj)); | |
| 50 | 50 | ||
| 51 | 51 | value = default; | |
| 52 | 52 | ||
| 53 | - if (!Runtime.PyTuple_Check(pyObj.Handle)) return false; | ||
| 53 | + if (!Runtime.PyTuple_Check(pyObj)) return false; | ||
| 54 | 54 | ||
| 55 | 55 | if (typeof(T) == typeof(object)) | |
| 56 | 56 | { | |
| 57 | - bool converted = Decode(pyObj, out object result); | ||
| 57 | + bool converted = Decode(pyObj, out object? result); | ||
| 58 | 58 | if (converted) | |
| 59 | 59 | { | |
| 60 | - value = (T)result; | ||
| 60 | + value = (T?)result; | ||
| 61 | 61 | return true; | |
| 62 | 62 | } | |
| 63 | 63 | ||
| 64 | 64 | return false; | |
| 65 | 65 | } | |
| 66 | 66 | ||
| 67 | 67 | var itemTypes = typeof(T).GetGenericArguments(); | |
| 68 | - long itemCount = Runtime.PyTuple_Size(pyObj.Handle); | ||
| 68 | + nint itemCount = Runtime.PyTuple_Size(pyObj); | ||
| 69 | 69 | if (itemTypes.Length != itemCount) return false; | |
| 70 | 70 | ||
| 71 | 71 | if (itemCount == 0) | |
@@ -74,10 +74,10 @@ public bool TryDecode<T>(PyObject pyObj, out T value) | |||
| 74 | 74 | return true; | |
| 75 | 75 | } | |
| 76 | 76 | ||
| 77 | - var elements = new object[itemCount]; | ||
| 77 | + var elements = new object?[itemCount]; | ||
| 78 | 78 | for (int itemIndex = 0; itemIndex < itemTypes.Length; itemIndex++) | |
| 79 | 79 | { | |
| 80 | - IntPtr pyItem = Runtime.PyTuple_GetItem(pyObj.Handle, itemIndex); | ||
| 80 | + BorrowedReference pyItem = Runtime.PyTuple_GetItem(pyObj, itemIndex); | ||
| 81 | 81 | if (!Converter.ToManaged(pyItem, itemTypes[itemIndex], out elements[itemIndex], setError: false)) | |
| 82 | 82 | { | |
| 83 | 83 | Exceptions.Clear(); | |
@@ -89,20 +89,20 @@ public bool TryDecode<T>(PyObject pyObj, out T value) | |||
| 89 | 89 | return true; | |
| 90 | 90 | } | |
| 91 | 91 | ||
| 92 | - static bool Decode(PyObject tuple, out object value) | ||
| 92 | + static bool Decode(PyObject tuple, out object? value) | ||
| 93 | 93 | { | |
| 94 | - long itemCount = Runtime.PyTuple_Size(tuple.Handle); | ||
| 94 | + long itemCount = Runtime.PyTuple_Size(tuple); | ||
| 95 | 95 | if (itemCount == 0) | |
| 96 | 96 | { | |
| 97 | 97 | value = EmptyTuple; | |
| 98 | 98 | return true; | |
| 99 | 99 | } | |
| 100 | - var elements = new object[itemCount]; | ||
| 100 | + var elements = new object?[itemCount]; | ||
| 101 | 101 | var itemTypes = new Type[itemCount]; | |
| 102 | 102 | value = null; | |
| 103 | 103 | for (int itemIndex = 0; itemIndex < elements.Length; itemIndex++) | |
| 104 | 104 | { | |
| 105 | - var pyItem = Runtime.PyTuple_GetItem(tuple.Handle, itemIndex); | ||
| 105 | + var pyItem = Runtime.PyTuple_GetItem(tuple, itemIndex); | ||
| 106 | 106 | if (!Converter.ToManaged(pyItem, typeof(object), out elements[itemIndex], setError: false)) | |
| 107 | 107 | { | |
| 108 | 108 | Exceptions.Clear(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -527,7 +527,7 @@ static bool DecodableByUser(Type type) | |||
| 527 | 527 | || typeCode is TypeCode.Object or TypeCode.Decimal or TypeCode.DateTime; | |
| 528 | 528 | } | |
| 529 | 529 | ||
| 530 | - internal delegate bool TryConvertFromPythonDelegate(IntPtr pyObj, out object result); | ||
| 530 | + internal delegate bool TryConvertFromPythonDelegate(BorrowedReference pyObj, out object? result); | ||
| 531 | 531 | ||
| 532 | 532 | internal static int ToInt32(BorrowedReference value) | |
| 533 | 533 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,7 +24,7 @@ public interface IPyObjectDecoder | |||
| 24 | 24 | /// <param name="pyObj">Object to decode</param> | |
| 25 | 25 | /// <param name="value">The variable, that will receive decoding result</param> | |
| 26 | 26 | /// <returns></returns> | |
| 27 | - bool TryDecode<T>(PyObject pyObj, out T value); | ||
| 27 | + bool TryDecode<T>(PyObject pyObj, out T? value); | ||
| 28 | 28 | } | |
| 29 | 29 | ||
| 30 | 30 | /// <summary> | |
@@ -39,7 +39,7 @@ public interface IPyObjectEncoder | |||
| 39 | 39 | /// <summary> | |
| 40 | 40 | /// Attempts to encode CLR object <paramref name="value"/> into Python object | |
| 41 | 41 | /// </summary> | |
| 42 | - PyObject TryEncode(object value); | ||
| 42 | + PyObject? TryEncode(object value); | ||
| 43 | 43 | } | |
| 44 | 44 | ||
| 45 | 45 | /// <summary> | |
@@ -80,7 +80,7 @@ public static void RegisterDecoder(IPyObjectDecoder decoder) | |||
| 80 | 80 | } | |
| 81 | 81 | ||
| 82 | 82 | #region Encoding | |
| 83 | - internal static PyObject TryEncode(object obj, Type type) | ||
| 83 | + internal static PyObject? TryEncode(object obj, Type type) | ||
| 84 | 84 | { | |
| 85 | 85 | if (obj == null) throw new ArgumentNullException(nameof(obj)); | |
| 86 | 86 | if (type == null) throw new ArgumentNullException(nameof(type)); | |
@@ -106,13 +106,12 @@ static IPyObjectEncoder[] GetEncoders(Type type) | |||
| 106 | 106 | #endregion | |
| 107 | 107 | ||
| 108 | 108 | #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) | ||
| 114 | 113 | { | |
| 115 | - if (pyHandle == IntPtr.Zero) throw new ArgumentNullException(nameof(pyHandle)); | ||
| 114 | + if (pyHandle == null) throw new ArgumentNullException(nameof(pyHandle)); | ||
| 116 | 115 | if (pyType == IntPtr.Zero) throw new ArgumentNullException(nameof(pyType)); | |
| 117 | 116 | if (targetType == null) throw new ArgumentNullException(nameof(targetType)); | |
| 118 | 117 | ||
@@ -122,7 +121,7 @@ internal static bool TryDecode(IntPtr pyHandle, IntPtr pyType, Type targetType, | |||
| 122 | 121 | return decoder.Invoke(pyHandle, out result); | |
| 123 | 122 | } | |
| 124 | 123 | ||
| 125 | - static Converter.TryConvertFromPythonDelegate GetDecoder(IntPtr sourceType, Type targetType) | ||
| 124 | + static Converter.TryConvertFromPythonDelegate? GetDecoder(IntPtr sourceType, Type targetType) | ||
| 126 | 125 | { | |
| 127 | 126 | IPyObjectDecoder decoder; | |
| 128 | 127 | var sourceTypeRef = new BorrowedReference(sourceType); | |
@@ -138,10 +137,10 @@ static Converter.TryConvertFromPythonDelegate GetDecoder(IntPtr sourceType, Type | |||
| 138 | 137 | ||
| 139 | 138 | var decode = genericDecode.MakeGenericMethod(targetType); | |
| 140 | 139 | ||
| 141 | - bool TryDecode(IntPtr pyHandle, out object result) | ||
| 140 | + bool TryDecode(BorrowedReference pyHandle, out object? result) | ||
| 142 | 141 | { | |
| 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 }; | ||
| 145 | 144 | bool success = (bool)decode.Invoke(decoder, @params); | |
| 146 | 145 | if (!success) | |
| 147 | 146 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments