A class decorator that defines __unicode__ and __str__ methods under Python 2. Under Python 3 it does nothing. To support Python 2 and 3 with a single code base, define a __str__ method returning text and apply this decorator to the class.
(klass)
| 961 | |
| 962 | |
| 963 | def python_2_unicode_compatible(klass): |
| 964 | """ |
| 965 | A class decorator that defines __unicode__ and __str__ methods under Python 2. |
| 966 | Under Python 3 it does nothing. |
| 967 | |
| 968 | To support Python 2 and 3 with a single code base, define a __str__ method |
| 969 | returning text and apply this decorator to the class. |
| 970 | """ |
| 971 | if PY2: |
| 972 | if '__str__' not in klass.__dict__: |
| 973 | raise ValueError("@python_2_unicode_compatible cannot be applied " |
| 974 | "to %s because it doesn't define __str__()." % |
| 975 | klass.__name__) |
| 976 | klass.__unicode__ = klass.__str__ |
| 977 | klass.__str__ = lambda self: self.__unicode__().encode('utf-8') |
| 978 | return klass |
| 979 | |
| 980 | |
| 981 | # Complete the moves implementation. |
nothing calls this directly
no test coverage detected