Sublime Forum

[SOLVED] Build results — unicode output

#1

Why I’m getting this with build system?

# coding: utf-8 print u"будь проклят тот день когда я сел за интерпретатор этого байткода"

[quote]Traceback (most recent call last):
File “D:\123.py”, line 2, in
print u"будь проклят тот день когда я сел за интерпретатор этого байткода"
UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-3: ordinal not in range(128)
[Finished in 0.2s with exit code 1][/quote]

And this with sublime console

[quote]>>> print u"Тест"
Тест[/quote]

0 Likes

#2

What build system are you using ?

The software building whatever you’re building is probably using your system locale by default, you’ll need to check its documentation to see how to change that.

0 Likes

#3

Default sublime text python build system on windows. The system locale is cp1251 and stdout is cp866 (madness! :unamused:), this probably causing problems like

[quote]D:>python test.py
будь проклят тот день когда я сел за интерпретатор этого байткода

D:>python test.py > file.txt
UnicodeEncodeError: ‘ascii’ codec can’t encode characters …][/quote]

I’ve found a solution, perhaps this code should be included in sublime text?

[code]# coding: utf-8

source: http://habrahabr.ru/post/117236/

import sys
import codecs

def setup_console(sys_enc=“utf-8”):
“”"
Set sys.defaultencoding to sys_enc and update stdout/stderr writers to corresponding encoding
For Win32 the OEM console encoding will be used istead of sys_enc
“”"
reload(sys)
try:
if sys.platform.startswith(“win”):
import ctypes
enc = “cp%d” % ctypes.windll.kernel32.GetOEMCP() #TODO: win64/python64 implementation
else:
enc = (sys.stdout.encoding if sys.stdout.isatty() else
sys.stderr.encoding if sys.stderr.isatty() else
sys.getfilesystemencoding() or sys_enc)

    sys.setdefaultencoding(sys_enc)

    # redefine stdout/stderr in console
    if sys.stdout.isatty() and sys.stdout.encoding != enc:
        sys.stdout = codecs.getwriter(enc)(sys.stdout, 'replace')

    if sys.stderr.isatty() and sys.stderr.encoding != enc:
        sys.stderr = codecs.getwriter(enc)(sys.stderr, 'replace')

except:
    pass

setup_console()
print u"будь проклят тот день когда я сел за интерпретатор этого байткода"[/code]

0 Likes

Windows ST3: Python 3 unicode output