From: Mike Bayer Date: Wed, 28 Nov 2007 00:44:16 +0000 (+0000) Subject: default value of assert_unicode is None on String, False on create_engine(), and... X-Git-Tag: rel_0_4_2~127 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=d56e11ffe290515e44d8bdeda76f06ff87a2b096;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git default value of assert_unicode is None on String, False on create_engine(), and True on Unicode type. --- diff --git a/CHANGES b/CHANGES index 9ff6189529..49adb45eec 100644 --- a/CHANGES +++ b/CHANGES @@ -4,12 +4,13 @@ CHANGES 0.4.2 ----- - sql - - added new flag to String and create_engine(), assert_unicode=(True|False|None). - When convert_unicode=True, this flag also defaults to `True`, and results in all - unicode conversion operations raising an exception when a non-unicode bytestring - is passed as a bind parameter. It is strongly advised that all unicode-aware - applications make proper use of Python unicode objects (i.e. u'hello' and - not 'hello'). + - added new flag to String and create_engine(), + assert_unicode=(True|False|None). Defaults to `False` or `None` on + create_engine() and String, `True` on the Unicode type. When `True`, + results in all unicode conversion operations raising an exception when a + non-unicode bytestring is passed as a bind parameter. It is strongly + advised that all unicode-aware applications make proper use of Python + unicode objects (i.e. u'hello' and not 'hello'). - column labels in the form "tablename.columname", i.e. with a dot, are now supported. diff --git a/doc/build/content/dbengine.txt b/doc/build/content/dbengine.txt index 9fc0ae5910..c83cf30237 100644 --- a/doc/build/content/dbengine.txt +++ b/doc/build/content/dbengine.txt @@ -126,7 +126,7 @@ Keyword options can also be specified to `create_engine()`, following the string A list of all standard options, as well as several that are used by particular database dialects, is as follows: -* **assert_unicode=None** - defaults to `True` when `convert_unicode==True`. This will assert that all incoming string bind parameters are instances of `unicode`. Only takes effect when `convert_unicode==True`. Set to `False` to disable unicode assertions when `convert_unicode==True`. This flag is also available on the `String` type and its decsendants. New in 0.4.2. +* **assert_unicode=False** - When set to `True` alongside convert_unicode=`True`, asserts that incoming string bind parameters are instances of `unicode`, otherwise raises an error. Only takes effect when `convert_unicode==True`. This flag is also available on the `String` type and its decsendants. New in 0.4.2. * **connect_args** - a dictionary of options which will be passed directly to the DBAPI's `connect()` method as additional keyword arguments. * **convert_unicode=False** - if set to True, all String/character based types will convert Unicode values to raw byte values going into the database, and all raw byte values to Python Unicode coming out in result sets. This is an engine-wide method to provide unicode conversion across the board. For unicode conversion on a column-by-column level, use the `Unicode` column type instead, described in [types](rel:types). * **creator** - a callable which returns a DBAPI connection. This creation function will be passed to the underlying connection pool and will be used to create all new database connections. Usage of this function causes connection parameters specified in the URL argument to be bypassed. diff --git a/doc/build/content/types.txt b/doc/build/content/types.txt index 364599f0be..227f5b1870 100644 --- a/doc/build/content/types.txt +++ b/doc/build/content/types.txt @@ -23,7 +23,7 @@ This type is the base type for all string and character types, such as `Unicode` `convert_unicode=True` indicates that incoming strings, if they are Python `unicode` strings, will be encoded into a raw bytestring using the `encoding` attribute of the dialect (defaults to `utf-8`). Similarly, raw bytestrings coming back from the database will be decoded into `unicode` objects on the way back. -`assert_unicode=True` is set to true by default when `convert_unicode=True`, and indicates that incoming bind parameters will be checked that they are in fact `unicode` objects, else an error is raised. (this flag is new as of version 0.4.2) +`assert_unicode` is set to `None` by default. When `True`, it indicates that incoming bind parameters will be checked that they are in fact `unicode` objects, else an error is raised. Setting it to `None` indicates that the dialect-level `convert_unicode` setting should take place, whereas setting it to `False` disables it unconditionally (this flag is new as of version 0.4.2). Both `convert_unicode` and `assert_unicode` may be set at the engine level as flags to `create_engine()`. diff --git a/lib/sqlalchemy/databases/mysql.py b/lib/sqlalchemy/databases/mysql.py index 03b9a749ce..7421644128 100644 --- a/lib/sqlalchemy/databases/mysql.py +++ b/lib/sqlalchemy/databases/mysql.py @@ -725,7 +725,7 @@ class MSText(_StringType, sqltypes.TEXT): _StringType.__init__(self, **kwargs) sqltypes.TEXT.__init__(self, length, - kwargs.get('convert_unicode', False)) + kwargs.get('convert_unicode', False), kwargs.get('assert_unicode', None)) def get_col_spec(self): if self.length: @@ -889,7 +889,7 @@ class MSString(_StringType, sqltypes.String): _StringType.__init__(self, **kwargs) sqltypes.String.__init__(self, length, - kwargs.get('convert_unicode', False)) + kwargs.get('convert_unicode', False), kwargs.get('assert_unicode', None)) def get_col_spec(self): if self.length: diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index fab5d05b05..0e50093ee6 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -35,7 +35,7 @@ class DefaultDialect(base.Dialect): supports_pk_autoincrement = True dbapi_type_map = {} - def __init__(self, convert_unicode=False, assert_unicode=None, encoding='utf-8', default_paramstyle='named', paramstyle=None, dbapi=None, **kwargs): + def __init__(self, convert_unicode=False, assert_unicode=False, encoding='utf-8', default_paramstyle='named', paramstyle=None, dbapi=None, **kwargs): self.convert_unicode = convert_unicode self.assert_unicode = assert_unicode self.encoding = encoding diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 93e7d60668..e23a695f3c 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -307,16 +307,14 @@ class String(Concatenable, TypeEngine): self.assert_unicode = assert_unicode def adapt(self, impltype): - return impltype(length=self.length, convert_unicode=self.convert_unicode) + return impltype(length=self.length, convert_unicode=self.convert_unicode, assert_unicode=self.assert_unicode) def bind_processor(self, dialect): if self.convert_unicode or dialect.convert_unicode: - if self.assert_unicode is not None: - assert_unicode = self.assert_unicode - elif dialect.assert_unicode is not None: - assert_unicode = dialect.assert_unicode + if self.assert_unicode is None: + assert_unicode = bool(dialect.assert_unicode) else: - assert_unicode = True + assert_unicode = self.assert_unicode def process(value): if isinstance(value, unicode): return value.encode(dialect.encoding) diff --git a/test/sql/testtypes.py b/test/sql/testtypes.py index 8c247897f4..3d44a52941 100644 --- a/test/sql/testtypes.py +++ b/test/sql/testtypes.py @@ -290,9 +290,20 @@ class UnicodeTest(AssertMixin): def testassert(self): try: unicode_table.insert().execute(unicode_varchar='im not unicode') + assert False except exceptions.InvalidRequestError, e: assert str(e) == "Received non-unicode bind param value 'im not unicode'" + unicode_engine = engines.utf8_engine(options={'convert_unicode':True, 'assert_unicode':True}) + try: + try: + unicode_engine.execute(unicode_table.insert(), plain_varchar='im not unicode') + assert False + except exceptions.InvalidRequestError, e: + assert str(e) == "Received non-unicode bind param value 'im not unicode'" + finally: + unicode_engine.dispose() + @testing.unsupported('oracle') def testblanks(self): unicode_table.insert().execute(unicode_varchar=u'')