From: Jeff A. <ja...@fa...> - 2012-07-07 18:43:52
|
In the implementation of bytearray for Jython, I used the C source extensively to understand the intended behaviour. I observed that CPython makes heavy use of the Buffer API in bytearrayobject.c. In fact, the answer to "what type is this argument" is frequently "anything supporting the buffer API". Earlier this year, Jim Baker contributed an outline "MemoryView" to the Jython code base intended to fill this role, but not enough to use directly, or for me at the time, even to see how it should develop. PyByteArray therefore uses a "View" wrapper, where needed around arguments of well-known type, that provides privately to bytearray many of the benefits of the buffer API. But Jython's bytearray will never really be finished while a new type can come along that (in CPython) presents the buffer API. And now, it turns out, code in other modules needs to access bytearray through the expected buffer API. So I bit the bullet, and propose to implement a Java-flavour of the buffer API that I will put on PyByteArray and PyString. It will certainly work for one-dimensional byte arrays, and is faithful enough to CPython design (taking the hint from Jim's work) that I think it would work for NumPy and PIL-style arrays but I don't intend to implement a support class for those. My homework is here on the wiki: http://wiki.python.org/jython/BufferProtocol I'm going to claim contributor's rights on questions of taste and style, but does anyone out there, especially anyone who has followed development of the CPython buffer API, or put it to sophisticated use, see a massive pitfall? Jeff Allen |