| 1 | def main(): |
|---|
| 2 | import locale |
|---|
| 3 | import os |
|---|
| 4 | import sys |
|---|
| 5 | |
|---|
| 6 | def get_file_encoding(f): |
|---|
| 7 | if hasattr(f, "encoding"): |
|---|
| 8 | return f.encoding |
|---|
| 9 | else: |
|---|
| 10 | return "not set" |
|---|
| 11 | |
|---|
| 12 | print "sys.stdin.encoding:", get_file_encoding(sys.stdin) |
|---|
| 13 | print "sys.stdout.encoding:", get_file_encoding(sys.stdout) |
|---|
| 14 | print "sys.stderr.encoding:", get_file_encoding(sys.stderr) |
|---|
| 15 | print "sys.getdefaultencoding():", sys.getdefaultencoding() |
|---|
| 16 | print "sys.getfilesystemencoding():", sys.getfilesystemencoding() |
|---|
| 17 | print "locale.getpreferredencoding():", locale.getpreferredencoding() |
|---|
| 18 | |
|---|
| 19 | if __name__ == "__main__": |
|---|
| 20 | main() |
|---|
| 21 | |
|---|
| 22 | # Debian UTF-8 environment: |
|---|
| 23 | # |
|---|
| 24 | # % LANG=en_GB.UTF-8 python tmp/unicodetest.py |
|---|
| 25 | # sys.stdin.encoding: UTF-8 |
|---|
| 26 | # sys.stdout.encoding: UTF-8 |
|---|
| 27 | # sys.stderr.encoding: None |
|---|
| 28 | # sys.getdefaultencoding(): ascii |
|---|
| 29 | # sys.getfilesystemencoding(): UTF-8 |
|---|
| 30 | # locale.getpreferredencoding(): UTF-8 |
|---|
| 31 | |
|---|
| 32 | # Debian ISO-8859-1 environment: |
|---|
| 33 | # |
|---|
| 34 | # % LANG=en_GB.ISO-8859-1 python tmp/unicodetest.py |
|---|
| 35 | # sys.stdin.encoding: ISO-8859-1 |
|---|
| 36 | # sys.stdout.encoding: ISO-8859-1 |
|---|
| 37 | # sys.stderr.encoding: None |
|---|
| 38 | # sys.getdefaultencoding(): ascii |
|---|
| 39 | # sys.getfilesystemencoding(): ISO-8859-1 |
|---|
| 40 | # locale.getpreferredencoding(): ISO-8859-1 |
|---|
| 41 | |
|---|
| 42 | # Debian UTF-8 environment, stdin/stdout redirected: |
|---|
| 43 | # |
|---|
| 44 | # % LANG=en_GB.UTF-8 python tmp/unicodetest.py </dev/null >unicodetest.out |
|---|
| 45 | # % cat unicodetest.out |
|---|
| 46 | # sys.stdin.encoding: None |
|---|
| 47 | # sys.stdout.encoding: None |
|---|
| 48 | # sys.stderr.encoding: None |
|---|
| 49 | # sys.getdefaultencoding(): ascii |
|---|
| 50 | # sys.getfilesystemencoding(): UTF-8 |
|---|
| 51 | # locale.getpreferredencoding(): UTF-8 |
|---|
| 52 | |
|---|
| 53 | # Windows 2000 command shell: |
|---|
| 54 | # |
|---|
| 55 | # H:\>python unicodetest.py |
|---|
| 56 | # sys.stdin.encoding: cp850 |
|---|
| 57 | # sys.stdout.encoding: cp850 |
|---|
| 58 | # sys.stderr.encoding: None |
|---|
| 59 | # sys.getdefaultencoding(): ascii |
|---|
| 60 | # sys.getfilesystemencoding(): mbcs |
|---|
| 61 | # locale.getpreferredencoding(): cp1252 |
|---|
| 62 | |
|---|
| 63 | # Windows 2000, IDLE: |
|---|
| 64 | # sys.stdin.encoding: cp1252 |
|---|
| 65 | # sys.stdout.encoding: cp1252 |
|---|
| 66 | # sys.stderr.encoding: cp1252 |
|---|
| 67 | # sys.getdefaultencoding(): ascii |
|---|
| 68 | # sys.getfilesystemencoding(): mbcs |
|---|
| 69 | # locale.getpreferredencoding(): cp1252 |
|---|