forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonTypeInfo.cs
More file actions
2094 lines (1768 loc) · 92.3 KB
/
PythonTypeInfo.cs
File metadata and controls
2094 lines (1768 loc) · 92.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using Microsoft.Scripting;
using Microsoft.Scripting.Actions;
using Microsoft.Scripting.Generation;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using IronPython.Runtime.Binding;
using IronPython.Runtime.Operations;
namespace IronPython.Runtime.Types {
/// <summary>
/// Helpers for interacting w/ .NET types. This includes:
///
/// Member resolution via GetMember/GetMembers. This performs a member lookup which includes the registered
/// extension types in the PythonBinder. Internally the class has many MemberResolver's which provide
/// the various resolution behaviors.
///
/// Cached member access - this is via static classes such as Object and provides various MemberInfo's so we're
/// not constantly looking up via reflection.
/// </summary>
internal static partial class PythonTypeInfo {
/// <summary> list of resolvers which we run to resolve items </summary>
private static readonly MemberResolver/*!*/[]/*!*/ _resolvers = MakeResolverTable();
[MultiRuntimeAware]
private static DocumentationDescriptor _docDescr;
[MultiRuntimeAware]
internal static Dictionary<string, PythonOperationKind> _pythonOperatorTable;
#region Public member resolution
/// <summary>
/// Gets the statically known member from the type with the specific name. Searches the entire type hierarchy to find the specified member.
/// </summary>
public static MemberGroup/*!*/ GetMemberAll(PythonBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
Assert.NotNull(binder, action, type, name);
PerfTrack.NoteEvent(PerfTrack.Categories.ReflectedTypes, String.Format("ResolveMember: {0} {1}", type.Name, name));
return GetMemberGroup(new ResolveBinder(binder), action, type, name);
}
/// <summary>
/// Gets all the statically known members from the specified type. Searches the entire type hierarchy to get all possible members.
///
/// The result may include multiple resolution. It is the callers responsibility to only treat the 1st one by name as existing.
/// </summary>
public static IList<ResolvedMember/*!*/>/*!*/ GetMembersAll(PythonBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
Assert.NotNull(binder, action, type);
return GetResolvedMembers(new ResolveBinder(binder), action, type);
}
/// <summary>
/// Gets the statically known member from the type with the specific name. Searches only the specified type to find the member.
/// </summary>
public static MemberGroup/*!*/ GetMember(PythonBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
Assert.NotNull(binder, action, type, name);
PerfTrack.NoteEvent(PerfTrack.Categories.ReflectedTypes, String.Format("LookupMember: {0} {1}", type.Name, name));
return GetMemberGroup(new LookupBinder(binder), action, type, name);
}
/// <summary>
/// Gets all the statically known members from the specified type. Searches only the specified type to find the members.
///
/// The result may include multiple resolution. It is the callers responsibility to only treat the 1st one by name as existing.
/// </summary>
public static IList<ResolvedMember/*!*/>/*!*/ GetMembers(PythonBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
Assert.NotNull(binder, action, type);
return GetResolvedMembers(new LookupBinder(binder), action, type);
}
#endregion
#region Cached type members
public static class _Object {
public static new readonly MethodInfo/*!*/ GetType = typeof(object).GetMethod("GetType");
}
public static class _IPythonObject {
public static readonly PropertyInfo/*!*/ PythonType = typeof(IPythonObject).GetProperty(nameof(IPythonObject.PythonType));
public static readonly PropertyInfo/*!*/ Dict = typeof(IPythonObject).GetProperty(nameof(IPythonObject.Dict));
}
public static class _PythonOps {
public static readonly MethodInfo/*!*/ SlotTryGetBoundValue = typeof(PythonOps).GetMethod(nameof(PythonOps.SlotTryGetBoundValue));
public static readonly MethodInfo/*!*/ GetTypeVersion = typeof(PythonOps).GetMethod(nameof(PythonOps.GetTypeVersion));
public static readonly MethodInfo/*!*/ CheckTypeVersion = typeof(PythonOps).GetMethod(nameof(PythonOps.CheckTypeVersion));
}
public static class _OperationFailed {
public static readonly FieldInfo/*!*/ Value = typeof(OperationFailed).GetField(nameof(OperationFailed.Value));
}
public static class _PythonDictionary {
public static readonly MethodInfo/*!*/ TryGetvalue = typeof(PythonDictionary).GetMethod(nameof(PythonDictionary.TryGetValue));
}
public static class _PythonGenerator {
public static readonly ConstructorInfo Ctor = typeof(PythonGenerator).GetConstructor(new Type[] { typeof(PythonFunction) });
}
#endregion
#region MemberResolver implementations and infrastructure
/// <summary>
/// Abstract class used for resolving members. This provides two methods of member look. The first is looking
/// up a single member by name. The other is getting all of the members.
///
/// There are various subclasses of this which have different methods of resolving the members. The primary
/// function of the resolvers are to provide the name->value lookup. They also need to provide a simple name
/// enumerator. The enumerator is kept simple because it's allowed to return duplicate names as well as return
/// names of members that don't exist. The base MemberResolver will then verify their existance as well as
/// filter duplicates.
/// </summary>
private abstract class MemberResolver {
/// <summary>
/// Looks up an individual member and returns a MemberGroup with the given members.
/// </summary>
public abstract MemberGroup/*!*/ ResolveMember(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name);
/// <summary>
/// Returns a list of members that exist on the type. The ResolvedMember structure indicates both
/// the name and provides the MemberGroup.
/// </summary>
public IList<ResolvedMember/*!*/>/*!*/ ResolveMembers(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
Dictionary<string, ResolvedMember> members = new Dictionary<string, ResolvedMember>();
foreach (string name in GetCandidateNames(binder, action, type)) {
if (members.ContainsKey(name)) {
continue;
}
MemberGroup member = ResolveMember(binder, action, type, name);
if (member.Count > 0) {
members[name] = new ResolvedMember(name, member);
}
}
ResolvedMember[] res = new ResolvedMember[members.Count];
members.Values.CopyTo(res, 0);
return res;
}
/// <summary>
/// Returns a list of possible members which could exist. ResolveMember needs to be called to verify their existance. Duplicate
/// names can also be returned.
/// </summary>
protected abstract IEnumerable<string/*!*/>/*!*/ GetCandidateNames(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type);
}
/// <summary>
/// One off resolver for various special methods which are known by name. A delegate is provided to provide the actual member which
/// will be resolved.
/// </summary>
private class OneOffResolver : MemberResolver {
private string/*!*/ _name;
private Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/>/*!*/ _resolver;
public OneOffResolver(string/*!*/ name, Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/>/*!*/ resolver) {
Assert.NotNull(name, resolver);
_name = name;
_resolver = resolver;
}
public override MemberGroup/*!*/ ResolveMember(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
Assert.NotNull(binder, action, type, name);
if (name == _name) {
return _resolver(binder, type);
}
return MemberGroup.EmptyGroup;
}
protected override IEnumerable<string/*!*/>/*!*/ GetCandidateNames(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
yield return _name;
}
}
/// <summary>
/// Standard resolver for looking up .NET members. Uses reflection to get the members by name.
/// </summary>
private class StandardResolver : MemberResolver {
public override MemberGroup/*!*/ ResolveMember(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
if (name == ".ctor" || name == ".cctor") return MemberGroup.EmptyGroup;
// normal binding
MemberGroup res;
foreach (Type curType in binder.GetContributingTypes(type)) {
res = FilterObjectMembers(FilterSpecialNames(binder.GetMember(curType, name), name, action));
if (res.Count > 0) {
return res;
}
}
if (type.IsInterface) {
foreach (Type t in type.GetInterfaces()) {
res = FilterSpecialNames(binder.GetMember(t, name), name, action);
if (res.Count > 0) {
return res;
}
}
}
return MemberGroup.EmptyGroup;
}
protected override IEnumerable<string/*!*/>/*!*/ GetCandidateNames(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
foreach (Type curType in binder.GetContributingTypes(type)) {
foreach (MemberInfo mi in curType.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) {
if (mi.MemberType == MemberTypes.Method) {
MethodInfo meth = (MethodInfo)mi;
if (meth.IsSpecialName) {
if (meth.IsDefined(typeof(PropertyMethodAttribute), true)) {
if (meth.Name.StartsWith("Get") || meth.Name.StartsWith("Set")) {
yield return meth.Name.Substring(3);
} else {
Debug.Assert(meth.Name.StartsWith("Delete"));
yield return meth.Name.Substring(6);
}
}
continue;
}
}
yield return mi.Name;
}
}
}
}
/// <summary>
/// Resolver that resolves members on Object and ObjectOps by reflection.
/// </summary>
private class ObjectResolver : StandardResolver {
public override MemberGroup/*!*/ ResolveMember(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
if (name == ".ctor" || name == ".cctor") return MemberGroup.EmptyGroup;
IList<Type> contributingTypes = binder.GetContributingTypes(type);
MemberGroup res;
foreach (Type curType in binder.GetContributingTypes(typeof(object))) {
if (!contributingTypes.Contains(curType)) {
continue;
}
res = FilterSpecialNames(binder.GetMember(curType, name), name, action);
if (res.Count > 0) {
return res;
}
}
return MemberGroup.EmptyGroup;
}
}
/// <summary>
/// Resolves methods mapped to __eq__ and __ne__ from:
/// 1. IStructuralEquatable.Equals
/// 2. IValueEquality.Equals (CLR2 only)
/// </summary>
private class EqualityResolver : MemberResolver {
public static readonly EqualityResolver Instance = new EqualityResolver();
private EqualityResolver() { }
public override MemberGroup/*!*/ ResolveMember(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
Assert.NotNull(binder, action, type, name);
bool equality;
switch (name) {
case "__eq__": equality = true; break;
case "__ne__": equality = false; break;
default:
return MemberGroup.EmptyGroup;
}
if (typeof(IStructuralEquatable).IsAssignableFrom(type)) {
return new MemberGroup(
GetEqualityMethods(type, equality ? "StructuralEqualityMethod" : "StructuralInequalityMethod")
);
}
return MemberGroup.EmptyGroup;
}
protected override IEnumerable<string/*!*/>/*!*/ GetCandidateNames(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
yield return "__eq__";
yield return "__ne__";
}
}
/// <summary>
/// Resolves methods mapped to __gt__, __lt__, __ge__, __le__, as well as providing an alternate resolution
/// for __eq__ and __ne__, from the comparable type's CompareTo method.
///
/// This should be run after the EqualityResolver.
/// </summary>
private class ComparisonResolver : MemberResolver {
private readonly bool _excludePrimitiveTypes;
private readonly Type/*!*/ _comparable;
private readonly Dictionary<string/*!*/, string/*!*/>/*!*/ _helperMap;
public ComparisonResolver(Type/*!*/ comparable, string/*!*/ helperPrefix) {
Assert.NotNull(comparable, helperPrefix);
_excludePrimitiveTypes = comparable == typeof(IComparable);
_comparable = comparable;
_helperMap = new Dictionary<string, string>();
_helperMap["__eq__"] = helperPrefix + "Equality";
_helperMap["__ne__"] = helperPrefix + "Inequality";
_helperMap["__gt__"] = helperPrefix + "GreaterThan";
_helperMap["__lt__"] = helperPrefix + "LessThan";
_helperMap["__ge__"] = helperPrefix + "GreaterEqual";
_helperMap["__le__"] = helperPrefix + "LessEqual";
}
public override MemberGroup/*!*/ ResolveMember(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
Assert.NotNull(binder, action, type, name);
// Do not map IComparable if this is a primitive builtin type.
if (_excludePrimitiveTypes) {
if (type.IsPrimitive || type == typeof(BigInteger) ||
type == typeof(string) || type == typeof(decimal)) {
return MemberGroup.EmptyGroup;
}
}
string helperName;
if (_helperMap.TryGetValue(name, out helperName) &&
_comparable.IsAssignableFrom(type)) {
return new MemberGroup(GetEqualityMethods(type, helperName));
}
return MemberGroup.EmptyGroup;
}
protected override IEnumerable<string/*!*/>/*!*/ GetCandidateNames(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
return _helperMap.Keys;
}
}
/// <summary>
/// Resolves methods mapped to __*__ methods automatically from the .NET operator.
/// </summary>
private class OperatorResolver : MemberResolver {
public override MemberGroup/*!*/ ResolveMember(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
if (type.IsSealed && type.IsAbstract) {
// static types don't have PythonOperationKind
return MemberGroup.EmptyGroup;
}
// try mapping __*__ methods to .NET method names
PythonOperationKind opMap;
EnsureOperatorTable();
if (_pythonOperatorTable.TryGetValue(name, out opMap)) {
if (IncludeOperatorMethod(type, opMap)) {
OperatorMapping opInfo;
if (IsReverseOperator(opMap)) {
opInfo = OperatorMapping.GetOperatorMapping(opMap & ~PythonOperationKind.Reversed);
} else {
opInfo = OperatorMapping.GetOperatorMapping(opMap);
}
if (opInfo != null) {
foreach (Type curType in binder.GetContributingTypes(type)) {
if (curType == typeof(double)) {
if ((opInfo.Operator & PythonOperationKind.Comparison) != 0) {
// we override these with our own comparisons in DoubleOps
continue;
}
} else
if (curType == typeof(BigInteger)) {
if (opInfo.Operator == PythonOperationKind.Mod ||
opInfo.Operator == PythonOperationKind.RightShift ||
opInfo.Operator == PythonOperationKind.LeftShift ||
opInfo.Operator == PythonOperationKind.Compare ||
opInfo.Operator == PythonOperationKind.TrueDivide) {
// we override these with our own modulus/power PythonOperationKind which are different from BigInteger.
continue;
}
} else if (curType == typeof(Complex) && opInfo.Operator == PythonOperationKind.TrueDivide) {
// we override this with our own division PythonOperationKind which is different from .NET Complex.
continue;
}
Debug.Assert(opInfo.Name != "Equals");
MemberGroup res = binder.GetMember(curType, opInfo.Name);
if (res.Count == 0 && opInfo.AlternateName != null) {
res = binder.GetMember(curType, opInfo.AlternateName);
if (opInfo.AlternateName == "Equals") {
// "Equals" is available as an alternate method name. Because it's also on object and Python
// doesn't define it on object we need to filter it out.
res = FilterObjectEquality(res);
}
res = FilterAlternateMethods(opInfo, res);
}
if (res.Count > 0) {
return FilterForwardReverseMethods(name, res, type, opMap);
}
}
}
}
}
if (name == "__call__") {
MemberGroup res = binder.GetMember(type, "Call");
if (res.Count > 0) {
return res;
}
}
return MemberGroup.EmptyGroup;
}
/// <summary>
/// Filters alternative methods out that don't match the expected signature and therefore
/// are just sharing a common method name.
/// </summary>
private static MemberGroup FilterAlternateMethods(OperatorMapping opInfo, MemberGroup res) {
if (res.Count > 0 && opInfo.AlternateExpectedType != null) {
List<MemberTracker> matchingMethods = new List<MemberTracker>();
for (int i = 0; i < res.Count; i++) {
MemberTracker mt = res[i];
if (mt.MemberType == TrackerTypes.Method &&
((MethodTracker)mt).Method.ReturnType == opInfo.AlternateExpectedType) {
matchingMethods.Add(mt);
}
}
if (matchingMethods.Count == 0) {
res = MemberGroup.EmptyGroup;
} else {
res = new MemberGroup(matchingMethods.ToArray());
}
}
return res;
}
/// <summary>
/// Removes Object.Equals methods as we never return these for PythonOperationKind.
/// </summary>
private static MemberGroup/*!*/ FilterObjectEquality(MemberGroup/*!*/ group) {
List<MemberTracker> res = null;
for (int i = 0; i < group.Count; i++) {
MemberTracker mt = group[i];
if (mt.MemberType == TrackerTypes.Method && (mt.DeclaringType == typeof(object) || mt.DeclaringType == typeof(double) || mt.DeclaringType == typeof(float)) && mt.Name == "Equals") {
if (res == null) {
res = new List<MemberTracker>();
for (int j = 0; j < i; j++) {
res.Add(group[j]);
}
}
} else if (mt.MemberType == TrackerTypes.Method && mt.DeclaringType == typeof(ValueType) && mt.Name == "Equals") {
// ValueType.Equals overrides object.Equals but we can't call it w/ a boxed value type therefore
// we return the object version and the virtual call will dispatch to ValueType.Equals.
if (res == null) {
res = new List<MemberTracker>();
for (int j = 0; j < i; j++) {
res.Add(group[j]);
}
}
res.Add(MemberTracker.FromMemberInfo(typeof(object).GetMethod("Equals", new Type[] { typeof(object) })));
} else {
res?.Add(@group[i]);
}
}
if (res != null) {
if (res.Count == 0) {
return MemberGroup.EmptyGroup;
}
return new MemberGroup(res.ToArray());
}
return group;
}
protected override IEnumerable<string/*!*/>/*!*/ GetCandidateNames(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
EnsureOperatorTable();
foreach (string si in _pythonOperatorTable.Keys) {
yield return si;
}
yield return "__call__";
}
}
/// <summary>
/// Provides bindings to private members when that global option is enabled.
/// </summary>
private class PrivateBindingResolver : MemberResolver {
private const BindingFlags _privateFlags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
public override MemberGroup/*!*/ ResolveMember(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
if (binder.DomainManager.Configuration.PrivateBinding) {
// in private binding mode Python exposes private members under a mangled name.
string header = "_" + type.Name + "__";
if (name.StartsWith(header)) {
string memberName = name.Substring(header.Length);
MemberGroup res = new MemberGroup(type.GetMember(memberName, _privateFlags));
if (res.Count > 0) {
return FilterFieldAndEvent(res);
}
res = new MemberGroup(type.GetMember(memberName, BindingFlags.FlattenHierarchy | _privateFlags));
if (res.Count > 0) {
return FilterFieldAndEvent(res);
}
}
}
return MemberGroup.EmptyGroup;
}
protected override IEnumerable<string/*!*/>/*!*/ GetCandidateNames(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
if (!binder.DomainManager.Configuration.PrivateBinding) {
yield break;
}
foreach (MemberInfo mi in type.GetMembers(_privateFlags | BindingFlags.FlattenHierarchy)) {
yield return String.Concat("_", mi.DeclaringType.Name, "__", mi.Name);
}
}
}
/// <summary>
/// Provides resolutions for protected members that haven't yet been
/// subclassed by NewTypeMaker.
/// </summary>
private class ProtectedMemberResolver : MemberResolver {
public override MemberGroup/*!*/ ResolveMember(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type, string/*!*/ name) {
foreach (Type t in binder.GetContributingTypes(type)) {
MemberGroup res = new MemberGroup(
t.GetMember(name, BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
.Where(ProtectedOnly)
.ToArray());
for (int i = 0; i < res.Count; i++) {
if (!(res[i] is MethodTracker meth)) {
continue;
}
if (meth.Name == "Finalize" && meth.Method.GetBaseDefinition() == typeof(object).GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance)) {
MemberTracker[] retained = new MemberTracker[res.Count - 1];
if (res.Count == 1) {
res = MemberGroup.EmptyGroup;
} else {
for (int j = 0; j < i; j++) {
retained[j] = res[j];
}
for (int j = i + 1; j < res.Count; j++) {
retained[j - 1] = res[j];
}
res = new MemberGroup(retained);
}
break;
}
}
return FilterSpecialNames(res, name, action);
}
return MemberGroup.EmptyGroup;
}
protected override IEnumerable<string/*!*/>/*!*/ GetCandidateNames(MemberBinder/*!*/ binder, MemberRequestKind/*!*/ action, Type/*!*/ type) {
// these members are visible but only accept derived types.
foreach (Type t in binder.GetContributingTypes(type)) {
foreach (MemberInfo mi in t.GetMembers(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic)) {
if (ProtectedOnly(mi)) {
yield return mi.Name;
}
}
}
}
}
/// <summary>
/// Creates the resolver table which includes all the possible resolutions.
/// </summary>
/// <returns></returns>
private static MemberResolver/*!*/[]/*!*/ MakeResolverTable() {
return new MemberResolver[] {
// The standard resolver looks for types using .NET reflection by name,
// but not those that live on object; those that live on object are
// picked by the ObjectResolver
new StandardResolver(),
// The following members and __format__ live on object
new OneOffResolver("__str__", StringResolver),
new OneOffResolver("__new__", NewResolver),
new OneOffResolver("__repr__", ReprResolver),
new OneOffResolver("__hash__", HashResolver),
new OneOffResolver("__iter__", IterResolver),
#if FEATURE_SERIALIZATION
new OneOffResolver("__reduce_ex__", SerializationResolver),
#endif
// Runs after StandardResolver so custom __eq__ methods can be added
// that support things like returning NotImplemented vs. IValueEquality
// which only supports true/false. Runs before OperatorResolver so that
// IStructuralEquatable and IValueEquality take precedence over Equals,
// which can be provided for nice .NET interop.
EqualityResolver.Instance,
new ComparisonResolver(typeof(IStructuralComparable), "StructuralComparable"),
new OneOffResolver("__all__", AllResolver),
new OneOffResolver("__contains__", ContainsResolver),
new OneOffResolver("__dir__", DirResolver),
new OneOffResolver("__doc__", DocResolver),
new OneOffResolver("__enter__", EnterResolver),
new OneOffResolver("__exit__", ExitResolver),
new OneOffResolver("__len__", LengthResolver),
new OneOffResolver("__format__", FormatResolver),
new OneOffResolver("__next__", NextResolver),
new OneOffResolver("__complex__", ComplexResolver),
new OneOffResolver("__float__", FloatResolver),
new OneOffResolver("__int__", IntResolver),
new OneOffResolver("__long__", BigIntegerResolver),
// non standard PythonOperationKind which are Python specific
new OneOffResolver("__truediv__", new OneOffOperatorBinder("TrueDivide", "__truediv__", PythonOperationKind.TrueDivide).Resolver),
new OneOffResolver("__rtruediv__", new OneOffOperatorBinder("TrueDivide", "__rtruediv__", PythonOperationKind.ReverseTrueDivide).Resolver),
new OneOffResolver("__itruediv__", new OneOffOperatorBinder("InPlaceTrueDivide", "__itruediv__", PythonOperationKind.InPlaceTrueDivide).Resolver),
new OneOffResolver("__floordiv__", new OneOffOperatorBinder("FloorDivide", "__floordiv__", PythonOperationKind.FloorDivide).Resolver),
new OneOffResolver("__rfloordiv__", new OneOffOperatorBinder("FloorDivide", "__rfloordiv__", PythonOperationKind.ReverseFloorDivide).Resolver),
new OneOffResolver("__ifloordiv__", new OneOffOperatorBinder("InPlaceFloorDivide", "__ifloordiv__", PythonOperationKind.InPlaceFloorDivide).Resolver),
new OneOffResolver("__pow__", new OneOffPowerBinder("__pow__", PythonOperationKind.Power).Resolver),
new OneOffResolver("__rpow__", new OneOffPowerBinder("__rpow__", PythonOperationKind.ReversePower).Resolver),
new OneOffResolver("__ipow__", new OneOffOperatorBinder("InPlacePower", "__ipow__", PythonOperationKind.InPlacePower).Resolver),
new OneOffResolver("__abs__", new OneOffOperatorBinder("Abs", "__abs__", PythonOperationKind.AbsoluteValue).Resolver),
new OneOffResolver("__divmod__", new OneOffOperatorBinder("DivMod", "__divmod__", PythonOperationKind.DivMod).Resolver),
new OneOffResolver("__rdivmod__", new OneOffOperatorBinder("DivMod", "__rdivmod__", PythonOperationKind.DivMod).Resolver),
// The operator resolver maps standard .NET operator methods into Python operator
// methods
new OperatorResolver(),
// Runs after operator resolver to map default members to __getitem__/__setitem__
new OneOffResolver("__getitem__", GetItemResolver),
new OneOffResolver("__setitem__", SetItemResolver),
// Runs after operator resolver to map __ne__ -> !__eq__
new OneOffResolver("__ne__", FallbackInequalityResolver),
// Runs after the operator resolver to map IComparable
new ComparisonResolver(typeof(IComparable), "Comparable"),
new ObjectResolver(),
// Protected members are visible but only usable from derived types
new ProtectedMemberResolver(),
// Support binding to private members if the user has enabled that feature
new PrivateBindingResolver(),
};
}
#endregion
#region One-off resolvers
#region Resolving numerical conversions (__complex__, __float__, __int__, and __long__)
/// <summary>
/// Provides a resolution for __complex__
/// </summary>
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/>/*!*/ ComplexResolver {
get {
if (_ComplexResolver != null) return _ComplexResolver;
_ComplexResolver = MakeConversionResolver(new List<Type> {
typeof(Complex), typeof(ExtensibleComplex), typeof(Extensible<Complex>),
typeof(double), typeof(Extensible<double>)
});
return _ComplexResolver;
}
}
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/> _ComplexResolver;
/// <summary>
/// Provides a resolution for __float__
/// </summary>
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/>/*!*/ FloatResolver {
get {
if (_FloatResolver != null) return _FloatResolver;
_FloatResolver = MakeConversionResolver(new List<Type> {
typeof(double), typeof(Extensible<double>)
});
return _FloatResolver;
}
}
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/> _FloatResolver;
/// <summary>
/// Provides a resolution for __int__
/// </summary>
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/>/*!*/ IntResolver {
get {
if (_IntResolver != null) return _IntResolver;
_IntResolver = MakeConversionResolver(new List<Type> {
typeof(int), typeof(Extensible<int>),
typeof(BigInteger), typeof(Extensible<BigInteger>)
});
return _IntResolver;
}
}
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/> _IntResolver;
/// <summary>
/// Provides a resolution for __long__
/// </summary>
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/>/*!*/ BigIntegerResolver {
get {
if (_BigIntegerResolver != null) return _BigIntegerResolver;
_BigIntegerResolver = MakeConversionResolver(new List<Type> {
typeof(BigInteger), typeof(Extensible<BigInteger>),
typeof(int), typeof(Extensible<int>)
});
return _BigIntegerResolver;
}
}
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/> _BigIntegerResolver;
/// <summary>
/// Provides a resolution for __getitem__
/// </summary>
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/>/*!*/ GetItemResolver {
get {
if (_GetItemResolver == null) {
_GetItemResolver = MakeIndexerResolver(false);
}
return _GetItemResolver;
}
}
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/> _GetItemResolver;
/// <summary>
/// Provides a resolution for __setitem__
/// </summary>
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/>/*!*/ SetItemResolver {
get {
if (_SetItemResolver == null) {
_SetItemResolver = MakeIndexerResolver(true);
}
return _SetItemResolver;
}
}
private static Func<MemberBinder/*!*/, Type/*!*/, MemberGroup/*!*/> _SetItemResolver;
#endregion
/// <summary>
/// Provides a resolution for __str__.
/// </summary>
private static MemberGroup/*!*/ StringResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
if (type != typeof(double) && type != typeof(float)
&& type != typeof(Complex)) {
MethodInfo tostr = type.GetMethod("ToString", ReflectionUtils.EmptyTypes);
if (tostr != null && tostr.DeclaringType != typeof(object)) {
return GetInstanceOpsMethod(type, "ToStringMethod");
}
}
return MemberGroup.EmptyGroup;
}
/// <summary>
/// Provides a resolution for __repr__
/// </summary>
private static MemberGroup/*!*/ ReprResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
// __repr__ for normal .NET types is special, if we're a Python type then
// we'll use one of the built-in reprs (from object or from the type)
if (!PythonBinder.IsPythonType(type) &&
(!type.IsSealed || !type.IsAbstract)) { // static types don't get __repr__
// check and see if __repr__ has been overridden by the base type.
foreach (Type t in binder.GetContributingTypes(type)) {
if (t == typeof(ObjectOps) && type != typeof(object)) {
break;
}
if (t.GetMember("__repr__").Length > 0) {
// type has a specific __repr__ overload, pick it up normally later
return MemberGroup.EmptyGroup;
}
}
// no override, pick up the default fancy .NET __repr__
return binder.GetBaseInstanceMethod(type, "FancyRepr");
}
return MemberGroup.EmptyGroup;
}
#if FEATURE_SERIALIZATION
private static MemberGroup/*!*/ SerializationResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
if (type.IsSerializable && !PythonBinder.IsPythonType(type)) {
string methodName = "__reduce_ex__";
if (!TypeOverridesMethod(binder, type, methodName)) {
return GetInstanceOpsMethod(type, "SerializeReduce");
}
}
return MemberGroup.EmptyGroup;
}
#endif
/// <summary>
/// Helper to see if the type explicitly overrides the method. This ignores members
/// defined on object.
/// </summary>
private static bool TypeOverridesMethod(MemberBinder/*!*/ binder, Type/*!*/ type, string/*!*/ methodName) {
// check and see if the method has been overridden by the base type.
foreach (Type t in binder.GetContributingTypes(type)) {
if (!PythonBinder.IsPythonType(type) && t == typeof(ObjectOps) && type != typeof(object)) {
break;
}
MemberInfo[] reduce = t.GetMember(methodName);
if (reduce.Length > 0) {
// type has a specific overload
return true;
}
}
return false;
}
/// <summary>
/// Provides a resolution for __hash__, first looking for IStructuralEquatable.GetHashCode,
/// then IValueEquality.GetValueHashCode.
/// </summary>
private static MemberGroup/*!*/ HashResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
if (typeof(IStructuralEquatable).IsAssignableFrom(type) && !type.IsInterface) {
// check and see if __hash__ has been overridden by the base type.
foreach (Type t in binder.GetContributingTypes(type)) {
// if it's defined on object, it's not overridden
if (t == typeof(ObjectOps) || t == typeof(object)) {
break;
}
MemberInfo[] hash = t.GetMember("__hash__");
if (hash.Length > 0) {
return MemberGroup.EmptyGroup;
}
}
return GetInstanceOpsMethod(type, "StructuralHashMethod");
}
// otherwise we'll pick up __hash__ from ObjectOps which will call .NET's .GetHashCode therefore
// we don't explicitly search to see if the object overrides GetHashCode here.
return MemberGroup.EmptyGroup;
}
/// <summary>
/// Provides a resolution for __new__. For standard .NET types __new__ resolves to their
/// constructor. For Python types they inherit __new__ from their base class.
///
/// TODO: Can we just always fallback to object.__new__? If not why not?
/// </summary>
private static MemberGroup/*!*/ NewResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
if (type.IsSealed && type.IsAbstract) {
// static types don't have __new__
return MemberGroup.EmptyGroup;
}
bool isPythonType = typeof(IPythonObject).IsAssignableFrom(type);
// check and see if __new__ has been overridden by the base type.
foreach (Type t in binder.GetContributingTypes(type)) {
if (!isPythonType && t == typeof(ObjectOps) && type != typeof(object)) {
break;
}
MemberInfo[] news = t.GetMember("__new__");
if (news.Length > 0) {
// type has a specific __new__ overload, return that for the constructor
return GetExtensionMemberGroup(type, news);
}
}
// TODO: CompilerHelpers.GetConstructors(type, binder.DomainManager.Configuration.PrivateBinding, true);
var ctors = CompilerHelpers.FilterConstructorsToPublicAndProtected(
type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
).ToArray();
// type has no Python __new__, just return the .NET constructors if they have
// a custom new
if (!PythonTypeOps.IsDefaultNew(ctors)) {
return new MemberGroup(ctors);
}
// if no ctor w/ parameters are defined, fall back to object.__new__ which
// will ignore all the extra arguments allowing the user to just override
// __init__.
return MemberGroup.EmptyGroup;
}
internal static MemberGroup GetExtensionMemberGroup(Type type, MemberInfo[] news) {
List<MemberTracker> mts = new List<MemberTracker>();
foreach (MemberInfo mi in news) {
if (mi.MemberType == MemberTypes.Method) {
if (mi.DeclaringType.IsAssignableFrom(type)) {
mts.Add(MethodTracker.FromMemberInfo(mi));
} else {
mts.Add(MethodTracker.FromMemberInfo(mi, type));
}
}
}
return new MemberGroup(mts.ToArray());
}
/// <summary>
/// Provides a resolution for next
/// </summary>
private static MemberGroup/*!*/ NextResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
if (typeof(IEnumerator).IsAssignableFrom(type)) {
return GetInstanceOpsMethod(type, "NextMethod");
}
return MemberGroup.EmptyGroup;
}
/// <summary>
/// Provides a resolution for __len__
/// </summary>
private static MemberGroup/*!*/ LengthResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
if (!type.IsDefined(typeof(DontMapICollectionToLenAttribute), true)) {
if (binder.GetInterfaces(type).Contains(typeof(ICollection))) {
return GetInstanceOpsMethod(type, "LengthMethod");
}
foreach (Type t in binder.GetInterfaces(type)) {
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICollection<>)) {
MethodInfo genMeth = typeof(InstanceOps).GetMethod(nameof(InstanceOps.GenericLengthMethod));
return new MemberGroup(
MethodTracker.FromMemberInfo(genMeth.MakeGenericMethod(t.GetGenericArguments()), type)
);
}
}
}
return MemberGroup.EmptyGroup;
}
/// <summary>
/// Provides a resolution for __iter__
/// </summary>
private static MemberGroup/*!*/ IterResolver(MemberBinder/*!*/ binder, Type/*!*/ type) {
if (type == typeof(string)) {
// __iter__ is only exposed in 3.0
return GetInstanceOpsMethod(type, nameof(InstanceOps.IterMethodForString));
}
if (typeof(Bytes).IsAssignableFrom(type)) {
// __iter__ is only exposed in 3.0
return GetInstanceOpsMethod(type, nameof(InstanceOps.IterMethodForBytes));
}
foreach (Type t in binder.GetContributingTypes(type)) {
MemberInfo[] news = t.GetMember("__iter__");
if (news.Length > 0) {
// type has a specific __iter__ overload, we'll pick it up later
return MemberGroup.EmptyGroup;