[Zope-CVS] SVN: psycopgda/trunk/ Unicode strings are returned as UTF-8 encoded strings, so we need to

Stephan Richter srichter at cosmos.phy.tufts.edu
Sat Oct 9 15:40:07 EDT 2004


Log message for revision 27872:
  Unicode strings are returned as UTF-8 encoded strings, so we need to 
  convert them to unicode using an adapter.
  


Changed:
  U   psycopgda/trunk/adapter.py
  U   psycopgda/trunk/tests.py


-=-
Modified: psycopgda/trunk/adapter.py
===================================================================
--- psycopgda/trunk/adapter.py	2004-10-09 18:25:39 UTC (rev 27871)
+++ psycopgda/trunk/adapter.py	2004-10-09 19:40:06 UTC (rev 27872)
@@ -21,7 +21,9 @@
 import psycopg
 import re
 
-# These OIDs are taken from pg_types.h from PostgreSQL headers.
+PG_ENCODING = 'utf8'
+
+# These OIDs are taken from include/server/pg_type.h from PostgreSQL headers.
 # Unfortunatelly psycopg does not export them as constants, and
 # we cannot use psycopg.FOO.values because they overlap.
 DATE_OID        = 1082
@@ -30,6 +32,12 @@
 TIMESTAMP_OID   = 1114
 TIMESTAMPTZ_OID = 1184
 INTERVAL_OID    = 1186
+
+CHAR_OID = 18
+TEXT_OID = 25
+BPCHAR_OID = 1042
+VARCHAR_OID = 1043
+
 # The following ones are obsolete and we don't handle them
 #ABSTIME_OID     = 702
 #RELTIME_OID     = 703
@@ -255,6 +263,9 @@
             return timedelta(days=d, hours=hr, minutes=mn, seconds=sc)
 
 
+def _conv_string(str):
+    return str.decode(PG_ENCODING)
+
 # User-defined types
 DATE = psycopg.new_type((DATE_OID,), "ZDATE", _conv_date)
 TIME = psycopg.new_type((TIME_OID,), "ZTIME", _conv_time)
@@ -264,7 +275,10 @@
                                 _conv_timestamptz)
 INTERVAL = psycopg.new_type((INTERVAL_OID,), "ZINTERVAL", _conv_interval)
 
+STRING = psycopg.new_type((CHAR_OID, TEXT_OID, BPCHAR_OID, VARCHAR_OID),
+                          "ZSTRING", _conv_string)
 
+
 dsn2option_mapping = {'host': 'host',
                       'port': 'port',
                       'dbname': 'dbname',
@@ -306,3 +320,5 @@
         psycopg.register_type(TIMESTAMP)
         psycopg.register_type(TIMESTAMPTZ)
         psycopg.register_type(INTERVAL)
+        psycopg.register_type(STRING)
+            

Modified: psycopgda/trunk/tests.py
===================================================================
--- psycopgda/trunk/tests.py	2004-10-09 18:25:39 UTC (rev 27871)
+++ psycopgda/trunk/tests.py	2004-10-09 19:40:06 UTC (rev 27872)
@@ -150,6 +150,12 @@
         self.assertRaises(ValueError, c, '2days')
         self.assertRaises(ValueError, c, '123')
 
+    def test_conv_string(self):
+        from psycopgda.adapter import _conv_string
+        self.assertEquals(_conv_string(''), u'')
+        self.assertEquals(_conv_string('c'), u'c')
+        self.assertEquals(_conv_string('\xc3\x82\xc2\xa2'), u'\xc2\xa2')
+        self.assertEquals(_conv_string('c\xc3\x82\xc2\xa2'), u'c\xc2\xa2')
 
 class TestPsycopgAdapter(TestCase):
 



More information about the Zope-CVS mailing list