FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/methodbinder.cs at master · sdpython/pythonnet · GitHub
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
sdpython
pythonnet
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
methodbinder.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
1016 lines (916 loc) · 37.4 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
methodbinder.cs
Copy path
File metadata and controls
1016 lines (916 loc) · 37.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
using
System
;
using
System
.
Collections
;
using
System
.
Reflection
;
using
System
.
Text
;
using
System
.
Collections
.
Generic
;
using
System
.
Linq
;
namespace
Python
.
Runtime
{
using
MaybeMethodBase
=
MaybeMethodBase
<
MethodBase
>
;
/// <summary>
/// A MethodBinder encapsulates information about a (possibly overloaded)
/// managed method, and is responsible for selecting the right method given
/// a set of Python arguments. This is also used as a base class for the
/// ConstructorBinder, a minor variation used to invoke constructors.
/// </summary>
[
Serializable
]
internal
class
MethodBinder
{
public
List
<
MaybeMethodBase
>
list
;
[
NonSerialized
]
public
MethodBase
[
]
methods
;
[
NonSerialized
]
public
bool
init
=
false
;
public
const
bool
DefaultAllowThreads
=
true
;
public
bool
allow_threads
=
DefaultAllowThreads
;
internal
MethodBinder
(
)
{
list
=
new
List
<
MaybeMethodBase
>
(
)
;
}
internal
MethodBinder
(
MethodInfo
mi
)
{
list
=
new
List
<
MaybeMethodBase
>
{
new
MaybeMethodBase
(
mi
)
}
;
}
public
int
Count
{
get
{
return
list
.
Count
;
}
}
internal
void
AddMethod
(
MethodBase
m
)
{
list
.
Add
(
m
)
;
}
/// <summary>
/// Given a sequence of MethodInfo and a sequence of types, return the
/// MethodInfo that matches the signature represented by those types.
/// </summary>
internal
static
MethodInfo
MatchSignature
(
MethodInfo
[
]
mi
,
Type
[
]
tp
)
{
if
(
tp
==
null
)
{
return
null
;
}
int
count
=
tp
.
Length
;
foreach
(
MethodInfo
t
in
mi
)
{
ParameterInfo
[
]
pi
=
t
.
GetParameters
(
)
;
if
(
pi
.
Length
!=
count
)
{
continue
;
}
for
(
var
n
=
0
;
n
<
pi
.
Length
;
n
++
)
{
if
(
tp
[
n
]
!=
pi
[
n
]
.
ParameterType
)
{
break
;
}
if
(
n
==
pi
.
Length
-
1
)
{
return
t
;
}
}
}
return
null
;
}
/// <summary>
/// Given a sequence of MethodInfo and a sequence of type parameters,
/// return the MethodInfo that represents the matching closed generic.
/// </summary>
internal
static
MethodInfo
MatchParameters
(
MethodInfo
[
]
mi
,
Type
[
]
tp
)
{
if
(
tp
==
null
)
{
return
null
;
}
int
count
=
tp
.
Length
;
foreach
(
MethodInfo
t
in
mi
)
{
if
(
!
t
.
IsGenericMethodDefinition
)
{
continue
;
}
Type
[
]
args
=
t
.
GetGenericArguments
(
)
;
if
(
args
.
Length
!=
count
)
{
continue
;
}
return
t
.
MakeGenericMethod
(
tp
)
;
}
return
null
;
}
/// <summary>
/// Given a sequence of MethodInfo and two sequences of type parameters,
/// return the MethodInfo that matches the signature and the closed generic.
/// </summary>
internal
static
MethodInfo
MatchSignatureAndParameters
(
MethodInfo
[
]
mi
,
Type
[
]
genericTp
,
Type
[
]
sigTp
)
{
if
(
genericTp
==
null
||
sigTp
==
null
)
{
return
null
;
}
int
genericCount
=
genericTp
.
Length
;
int
signatureCount
=
sigTp
.
Length
;
foreach
(
MethodInfo
t
in
mi
)
{
if
(
!
t
.
IsGenericMethodDefinition
)
{
continue
;
}
Type
[
]
genericArgs
=
t
.
GetGenericArguments
(
)
;
if
(
genericArgs
.
Length
!=
genericCount
)
{
continue
;
}
ParameterInfo
[
]
pi
=
t
.
GetParameters
(
)
;
if
(
pi
.
Length
!=
signatureCount
)
{
continue
;
}
for
(
var
n
=
0
;
n
<
pi
.
Length
;
n
++
)
{
if
(
sigTp
[
n
]
!=
pi
[
n
]
.
ParameterType
)
{
break
;
}
if
(
n
==
pi
.
Length
-
1
)
{
MethodInfo
match
=
t
;
if
(
match
.
IsGenericMethodDefinition
)
{
// FIXME: typeArgs not used
Type
[
]
typeArgs
=
match
.
GetGenericArguments
(
)
;
return
match
.
MakeGenericMethod
(
genericTp
)
;
}
return
match
;
}
}
}
return
null
;
}
/// <summary>
/// Return the array of MethodInfo for this method. The result array
/// is arranged in order of precedence (done lazily to avoid doing it
/// at all for methods that are never called).
/// </summary>
internal
MethodBase
[
]
GetMethods
(
)
{
if
(
!
init
)
{
// I'm sure this could be made more efficient.
list
.
Sort
(
new
MethodSorter
(
)
)
;
methods
=
(
from
method
in
list
where
method
.
Valid
select
method
.
Value
)
.
ToArray
(
)
;
init
=
true
;
}
return
methods
;
}
/// <summary>
/// Precedence algorithm largely lifted from Jython - the concerns are
/// generally the same so we'll start with this and tweak as necessary.
/// </summary>
/// <remarks>
/// Based from Jython `org.python.core.ReflectedArgs.precedence`
/// See: https://github.com/jythontools/jython/blob/master/src/org/python/core/ReflectedArgs.java#L192
/// </remarks>
internal
static
int
GetPrecedence
(
MethodBase
mi
)
{
if
(
mi
==
null
)
{
return
int
.
MaxValue
;
}
ParameterInfo
[
]
pi
=
mi
.
GetParameters
(
)
;
int
val
=
mi
.
IsStatic
?
3000
:
0
;
int
num
=
pi
.
Length
;
val
+=
mi
.
IsGenericMethod
?
1
:
0
;
for
(
var
i
=
0
;
i
<
num
;
i
++
)
{
val
+=
ArgPrecedence
(
pi
[
i
]
.
ParameterType
)
;
}
return
val
;
}
/// <summary>
/// Return a precedence value for a particular Type object.
/// </summary>
internal
static
int
ArgPrecedence
(
Type
t
)
{
Type
objectType
=
typeof
(
object
)
;
if
(
t
==
objectType
)
{
return
3000
;
}
if
(
t
.
IsArray
)
{
Type
e
=
t
.
GetElementType
(
)
;
if
(
e
==
objectType
)
{
return
2500
;
}
return
100
+
ArgPrecedence
(
e
)
;
}
TypeCode
tc
=
Type
.
GetTypeCode
(
t
)
;
// TODO: Clean up
switch
(
tc
)
{
case
TypeCode
.
Object
:
return
1
;
case
TypeCode
.
UInt64
:
return
10
;
case
TypeCode
.
UInt32
:
return
11
;
case
TypeCode
.
UInt16
:
return
12
;
case
TypeCode
.
Int64
:
return
13
;
case
TypeCode
.
Int32
:
return
14
;
case
TypeCode
.
Int16
:
return
15
;
case
TypeCode
.
Char
:
return
16
;
case
TypeCode
.
SByte
:
return
17
;
case
TypeCode
.
Byte
:
return
18
;
case
TypeCode
.
Single
:
return
20
;
case
TypeCode
.
Double
:
return
21
;
case
TypeCode
.
String
:
return
30
;
case
TypeCode
.
Boolean
:
return
40
;
}
return
2000
;
}
/// <summary>
/// Bind the given Python instance and arguments to a particular method
/// overload and return a structure that contains the converted Python
/// instance, converted arguments and the correct method to call.
/// </summary>
internal
Binding
Bind
(
IntPtr
inst
,
IntPtr
args
,
IntPtr
kw
)
{
return
Bind
(
inst
,
args
,
kw
,
null
,
null
)
;
}
internal
Binding
Bind
(
IntPtr
inst
,
IntPtr
args
,
IntPtr
kw
,
MethodBase
info
)
{
return
Bind
(
inst
,
args
,
kw
,
info
,
null
)
;
}
private
readonly
struct
MatchedMethod
{
public
MatchedMethod
(
int
kwargsMatched
,
int
defaultsNeeded
,
object
[
]
margs
,
int
outs
,
MethodBase
mb
)
{
KwargsMatched
=
kwargsMatched
;
DefaultsNeeded
=
defaultsNeeded
;
ManagedArgs
=
margs
;
Outs
=
outs
;
Method
=
mb
;
}
public
int
KwargsMatched
{
get
;
}
public
int
DefaultsNeeded
{
get
;
}
public
object
[
]
ManagedArgs
{
get
;
}
public
int
Outs
{
get
;
}
public
MethodBase
Method
{
get
;
}
}
internal
Binding
Bind
(
IntPtr
inst
,
IntPtr
args
,
IntPtr
kw
,
MethodBase
info
,
MethodInfo
[
]
methodinfo
)
{
// loop to find match, return invoker w/ or /wo error
MethodBase
[
]
_methods
=
null
;
var
kwargDict
=
new
Dictionary
<
string
,
IntPtr
>
(
)
;
if
(
kw
!=
IntPtr
.
Zero
)
{
var
pynkwargs
=
(
int
)
Runtime
.
PyDict_Size
(
kw
)
;
IntPtr
keylist
=
Runtime
.
PyDict_Keys
(
kw
)
;
IntPtr
valueList
=
Runtime
.
PyDict_Values
(
kw
)
;
for
(
int
i
=
0
;
i
<
pynkwargs
;
++
i
)
{
var
keyStr
=
Runtime
.
GetManagedString
(
Runtime
.
PyList_GetItem
(
new
BorrowedReference
(
keylist
)
,
i
)
)
;
kwargDict
[
keyStr
]
=
Runtime
.
PyList_GetItem
(
new
BorrowedReference
(
valueList
)
,
i
)
.
DangerousGetAddress
(
)
;
}
Runtime
.
XDecref
(
keylist
)
;
Runtime
.
XDecref
(
valueList
)
;
}
var
pynargs
=
(
int
)
Runtime
.
PyTuple_Size
(
args
)
;
var
isGeneric
=
false
;
if
(
info
!=
null
)
{
_methods
=
new
MethodBase
[
1
]
;
_methods
.
SetValue
(
info
,
0
)
;
}
else
{
_methods
=
GetMethods
(
)
;
}
var
argMatchedMethods
=
new
List
<
MatchedMethod
>
(
_methods
.
Length
)
;
// TODO: Clean up
foreach
(
MethodBase
mi
in
_methods
)
{
if
(
mi
.
IsGenericMethod
)
{
isGeneric
=
true
;
}
ParameterInfo
[
]
pi
=
mi
.
GetParameters
(
)
;
ArrayList
defaultArgList
;
bool
paramsArray
;
int
kwargsMatched
;
int
defaultsNeeded
;
bool
isOperator
=
OperatorMethod
.
IsOperatorMethod
(
mi
)
;
// Binary operator methods will have 2 CLR args but only one Python arg
// (unary operators will have 1 less each), since Python operator methods are bound.
isOperator
=
isOperator
&&
pynargs
==
pi
.
Length
-
1
;
bool
isReverse
=
isOperator
&&
OperatorMethod
.
IsReverse
(
(
MethodInfo
)
mi
)
;
// Only cast if isOperator.
if
(
isReverse
&&
OperatorMethod
.
IsComparisonOp
(
(
MethodInfo
)
mi
)
)
continue
;
// Comparison operators in Python have no reverse mode.
if
(
!
MatchesArgumentCount
(
pynargs
,
pi
,
kwargDict
,
out
paramsArray
,
out
defaultArgList
,
out
kwargsMatched
,
out
defaultsNeeded
)
&&
!
isOperator
)
{
continue
;
}
// Preprocessing pi to remove either the first or second argument.
if
(
isOperator
&&
!
isReverse
)
{
// The first Python arg is the right operand, while the bound instance is the left.
// We need to skip the first (left operand) CLR argument.
pi
=
pi
.
Skip
(
1
)
.
ToArray
(
)
;
}
else
if
(
isOperator
&&
isReverse
)
{
// The first Python arg is the left operand.
// We need to take the first CLR argument.
pi
=
pi
.
Take
(
1
)
.
ToArray
(
)
;
}
var
outs
=
0
;
var
margs
=
TryConvertArguments
(
pi
,
paramsArray
,
args
,
pynargs
,
kwargDict
,
defaultArgList
,
needsResolution
:
_methods
.
Length
>
1
,
// If there's more than one possible match.
outs
:
out
outs
)
;
if
(
margs
==
null
)
{
continue
;
}
if
(
isOperator
)
{
if
(
inst
!=
IntPtr
.
Zero
)
{
if
(
ManagedType
.
GetManagedObject
(
inst
)
is
CLRObject
co
)
{
bool
isUnary
=
pynargs
==
0
;
// Postprocessing to extend margs.
var
margsTemp
=
isUnary
?
new
object
[
1
]
:
new
object
[
2
]
;
// If reverse, the bound instance is the right operand.
int
boundOperandIndex
=
isReverse
?
1
:
0
;
// If reverse, the passed instance is the left operand.
int
passedOperandIndex
=
isReverse
?
0
:
1
;
margsTemp
[
boundOperandIndex
]
=
co
.
inst
;
if
(
!
isUnary
)
{
margsTemp
[
passedOperandIndex
]
=
margs
[
0
]
;
}
margs
=
margsTemp
;
}
else
{
break
;
}
}
}
var
matchedMethod
=
new
MatchedMethod
(
kwargsMatched
,
defaultsNeeded
,
margs
,
outs
,
mi
)
;
argMatchedMethods
.
Add
(
matchedMethod
)
;
}
if
(
argMatchedMethods
.
Count
>
0
)
{
var
bestKwargMatchCount
=
argMatchedMethods
.
Max
(
x
=>
x
.
KwargsMatched
)
;
var
fewestDefaultsRequired
=
argMatchedMethods
.
Where
(
x
=>
x
.
KwargsMatched
==
bestKwargMatchCount
)
.
Min
(
x
=>
x
.
DefaultsNeeded
)
;
int
bestCount
=
0
;
int
bestMatchIndex
=
-
1
;
for
(
int
index
=
0
;
index
<
argMatchedMethods
.
Count
;
index
++
)
{
var
testMatch
=
argMatchedMethods
[
index
]
;
if
(
testMatch
.
DefaultsNeeded
==
fewestDefaultsRequired
&&
testMatch
.
KwargsMatched
==
bestKwargMatchCount
)
{
bestCount
++
;
if
(
bestMatchIndex
==
-
1
)
bestMatchIndex
=
index
;
}
}
if
(
bestCount
>
1
&&
fewestDefaultsRequired
>
0
)
{
// Best effort for determining method to match on gives multiple possible
// matches and we need at least one default argument - bail from this point
return
null
;
}
// If we're here either:
// (a) There is only one best match
// (b) There are multiple best matches but none of them require
// default arguments
// in the case of (a) we're done by default. For (b) regardless of which
// method we choose, all arguments are specified _and_ can be converted
// from python to C# so picking any will suffice
MatchedMethod
bestMatch
=
argMatchedMethods
[
bestMatchIndex
]
;
var
margs
=
bestMatch
.
ManagedArgs
;
var
outs
=
bestMatch
.
Outs
;
var
mi
=
bestMatch
.
Method
;
object
target
=
null
;
if
(
!
mi
.
IsStatic
&&
inst
!=
IntPtr
.
Zero
)
{
//CLRObject co = (CLRObject)ManagedType.GetManagedObject(inst);
// InvalidCastException: Unable to cast object of type
// 'Python.Runtime.ClassObject' to type 'Python.Runtime.CLRObject'
var
co
=
ManagedType
.
GetManagedObject
(
inst
)
as
CLRObject
;
// Sanity check: this ensures a graceful exit if someone does
// something intentionally wrong like call a non-static method
// on the class rather than on an instance of the class.
// XXX maybe better to do this before all the other rigmarole.
if
(
co
==
null
)
{
return
null
;
}
target
=
co
.
inst
;
}
return
new
Binding
(
mi
,
target
,
margs
,
outs
)
;
}
// We weren't able to find a matching method but at least one
// is a generic method and info is null. That happens when a generic
// method was not called using the [] syntax. Let's introspect the
// type of the arguments and use it to construct the correct method.
if
(
isGeneric
&&
info
==
null
&&
methodinfo
!=
null
)
{
Type
[
]
types
=
Runtime
.
PythonArgsToTypeArray
(
args
,
true
)
;
MethodInfo
mi
=
MatchParameters
(
methodinfo
,
types
)
;
return
Bind
(
inst
,
args
,
kw
,
mi
,
null
)
;
}
return
null
;
}
static
IntPtr
HandleParamsArray
(
IntPtr
args
,
int
arrayStart
,
int
pyArgCount
,
out
bool
isNewReference
)
{
isNewReference
=
false
;
IntPtr
op
;
// for a params method, we may have a sequence or single/multiple items
// here we look to see if the item at the paramIndex is there or not
// and then if it is a sequence itself.
if
(
(
pyArgCount
-
arrayStart
)
==
1
)
{
// we only have one argument left, so we need to check it
// to see if it is a sequence or a single item
IntPtr
item
=
Runtime
.
PyTuple_GetItem
(
args
,
arrayStart
)
;
if
(
!
Runtime
.
PyString_Check
(
item
)
&&
Runtime
.
PySequence_Check
(
item
)
)
{
// it's a sequence (and not a string), so we use it as the op
op
=
item
;
}
else
{
isNewReference
=
true
;
op
=
Runtime
.
PyTuple_GetSlice
(
args
,
arrayStart
,
pyArgCount
)
;
}
}
else
{
isNewReference
=
true
;
op
=
Runtime
.
PyTuple_GetSlice
(
args
,
arrayStart
,
pyArgCount
)
;
}
return
op
;
}
/// <summary>
/// Attempts to convert Python positional argument tuple and keyword argument table
/// into an array of managed objects, that can be passed to a method.
/// </summary>
/// <param name="pi">Information about expected parameters</param>
/// <param name="paramsArray"><c>true</c>, if the last parameter is a params array.</param>
/// <param name="args">A pointer to the Python argument tuple</param>
/// <param name="pyArgCount">Number of arguments, passed by Python</param>
/// <param name="kwargDict">Dictionary of keyword argument name to python object pointer</param>
/// <param name="defaultArgList">A list of default values for omitted parameters</param>
/// <param name="needsResolution"><c>true</c>, if overloading resolution is required</param>
/// <param name="outs">Returns number of output parameters</param>
/// <returns>An array of .NET arguments, that can be passed to a method.</returns>
static
object
[
]
TryConvertArguments
(
ParameterInfo
[
]
pi
,
bool
paramsArray
,
IntPtr
args
,
int
pyArgCount
,
Dictionary
<
string
,
IntPtr
>
kwargDict
,
ArrayList
defaultArgList
,
bool
needsResolution
,
out
int
outs
)
{
outs
=
0
;
var
margs
=
new
object
[
pi
.
Length
]
;
int
arrayStart
=
paramsArray
?
pi
.
Length
-
1
:
-
1
;
for
(
int
paramIndex
=
0
;
paramIndex
<
pi
.
Length
;
paramIndex
++
)
{
var
parameter
=
pi
[
paramIndex
]
;
bool
hasNamedParam
=
kwargDict
.
ContainsKey
(
parameter
.
Name
)
;
bool
isNewReference
=
false
;
if
(
paramIndex
>=
pyArgCount
&&
!
(
hasNamedParam
||
(
paramsArray
&&
paramIndex
==
arrayStart
)
)
)
{
if
(
defaultArgList
!=
null
)
{
margs
[
paramIndex
]
=
defaultArgList
[
paramIndex
-
pyArgCount
]
;
}
continue
;
}
IntPtr
op
;
if
(
hasNamedParam
)
{
op
=
kwargDict
[
parameter
.
Name
]
;
}
else
{
if
(
arrayStart
==
paramIndex
)
{
op
=
HandleParamsArray
(
args
,
arrayStart
,
pyArgCount
,
out
isNewReference
)
;
}
else
{
op
=
Runtime
.
PyTuple_GetItem
(
args
,
paramIndex
)
;
}
}
bool
isOut
;
if
(
!
TryConvertArgument
(
op
,
parameter
.
ParameterType
,
needsResolution
,
out
margs
[
paramIndex
]
,
out
isOut
)
)
{
return
null
;
}
if
(
isNewReference
)
{
// TODO: is this a bug? Should this happen even if the conversion fails?
// GetSlice() creates a new reference but GetItem()
// returns only a borrow reference.
Runtime
.
XDecref
(
op
)
;
}
if
(
isOut
)
{
outs
++
;
}
}
return
margs
;
}
/// <summary>
/// Try to convert a Python argument object to a managed CLR type.
/// </summary>
/// <param name="op">Pointer to the object at a particular parameter.</param>
/// <param name="parameterType">That parameter's managed type.</param>
/// <param name="needsResolution">There are multiple overloading methods that need resolution.</param>
/// <param name="arg">Converted argument.</param>
/// <param name="isOut">Whether the CLR type is passed by reference.</param>
/// <returns></returns>
static
bool
TryConvertArgument
(
IntPtr
op
,
Type
parameterType
,
bool
needsResolution
,
out
object
arg
,
out
bool
isOut
)
{
arg
=
null
;
isOut
=
false
;
var
clrtype
=
TryComputeClrArgumentType
(
parameterType
,
op
,
needsResolution
:
needsResolution
)
;
if
(
clrtype
==
null
)
{
return
false
;
}
if
(
!
Converter
.
ToManaged
(
op
,
clrtype
,
out
arg
,
false
)
)
{
Exceptions
.
Clear
(
)
;
return
false
;
}
isOut
=
clrtype
.
IsByRef
;
return
true
;
}
static
Type
TryComputeClrArgumentType
(
Type
parameterType
,
IntPtr
argument
,
bool
needsResolution
)
{
// this logic below handles cases when multiple overloading methods
// are ambiguous, hence comparison between Python and CLR types
// is necessary
Type
clrtype
=
null
;
IntPtr
pyoptype
;
if
(
needsResolution
)
{
// HACK: each overload should be weighted in some way instead
pyoptype
=
Runtime
.
PyObject_Type
(
argument
)
;
Exceptions
.
Clear
(
)
;
if
(
pyoptype
!=
IntPtr
.
Zero
)
{
clrtype
=
Converter
.
GetTypeByAlias
(
pyoptype
)
;
}
Runtime
.
XDecref
(
pyoptype
)
;
}
if
(
clrtype
!=
null
)
{
var
typematch
=
false
;
if
(
(
parameterType
!=
typeof
(
object
)
)
&&
(
parameterType
!=
clrtype
)
)
{
IntPtr
pytype
=
Converter
.
GetPythonTypeByAlias
(
parameterType
)
;
pyoptype
=
Runtime
.
PyObject_Type
(
argument
)
;
Exceptions
.
Clear
(
)
;
if
(
pyoptype
!=
IntPtr
.
Zero
)
{
if
(
pytype
!=
pyoptype
)
{
typematch
=
false
;
}
else
{
typematch
=
true
;
clrtype
=
parameterType
;
}
}
if
(
!
typematch
)
{
// this takes care of enum values
TypeCode
argtypecode
=
Type
.
GetTypeCode
(
parameterType
)
;
TypeCode
paramtypecode
=
Type
.
GetTypeCode
(
clrtype
)
;
if
(
argtypecode
==
paramtypecode
)
{
typematch
=
true
;
clrtype
=
parameterType
;
}
}
Runtime
.
XDecref
(
pyoptype
)
;
if
(
!
typematch
)
{
return
null
;
}
}
else
{
typematch
=
true
;
clrtype
=
parameterType
;
}
}
else
{
clrtype
=
parameterType
;
}
return
clrtype
;
}
/// <summary>
/// Check whether the number of Python and .NET arguments match, and compute additional arg information.
/// </summary>
/// <param name="positionalArgumentCount">Number of positional args passed from Python.</param>
/// <param name="parameters">Parameters of the specified .NET method.</param>
/// <param name="kwargDict">Keyword args passed from Python.</param>
/// <param name="paramsArray">True if the final param of the .NET method is an array (`params` keyword).</param>
/// <param name="defaultArgList">List of default values for arguments.</param>
/// <param name="kwargsMatched">Number of kwargs from Python that are also present in the .NET method.</param>
/// <param name="defaultsNeeded">Number of non-null defaultsArgs.</param>
/// <returns></returns>
static
bool
MatchesArgumentCount
(
int
positionalArgumentCount
,
ParameterInfo
[
]
parameters
,
Dictionary
<
string
,
IntPtr
>
kwargDict
,
out
bool
paramsArray
,
out
ArrayList
defaultArgList
,
out
int
kwargsMatched
,
out
int
defaultsNeeded
)
{
defaultArgList
=
null
;
var
match
=
false
;
paramsArray
=
parameters
.
Length
>
0
?
Attribute
.
IsDefined
(
parameters
[
parameters
.
Length
-
1
]
,
typeof
(
ParamArrayAttribute
)
)
:
false
;
kwargsMatched
=
0
;
defaultsNeeded
=
0
;
if
(
positionalArgumentCount
==
parameters
.
Length
&&
kwargDict
.
Count
==
0
)
{
match
=
true
;
}
else
if
(
positionalArgumentCount
<
parameters
.
Length
&&
(
!
paramsArray
||
positionalArgumentCount
==
parameters
.
Length
-
1
)
)
{
match
=
true
;
// every parameter past 'positionalArgumentCount' must have either
// a corresponding keyword arg or a default param, unless the method
// method accepts a params array (which cannot have a default value)
defaultArgList
=
new
ArrayList
(
)
;
for
(
var
v
=
positionalArgumentCount
;
v
<
parameters
.
Length
;
v
++
)
{
if
(
kwargDict
.
ContainsKey
(
parameters
[
v
]
.
Name
)
)
{
// we have a keyword argument for this parameter,
// no need to check for a default parameter, but put a null
// placeholder in defaultArgList
defaultArgList
.
Add
(
null
)
;
kwargsMatched
++
;
}
else
if
(
parameters
[
v
]
.
IsOptional
)
{
// IsOptional will be true if the parameter has a default value,
// or if the parameter has the [Optional] attribute specified.
// The GetDefaultValue() extension method will return the value
// to be passed in as the parameter value
defaultArgList
.
Add
(
parameters
[
v
]
.
GetDefaultValue
(
)
)
;
defaultsNeeded
++
;
}
else
if
(
!
paramsArray
)
{
match
=
false
;
}
}
}
else
if
(
positionalArgumentCount
>
parameters
.
Length
&&
parameters
.
Length
>
0
&&
Attribute
.
IsDefined
(
parameters
[
parameters
.
Length
-
1
]
,
typeof
(
ParamArrayAttribute
)
)
)
{
// This is a `foo(params object[] bar)` style method
match
=
true
;
paramsArray
=
true
;
}
return
match
;
}
internal
virtual
IntPtr
Invoke
(
IntPtr
inst
,
IntPtr
args
,
IntPtr
kw
)
{
return
Invoke
(
inst
,
args
,
kw
,
null
,
null
)
;
}
internal
virtual
IntPtr
Invoke
(
IntPtr
inst
,
IntPtr
args
,
IntPtr
kw
,
MethodBase
info
)
{
return
Invoke
(
inst
,
args
,
kw
,
info
,
null
)
;
}
protected
static
void
AppendArgumentTypes
(
StringBuilder
to
,
IntPtr
args
)
{
long
argCount
=
Runtime
.
PyTuple_Size
(
args
)
;
to
.
Append
(
"("
)
;
for
(
long
argIndex
=
0
;
argIndex
<
argCount
;
argIndex
++
)
{
var
arg
=
Runtime
.
PyTuple_GetItem
(
args
,
argIndex
)
;
if
(
arg
!=
IntPtr
.
Zero
)
{
var
type
=
Runtime
.
PyObject_Type
(
arg
)
;
if
(
type
!=
IntPtr
.
Zero
)
{
try
{
var
description
=
Runtime
.
PyObject_Unicode
(
type
)
;
if
(
description
!=
IntPtr
.
Zero
)
{
to
.
Append
(
Runtime
.
GetManagedString
(
description
)
)
;
Runtime
.
XDecref
(
description
)
;
}
}
finally
{
Runtime
.
XDecref
(
type
)
;
}
}
}
if
(
argIndex
+
1
<
argCount
)
to
.
Append
(
", "
)
;
}
to
.
Append
(
')'
)
;
}
internal
virtual
IntPtr
Invoke
(
IntPtr
inst
,
IntPtr
args
,
IntPtr
kw
,
MethodBase
info
,
MethodInfo
[
]
methodinfo
)
{
// No valid methods, nothing to bind.
if
(
GetMethods
(
)
.
Length
==
0
)
{
var
msg
=
new
StringBuilder
(
"The underlying C# method(s) have been deleted"
)
;
if
(
list
.
Count
>
0
&&
list
[
0
]
.
Name
!=
null
)
{
msg
.
Append
(
$
":
{
list
[
0
]
.
ToString
(
)
}
"
)
;
}
return
Exceptions
.
RaiseTypeError
(
msg
.
ToString
(
)
)
;
;
}
Binding
binding
=
Bind
(
inst
,
args
,
kw
,
info
,
methodinfo
)
;
object
result
;
IntPtr
ts
=
IntPtr
.
Zero
;
if
(
binding
==
null
)
{
var
value
=
new
StringBuilder
(
"No method matches given arguments"
)
;
if
(
methodinfo
!=
null
&&
methodinfo
.
Length
>
0
)
{
value
.
Append
(
$
" for
{
methodinfo
[
0
]
.
Name
}
"
)
;
}
value
.
Append
(
": "
)
;
AppendArgumentTypes
(
to
:
value
,
args
)
;
Exceptions
.
SetError
(
Exceptions
.
TypeError
,
value
.
ToString
(
)
)
;
return
IntPtr
.
Zero
;
}
if
(
allow_threads
)
{
ts
=
PythonEngine
.
BeginAllowThreads
(
)
;
}
try
{
result
=
binding
.
info
.
Invoke
(
binding
.
inst
,
BindingFlags
.
Default
,
null
,
binding
.
args
,
null
)
;
}
catch
(
Exception
e
)
{
if
(
e
.
InnerException
!=
null
)
{
e
=
e
.
InnerException
;
}
if
(
allow_threads
)
{
PythonEngine
.
EndAllowThreads
(
ts
)
;
}
Exceptions
.
SetError
(
e
)
;
return
IntPtr
.
Zero
;
}
if
(
allow_threads
)
{
PythonEngine
.
EndAllowThreads
(
ts
)
;
}
// If there are out parameters, we return a tuple containing
// the result followed by the out parameters. If there is only
// one out parameter and the return type of the method is void,
// we return the out parameter as the result to Python (for
// code compatibility with ironpython).
var
mi
=
(
MethodInfo
)
binding
.
info
;
if
(
binding
.
outs
==
1
&&
mi
.
ReturnType
==
typeof
(
void
)
)
{
}
if
(
binding
.
outs
>
0
)
{
ParameterInfo
[
]
pi
=
mi
.
GetParameters
(
)
;
int
c
=
pi
.
Length
;
var
n
=
0
;
IntPtr
t
=
Runtime
.
PyTuple_New
(
binding
.
outs
+
1
)
;
IntPtr
v
=
Converter
.
ToPython
(
result
,
mi
.
ReturnType
)
;
Runtime
.
PyTuple_SetItem
(
t
,
n
,
v
)
;
n
++
;
for
(
var
i
=
0
;
i
<
c
;
i
++
)
{
Type
pt
=
pi
[
i
]
.
ParameterType
;
if
(
pi
[
i
]
.
IsOut
||
pt
.
IsByRef
)
{
v
=
Converter
.
ToPython
(
binding
.
args
[
i
]
,
pt
.
GetElementType
(
)
)
;
Runtime
.
PyTuple_SetItem
(
t
,
n
,
v
)
;
n
++
;
}
}
if
(
binding
.
outs
==
1
&&
mi
.
ReturnType
==
typeof
(
void
)
)
{
v
=
Runtime
.
PyTuple_GetItem
(
t
,
1
)
;
Runtime
.
XIncref
(
v
)
;
Runtime
.
XDecref
(
t
)
;
return
v
;
}
return
t
;
}
return
Converter
.
ToPython
(
result
,
mi
.
ReturnType
)
;
}
}
/// <summary>
/// Utility class to sort method info by parameter type precedence.
/// </summary>
internal
class
MethodSorter
:
IComparer
<
MaybeMethodBase
>
{
int
IComparer
<
MaybeMethodBase
>
.
Compare
(
MaybeMethodBase
m1
,
MaybeMethodBase
m2
)
{
MethodBase
me1
=
m1
.
UnsafeValue
;
MethodBase
me2
=
m2
.
UnsafeValue
;
if
(
me1
==
null
&&
me2
==
null
)
{
return
0
;
}
else
if
(
me1
==
null
)
{
return
-
1
;
}
else
if
(
me2
==
null
)
{
return
1
;
}
if
(
me1
.
DeclaringType
!=
me2
.
DeclaringType
)
{
// m2's type derives from m1's type, favor m2
if
(
me1
.
DeclaringType
.
IsAssignableFrom
(
me2
.
DeclaringType
)
)
return
1
;
// m1's type derives from m2's type, favor m1
if
(
me2
.
DeclaringType
.
IsAssignableFrom
(
me1
.
DeclaringType
)
)
return
-
1
;
}
int
p1
=
MethodBinder
.
GetPrecedence
(
me1
)
;
int
p2
=
MethodBinder
.
GetPrecedence
(
me2
)
;
if
(
p1
<
p2
)
{
return
-
1
;
}
if
(
p1
>
p2
)
{
return
1
;
}
return
0
;
}
}
/// <summary>
/// A Binding is a utility instance that bundles together a MethodInfo
/// representing a method to call, a (possibly null) target instance for
/// the call, and the arguments for the call (all as managed values).
/// </summary>
internal
class
Binding
{
public
MethodBase
info
;
public
object
[
]
args
;
public
object
inst
;
public
int
outs
;
internal
Binding
(
MethodBase
info
,
object
inst
,
object
[
]
args
,
int
outs
)
{
this
.
info
=
info
;
this
.
inst
=
inst
;
this
.
args
=
args
;
this
.
outs
=
outs
;
}
}
static
internal
class
ParameterInfoExtensions
{
public
static
object
GetDefaultValue
(
this
ParameterInfo
parameterInfo
)
{
// parameterInfo.HasDefaultValue is preferable but doesn't exist in .NET 4.0
bool
hasDefaultValue
=
(
parameterInfo
.
Attributes
&
ParameterAttributes
.
HasDefault
)
==
ParameterAttributes
.
HasDefault
;
if
(
hasDefaultValue
)
{
return
parameterInfo
.
DefaultValue
;
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL