[Pyobjc-dev] PyBuffer* vs. array.array()
Brought to you by:
ronaldoussoren
From: Bill B. <bb...@co...> - 2003-01-05 15:27:44
|
I'm in the process of bridging various methods in PyObjC that take (void *) types as arguments or return (void *) buffer references. On the C side of the fence, I'm using PyBuffer_FromReadWriteMemory() or PyBuffer_FromMemory() as appropriate to create the buffer. On the Python side, I use array.array('B') to create the buffer that is passed across the bridge and mapped to the (void *) method arguments. However, I noticed that the two types are not the same. They both work as byte buffers just fine, but the result of PyBuffer*() acts as, basically, a big <str> on the Python side whereas array.array('B') is a buffer of specifically typed unsigned chars. In writing the unit tests, I came across a problematic situation that could easily arise in code (feel free to comment on the silliness of this code, if any... and note that I'm using the comprehension style even after that long rant I posted earlier :-): singlePlane = array.array('B') singlePlane.fromlist([0 for x in range(0, width*height*3)] ) for i in range(0, 256*256): si = i * 3 singlePlane[si] = rPlane[i] singlePlane[si+1] = gPlane[i] singlePlane[si+2] = bPlane[i] i2 = ... create i2 using data in singlePlane ... .... then ... bitmapData = i2.bitmapData() self.assertEquals(len(bitmapData), len(singlePlane)) for i in range(0,100): self.assertEquals(bitmapData[i], singlePlane[i], "bitmapData and singlePlane differ at byte %d" % i) The contents of singlePlane and bitmapData are identical, but the unit test fails: <type 'str'> -- type of element 0 of bitmapData <type 'int'> -- type of element 0 of singlePlane F. ====================================================================== FAIL: testImageData (__main__.TestNSBitmapImageRep) ---------------------------------------------------------------------- Traceback (most recent call last): File "Lib/AppKit/test/test_nsbitmapimagerep.py", line 74, in testImageData self.assertEquals(bitmapData[i], singlePlane[i], "bitmapData and singlePlane differ at byte %d" % i) File "/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/ unittest.py", line 292, in failUnlessEqual raise self.failureException, \ AssertionError: bitmapData and singlePlane differ at byte 0 ---------------------------------------------------------------------- Ran 2 tests in 1.564s FAILED (failures=1) -- The real problem is that something created via array.array() comes back as an object that behaves differently. Clearly, a bug in my bridge code, yes, but raises a question: What should I be using? From the python side, it appears to only be possible to create buffer style objects-- big byte bags-- via the array module [yes, a <str> will work, but a byte-array seems so much more correct]. On the C side, it seems that the only way to create a byte buffer like object is to use the PyBuffer* API. Advice, please? BTW: All of this lays the foundation for creating a PILImage object in Cocoa that is a seamless part of the NSImage family of classes. I.e. PIL would then be fully integrated into any Cocoa app that can dynamically load the python interpreter (another problem I'm working through). Thanks. b.bum b.bum We gladly feast on those who would subdue us. |