]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- walk back r6851 slightly. Still emit a warning when the Unicode or UnicodeText...
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 25 Feb 2010 22:16:34 +0000 (22:16 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 25 Feb 2010 22:16:34 +0000 (22:16 +0000)
is passed a bytestring even if the backend accepts unicodes directly.

CHANGES
lib/sqlalchemy/types.py
test/sql/test_types.py

diff --git a/CHANGES b/CHANGES
index 7c33f623986f6b23204c0350e8dcb3c11bdb666e..82a15639eb7f1bc45e51a9d1bdf8ae46409c03e8 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -183,8 +183,9 @@ CHANGES
 
   - The assert_unicode flag is deprecated.  SQLAlchemy will raise
     a warning in all cases where it is asked to encode a non-unicode
-    Python string, and will do nothing for DBAPIs that already 
-    accept Python unicode objects.
+    Python string, as well as when a Unicode or UnicodeType type
+    is explicitly passed a bytestring.  The String type will do nothing 
+    for DBAPIs that already accept Python unicode objects.
     
 - metadata
   - Added the ability to strip schema information when using
index aed17e67e4e68957461e399d3ea0b02ad1f5b7ec..a51e271f9d07d7218756e1b7787d93ca5e4d5adc 100644 (file)
@@ -501,7 +501,9 @@ class String(Concatenable, TypeEngine):
     __visit_name__ = 'string'
 
     def __init__(self, length=None, convert_unicode=False, 
-                        assert_unicode=None, unicode_error=None):
+                        assert_unicode=None, unicode_error=None,
+                        _warn_on_bytestring=False
+                        ):
         """
         Create a string-holding type.
 
@@ -571,17 +573,27 @@ class String(Concatenable, TypeEngine):
         self.length = length
         self.convert_unicode = convert_unicode
         self.unicode_error = unicode_error
-        
+        self._warn_on_bytestring = _warn_on_bytestring
         
     def adapt(self, impltype):
         return impltype(
                     length=self.length,
-                    convert_unicode=self.convert_unicode)
+                    convert_unicode=self.convert_unicode,
+                    _warn_on_bytestring=True,
+                    )
 
     def bind_processor(self, dialect):
         if self.convert_unicode or dialect.convert_unicode:
             if dialect.supports_unicode_binds and self.convert_unicode != 'force':
-                return None
+                if self._warn_on_bytestring:
+                    def process(value):
+                        if isinstance(value, str):
+                            util.warn("Unicode type received non-unicode bind "
+                                      "param value %r" % value)
+                        return value
+                    return process
+                else:
+                    return None
             else:
                 encoder = codecs.getencoder(dialect.encoding)
                 def process(value):
@@ -671,7 +683,7 @@ class Unicode(String):
     """
 
     __visit_name__ = 'unicode'
-
+    
     def __init__(self, length=None, **kwargs):
         """
         Create a Unicode-converting String type.
@@ -688,7 +700,7 @@ class Unicode(String):
           
         """
         kwargs.setdefault('convert_unicode', True)
-        super(Unicode, self).__init__(length=length, **kwargs)
+        super(Unicode, self).__init__(length=length, _warn_on_bytestring=True, **kwargs)
 
 class UnicodeText(Text):
     """An unbounded-length Unicode string.
@@ -716,7 +728,7 @@ class UnicodeText(Text):
 
         """
         kwargs.setdefault('convert_unicode', True)
-        super(UnicodeText, self).__init__(length=length, **kwargs)
+        super(UnicodeText, self).__init__(length=length, _warn_on_bytestring=True, **kwargs)
 
 
 class Integer(_DateAffinity, TypeEngine):
index fb7249ae21cc2766a2afbc05c0d72a4157305267..4b2370afcbf45c6157c578e85dadeedfdefdf208 100644 (file)
@@ -391,6 +391,29 @@ class UnicodeTest(TestBase, AssertsExecutionResults):
                         u"une drôle de petit voix m’a réveillé. "\
                         u"Elle disait: « S’il vous plaît… dessine-moi un mouton! »"
 
+        u = Unicode()
+        uni = u.dialect_impl(testing.db.dialect).bind_processor(testing.db.dialect)
+        if testing.db.dialect.supports_unicode_binds:
+            # Py3K
+            #assert_raises(exc.SAWarning, uni, b'x')
+            #assert isinstance(uni(unicodedata), str)
+            # Py2K
+            assert_raises(exc.SAWarning, uni, 'x')
+            assert isinstance(uni(unicodedata), unicode)
+            # end Py2K
+
+            eq_(uni(unicodedata), unicodedata)
+        else:
+            # Py3K
+            #assert_raises(exc.SAWarning, uni, b'x')
+            #assert isinstance(uni(unicodedata), bytes)
+            # Py2K
+            assert_raises(exc.SAWarning, uni, 'x')
+            assert isinstance(uni(unicodedata), str)
+            # end Py2K
+        
+            eq_(uni(unicodedata), unicodedata.encode('utf-8'))
+        
         unicode_engine = engines.utf8_engine(options={'convert_unicode':True,})
         unicode_engine.dialect.supports_unicode_binds = False