Обсуждение: psycopg2.Error.pgerror encoding ?

Поиск
Список
Период
Сортировка

psycopg2.Error.pgerror encoding ?

От
Karsten Hilbert
Дата:
I have a simple (?) question regarding psycopg2.Error

    http://initd.org/psycopg/docs/module.html#exceptions

Which encoding is the string attribute .pgerror
going to be in ?

Karsten
--
GPG key ID E4071346 @ gpg-keyserver.de
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346


Re: psycopg2.Error.pgerror encoding ?

От
Daniele Varrazzo
Дата:
On Wed, Nov 13, 2013 at 1:52 AM, Karsten Hilbert
<Karsten.Hilbert@gmx.net> wrote:
> I have a simple (?) question regarding psycopg2.Error
>
>         http://initd.org/psycopg/docs/module.html#exceptions
>
> Which encoding is the string attribute .pgerror
> going to be in ?

In Python 2 it will be in the connection encoding; specifically we
receive the 8-bit message from the backend and we just create a Python
string out of that data, without re-checking the data is valid in that
encoding (we trust the database).

In Python 3 it is unicode decoded with the connection encoding (with
"replace" error handling, because we trust the database, yet we don't
want to die if things have gone really awry).

-- Daniele


Re: psycopg2.Error.pgerror encoding ?

От
Karsten Hilbert
Дата:
On Wed, Nov 13, 2013 at 10:26:30AM +0000, Daniele Varrazzo wrote:

> > I have a simple (?) question regarding psycopg2.Error
> >
> >         http://initd.org/psycopg/docs/module.html#exceptions
> >
> > Which encoding is the string attribute .pgerror
> > going to be in ?
>
> In Python 2 it will be in the connection encoding; specifically we
> receive the 8-bit message from the backend and we just create a Python
> string out of that data, without re-checking the data is valid in that
> encoding (we trust the database).

In other words:

    unicode(exception.pgerror, exception.cursor.connection.encoding, 'replace')

"should" do the "right" thing ?

Karsten
--
GPG key ID E4071346 @ gpg-keyserver.de
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346


Re: psycopg2.Error.pgerror encoding ?

От
Daniele Varrazzo
Дата:
On Wed, Nov 13, 2013 at 12:47 PM, Karsten Hilbert
<Karsten.Hilbert@gmx.net> wrote:
> On Wed, Nov 13, 2013 at 10:26:30AM +0000, Daniele Varrazzo wrote:
>
>> > I have a simple (?) question regarding psycopg2.Error
>> >
>> >         http://initd.org/psycopg/docs/module.html#exceptions
>> >
>> > Which encoding is the string attribute .pgerror
>> > going to be in ?
>>
>> In Python 2 it will be in the connection encoding; specifically we
>> receive the 8-bit message from the backend and we just create a Python
>> string out of that data, without re-checking the data is valid in that
>> encoding (we trust the database).
>
> In other words:
>
>         unicode(exception.pgerror, exception.cursor.connection.encoding, 'replace')
>
> "should" do the "right" thing ?

Yes, it should be the right way to decode error messages,
notifications and every other string received from the database. You
should not assume exception.cursor is always present though: it's not
there if the error wasn't raised by a cursor (but e.g. by cnn.commit()
or connect()).

-- Daniele