diff options
author | John Szakmeister <john@szakmeister.net> | 2013-08-30 15:13:19 -0400 |
---|---|---|
committer | John Szakmeister <john@szakmeister.net> | 2013-08-30 18:02:32 -0400 |
commit | b588df70121faf84fdbda11fa3a4923f51b4d40c (patch) | |
tree | 1f56b0f95ce843c0fc26833f8b0f4e68390f79c1 /nose | |
parent | a248e37ad5b20ead46583afbd2821b9d68c640c8 (diff) | |
download | nose-b588df70121faf84fdbda11fa3a4923f51b4d40c.tar.gz |
pyversion: try unicode() before stringifying in force_unicode()
In Python 3, just stringify the argument. In all other Python
implementations, let's attempt to use unicode() first, and then fallback
to using str() and decoding.
Diffstat (limited to 'nose')
-rw-r--r-- | nose/pyversion.py | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/nose/pyversion.py b/nose/pyversion.py index 268b2b7..5799123 100644 --- a/nose/pyversion.py +++ b/nose/pyversion.py @@ -18,11 +18,17 @@ __all__ = ['make_instancemethod', 'cmp_to_key', 'sort_list', 'ClassType', # then become true) UNICODE_STRINGS = (type(unicode()) == type(str())) -def force_unicode(s, encoding='UTF-8'): - if not UNICODE_STRINGS: - if isinstance(s, str): - s = s.decode(encoding, 'replace') - return s +if sys.version_info[:2] < (3, 0): + def force_unicode(s, encoding='UTF-8'): + try: + s = unicode(s) + except UnicodeDecodeError: + s = str(s).decode(encoding, 'replace') + + return s +else: + def force_unicode(s, encoding='UTF-8'): + return str(s) # new.instancemethod() is obsolete for new-style classes (Python 3.x) # We need to use descriptor methods instead. |