FirebirdSQL / python3-driver

Firebird driver for Python that uses new Firebird API
https://www.firebirdsql.org/en/devel-python-driver/
MIT License
26 stars 10 forks source link

encoding_errors not working for database exceptions #25

Closed paulo242 closed 11 months ago

paulo242 commented 11 months ago

We tested a database that uses WIN1252 and has many exceptions with special characters. So we tried:

driver_config.server_defaults.encoding_errors.value = 'cp1252' and driver_config.server_defaults.encoding_errors.value = 'WIN1252'

before the connection and a command that triggers an exception. The result wasn't encoded with WIN1252 and we couldn't convert it. teste_firebird_driver.zip

pcisar commented 11 months ago

Value for encoding_errors parameter is NOT character set name! It's the error handler name, see https://docs.python.org/3/library/codecs.html#error-handlers

paulo242 commented 11 months ago

Sorry about the misuse! Is there a way to know which encoding is comming since my chatset is WIN1252 or there's a way to proper decode it to UTF-8? We tries many encode+decode but no success. The property name suggests an encoding for database errors, so it caused our misuse.

pcisar commented 11 months ago

First, this is Python 3, so strings are unicode. The driver converts bytestrings from the server to unicode using connection charset. It works without problems with two exceptions:

  1. The data are in fact not in connection charset.
  2. The error message may contain strings from OS (or other source) that is not in connection charset.

In such cases string conversion to unicode may fail. The standard error codecs handler is used by default, which is 'strict' which means that conversion exception is thrown. You can chose different error handler like 'ignore', 'replace' or 'surrogateescape' that do not throw conversion exception.

And no, the property name DOES NOT suggest and encoding name, and especially the documentation for the config option clearly says: Handler used for encoding errors. See codecs error handlers for details.