summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-12-14 02:21:49 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-12-21 04:02:14 +0000
commitb96dcef8a264ab166608c9f821e73d8510c2c871 (patch)
tree1e2cf5d7a3192a8b98e184a8af057b266ef1e500
parent4635c2aa4f41c1fe05ec4d5d562f89eb3c4f9d27 (diff)
downloadpsycopg2-b96dcef8a264ab166608c9f821e73d8510c2c871.tar.gz
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.
-rw-r--r--ChangeLog4
-rw-r--r--lib/extensions.py8
-rw-r--r--tests/test_connection.py8
3 files changed, 20 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 41a90c6..11081e5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/lib/extensions.py b/lib/extensions.py
index fca7973..7618751 100644
--- a/lib/extensions.py
+++ b/lib/extensions.py
@@ -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())
diff --git a/tests/test_connection.py b/tests/test_connection.py
index 8d87e73..de775e0 100644
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -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):