Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8d93c39

Browse files
committed
Changed signature of IPyObjectDecoder.CanDecode
first parameter (objectType) type changed from PyObject to PyType
1 parent79e34bc commit8d93c39

File tree

9 files changed

+29
-24
lines changed

9 files changed

+29
-24
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 13 additions & 12 deletions
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
varpyList=newPyList(items.ToArray());
144+
varlistType=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
publicclassEverythingElseToSelfDecoder:IPyObjectDecoder
382383
{
383-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
384+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
384385
{
385386
returntargetType.IsAssignableFrom(typeof(EverythingElseToSelfDecoder));
386387
}
@@ -399,7 +400,7 @@ public ValueErrorWrapper(string message) : base(message) { }
399400

400401
classValueErrorCodec:IPyObjectEncoder,IPyObjectDecoder
401402
{
402-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
403+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
403404
=>this.CanEncode(targetType)
404405
&&PythonReferenceComparer.Instance.Equals(objectType,PythonEngine.Eval("ValueError"));
405406

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

427-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
428+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
428429
=>PythonReferenceComparer.Instance.Equals(PyErr,objectType);
429430

430431
publicboolTryDecode<T>(PyObjectpyObj,outTvalue)
@@ -466,7 +467,7 @@ public DecoderReturningPredefinedValue(PyObject objectType, TTarget decodeResult
466467
this.DecodeResult=decodeResult;
467468
}
468469

469-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
470+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
470471
=>objectType.Handle==TheOnlySupportedSourceType.Handle
471472
&&targetType==typeof(TTarget);
472473
publicboolTryDecode<T>(PyObjectpyObj,outTvalue)
@@ -485,7 +486,7 @@ public static void Setup()
485486
PyObjectConversions.RegisterDecoder(newDateTimeDecoder());
486487
}
487488

488-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
489+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
489490
{
490491
returntargetType==typeof(DateTime);
491492
}

‎src/runtime/Codecs/DecoderGroup.cs

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

2929
/// <inheritdoc />
30-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
30+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
3131
=>this.decoders.Any(decoder=>decoder.CanDecode(objectType,targetType));
3232
/// <inheritdoc />
3333
publicboolTryDecode<T>(PyObjectpyObj,outTvalue)
@@ -58,7 +58,7 @@ public static class DecoderGroupExtensions
5858
/// </summary>
5959
publicstaticIPyObjectDecoderGetDecoder(
6060
thisIPyObjectDecoderdecoder,
61-
PyObjectobjectType,TypetargetType)
61+
PyTypeobjectType,TypetargetType)
6262
{
6363
if(decoderisnull)thrownewArgumentNullException(nameof(decoder));
6464

‎src/runtime/Codecs/EnumPyIntCodec.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public sealed class EnumPyIntCodec : IPyObjectEncoder, IPyObjectDecoder
77
{
88
publicstaticEnumPyIntCodecInstance{get;}=newEnumPyIntCodec();
99

10-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
10+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
1111
{
1212
returntargetType.IsEnum
1313
&&objectType.IsSubclass(newBorrowedReference(Runtime.PyLongType));

‎src/runtime/Codecs/IterableDecoder.cs

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

20-
internalstaticboolIsIterable(PyObjectobjectType)
20+
internalstaticboolIsIterable(PyTypeobjectType)
2121
{
2222
returnobjectType.HasAttr("__iter__");
2323
}
2424

25-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
25+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
2626
{
2727
returnIsIterable(objectType)&&IsIterable(targetType);
2828
}

‎src/runtime/Codecs/ListDecoder.cs

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

16-
privatestaticboolIsList(PyObjectobjectType)
16+
privatestaticboolIsList(PyTypeobjectType)
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
returnobjectType.Handle==Runtime.PyListType;
2424
}
2525

26-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
26+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
2727
{
2828
returnIsList(objectType)&&IsList(targetType);
2929
}

‎src/runtime/Codecs/SequenceDecoder.cs

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

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

28-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
28+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
2929
{
3030
returnIsSequence(objectType)&&IsSequence(targetType);
3131
}

‎src/runtime/Codecs/TupleCodecs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public PyObject TryEncode(object value)
4141
returnnewPyTuple(StolenReference.DangerousFromPointer(tuple));
4242
}
4343

44-
publicboolCanDecode(PyObjectobjectType,TypetargetType)
44+
publicboolCanDecode(PyTypeobjectType,TypetargetType)
4545
=>objectType.Handle==Runtime.PyTupleType&&this.CanEncode(targetType);
4646

4747
publicboolTryDecode<T>(PyObjectpyObj,outTvalue)

‎src/runtime/converterextensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Python.Runtime
33
usingSystem;
44
usingSystem.Collections.Concurrent;
55
usingSystem.Collections.Generic;
6+
usingSystem.Diagnostics;
67
usingSystem.Linq;
78
usingSystem.Reflection;
89
usingPython.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-
boolCanDecode(PyObjectobjectType,TypetargetType);
19+
boolCanDecode(PyTypeobjectType,TypetargetType);
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
staticConverter.TryConvertFromPythonDelegateGetDecoder(IntPtrsourceType,TypetargetType)
125126
{
126127
IPyObjectDecoderdecoder;
127-
using(varpyType=newPyObject(Runtime.SelfIncRef(sourceType)))
128+
varsourceTypeRef=newBorrowedReference(sourceType);
129+
Debug.Assert(PyType.IsType(sourceTypeRef));
130+
using(varpyType=newPyType(sourceTypeRef,prevalidated:true))
128131
{
129132
lock(decoders)
130133
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp