]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- add "force_nocheck" as a way to turn on unicode=force without even
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 16 Dec 2013 02:23:01 +0000 (21:23 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 16 Dec 2013 02:23:01 +0000 (21:23 -0500)
doing the isinstance() check - currently used only by psycopg2 + native enum + py2k.
- didn't realize psycopg2 had UNICODEARRAY extension all this time; replace _PGArray
with just using UNICODEARRAY instead.
- replace unnecessary/inconsistent __import__ in _isolation_lookup.

doc/build/changelog/changelog_09.rst
lib/sqlalchemy/dialects/postgresql/psycopg2.py
lib/sqlalchemy/sql/sqltypes.py

index c40c4c6c8df4ba130860f5085dba747806ceeb8f..2cfa424e88bcfb0682a8f8dd9610846b97d2a15b 100644 (file)
 .. changelog::
     :version: 0.9.0b2
 
+    .. change::
+        :tags: bug, postgresql
+
+        Now using psycopg2 UNICODEARRAY extension for handling unicode arrays
+        with psycopg2 + normal "native unicode" mode, in the same way the
+        UNICODE extension is used.
+
     .. change::
         :tags: bug, sql
         :tickets: 2883
index d7ce6eb90b8978c4d25d116f7c4b8ec4ed31d089..c3c749523cbe6bde6cee12675e06046fb5e38eeb 100644 (file)
@@ -212,23 +212,13 @@ class _PGNumeric(sqltypes.Numeric):
 
 
 class _PGEnum(ENUM):
-    def __init__(self, *arg, **kw):
-        super(_PGEnum, self).__init__(*arg, **kw)
-        if util.py2k:
-            if self.convert_unicode:
-                self.convert_unicode = "force"
-
-
-class _PGArray(ARRAY):
-    def __init__(self, *arg, **kw):
-        super(_PGArray, self).__init__(*arg, **kw)
-        if util.py2k:
-            # FIXME: this check won't work for setups that
-            # have convert_unicode only on their create_engine().
-            if isinstance(self.item_type, sqltypes.String) and \
-                        self.item_type.convert_unicode:
-                self.item_type.convert_unicode = "force"
-
+    def result_processor(self, dialect, coltype):
+        if util.py2k and self.convert_unicode is True:
+            # we can't easily use PG's extensions here because
+            # the OID is on the fly, and we need to give it a python
+            # function anyway - not really worth it.
+            self.convert_unicode = "force_nocheck"
+        return super(_PGEnum, self).result_processor(dialect, coltype)
 
 class _PGHStore(HSTORE):
     def bind_processor(self, dialect):
@@ -334,7 +324,6 @@ class PGDialect_psycopg2(PGDialect):
             sqltypes.Numeric: _PGNumeric,
             ENUM: _PGEnum,  # needs force_unicode
             sqltypes.Enum: _PGEnum,  # needs force_unicode
-            ARRAY: _PGArray,  # needs force_unicode
             HSTORE: _PGHStore,
         }
     )
@@ -371,7 +360,7 @@ class PGDialect_psycopg2(PGDialect):
 
     @util.memoized_property
     def _isolation_lookup(self):
-        extensions = __import__('psycopg2.extensions').extensions
+        from psycopg2 import extensions
         return {
             'AUTOCOMMIT': extensions.ISOLATION_LEVEL_AUTOCOMMIT,
             'READ COMMITTED': extensions.ISOLATION_LEVEL_READ_COMMITTED,
@@ -409,6 +398,7 @@ class PGDialect_psycopg2(PGDialect):
         if self.dbapi and self.use_native_unicode:
             def on_connect(conn):
                 extensions.register_type(extensions.UNICODE, conn)
+                extensions.register_type(extensions.UNICODEARRAY, conn)
             fns.append(on_connect)
 
         if self.dbapi and self.use_native_hstore:
index 1c475a28b1d83edba4b3c614df0405e3f55f79f0..6ed20084b70533a7c45591e42c6bd6bceafa01ff 100644 (file)
@@ -192,13 +192,18 @@ class String(Concatenable, TypeEngine):
         wants_unicode = self.convert_unicode or dialect.convert_unicode
         needs_convert = wants_unicode and \
                         (dialect.returns_unicode_strings is not True or
-                        self.convert_unicode == 'force')
+                        self.convert_unicode in ('force', 'force_nocheck'))
+        needs_isinstance = (
+                                needs_convert and
+                                dialect.returns_unicode_strings and
+                                self.convert_unicode != 'force_nocheck'
+                            )
 
         if needs_convert:
             to_unicode = processors.to_unicode_processor_factory(
                                     dialect.encoding, self.unicode_error)
 
-            if dialect.returns_unicode_strings:
+            if needs_isinstance:
                 # we wouldn't be here unless convert_unicode='force'
                 # was specified, or the driver has erratic unicode-returning
                 # habits.  since we will be getting back unicode