public inbox for [email protected]  
help / color / mirror / Atom feed
From: Karl O. Pinc <[email protected]>
To: Daniele Varrazzo <[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 09:42:03 -0600
Message-ID: <[email protected]> (raw)
In-Reply-To: <CA+mi_8Y0jzEZA+3kCnTtWCS8cvWTUFcD=0J+ihiamh=y8GvOxg@mail.gmail.com>
References: <[email protected]>
	<CA+mi_8Y0jzEZA+3kCnTtWCS8cvWTUFcD=0J+ihiamh=y8GvOxg@mail.gmail.com>

Hi Daniele,

On Wed, 14 Feb 2024 15:30:33 +0100
Daniele Varrazzo <[email protected]> wrote:

> 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)

Thanks very much for the help.  Working directly with the encoding
of the server side, translated to python, is indeed a more
direct approach.

I did not use conn.info.encoding because the docs say that it
contains the _client_ encoding, not the server-side encoding
used to store the db content.

From the link above:

```
encoding

The Python codec name of the connection’s client encoding.

The value returned is always normalized to the Python codec name:

conn.execute("SET client_encoding TO LATIN9")
conn.info.encoding
'iso8859-15'
```

Confirming the encodings, connecting to the  "latin1" db with psql shows:

```
$ psql -U kop latin1
psql (15.5 (Debian 15.5-0+deb12u1))
Type "help" for help.

kop_latin1=> show client_encoding;
 client_encoding 
-----------------
 UTF8
(1 row)

kop_latin1=> show server_encoding;
 server_encoding 
-----------------
 LATIN1
(1 row)
```

But, conn.info.encoding does return iso8859-1.

So I think your documentation has confused client
and server in this case.  If you can confirm this
for me I'll go ahead and use conn.info.encoding.

Thanks again.

Regards,

Karl <[email protected]>
Free Software:  "You don't pay back, you pay forward."
                 -- Robert A. Heinlein





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: <[email protected]>

* 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