Commit | Line | Data |
---|---|---|
3dbdb061 BP |
1 | import functools |
2 | import warnings | |
a80c74bb | 3 | |
3dbdb061 BP |
4 | import six |
5 | ||
6 | if six.PY3: | |
7f342c72 | 7 | from email.mime.text import MIMEText |
7f342c72 BP |
8 | else: |
9 | from email.MIMEText import MIMEText | |
0b2572b9 BP |
10 | |
11 | ||
3dbdb061 BP |
12 | def encode_to_utf8(method): |
13 | def wrapper(self): | |
14 | if six.PY2 and isinstance(method(self), six.text_type): | |
15 | return method(self).encode('utf-8') | |
16 | return method(self) | |
17 | functools.update_wrapper(wrapper, method, ['__name__', '__doc__']) | |
18 | return wrapper | |
19 | ||
20 | ||
21 | # based on django.utils.encoding.python_2_unicode_compatible | |
0b2572b9 | 22 | def py2_unicode(klass): |
3dbdb061 | 23 | if six.PY2: |
dce76c3e | 24 | if '__str__' not in klass.__dict__: |
3dbdb061 BP |
25 | warnings.warn("@py2_unicode cannot be applied " |
26 | "to %s because it doesn't define __str__()." % | |
27 | klass.__name__) | |
0b2572b9 | 28 | klass.__unicode__ = klass.__str__ |
3dbdb061 BP |
29 | klass.__str__ = encode_to_utf8(klass.__unicode__) |
30 | if '__repr__' in klass.__dict__: | |
31 | klass.__repr__ = encode_to_utf8(klass.__repr__) | |
0b2572b9 | 32 | return klass |