| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,6 +74,7 @@ Python `float` will continue to be converted to `System.Double`. | |||
| 74 | 74 | - BREAKING: `PyObject.GetAttr(name, default)` now only ignores `AttributeError` (previously ignored all exceptions). | |
| 75 | 75 | - BREAKING: `PyObject` no longer implements `IEnumerable<PyObject>`. | |
| 76 | 76 | Instead, `PyIterable` does that. | |
| 77 | + - BREAKING: `IPyObjectDecoder.CanDecode` `objectType` parameter type changed from `PyObject` to `PyType` | ||
| 77 | 78 | ||
| 78 | 79 | ### Fixed | |
| 79 | 80 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -141,16 +141,17 @@ public void SequenceDecoderTest() | |||
| 141 | 141 | ||
| 142 | 142 | //SequenceConverter can only convert to any ICollection | |
| 143 | 143 | var pyList = new PyList(items.ToArray()); | |
| 144 | + var listType = pyList.GetPythonType(); | ||
| 144 | 145 | //it can convert a PyList, since PyList satisfies the python sequence protocol | |
| 145 | 146 | ||
| 146 | - Assert.IsFalse(codec.CanDecode(pyList, typeof(bool))); | ||
| 147 | - Assert.IsFalse(codec.CanDecode(pyList, typeof(IList<int>))); | ||
| 148 | - Assert.IsFalse(codec.CanDecode(pyList, typeof(System.Collections.IEnumerable))); | ||
| 149 | - Assert.IsFalse(codec.CanDecode(pyList, typeof(IEnumerable<int>))); | ||
| 147 | + Assert.IsFalse(codec.CanDecode(listType, typeof(bool))); | ||
| 148 | + Assert.IsFalse(codec.CanDecode(listType, typeof(IList<int>))); | ||
| 149 | + Assert.IsFalse(codec.CanDecode(listType, typeof(System.Collections.IEnumerable))); | ||
| 150 | + Assert.IsFalse(codec.CanDecode(listType, typeof(IEnumerable<int>))); | ||
| 150 | 151 | ||
| 151 | - Assert.IsTrue(codec.CanDecode(pyList, typeof(ICollection<float>))); | ||
| 152 | - Assert.IsTrue(codec.CanDecode(pyList, typeof(ICollection<string>))); | ||
| 153 | - Assert.IsTrue(codec.CanDecode(pyList, typeof(ICollection<int>))); | ||
| 152 | + Assert.IsTrue(codec.CanDecode(listType, typeof(ICollection<float>))); | ||
| 153 | + Assert.IsTrue(codec.CanDecode(listType, typeof(ICollection<string>))); | ||
| 154 | + Assert.IsTrue(codec.CanDecode(listType, typeof(ICollection<int>))); | ||
| 154 | 155 | ||
| 155 | 156 | //convert to collection of int | |
| 156 | 157 | ICollection<int> intCollection = null; | |
@@ -380,7 +381,7 @@ public void As_Object_AffectedByDecoders() | |||
| 380 | 381 | ||
| 381 | 382 | public class EverythingElseToSelfDecoder : IPyObjectDecoder | |
| 382 | 383 | { | |
| 383 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 384 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 384 | 385 | { | |
| 385 | 386 | return targetType.IsAssignableFrom(typeof(EverythingElseToSelfDecoder)); | |
| 386 | 387 | } | |
@@ -399,7 +400,7 @@ public ValueErrorWrapper(string message) : base(message) { } | |||
| 399 | 400 | ||
| 400 | 401 | class ValueErrorCodec : IPyObjectEncoder, IPyObjectDecoder | |
| 401 | 402 | { | |
| 402 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 403 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 403 | 404 | => this.CanEncode(targetType) | |
| 404 | 405 | && PythonReferenceComparer.Instance.Equals(objectType, PythonEngine.Eval("ValueError")); | |
| 405 | 406 | ||
@@ -424,7 +425,7 @@ class InstancelessExceptionDecoder : IPyObjectDecoder | |||
| 424 | 425 | { | |
| 425 | 426 | readonly PyObject PyErr = Py.Import("clr.interop").GetAttr("PyErr"); | |
| 426 | 427 | ||
| 427 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 428 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 428 | 429 | => PythonReferenceComparer.Instance.Equals(PyErr, objectType); | |
| 429 | 430 | ||
| 430 | 431 | public bool TryDecode<T>(PyObject pyObj, out T value) | |
@@ -466,7 +467,7 @@ public DecoderReturningPredefinedValue(PyObject objectType, TTarget decodeResult | |||
| 466 | 467 | this.DecodeResult = decodeResult; | |
| 467 | 468 | } | |
| 468 | 469 | ||
| 469 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 470 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 470 | 471 | => objectType.Handle == TheOnlySupportedSourceType.Handle | |
| 471 | 472 | && targetType == typeof(TTarget); | |
| 472 | 473 | public bool TryDecode<T>(PyObject pyObj, out T value) | |
@@ -485,7 +486,7 @@ public static void Setup() | |||
| 485 | 486 | PyObjectConversions.RegisterDecoder(new DateTimeDecoder()); | |
| 486 | 487 | } | |
| 487 | 488 | ||
| 488 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 489 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 489 | 490 | { | |
| 490 | 491 | return targetType == typeof(DateTime); | |
| 491 | 492 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,7 +27,7 @@ public void Add(IPyObjectDecoder item) | |||
| 27 | 27 | public void Clear() => this.decoders.Clear(); | |
| 28 | 28 | ||
| 29 | 29 | /// <inheritdoc /> | |
| 30 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 30 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 31 | 31 | => this.decoders.Any(decoder => decoder.CanDecode(objectType, targetType)); | |
| 32 | 32 | /// <inheritdoc /> | |
| 33 | 33 | public bool TryDecode<T>(PyObject pyObj, out T value) | |
@@ -58,7 +58,7 @@ public static class DecoderGroupExtensions | |||
| 58 | 58 | /// </summary> | |
| 59 | 59 | public static IPyObjectDecoder GetDecoder( | |
| 60 | 60 | this IPyObjectDecoder decoder, | |
| 61 | - PyObject objectType, Type targetType) | ||
| 61 | + PyType objectType, Type targetType) | ||
| 62 | 62 | { | |
| 63 | 63 | if (decoder is null) throw new ArgumentNullException(nameof(decoder)); | |
| 64 | 64 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,7 @@ public sealed class EnumPyIntCodec : IPyObjectEncoder, IPyObjectDecoder | |||
| 7 | 7 | { | |
| 8 | 8 | public static EnumPyIntCodec Instance { get; } = new EnumPyIntCodec(); | |
| 9 | 9 | ||
| 10 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 10 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 11 | 11 | { | |
| 12 | 12 | return targetType.IsEnum | |
| 13 | 13 | && objectType.IsSubclass(new BorrowedReference(Runtime.PyLongType)); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,12 +17,12 @@ internal static bool IsIterable(Type targetType) | |||
| 17 | 17 | return targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>); | |
| 18 | 18 | } | |
| 19 | 19 | ||
| 20 | - internal static bool IsIterable(PyObject objectType) | ||
| 20 | + internal static bool IsIterable(PyType objectType) | ||
| 21 | 21 | { | |
| 22 | 22 | return objectType.HasAttr("__iter__"); | |
| 23 | 23 | } | |
| 24 | 24 | ||
| 25 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 25 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 26 | 26 | { | |
| 27 | 27 | return IsIterable(objectType) && IsIterable(targetType); | |
| 28 | 28 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ private static bool IsList(Type targetType) | |||
| 13 | 13 | return targetType.GetGenericTypeDefinition() == typeof(IList<>); | |
| 14 | 14 | } | |
| 15 | 15 | ||
| 16 | - private static bool IsList(PyObject objectType) | ||
| 16 | + private static bool IsList(PyType objectType) | ||
| 17 | 17 | { | |
| 18 | 18 | //TODO accept any python object that implements the sequence and list protocols | |
| 19 | 19 | //must implement sequence protocol to fully implement list protocol | |
@@ -23,7 +23,7 @@ private static bool IsList(PyObject objectType) | |||
| 23 | 23 | return objectType.Handle == Runtime.PyListType; | |
| 24 | 24 | } | |
| 25 | 25 | ||
| 26 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 26 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 27 | 27 | { | |
| 28 | 28 | return IsList(objectType) && IsList(targetType); | |
| 29 | 29 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ internal static bool IsSequence(Type targetType) | |||
| 13 | 13 | return targetType.GetGenericTypeDefinition() == typeof(ICollection<>); | |
| 14 | 14 | } | |
| 15 | 15 | ||
| 16 | - internal static bool IsSequence(PyObject objectType) | ||
| 16 | + internal static bool IsSequence(PyType objectType) | ||
| 17 | 17 | { | |
| 18 | 18 | //must implement iterable protocol to fully implement sequence protocol | |
| 19 | 19 | if (!IterableDecoder.IsIterable(objectType)) return false; | |
@@ -25,7 +25,7 @@ internal static bool IsSequence(PyObject objectType) | |||
| 25 | 25 | return objectType.HasAttr("__getitem__"); | |
| 26 | 26 | } | |
| 27 | 27 | ||
| 28 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 28 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 29 | 29 | { | |
| 30 | 30 | return IsSequence(objectType) && IsSequence(targetType); | |
| 31 | 31 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,7 +41,7 @@ public PyObject TryEncode(object value) | |||
| 41 | 41 | return new PyTuple(StolenReference.DangerousFromPointer(tuple)); | |
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | - public bool CanDecode(PyObject objectType, Type targetType) | ||
| 44 | + public bool CanDecode(PyType objectType, Type targetType) | ||
| 45 | 45 | => objectType.Handle == Runtime.PyTupleType && this.CanEncode(targetType); | |
| 46 | 46 | ||
| 47 | 47 | public bool TryDecode<T>(PyObject pyObj, out T value) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ namespace Python.Runtime | |||
| 3 | 3 | using System; | |
| 4 | 4 | using System.Collections.Concurrent; | |
| 5 | 5 | using System.Collections.Generic; | |
| 6 | + using System.Diagnostics; | ||
| 6 | 7 | using System.Linq; | |
| 7 | 8 | using System.Reflection; | |
| 8 | 9 | using Python.Runtime.Codecs; | |
@@ -15,7 +16,7 @@ public interface IPyObjectDecoder | |||
| 15 | 16 | /// <summary> | |
| 16 | 17 | /// Checks if this decoder can decode from <paramref name="objectType"/> to <paramref name="targetType"/> | |
| 17 | 18 | /// </summary> | |
| 18 | - bool CanDecode(PyObject objectType, Type targetType); | ||
| 19 | + bool CanDecode(PyType objectType, Type targetType); | ||
| 19 | 20 | /// <summary> | |
| 20 | 21 | /// Attempts do decode <paramref name="pyObj"/> into a variable of specified type | |
| 21 | 22 | /// </summary> | |
@@ -124,7 +125,9 @@ internal static bool TryDecode(IntPtr pyHandle, IntPtr pyType, Type targetType, | |||
| 124 | 125 | static Converter.TryConvertFromPythonDelegate GetDecoder(IntPtr sourceType, Type targetType) | |
| 125 | 126 | { | |
| 126 | 127 | IPyObjectDecoder decoder; | |
| 127 | - using (var pyType = new PyObject(Runtime.SelfIncRef(sourceType))) | ||
| 128 | + var sourceTypeRef = new BorrowedReference(sourceType); | ||
| 129 | + Debug.Assert(PyType.IsType(sourceTypeRef)); | ||
| 130 | + using (var pyType = new PyType(sourceTypeRef, prevalidated: true)) | ||
| 128 | 131 | { | |
| 129 | 132 | lock (decoders) | |
| 130 | 133 | { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments