chtd / psycopg2cffi

Port to cffi with some speed improvements
Other
177 stars 43 forks source link

Exception handler fails when error contains utf8 bytes #56

Closed mueslo closed 8 years ago

mueslo commented 8 years ago

https://github.com/chtd/psycopg2cffi/blob/ec8a83a07507bd18eb803955d30ab3b3d7ad4a79/psycopg2cffi/_impl/connection.py#L828

  File "/usr/local/lib/pypy2.7/dist-packages/psycopg2cffi/_impl/connection.py", line 828, in _create_exception
   pgmsg = bytes_to_ascii(ffi.string(pgmsg)) if pgmsg else None
 File "/usr/local/lib/pypy2.7/dist-packages/psycopg2cffi/_impl/adapters.py", line 311, in bytes_to_ascii
   return b.decode('ascii')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 164: ordinal not in range(128)

Postgres can return utf8 bytes in exceptions, e.g. when a query fails which contains utf8-bytes to be inserted into the database.

With regular psycopg2 I get the full error printed (which contains umlauts from the query).

karanlyons commented 8 years ago

I’m running into this issue as well. As far as I can tell the string codec issues are confined to bytes_to_ascii and perhaps ascii_to_bytes.

It’s should be safe to use UTF-8 as the encoding here as it’s ASCII compatible.

msva commented 8 years ago

And my fifty cents. Just to state, that issue is still actual.

lopuhin commented 8 years ago

Should be fixed now, will put a release on PyPI later today.

lopuhin commented 8 years ago

2.7.3 release is on PyPI