morepath.compat – Python 2/3 Compatibility

Infrastructure to help make Morepath work with both Python 2 and Python 3.

It used throughout the code to make Morepath portable across Python versions.

morepath.compat.PY3 = False

True if we are running on Python 3


The type used for non-bytes text.


Can be used with isinstance to determine whether an object is considered to be a string, i.e. isinstance(s, string_types).

morepath.compat.bytes_(s, encoding='latin-1', errors='strict')

Encode string if needed

If s is an instance of text_type, return s.encode(encoding, errors), otherwise return s