diff options
Diffstat (limited to 'lib/sqlalchemy/processors.py')
-rw-r--r-- | lib/sqlalchemy/processors.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/lib/sqlalchemy/processors.py b/lib/sqlalchemy/processors.py index 0abf063b3..d0f52e42b 100644 --- a/lib/sqlalchemy/processors.py +++ b/lib/sqlalchemy/processors.py @@ -15,6 +15,7 @@ They all share one common characteristic: None is passed through unchanged. import codecs import re import datetime +from . import util def str_to_datetime_processor_factory(regexp, type_): @@ -66,6 +67,21 @@ def py_fallback(): return decoder(value, errors)[0] return process + def to_conditional_unicode_processor_factory(encoding, errors=None): + decoder = codecs.getdecoder(encoding) + + def process(value): + if value is None: + return None + elif isinstance(value, util.text_type): + return value + else: + # decoder returns a tuple: (value, len). Simply dropping the + # len part is safe: it is done that way in the normal + # 'xx'.decode(encoding) code path. + return decoder(value, errors)[0] + return process + def to_decimal_processor_factory(target_class, scale): fstring = "%%.%df" % scale @@ -113,12 +129,17 @@ try: str_to_date def to_unicode_processor_factory(encoding, errors=None): - # this is cumbersome but it would be even more so on the C side if errors is not None: return UnicodeResultProcessor(encoding, errors).process else: return UnicodeResultProcessor(encoding).process + def to_conditional_unicode_processor_factory(encoding, errors=None): + if errors is not None: + return UnicodeResultProcessor(encoding, errors).conditional_process + else: + return UnicodeResultProcessor(encoding).conditional_process + def to_decimal_processor_factory(target_class, scale): # Note that the scale argument is not taken into account for integer # values in the C implementation while it is in the Python one. |