Skip to content
Browse files

Guess the encoding to use

If the output is not a terminal, there's no way python can guess the
encoding to use, so we use system's default encoding
  • Loading branch information...
1 parent 5269837 commit c37658c94f1f6695283eca07b898a59552eb240d @lucasdemarchi committed Jan 25, 2011
Showing with 6 additions and 1 deletion.
  1. +6 −1
@@ -27,6 +27,11 @@
# copied from
list_response_pattern = re.compile(r'\((?P<flags>.*?)\) "(?P<delimiter>.*)" (?P<name>.*)')
+if sys.stdout.isatty() and sys.stdout.encoding != None:
+ default_encoding = sys.stdout.encoding
+ import locale
+ default_encoding = locale.getpreferredencoding()
def parse_list_response(line):
flags, delimiter, mailbox_name = list_response_pattern.match(line).groups()
@@ -57,7 +62,7 @@ def decode_modified_utf7(s):
def safe_print(u):
- u = u.encode(sys.stdout.encoding, 'replace')
+ u = u.encode(default_encoding, 'replace')
def test():

2 comments on commit c37658c



As I've stated in the guiraldelli/Big-e-Mails-Hunter#2 , the locale.getpreferredencoding() unfortunately won't work because it returns a empty string as result. Maybe we should test the sys.stdout.encoding, them locale.getpreferredencoding(); if both of them failure, set ascii as a default encoding.


yes, this should work.

Please sign in to comment.
Something went wrong with that request. Please try again.