Fixed PG -> Py encodings mapping with non-alnum chars.

We mangle the encoding names a little bit before asking it to the
backend: be sure to be able to find the equivalent Python code back or
decoding (unicode cast or Py3) will barf.
This commit is contained in:
Daniele Varrazzo 2010-12-14 02:21:49 +00:00
parent 4635c2aa4f
commit b96dcef8a2
3 changed files with 20 additions and 0 deletions

View File

@ -1,3 +1,7 @@
2010-12-14 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* lib/extensions.py: Improved mapping from PG to Py encodings.
2010-12-04 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* setup.py: bumped to version 2.3.1.dev0

View File

@ -128,4 +128,12 @@ class SQL_IN(object):
__str__ = getquoted
# Add the "cleaned" version of the encodings to the key.
# When the encoding is set its name is cleaned up from - and _ and turned
# uppercase, so an encoding not respecting these rules wouldn't be found in the
# encodings keys and would raise an exception with the unicode typecaster
for k, v in encodings.items():
k = k.replace('_', '').replace('-', '').upper()
encodings[k] = v
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())

View File

@ -111,6 +111,14 @@ class ConnectionTests(unittest.TestCase):
self.assert_(time.time() - t0 < 3,
"something broken in concurrency")
def test_encoding_name(self):
self.conn.set_client_encoding("EUC_JP")
# conn.encoding is 'EUCJP' now.
cur = self.conn.cursor()
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE, cur)
cur.execute("select 'foo'::text;")
self.assertEqual(cur.fetchone()[0], u'foo')
class IsolationLevelsTestCase(unittest.TestCase):