Re: [Pyobjc-dev] conversion of tuples to structs
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2003-09-12 10:27:12
|
On 12 sep 2003, at 9:33, Just van Rossum wrote: > Tobias Sargeant wrote: > >> Looking at the code in objc_support.m, it's pretty obvious that >> structs (and arrays) can currently only be built from tuples. Is >> there any reason why anything that acts as a sequence won't do? > > Not really. In the Carbon(-ish) wrappers that come with MacPython we've > usually made sure this is possible. Then there's really no reason to not accept any sequences. The only reason I could come up with for not allowing lists when converting to a struct is that tuples are often used as struct-like objects in Python. > For example the Carbon.CG module > accepts any length-6 sequence where it expects an affine transform. > Very > useful (especially since I have a Transform class that also behaves as > a > length-6 sequence ;-). The same goes voor points and rects. > >> I appreciate that the symmetry of the pythonifying/depythonifying is >> neat, but it would be extremely useful (for example) to be able to >> pass Numeric arrays representing points to objc functions without the >> overhead (both in terms of execution speed and in terms of typing) of >> converting them to tuples. >> >> If there's no reason (technical or otherwise) why this shouldn't be >> the case, I'm happy to make the change and submit a patch. > > I've been thinking the same thing every now and then. I don't know how > easy it would be to fix. If those structs were unpacked with > PyArg_ParseTuple (and I think they aren't), the fix would be easy: just > use PyArg_Parse instead and add parenthesis to the format strings, eg. > "ff" would have to become "(ff)". But I have no idea where the struct > conversions live in the PyObjC code base... Ronald? The code lives in objc_support.m and doesn't use PyArg_Parse*, we directly deconstruct tuples based on the Objective-C type signature. The easiest way to add support for arbitrary sequences would be to use the PySequence_Fast interface. BTW. I'm sitting on a patch for objc_support.m that also touches this code, I'll add support for arbitrary sequences to that patch. I'd like to get the 1.0 release out before adding this to the source tree. The only item on my TODO-list for 1.0 is a bug that causes a crash of the interpreter when working with WebKit on Jaguar (the same code works fine on Panther, and when rewritten in ObjC). Ronald |