Return preferred encoding for text I/O.

def getpreferredencoding():
    """Return preferred encoding for text I/O."""
    encoding = locale.getpreferredencoding(False)
    if sys.platform == 'darwin' and encoding.startswith('mac-'):
        # Upgrade ancient MacOS encodings in Python < 2.7
        encoding = 'utf-8'
    return encoding