Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51723

Re: Problem with psycopg2, bytea, and memoryview

Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'api.': 0.05; 'binary': 0.07; 'see.': 0.07; 'bytes.': 0.09; 'chunk': 0.09; 'means,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'works.': 0.09; 'wrote': 0.14; '"b"': 0.16; "'b'": 0.16; "'c'": 0.16; '*why*': 0.16; 'antoine': 0.16; 'chunks': 0.16; 'equal.': 0.16; 'luckily': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'record,': 0.16; 'subject:Problem': 0.16; 'suggested,': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'shape': 0.19; 'written': 0.21; 'help.': 0.21; 'compare': 0.26; 'extension': 0.26; 'header:X -Complaints-To:1': 0.27; 'tried': 0.27; 'returned': 0.30; 'getting': 0.31; "skip:' 10": 0.31; '>>>>': 0.31; 'explained': 0.31; 'forces': 0.31; 'anyone': 0.31; 'another': 0.32; 'guess': 0.33; 'raw': 0.33; 'problem': 0.35; 'subject:with': 0.35; 'equal': 0.35; 'but': 0.35; 'returning': 0.36; 'thanks': 0.36; 'to:addr :python-list': 0.38; 'list,': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; "you're": 0.61; 'different': 0.65; 'frank': 0.68; 'results': 0.69; 'skip:n 40': 0.81
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From "Frank Millman" <frank@chagford.com>
Subject Re: Problem with psycopg2, bytea, and memoryview
Date Thu, 1 Aug 2013 10:03:03 +0200
References <ktam8i$hj3$1@ger.gmane.org><loom.20130731T114936-455@post.gmane.org><mailman.10.1375270989.1251.python-list@python.org> <b5sk3cFkiq8U1@mid.individual.net>
X-Gmane-NNTP-Posting-Host 197.87.50.121
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.3790.4657
X-RFC2646 Format=Flowed; Original
X-MimeOLE Produced By Microsoft MimeOLE V6.00.3790.4913
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.70.1375344202.1251.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1375344202 news.xs4all.nl 15891 [2001:888:2000:d::a6]:58833
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!news.franciliens.net!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:51723

Show key headers only | View raw


"Neil Cerutti" <neilc@norwich.edu> wrote in message 
news:b5sk3cFkiq8U1@mid.individual.net...
> On 2013-07-31, Frank Millman <frank@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


Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Problem with psycopg2, bytea, and memoryview "Frank Millman" <frank@chagford.com> - 2013-07-31 13:43 +0200
  Re: Problem with psycopg2, bytea, and memoryview Neil Cerutti <neilc@norwich.edu> - 2013-07-31 14:08 +0000
    Re: Problem with psycopg2, bytea, and memoryview "Frank Millman" <frank@chagford.com> - 2013-08-01 10:03 +0200

csiph-web