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

Skip to content

Commit 8d93c39

Browse files
committed
Changed signature of IPyObjectDecoder.CanDecode
first parameter (objectType) type changed from PyObject to PyType
1 parent 79e34bc commit 8d93c39

9 files changed

+29
-24
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Python `float` will continue to be converted to `System.Double`.
7474
- BREAKING: `PyObject.GetAttr(name, default)` now only ignores `AttributeError` (previously ignored all exceptions).
7575
- BREAKING: `PyObject` no longer implements `IEnumerable<PyObject>`.
7676
Instead, `PyIterable` does that.
77+
- BREAKING: `IPyObjectDecoder.CanDecode` `objectType` parameter type changed from `PyObject` to `PyType`
7778

7879
### Fixed
7980

src/embed_tests/Codecs.cs

+13-12
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,17 @@ public void SequenceDecoderTest()
141141

142142
//SequenceConverter can only convert to any ICollection
143143
var pyList = new PyList(items.ToArray());
144+
var listType = pyList.GetPythonType();
144145
//it can convert a PyList, since PyList satisfies the python sequence protocol
145146

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>)));
150151

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>)));
154155

155156
//convert to collection of int
156157
ICollection<int> intCollection = null;
@@ -380,7 +381,7 @@ public void As_Object_AffectedByDecoders()
380381

381382
public class EverythingElseToSelfDecoder : IPyObjectDecoder
382383
{
383-
public bool CanDecode(PyObject objectType, Type targetType)
384+
public bool CanDecode(PyType objectType, Type targetType)
384385
{
385386
return targetType.IsAssignableFrom(typeof(EverythingElseToSelfDecoder));
386387
}
@@ -399,7 +400,7 @@ public ValueErrorWrapper(string message) : base(message) { }
399400

400401
class ValueErrorCodec : IPyObjectEncoder, IPyObjectDecoder
401402
{
402-
public bool CanDecode(PyObject objectType, Type targetType)
403+
public bool CanDecode(PyType objectType, Type targetType)
403404
=> this.CanEncode(targetType)
404405
&& PythonReferenceComparer.Instance.Equals(objectType, PythonEngine.Eval("ValueError"));
405406

@@ -424,7 +425,7 @@ class InstancelessExceptionDecoder : IPyObjectDecoder
424425
{
425426
readonly PyObject PyErr = Py.Import("clr.interop").GetAttr("PyErr");
426427

427-
public bool CanDecode(PyObject objectType, Type targetType)
428+
public bool CanDecode(PyType objectType, Type targetType)
428429
=> PythonReferenceComparer.Instance.Equals(PyErr, objectType);
429430

430431
public bool TryDecode<T>(PyObject pyObj, out T value)
@@ -466,7 +467,7 @@ public DecoderReturningPredefinedValue(PyObject objectType, TTarget decodeResult
466467
this.DecodeResult = decodeResult;
467468
}
468469

469-
public bool CanDecode(PyObject objectType, Type targetType)
470+
public bool CanDecode(PyType objectType, Type targetType)
470471
=> objectType.Handle == TheOnlySupportedSourceType.Handle
471472
&& targetType == typeof(TTarget);
472473
public bool TryDecode<T>(PyObject pyObj, out T value)
@@ -485,7 +486,7 @@ public static void Setup()
485486
PyObjectConversions.RegisterDecoder(new DateTimeDecoder());
486487
}
487488

488-
public bool CanDecode(PyObject objectType, Type targetType)
489+
public bool CanDecode(PyType objectType, Type targetType)
489490
{
490491
return targetType == typeof(DateTime);
491492
}

src/runtime/Codecs/DecoderGroup.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void Add(IPyObjectDecoder item)
2727
public void Clear() => this.decoders.Clear();
2828

2929
/// <inheritdoc />
30-
public bool CanDecode(PyObject objectType, Type targetType)
30+
public bool CanDecode(PyType objectType, Type targetType)
3131
=> this.decoders.Any(decoder => decoder.CanDecode(objectType, targetType));
3232
/// <inheritdoc />
3333
public bool TryDecode<T>(PyObject pyObj, out T value)
@@ -58,7 +58,7 @@ public static class DecoderGroupExtensions
5858
/// </summary>
5959
public static IPyObjectDecoder GetDecoder(
6060
this IPyObjectDecoder decoder,
61-
PyObject objectType, Type targetType)
61+
PyType objectType, Type targetType)
6262
{
6363
if (decoder is null) throw new ArgumentNullException(nameof(decoder));
6464

src/runtime/Codecs/EnumPyIntCodec.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public sealed class EnumPyIntCodec : IPyObjectEncoder, IPyObjectDecoder
77
{
88
public static EnumPyIntCodec Instance { get; } = new EnumPyIntCodec();
99

10-
public bool CanDecode(PyObject objectType, Type targetType)
10+
public bool CanDecode(PyType objectType, Type targetType)
1111
{
1212
return targetType.IsEnum
1313
&& objectType.IsSubclass(new BorrowedReference(Runtime.PyLongType));

src/runtime/Codecs/IterableDecoder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ internal static bool IsIterable(Type targetType)
1717
return targetType.GetGenericTypeDefinition() == typeof(IEnumerable<>);
1818
}
1919

20-
internal static bool IsIterable(PyObject objectType)
20+
internal static bool IsIterable(PyType objectType)
2121
{
2222
return objectType.HasAttr("__iter__");
2323
}
2424

25-
public bool CanDecode(PyObject objectType, Type targetType)
25+
public bool CanDecode(PyType objectType, Type targetType)
2626
{
2727
return IsIterable(objectType) && IsIterable(targetType);
2828
}

src/runtime/Codecs/ListDecoder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private static bool IsList(Type targetType)
1313
return targetType.GetGenericTypeDefinition() == typeof(IList<>);
1414
}
1515

16-
private static bool IsList(PyObject objectType)
16+
private static bool IsList(PyType objectType)
1717
{
1818
//TODO accept any python object that implements the sequence and list protocols
1919
//must implement sequence protocol to fully implement list protocol
@@ -23,7 +23,7 @@ private static bool IsList(PyObject objectType)
2323
return objectType.Handle == Runtime.PyListType;
2424
}
2525

26-
public bool CanDecode(PyObject objectType, Type targetType)
26+
public bool CanDecode(PyType objectType, Type targetType)
2727
{
2828
return IsList(objectType) && IsList(targetType);
2929
}

src/runtime/Codecs/SequenceDecoder.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static bool IsSequence(Type targetType)
1313
return targetType.GetGenericTypeDefinition() == typeof(ICollection<>);
1414
}
1515

16-
internal static bool IsSequence(PyObject objectType)
16+
internal static bool IsSequence(PyType objectType)
1717
{
1818
//must implement iterable protocol to fully implement sequence protocol
1919
if (!IterableDecoder.IsIterable(objectType)) return false;
@@ -25,7 +25,7 @@ internal static bool IsSequence(PyObject objectType)
2525
return objectType.HasAttr("__getitem__");
2626
}
2727

28-
public bool CanDecode(PyObject objectType, Type targetType)
28+
public bool CanDecode(PyType objectType, Type targetType)
2929
{
3030
return IsSequence(objectType) && IsSequence(targetType);
3131
}

src/runtime/Codecs/TupleCodecs.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public PyObject TryEncode(object value)
4141
return new PyTuple(StolenReference.DangerousFromPointer(tuple));
4242
}
4343

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

4747
public bool TryDecode<T>(PyObject pyObj, out T value)

src/runtime/converterextensions.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Python.Runtime
33
using System;
44
using System.Collections.Concurrent;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Linq;
78
using System.Reflection;
89
using Python.Runtime.Codecs;
@@ -15,7 +16,7 @@ public interface IPyObjectDecoder
1516
/// <summary>
1617
/// Checks if this decoder can decode from <paramref name="objectType"/> to <paramref name="targetType"/>
1718
/// </summary>
18-
bool CanDecode(PyObject objectType, Type targetType);
19+
bool CanDecode(PyType objectType, Type targetType);
1920
/// <summary>
2021
/// Attempts do decode <paramref name="pyObj"/> into a variable of specified type
2122
/// </summary>
@@ -124,7 +125,9 @@ internal static bool TryDecode(IntPtr pyHandle, IntPtr pyType, Type targetType,
124125
static Converter.TryConvertFromPythonDelegate GetDecoder(IntPtr sourceType, Type targetType)
125126
{
126127
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))
128131
{
129132
lock (decoders)
130133
{

0 commit comments

Comments
 (0)