def detect_console_encoding():
global _initial_defencoding<EOL>encoding = None<EOL>try:<EOL><INDENT>encoding = sys.stdout.encoding or sys.stdin.encoding<EOL><DEDENT>except (AttributeError, IOError):<EOL><INDENT>pass<EOL><DEDENT>if not encoding or '<STR_LIT:ascii>' in encoding.lower():<EOL><INDENT>try:<EOL><INDENT>encoding = locale.getpreferredencoding()<EOL><DEDENT>except Exception:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if not encoding or '<STR_LIT:ascii>' in encoding.lower():<EOL><INDENT>encoding = sys.getdefaultencoding()<EOL><DEDENT>if not _initial_defencoding:<EOL><INDENT>_initial_defencoding = sys.getdefaultencoding()<EOL><DEDENT>return encoding<EOL>
Try to find the most capable encoding supported by the console.
slightly modified from the way IPython handles the same issue.