Problem with psycopg2, bytea, and memoryview

Frank Millman frank at chagford.com
Fri Aug 2 02:23:01 EDT 2013


"Terry Reedy" <tjreedy at udel.edu> wrote in message 
news:ktbj9i$4au$1 at ger.gmane.org...
> On 7/31/2013 9:07 AM, Antoine Pitrou wrote:
>>
>> I would suggest asking the psycopg2 project why they made this choice, 
>> and
>> if they would reconsider. Returning a memoryview doesn't make much sense 
>> IMHO.
>
> I agree.
> "memoryview objects allow Python code to access the internal data of an 
> object that supports the buffer protocol without copying."
> Example: the binary image data of an image object.
> They are not intended to be a standalone objects when there is an obvious 
> alternative (in this case, bytes).

For the record, I forwarded this to the psycopg2 list, and got the following 
reply from Daniele Varrazzo -

<reply>
Hi Frank, thank you for forwarding the thread.

Until a not very long time ago, this was exactly the case: upon
reading bytea we were handed over some data to pass to a libpq
function to decode. The resulting decoded string was to be released by
PQfreemem, and the memoryview was the right object to reconcile the
lifetime of the python object with this deallocation requirement.
Later things have changed: because of the change in bytea format in
PostgreSQL 9.0 (the new format was the default and wasn't handled by
libpq < 9.0, which created widespread problems) we wrote our own
parser. As a consequence the memoryview is not really needed anymore,
but we preferred to avoid breaking programs already using the current
interface.

So yes: bytes (and str on Py2) is definitely a better object to get in
Python. It is already on the list of what I want in a version that
would break compatibility in order to gain feature or improve the
adapter in other ways (e.g. psycopg3).
</reply>

Frank






More information about the Python-list mailing list