MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / python_2_unicode_compatible

Function python_2_unicode_compatible

pager_lib/six.py:963–978  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

961
962
963def 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.

Callers

nothing calls this directly

Calls 2

encodeMethod · 0.45
__unicode__Method · 0.45

Tested by

no test coverage detected