- 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
__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.
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):
"""
__visit_name__ = 'unicode'
-
+
def __init__(self, length=None, **kwargs):
"""
Create a Unicode-converting String type.
"""
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.
"""
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):
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