self.convert_unicode != 'force':
if self._warn_on_bytestring:
def process(value):
-# start Py3K
- if isinstance(value, bytes):
-# end Py3K
-# start Py2K
-# if isinstance(value, str):
-# end Py2K
+ if isinstance(value, util.binary_type):
util.warn("Unicode type received non-unicode bind "
"param value.")
return value
warn_on_bytestring = self._warn_on_bytestring
def process(value):
- if isinstance(value, str):
+ if isinstance(value, util.text_type):
return encoder(value, self.unicode_error)[0]
elif warn_on_bytestring and value is not None:
util.warn("Unicode type received non-unicode bind "
@property
def python_type(self):
if self.convert_unicode:
- return str
+ return util.text_type
else:
return str
def coerce_compared_value(self, op, value):
"""See :meth:`.TypeEngine.coerce_compared_value` for a description."""
- if isinstance(value, str):
+ if isinstance(value, util.string_types):
return self
else:
return super(_Binary, self).coerce_compared_value(op, value)
convert_unicode = kw.pop('convert_unicode', None)
if convert_unicode is None:
for e in enums:
- if isinstance(e, str):
+ if isinstance(e, util.string_types):
convert_unicode = True
break
else:
STRINGTYPE = String()
_type_map = {
- str: String(),
-# start Py3K
- bytes: LargeBinary(),
-# end Py3K
-# start Py2K
-# unicode: Unicode(),
-# end Py2K
int: Integer(),
float: Numeric(),
bool: BOOLEANTYPE,
dt.timedelta: Interval(),
NoneType: NULLTYPE
}
+
+if util.py3k:
+ _type_map[bytes] = LargeBinary()
+ _type_map[str] = Unicode()
+else:
+ _type_map[unicode] = Unicode()
+ _type_map[str] = String()
+
+
eq_(types.Integer().python_type, int)
eq_(types.Numeric().python_type, decimal.Decimal)
eq_(types.Numeric(asdecimal=False).python_type, float)
-# start Py3K
- eq_(types.LargeBinary().python_type, bytes)
-# end Py3K
-# start Py2K
-# eq_(types.LargeBinary().python_type, str)
-# end Py2K
+ eq_(types.LargeBinary().python_type, util.binary_type)
eq_(types.Float().python_type, float)
eq_(types.Interval().python_type, datetime.timedelta)
eq_(types.Date().python_type, datetime.date)
eq_(types.DateTime().python_type, datetime.datetime)
-# start Py3K
eq_(types.String().python_type, str)
-# end Py3K
-# start Py2K
-# eq_(types.String().python_type, str)
-# end Py2K
- eq_(types.Unicode().python_type, str)
- eq_(types.String(convert_unicode=True).python_type, str)
+ eq_(types.Unicode().python_type, util.text_type)
+ eq_(types.String(convert_unicode=True).python_type, util.text_type)
assert_raises(
NotImplementedError,
def test_processing(self):
users = self.tables.users
users.insert().execute(
- user_id=2, goofy='jack', goofy2='jack', goofy4='jack',
- goofy7='jack', goofy8=12, goofy9=12)
+ user_id=2, goofy='jack', goofy2='jack', goofy4=util.u('jack'),
+ goofy7=util.u('jack'), goofy8=12, goofy9=12)
users.insert().execute(
- user_id=3, goofy='lala', goofy2='lala', goofy4='lala',
- goofy7='lala', goofy8=15, goofy9=15)
+ user_id=3, goofy='lala', goofy2='lala', goofy4=util.u('lala'),
+ goofy7=util.u('lala'), goofy8=15, goofy9=15)
users.insert().execute(
- user_id=4, goofy='fred', goofy2='fred', goofy4='fred',
- goofy7='fred', goofy8=9, goofy9=9)
+ user_id=4, goofy='fred', goofy2='fred', goofy4=util.u('fred'),
+ goofy7=util.u('fred'), goofy8=9, goofy9=9)
l = users.select().order_by(users.c.user_id).execute().fetchall()
for assertstr, assertint, assertint2, row in zip(
eq_(row[5], assertint)
eq_(row[6], assertint2)
for col in row[3], row[4]:
- assert isinstance(col, str)
+ assert isinstance(col, util.text_type)
def test_typedecorator_impl(self):
for impl_, exp, kw in [
expected
)
- data = "Alors vous imaginez ma surprise, au lever du jour, quand "\
+ data = util.u("Alors vous imaginez ma surprise, au lever du jour, quand "\
"une drôle de petite voix m’a réveillé. "\
- "Elle disait: « S’il vous plaît… dessine-moi un mouton! »"
+ "Elle disait: « S’il vous plaît… dessine-moi un mouton! »")
def test_unicode_warnings_typelevel_native_unicode(self):
dialect = default.DefaultDialect()
dialect.supports_unicode_binds = True
uni = u.dialect_impl(dialect).bind_processor(dialect)
-# start Py3K
- assert_raises(exc.SAWarning, uni, b'x')
- assert isinstance(uni(unicodedata), str)
-# end Py3K
-# start Py2K
-# assert_raises(exc.SAWarning, uni, 'x')
-# assert isinstance(uni(unicodedata), unicode)
-# end Py2K
+ if util.py3k:
+ assert_raises(exc.SAWarning, uni, b'x')
+ assert isinstance(uni(unicodedata), str)
+ else:
+ assert_raises(exc.SAWarning, uni, 'x')
+ assert isinstance(uni(unicodedata), unicode)
def test_unicode_warnings_typelevel_sqla_unicode(self):
unicodedata = self.data
dialect = default.DefaultDialect()
dialect.supports_unicode_binds = False
uni = u.dialect_impl(dialect).bind_processor(dialect)
-# start Py3K
- assert_raises(exc.SAWarning, uni, b'x')
- assert isinstance(uni(unicodedata), bytes)
-# end Py3K
-# start Py2K
-# assert_raises(exc.SAWarning, uni, 'x')
-# assert isinstance(uni(unicodedata), str)
-# end Py2K
+ assert_raises(exc.SAWarning, uni, util.b('x'))
+ assert isinstance(uni(unicodedata), util.binary_type)
eq_(uni(unicodedata), unicodedata.encode('utf-8'))
s = String()
uni = s.dialect_impl(dialect).bind_processor(dialect)
- # this is not the unicode type - no warning
-# start Py3K
- uni(b'x')
- assert isinstance(uni(unicodedata), bytes)
-# end Py3K
-# start Py2K
-# uni('x')
-# assert isinstance(uni(unicodedata), str)
-# end Py2K
+
+ uni(util.b('x'))
+ assert isinstance(uni(unicodedata), util.binary_type)
eq_(uni(unicodedata), unicodedata.encode('utf-8'))