diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-11-25 23:14:03 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-11-25 23:14:03 +0000 |
| commit | e4056a17874a6b9c9af4f1c2ef48d15ebcc49160 (patch) | |
| tree | ceaf25bf8f985036797e7a5024618118439de078 /lib/sqlalchemy | |
| parent | 99827142d6b42bc2a83a22a9663cd46f13e94a4f (diff) | |
| download | sqlalchemy-e4056a17874a6b9c9af4f1c2ef48d15ebcc49160.tar.gz | |
- 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').
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/engine/default.py | 3 | ||||
| -rw-r--r-- | lib/sqlalchemy/types.py | 13 |
2 files changed, 13 insertions, 3 deletions
diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index 19ab22c9e..fab5d05b0 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -35,8 +35,9 @@ class DefaultDialect(base.Dialect): supports_pk_autoincrement = True dbapi_type_map = {} - def __init__(self, convert_unicode=False, encoding='utf-8', default_paramstyle='named', paramstyle=None, dbapi=None, **kwargs): + def __init__(self, convert_unicode=False, assert_unicode=None, encoding='utf-8', default_paramstyle='named', paramstyle=None, dbapi=None, **kwargs): self.convert_unicode = convert_unicode + self.assert_unicode = assert_unicode self.encoding = encoding self.positional = False self._ischema = None diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 0f93b8e34..ac9a36195 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -300,18 +300,27 @@ class Concatenable(object): return op class String(Concatenable, TypeEngine): - def __init__(self, length=None, convert_unicode=False): + def __init__(self, length=None, convert_unicode=False, assert_unicode=None): self.length = length self.convert_unicode = convert_unicode + self.assert_unicode = assert_unicode def adapt(self, impltype): return impltype(length=self.length, convert_unicode=self.convert_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 + else: + assert_unicode = True def process(value): if isinstance(value, unicode): return value.encode(dialect.encoding) + elif assert_unicode: + raise exceptions.InvalidRequestError("Received non-unicode bind param value %r" % value) else: return value return process @@ -344,7 +353,7 @@ class String(Concatenable, TypeEngine): class Unicode(String): def __init__(self, length=None, **kwargs): - kwargs['convert_unicode'] = True + kwargs['convert_unicode'] = kwargs['assert_unicode'] = True super(Unicode, self).__init__(length=length, **kwargs) class Integer(TypeEngine): |
