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

Commita19b51b

Browse files
committed
cleaned up MethodBinder.Bind a bit
1 parent93a8c83 commita19b51b

File tree

1 file changed

+136
-135
lines changed

1 file changed

+136
-135
lines changed

‎src/runtime/methodbinder.cs

Lines changed: 136 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
282282
varpynargs=(int)Runtime.PyTuple_Size(args);
283283
objectarg;
284284
varisGeneric=false;
285-
ArrayListdefaultArgList=null;
286285
if(info!=null)
287286
{
288287
_methods=newMethodBase[1];
@@ -301,180 +300,151 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
301300
isGeneric=true;
302301
}
303302
ParameterInfo[]pi=mi.GetParameters();
304-
varclrnargs=pi.Length;
305-
varmatch=false;
306-
vararrayStart=-1;
307-
varouts=0;
303+
ArrayListdefaultArgList;
304+
intarrayStart;
308305

309-
if(pynargs==clrnargs)
310-
{
311-
match=true;
306+
if(!MatchArgumentCount(pynargs,pi,outarrayStart,outdefaultArgList)){
307+
continue;
312308
}
313-
elseif(pynargs<clrnargs)
309+
varouts=0;
310+
varmargs=newobject[pi.Length];
311+
312+
for(intparamIndex=0;paramIndex<pi.Length;paramIndex++)
314313
{
315-
match=true;
316-
defaultArgList=newArrayList();
317-
for(varv=pynargs;v<clrnargs;v++)
314+
IntPtrop;
315+
if(paramIndex>=pynargs)
318316
{
319-
if(pi[v].DefaultValue==DBNull.Value)
317+
if(defaultArgList!=null)
320318
{
321-
match=false;
322-
}
323-
else
324-
{
325-
defaultArgList.Add(pi[v].DefaultValue);
319+
margs[paramIndex]=defaultArgList[paramIndex-pynargs];
326320
}
321+
322+
continue;
327323
}
328-
}
329-
elseif(pynargs>clrnargs&&clrnargs>0&&
330-
Attribute.IsDefined(pi[clrnargs-1],typeof(ParamArrayAttribute)))
331-
{
332-
// This is a `foo(params object[] bar)` style method
333-
match=true;
334-
arrayStart=clrnargs-1;
335-
}
336324

337-
if(match)
338-
{
339-
varmargs=newobject[clrnargs];
325+
if(arrayStart==paramIndex)
326+
{
327+
// map remaining Python arguments to a tuple since
328+
// the managed function accepts it - hopefully :]
329+
op=Runtime.PyTuple_GetSlice(args,arrayStart,pynargs);
330+
}
331+
else
332+
{
333+
op=Runtime.PyTuple_GetItem(args,paramIndex);
334+
}
340335

341-
for(intn=0;n<clrnargs;n++)
336+
// this logic below handles cases when multiple overloading methods
337+
// are ambiguous, hence comparison between Python and CLR types
338+
// is necessary
339+
clrtype=null;
340+
IntPtrpyoptype;
341+
if(_methods.Length>1)
342342
{
343-
IntPtrop;
344-
if(n<pynargs)
343+
pyoptype=IntPtr.Zero;
344+
pyoptype=Runtime.PyObject_Type(op);
345+
Exceptions.Clear();
346+
if(pyoptype!=IntPtr.Zero)
345347
{
346-
if(arrayStart==n)
347-
{
348-
// map remaining Python arguments to a tuple since
349-
// the managed function accepts it - hopefully :]
350-
op=Runtime.PyTuple_GetSlice(args,arrayStart,pynargs);
351-
}
352-
else
353-
{
354-
op=Runtime.PyTuple_GetItem(args,n);
355-
}
356-
357-
// this logic below handles cases when multiple overloading methods
358-
// are ambiguous, hence comparison between Python and CLR types
359-
// is necessary
360-
clrtype=null;
361-
IntPtrpyoptype;
362-
if(_methods.Length>1)
363-
{
364-
pyoptype=IntPtr.Zero;
365-
pyoptype=Runtime.PyObject_Type(op);
366-
Exceptions.Clear();
367-
if(pyoptype!=IntPtr.Zero)
368-
{
369-
clrtype=Converter.GetTypeByAlias(pyoptype);
370-
}
371-
Runtime.XDecref(pyoptype);
372-
}
348+
clrtype=Converter.GetTypeByAlias(pyoptype);
349+
}
350+
Runtime.XDecref(pyoptype);
351+
}
373352

374353

375-
if(clrtype!=null)
354+
if(clrtype!=null)
355+
{
356+
vartypematch=false;
357+
if((pi[paramIndex].ParameterType!=typeof(object))&&(pi[paramIndex].ParameterType!=clrtype))
358+
{
359+
IntPtrpytype=Converter.GetPythonTypeByAlias(pi[paramIndex].ParameterType);
360+
pyoptype=Runtime.PyObject_Type(op);
361+
Exceptions.Clear();
362+
if(pyoptype!=IntPtr.Zero)
376363
{
377-
vartypematch=false;
378-
if((pi[n].ParameterType!=typeof(object))&&(pi[n].ParameterType!=clrtype))
364+
if(pytype!=pyoptype)
379365
{
380-
IntPtrpytype=Converter.GetPythonTypeByAlias(pi[n].ParameterType);
381-
pyoptype=Runtime.PyObject_Type(op);
382-
Exceptions.Clear();
383-
if(pyoptype!=IntPtr.Zero)
384-
{
385-
if(pytype!=pyoptype)
386-
{
387-
typematch=false;
388-
}
389-
else
390-
{
391-
typematch=true;
392-
clrtype=pi[n].ParameterType;
393-
}
394-
}
395-
if(!typematch)
396-
{
397-
// this takes care of enum values
398-
TypeCodeargtypecode=Type.GetTypeCode(pi[n].ParameterType);
399-
TypeCodeparamtypecode=Type.GetTypeCode(clrtype);
400-
if(argtypecode==paramtypecode)
401-
{
402-
typematch=true;
403-
clrtype=pi[n].ParameterType;
404-
}
405-
}
406-
Runtime.XDecref(pyoptype);
407-
if(!typematch)
408-
{
409-
margs=null;
410-
break;
411-
}
366+
typematch=false;
412367
}
413368
else
414369
{
415370
typematch=true;
416-
clrtype=pi[n].ParameterType;
371+
clrtype=pi[paramIndex].ParameterType;
417372
}
418373
}
419-
else
374+
if(!typematch)
420375
{
421-
clrtype=pi[n].ParameterType;
422-
}
423-
424-
if(pi[n].IsOut||clrtype.IsByRef)
425-
{
426-
outs++;
376+
// this takes care of enum values
377+
TypeCodeargtypecode=Type.GetTypeCode(pi[paramIndex].ParameterType);
378+
TypeCodeparamtypecode=Type.GetTypeCode(clrtype);
379+
if(argtypecode==paramtypecode)
380+
{
381+
typematch=true;
382+
clrtype=pi[paramIndex].ParameterType;
383+
}
427384
}
428-
429-
if(!Converter.ToManaged(op,clrtype,outarg,false))
385+
Runtime.XDecref(pyoptype);
386+
if(!typematch)
430387
{
431-
Exceptions.Clear();
432388
margs=null;
433389
break;
434390
}
435-
if(arrayStart==n)
436-
{
437-
// GetSlice() creates a new reference but GetItem()
438-
// returns only a borrow reference.
439-
Runtime.XDecref(op);
440-
}
441-
margs[n]=arg;
442391
}
443392
else
444393
{
445-
if(defaultArgList!=null)
446-
{
447-
margs[n]=defaultArgList[n-pynargs];
448-
}
394+
typematch=true;
395+
clrtype=pi[paramIndex].ParameterType;
449396
}
450397
}
398+
else
399+
{
400+
clrtype=pi[paramIndex].ParameterType;
401+
}
451402

452-
if(margs==null)
403+
if(pi[paramIndex].IsOut||clrtype.IsByRef)
453404
{
454-
continue;
405+
outs++;
455406
}
456407

457-
objecttarget=null;
458-
if(!mi.IsStatic&&inst!=IntPtr.Zero)
408+
if(!Converter.ToManaged(op,clrtype,outarg,false))
459409
{
460-
//CLRObject co = (CLRObject)ManagedType.GetManagedObject(inst);
461-
// InvalidCastException: Unable to cast object of type
462-
// 'Python.Runtime.ClassObject' to type 'Python.Runtime.CLRObject'
463-
varco=ManagedType.GetManagedObject(inst)asCLRObject;
464-
465-
// Sanity check: this ensures a graceful exit if someone does
466-
// something intentionally wrong like call a non-static method
467-
// on the class rather than on an instance of the class.
468-
// XXX maybe better to do this before all the other rigmarole.
469-
if(co==null)
470-
{
471-
returnnull;
472-
}
473-
target=co.inst;
410+
Exceptions.Clear();
411+
margs=null;
412+
break;
474413
}
414+
if(arrayStart==paramIndex)
415+
{
416+
// GetSlice() creates a new reference but GetItem()
417+
// returns only a borrow reference.
418+
Runtime.XDecref(op);
419+
}
420+
margs[paramIndex]=arg;
421+
}
475422

476-
returnnewBinding(mi,target,margs,outs);
423+
if(margs==null)
424+
{
425+
continue;
477426
}
427+
428+
objecttarget=null;
429+
if(!mi.IsStatic&&inst!=IntPtr.Zero)
430+
{
431+
//CLRObject co = (CLRObject)ManagedType.GetManagedObject(inst);
432+
// InvalidCastException: Unable to cast object of type
433+
// 'Python.Runtime.ClassObject' to type 'Python.Runtime.CLRObject'
434+
varco=ManagedType.GetManagedObject(inst)asCLRObject;
435+
436+
// Sanity check: this ensures a graceful exit if someone does
437+
// something intentionally wrong like call a non-static method
438+
// on the class rather than on an instance of the class.
439+
// XXX maybe better to do this before all the other rigmarole.
440+
if(co==null)
441+
{
442+
returnnull;
443+
}
444+
target=co.inst;
445+
}
446+
447+
returnnewBinding(mi,target,margs,outs);
478448
}
479449
// We weren't able to find a matching method but at least one
480450
// is a generic method and info is null. That happens when a generic
@@ -489,6 +459,37 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info, Meth
489459
returnnull;
490460
}
491461

462+
staticboolMatchArgumentCount(intpynargs,ParameterInfo[]pi,outintparamsArrayStart,outArrayListdefaultArgList)
463+
{
464+
defaultArgList=null;
465+
varmatch=false;
466+
paramsArrayStart=-1;
467+
468+
if(pynargs==pi.Length)
469+
{
470+
match=true;
471+
}elseif(pynargs<pi.Length)
472+
{
473+
match=true;
474+
defaultArgList=newArrayList();
475+
for(varv=pynargs;v<pi.Length;v++){
476+
if(pi[v].DefaultValue==DBNull.Value){
477+
match=false;
478+
}else{
479+
defaultArgList.Add(pi[v].DefaultValue);
480+
}
481+
}
482+
}elseif(pynargs>pi.Length&&pi.Length>0&&
483+
Attribute.IsDefined(pi[pi.Length-1],typeof(ParamArrayAttribute)))
484+
{
485+
// This is a `foo(params object[] bar)` style method
486+
match=true;
487+
paramsArrayStart=pi.Length-1;
488+
}
489+
490+
returnmatch;
491+
}
492+
492493
internalvirtualIntPtrInvoke(IntPtrinst,IntPtrargs,IntPtrkw)
493494
{
494495
returnInvoke(inst,args,kw,null,null);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp