diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2018-02-26 09:38:05 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2018-03-01 10:34:56 -0800 |
commit | 123cf608fd054b902a893b83ced1ea39dbe31f9f (patch) | |
tree | 559ad925ebd456b69e0e9da19e2f9983cdaef1f1 /alot/helper.py | |
parent | 6a8cdaf9ae7d736e55c7a389444fa874905a8bda (diff) |
fix a bunch of utils tests for py3k
There are a few that are still broken because of bytes to unicode
conversion, and this may not all be correct, but most of the tests pass
Diffstat (limited to 'alot/helper.py')
-rw-r--r-- | alot/helper.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/alot/helper.py b/alot/helper.py index daed2128..368a3e46 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -638,6 +638,32 @@ def email_as_string(mail): return as_string +def email_as_bytes(mail): + string = email_as_string(mail) + charset = mail.get_charset() + if charset: + charset = str(charset) + else: + charsets = set(mail.get_charsets()) + if None in charsets: + # None is equal to US-ASCII + charsets.discard(None) + charsets.add('ascii') + + if len(charsets) == 1: + charset = list(charsets)[0] + elif 'ascii' in charsets: + # If we get here and the assert triggers it means that different + # parts of the email are encoded differently. I don't think we're + # likely to see that, but it's possible + assert {'utf-8', 'ascii'}.issuperset(charsets), 'This needs different handling.' + charset = 'utf-8' # It's a strict super-set + else: + charset = 'utf-8' + + return string.encode(charset) + + def get_xdg_env(env_name, fallback): """ Used for XDG_* env variables to return fallback if unset *or* empty """ env = os.environ.get(env_name) |