From 060e30118373c909d4f96f7c3cad2b723fce95ab Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 25 Feb 2010 22:16:34 +0000 Subject: [PATCH] - walk back r6851 slightly. Still emit a warning when the Unicode or UnicodeText type is passed a bytestring even if the backend accepts unicodes directly. --- CHANGES | 5 +++-- lib/sqlalchemy/types.py | 26 +++++++++++++++++++------- test/sql/test_types.py | 23 +++++++++++++++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/CHANGES b/CHANGES index 7c33f62398..82a15639eb 100644 --- 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 diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index aed17e67e4..a51e271f9d 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -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): diff --git a/test/sql/test_types.py b/test/sql/test_types.py index fb7249ae21..4b2370afcb 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -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 -- 2.47.3