Thread: Problem with memoryview
Hi all The following refers to Python 3.3. I know that if you create a column of type 'bytea', psycopg2 accepts a variety of inputs, but always returns a 'memoryview'. I would like to know if it is possible, through some customisation option, to tell it to return 'bytes'. My problem is that, after a roundtrip to the database and back, the object no longer compares equal to the original. >>> memoryview(b'abcdef') == b'abcdef' True >>> cur.execute('create table fmtemp (code int, xml bytea)') >>> cur.execute('insert into fmtemp values (%s, %s)', (1, b'abcdef')) >>> cur.execute('select * from fmtemp where code =1') >>> row = cur.fetchone() >>> row (1, <memory at 0xb725f77c>) >>> row[1] == b'abcdef' False >>> row[1].tobytes() == b'abcdef' True >>> TIA Frank Millman
On Wed, Jul 31, 2013 at 3:08 PM, Frank Millman <frank@chagford.com> wrote: > > The following refers to Python 3.3. > > I know that if you create a column of type 'bytea', psycopg2 accepts a > variety of inputs, but always returns a 'memoryview'. > > I would like to know if it is possible, through some customisation option, > to tell it to return 'bytes'. Yes: you can easily create a typecaster converting bytea data to bytes: def bytea2bytes(value, cur): m = psycopg2.BINARY(value, cur) if m is not None: return m.tobytes() BYTEA2BYTES = psycopg2.extensions.new_type( psycopg2.BINARY.values, 'BYTEA2BYTES', bytea2bytes) psycopg2.extensions.register_type(BYTEA2BYTES) cur.execute("select 'abcdef'::bytea") cur.fetchone()[0] # returns b'abcdef' This would change the behaviour of bytea globally; you can limit the scope of the change to a single connection or cursor passing this object as parameter to register_type(): check the function docs for details. Hope it helps, -- Daniele
----- Original Message ----- From: "Daniele Varrazzo" <daniele.varrazzo@gmail.com> To: "Frank Millman" <frank@chagford.com> Cc: <psycopg@postgresql.org> Sent: Wednesday, July 31, 2013 4:56 PM Subject: Re: [psycopg] Problem with memoryview > On Wed, Jul 31, 2013 at 3:08 PM, Frank Millman <frank@chagford.com> wrote: >> >> The following refers to Python 3.3. >> >> I know that if you create a column of type 'bytea', psycopg2 accepts a >> variety of inputs, but always returns a 'memoryview'. >> >> I would like to know if it is possible, through some customisation >> option, >> to tell it to return 'bytes'. > > Yes: you can easily create a typecaster converting bytea data to bytes: > [..] Thanks very much, Daniele. I have tested it, and it works like a charm :-) Frank