public inbox for [email protected]
help / color / mirror / Atom feedFrom: Daniele Varrazzo <[email protected]>
To: Karl O. Pinc <[email protected]>
Cc: [email protected] <[email protected]>
Subject: Re: Reporting UnicodeEncodeError info on arbitrary data sent to PG with psycopg3
Date: Wed, 14 Feb 2024 15:30:33 +0100
Message-ID: <CA+mi_8Y0jzEZA+3kCnTtWCS8cvWTUFcD=0J+ihiamh=y8GvOxg@mail.gmail.com> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
Hello Karl,
On Wed, 14 Feb 2024 at 02:37, Karl O. Pinc <[email protected]> wrote:
> This does not work. What is wrong with what I'm doing
> and how do I do what I want? (And how am I supposed to
> know why this does not work and what works?) I call the
> dumper because I want to rely on psycopg3's mechanisms
> and not have to query the server for its encoding
> and figure out the PG->Python encoding mappings myself.
Keep in mind that you are playing with objects that are somewhat
internal, so it wouldn't be impossible that these interfaces will
change in the future. It's not planned at the moment and it wouldn't
happen in a minor version anyway.
However, the main problem I see there is that
`conn.adapters.get_dumper()` returns a class. If you want a dumper you
must instantiate it. The following works as you expect:
>>> conn.execute("set client_encoding to 'latin1'")
>>> dumper = conn.adapters.get_dumper(str,
psycopg.adapt.PyFormat.TEXT)(str, conn)
>>> dumper.dump('€')
...
UnicodeEncodeError: 'latin-1' codec can't encode character
'\u20ac' in position 0: ordinal not in range(256)
Note however that if you just want to know the Python codec you can
find it in `conn.info.encoding`
(https://www.psycopg.org/psycopg3/docs/api/objects.html#psycopg.ConnectionInfo.encoding):
>>> conn.info.encoding
'iso8859-1'
>>> "€".encode(conn.info.encoding)
...
UnicodeEncodeError: 'latin-1' codec can't encode character
'\u20ac' in position 0: ordinal not in range(256)
Hope this helps
-- Daniele
view thread (6+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected]
Subject: Re: Reporting UnicodeEncodeError info on arbitrary data sent to PG with psycopg3
In-Reply-To: <CA+mi_8Y0jzEZA+3kCnTtWCS8cvWTUFcD=0J+ihiamh=y8GvOxg@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox