From: Mike Bayer Date: Sun, 15 Nov 2009 19:46:54 +0000 (+0000) Subject: start relying on new unicode detection fully - remove isinstance() from the unicode... X-Git-Tag: rel_0_6beta1~167 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=447dc44e1f8d0c021e92216eb4608d0043fc2eaf;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git start relying on new unicode detection fully - remove isinstance() from the unicode result processing. --- diff --git a/lib/sqlalchemy/dialects/oracle/cx_oracle.py b/lib/sqlalchemy/dialects/oracle/cx_oracle.py index 2db37a4fc6..47f66c0705 100644 --- a/lib/sqlalchemy/dialects/oracle/cx_oracle.py +++ b/lib/sqlalchemy/dialects/oracle/cx_oracle.py @@ -124,20 +124,18 @@ class _LOBMixin(object): def result_processor(self, dialect, coltype): if not dialect.auto_convert_lobs: # return the cx_oracle.LOB directly. - # don't even call super.result_processor here. return None super_process = super(_LOBMixin, self).result_processor(dialect, coltype) - lob = dialect.dbapi.LOB if super_process: def process(value): - if isinstance(value, lob): + if value is not None: return super_process(value.read()) else: return super_process(value) else: def process(value): - if isinstance(value, lob): + if value is not None: return value.read() else: return value @@ -158,10 +156,25 @@ class _OracleText(_LOBMixin, sqltypes.Text): def get_dbapi_type(self, dbapi): return dbapi.CLOB -class _OracleUnicodeText(_LOBMixin, sqltypes.UnicodeText): +class _OracleUnicodeText(sqltypes.UnicodeText): def get_dbapi_type(self, dbapi): return dbapi.NCLOB + def result_processor(self, dialect, coltype): + if not dialect.auto_convert_lobs: + # return the cx_oracle.LOB directly. + return None + + if dialect._cx_oracle_native_nvarchar: + def process(value): + if value is not None: + return value.read() + else: + return value + return process + else: + return super(_OracleUnicodeText, self).result_processor(dialect, coltype) + class _OracleInteger(sqltypes.Integer): def result_processor(self, dialect, coltype): def to_int(val): @@ -178,7 +191,7 @@ class _OracleBinary(_LOBMixin, sqltypes.Binary): return None -class _OracleRaw(_LOBMixin, oracle.RAW): +class _OracleRaw(oracle.RAW): pass @@ -195,6 +208,7 @@ colspecs = { # it would be nice if we could not use it otherwise. oracle.NUMBER : oracle.NUMBER, # don't let this get converted oracle.RAW: _OracleRaw, + sqltypes.Unicode: _OracleNVarChar, sqltypes.NVARCHAR : _OracleNVarChar, } @@ -266,7 +280,7 @@ class Oracle_cx_oracleExecutionContext(OracleExecutionContext): dbapi_type) if result_processor is not None: out_parameters[name] = \ - result_processor(self.out_parameters[name].getvalue(), dbapi_type) + result_processor(self.out_parameters[name].getvalue()) else: out_parameters[name] = self.out_parameters[name].getvalue() else: diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 3fa18b2c26..62dd6dfc7b 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -522,10 +522,16 @@ class String(Concatenable, TypeEngine): return None def result_processor(self, dialect, coltype): - if (not dialect.returns_unicode_strings or self.convert_unicode == 'force') \ - and (self.convert_unicode or dialect.convert_unicode): + wants_unicode = self.convert_unicode or dialect.convert_unicode + needs_convert = wants_unicode and \ + (not dialect.returns_unicode_strings or + self.convert_unicode == 'force') + + if needs_convert: + # note we *assume* that we do not have a unicode object + # here, instead of an expensive isinstance() check. def process(value): - if value is not None and not isinstance(value, unicode): + if value is not None: return value.decode(dialect.encoding) else: return value diff --git a/test/dialect/test_oracle.py b/test/dialect/test_oracle.py index 57be6fe618..9715821786 100644 --- a/test/dialect/test_oracle.py +++ b/test/dialect/test_oracle.py @@ -389,7 +389,7 @@ class TypesTest(TestBase, AssertsCompiledSQL): (Unicode(), Unicode), (Text(), cx_oracle._OracleText), (UnicodeText(), cx_oracle._OracleUnicodeText), - (NCHAR(), NCHAR), + (NCHAR(), cx_oracle._OracleNVarChar), (oracle.RAW(50), cx_oracle._OracleRaw), ]: assert isinstance(start.dialect_impl(dialect), test), "wanted %r got %r" % (test, start.dialect_impl(dialect)) diff --git a/test/sql/test_types.py b/test/sql/test_types.py index a3cb03022a..332e02ef5f 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -279,8 +279,10 @@ class UnicodeTest(TestBase, AssertsExecutionResults): unicode_table.insert().execute(unicode_varchar=unicodedata,unicode_text=unicodedata) x = unicode_table.select().execute().first() - self.assert_(isinstance(x['unicode_varchar'], unicode) and x['unicode_varchar'] == unicodedata) - self.assert_(isinstance(x['unicode_text'], unicode) and x['unicode_text'] == unicodedata) + assert isinstance(x['unicode_varchar'], unicode) + assert isinstance(x['unicode_text'], unicode) + eq_(x['unicode_varchar'], unicodedata) + eq_(x['unicode_text'], unicodedata) def test_round_trip_executemany(self): # cx_oracle was producing different behavior for cursor.executemany()