Re: [Pyobjc-dev] Calling function with variable number of arguments?
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2004-04-27 20:14:29
|
On Apr 27, 2004, at 4:04 PM, Pierce T.Wetter III wrote: > > On Apr 27, 2004, at 12:52 PM, Ronald Oussoren wrote: > >> >> On 27-apr-04, at 21:19, Pierce T.Wetter III wrote: >> >>> >>> So this line is giving me an error: >>> >>> qual= ObjectModel.EOQualifier.qualifierWithQualifierFormat_('fund = >>> %@ and date >= %@',fund,now60) >>> >>> TypeError: Need 1 arguments, got 3 >>> >>> (1.1 b2 downloaded about 15 minutes ago) >>> >>> Presumably because qualifierWithQualifierFormat takes a variable >>> number of arguments. >>> >>> Is there some sort of voodoo I can do to get this to work? >> >> The easiest solution is to do the formatting on the python side: >> >> qual= ObjectModel.EOQualifier.qualifierWithQualifierFormat_('fund = >> %s and date >= %s'%(fund,now60)) >> >> It is possible to write C code that would allow you to pass in a >> variable number of arguments, but you would have to implement most of >> the format-string parsing in the wrapper. > > Yeah, that doesn't work, because it just looks like a format string, > its actually parsed. For instance, an AND in the string produces an > EOAndQualifier etc. > > What I did instead was add this in a category to EOQualifier: > > + (EOQualifier *) qualifierWithQualifierFormat: (NSString *) string > arg1: (id) arg1 arg2:(id) arg2 > { > > return [self qualifierWithQualifierFormat: string,arg1,arg2]; > } > > Though that's a hack obviously. > > I wonder if it would be possible to do something like this: > > objc.makeVarArgsCall(ObjectModel.EOQualfier,"qualifierWithQualifierForm > at_",('fund = %s and date >= %s',fund,now60)) > > That is, add a method to the objc module that takes an object, a > selector and a list of arguments and makes the call? The difficulty is that many varargs functions, like +[NSString stringWithFormat], NSLog, printf, etc. take arbitrary types.. %s would be a c string, %@ would be an objc object, %d would be an integer. Python has to know about that in order to do the type conversion. It's probably possible to tokenize the string from python and build a one-off selector+signature for that call. In this case your function seems like it only takes ids so it's probably possible to do this kind of bridge without tokenizing the string at all. -bob |