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

Commit87d4db9

Browse files
authored
Merge pull request#1786 from losttech/bugs/KeyCollection
Multiple fixes related to Dictionary.Keys bug
2 parentsa80c685 +d854332 commit87d4db9

10 files changed

+91
-7
lines changed

‎CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
2121
-`__name__` and`__signature__` to reflected .NET methods
2222
- .NET collection types now implement standard Python collection interfaces from`collections.abc`.
2323
See[Mixins/collections.py](src/runtime/Mixins/collections.py).
24+
- you can cast objects to generic .NET interfaces without specifying generic arguments as long as there is no ambiguity.
2425
- .NET arrays implement Python buffer protocol
2526
- Python integer interoperability with`System.Numerics.BigInteger`
2627
- Python.NET will correctly resolve .NET methods, that accept`PyList`,`PyInt`,

‎src/runtime/ClassManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ internal static void RestoreRuntimeData(ClassManagerState storage)
133133
/// Return the ClassBase-derived instance that implements a particular
134134
/// reflected managed type, creating it if it doesn't yet exist.
135135
/// </summary>
136-
internalstaticReflectedClrTypeGetClass(Typetype)=>ReflectedClrType.GetOrCreate(type);
136+
internalstaticBorrowedReferenceGetClass(Typetype)=>ReflectedClrType.GetOrCreate(type);
137137

138138
internalstaticClassBaseGetClassImpl(Typetype)
139139
{

‎src/runtime/Mixins/collections.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
classIteratorMixin(col.Iterator):
99
defclose(self):
10-
self.Dispose()
10+
ifhasattr(self,'Dispose'):
11+
self.Dispose()
12+
else:
13+
fromSystemimportIDisposable
14+
IDisposable(self).Dispose()
1115

1216
classIterableMixin(col.Iterable):
1317
pass
@@ -16,7 +20,12 @@ class SizedMixin(col.Sized):
1620
def__len__(self):returnself.Count
1721

1822
classContainerMixin(col.Container):
19-
def__contains__(self,item):returnself.Contains(item)
23+
def__contains__(self,item):
24+
ifhasattr('self','Contains'):
25+
returnself.Contains(item)
26+
else:
27+
fromSystem.Collections.GenericimportICollection
28+
returnICollection(self).Contains(item)
2029

2130
try:
2231
abc_Collection=col.Collection

‎src/runtime/TypeManager.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,23 @@ static void GetPythonTypeName(Type clrType, System.Text.StringBuilder target)
217217
}
218218

219219
target.Append(']');
220+
221+
intnestedStart=fullName.IndexOf('+');
222+
while(nestedStart>=0)
223+
{
224+
target.Append('.');
225+
intnextNested=fullName.IndexOf('+',nestedStart+1);
226+
if(nextNested<0)
227+
{
228+
target.Append(fullName.Substring(nestedStart+1));
229+
}
230+
else
231+
{
232+
target.Append(fullName.Substring(nestedStart+1,length:nextNested-nestedStart-1));
233+
}
234+
nestedStart=nextNested;
235+
}
236+
220237
return;
221238
}
222239
}

‎src/runtime/Types/ClassObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public override NewReference type_subscript(BorrowedReference idx)
236236
returnExceptions.RaiseTypeError("type expected");
237237
}
238238
Typea=t.MakeArrayType();
239-
PyTypeo=ClassManager.GetClass(a);
239+
BorrowedReferenceo=ClassManager.GetClass(a);
240240
returnnewNewReference(o);
241241
}
242242

‎src/runtime/Types/ClrObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ internal static NewReference GetReference(object ob, BorrowedReference pyType)
4343

4444
internalstaticNewReferenceGetReference(objectob,Typetype)
4545
{
46-
PyTypecc=ClassManager.GetClass(type);
46+
BorrowedReferencecc=ClassManager.GetClass(type);
4747
returnCreate(ob,cc);
4848
}
4949

5050
internalstaticNewReferenceGetReference(objectob)
5151
{
52-
PyTypecc=ClassManager.GetClass(ob.GetType());
52+
BorrowedReferencecc=ClassManager.GetClass(ob.GetType());
5353
returnCreate(ob,cc);
5454
}
5555

‎src/runtime/Types/GenericType.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
usingSystem;
2+
usingSystem.Linq;
23

34
namespacePython.Runtime
45
{
@@ -20,10 +21,58 @@ internal GenericType(Type tp) : base(tp)
2021
/// </summary>
2122
publicstaticNewReferencetp_new(BorrowedReferencetp,BorrowedReferenceargs,BorrowedReferencekw)
2223
{
24+
varself=(GenericType)GetManagedObject(tp)!;
25+
if(!self.type.Valid)
26+
{
27+
returnExceptions.RaiseTypeError(self.type.DeletedMessage);
28+
}
29+
vartype=self.type.Value;
30+
31+
if(type.IsInterface&&!type.IsConstructedGenericType)
32+
{
33+
varnargs=Runtime.PyTuple_Size(args);
34+
if(nargs==1)
35+
{
36+
varinstance=Runtime.PyTuple_GetItem(args,0);
37+
returnAsGenericInterface(instance,type);
38+
}
39+
}
40+
2341
Exceptions.SetError(Exceptions.TypeError,"cannot instantiate an open generic type");
42+
2443
returndefault;
2544
}
2645

46+
staticNewReferenceAsGenericInterface(BorrowedReferenceinstance,TypetargetType)
47+
{
48+
if(GetManagedObject(instance)is notCLRObjectobj)
49+
{
50+
returnExceptions.RaiseTypeError("only .NET objects can be cast to .NET interfaces");
51+
}
52+
53+
Type[]supportedInterfaces=obj.inst.GetType().GetInterfaces();
54+
Type[]constructedInterfaces=supportedInterfaces
55+
.Where(i=>i.IsConstructedGenericType&&i.GetGenericTypeDefinition()==targetType)
56+
.ToArray();
57+
58+
if(constructedInterfaces.Length==1)
59+
{
60+
BorrowedReferencepythonic=ClassManager.GetClass(constructedInterfaces[0]);
61+
usingvarargs=Runtime.PyTuple_New(1);
62+
Runtime.PyTuple_SetItem(args.Borrow(),0,instance);
63+
returnRuntime.PyObject_CallObject(pythonic,args.Borrow());
64+
}
65+
66+
if(constructedInterfaces.Length>1)
67+
{
68+
stringinterfaces=string.Join(", ",constructedInterfaces.Select(TypeManager.GetPythonTypeName));
69+
returnExceptions.RaiseTypeError("Ambiguous cast to .NET interface. "
70+
+$"Object implements:{interfaces}");
71+
}
72+
73+
returnExceptions.RaiseTypeError("object does not implement "
74+
+TypeManager.GetPythonTypeName(targetType));
75+
}
2776

2877
/// <summary>
2978
/// Implements __call__ for reflected generic types.

‎src/runtime/Types/MethodObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference
191191
&&obj.instisIPythonDerivedType
192192
&&self.type.Value.IsInstanceOfType(obj.inst))
193193
{
194-
varbasecls=ClassManager.GetClass(self.type.Value);
194+
varbasecls=ReflectedClrType.GetOrCreate(self.type.Value);
195195
returnnewMethodBinding(self,newPyObject(ob),basecls).Alloc();
196196
}
197197

‎src/runtime/Types/ReflectedClrType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ internal sealed class ReflectedClrType : PyType
1212
{
1313
privateReflectedClrType(StolenReferencereference):base(reference,prevalidated:true){}
1414
internalReflectedClrType(ReflectedClrTypeoriginal):base(original,prevalidated:true){}
15+
internalReflectedClrType(BorrowedReferenceoriginal):base(original){}
1516
ReflectedClrType(SerializationInfoinfo,StreamingContextcontext):base(info,context){}
1617

1718
internalClassBaseImpl=>(ClassBase)ManagedType.GetManagedObject(this)!;

‎tests/test_collection_mixins.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ def test_dict_items():
1414
k,v=items[0]
1515
assertk==42
1616
assertv=="a"
17+
18+
# regression test for https://github.com/pythonnet/pythonnet/issues/1785
19+
deftest_dict_in_keys():
20+
d=C.Dictionary[str,int]()
21+
d["a"]=42
22+
assert"a"ind.Keys
23+
assert"b"notind.Keys

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp