]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
default value of assert_unicode is None on String, False on create_engine(), and...
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 28 Nov 2007 00:44:16 +0000 (00:44 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 28 Nov 2007 00:44:16 +0000 (00:44 +0000)
CHANGES
doc/build/content/dbengine.txt
doc/build/content/types.txt
lib/sqlalchemy/databases/mysql.py
lib/sqlalchemy/engine/default.py
lib/sqlalchemy/types.py
test/sql/testtypes.py

diff --git a/CHANGES b/CHANGES
index 9ff6189529bf89ce2143411f195ee8bd33027371..49adb45eec8b48a29a75a2ad07d8eb80fb2d0960 100644 (file)
--- 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.
index 9fc0ae5910c96ffd1a4e5b4ded54a11f4be76cd9..c83cf30237de05efa73aadd95969964c4c6ad97c 100644 (file)
@@ -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.
index 364599f0be0e01cd0ca35c5872e374ad25308bdb..227f5b1870bf830798a0b7164e7871eb59cdd3cf 100644 (file)
@@ -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()`.
 
index 03b9a749ce79214ee8803244c8e5b8ba9b12badd..742164412896bcddd4d9fa078ae79d686ea5ab1b 100644 (file)
@@ -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:
index fab5d05b051de887d8a0f22ff9e928b48b54af08..0e50093ee6f505bfcce4171225f93757211b4896 100644 (file)
@@ -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
index 93e7d60668018659a63ee034774239af68e943c5..e23a695f3c72fa817e81ca484c03865ef66bd928 100644 (file)
@@ -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)
index 8c247897f4bc00f2b3bc3021b56b871582ac9ebb..3d44a5294130ba7c9b0cbb21db5799e5d90a7282 100644 (file)
@@ -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'')