Problem with psycopg2, bytea, and memoryview

Frank Millman frank at chagford.com
Thu Aug 1 04:03:03 EDT 2013


"Neil Cerutti" <neilc at norwich.edu> wrote in message 
news:b5sk3cFkiq8U1 at mid.individual.net...
> On 2013-07-31, Frank Millman <frank at chagford.com> wrote:
>>
>>
>> Can anyone explain *why* the results do not compare equal? If I
>> understood the problem, I might be able to find a workaround.
>
> A memoryview will compare equal to another object that supports
> the buffer protocol when the format and shape are also equal. The
> database must be returning chunks of binary data in a different
> shape or format than you are writing it.
>
> Perhaps psycopg2 is returning a chunk of ints when you have
> written a chunk of bytes. Check the .format and .shape members of
> the return value to see.
>
>>>> x = memoryview(b"12345")
>>>> x.format
> 'B'
>>>> x.shape
> (5,)
>>>> x == b"12345"
> True
>
> My guess is you're getting format "I" from psycopg2. Hopefully
> there's a way to coerce your desired "B" format interpretation of
> the raw data using psycopg2's API.
>

Thanks very much for the explanation, Neil.

I tried what you suggested, and the object returned by psycopg2 has a format 
of 'c' and a shape of (5,).

I don't know what it means, but luckily I have found a workaround. I 
enquired on the psycopg2 list, and someone explained how I can create an 
extension that forces it to return 'bytes' instead of a 'memoryview'. I 
tested it and it works. Problem solved :-)

For the record, I passed on the suggestion from Antoine and Terry that they 
change their program to return 'bytes'. It will be interesting to see if 
anyone responds.

Thanks again to all for your help.

Frank






More information about the Python-list mailing list