Home Download Buy Blog Forum Support

[SOLVED] Build results — unicode output

[SOLVED] Build results — unicode output

Postby krevetka on Mon Jul 09, 2012 12:02 pm

Why I'm getting this with build system?

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


Traceback (most recent call last):
File "D:\123.py", line 2, in <module>
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]


And this with sublime console
>>> print u"Тест"
Тест
Last edited by krevetka on Tue Jul 10, 2012 9:13 am, edited 1 time in total.
krevetka
 
Posts: 5
Joined: Mon Jul 09, 2012 11:37 am

Re: Build results — unicode output

Postby thekyz on Tue Jul 10, 2012 7:14 am

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.
thekyz
 
Posts: 20
Joined: Fri Feb 04, 2011 9:27 am

Re: Build results — unicode output

Postby krevetka on Tue Jul 10, 2012 9:13 am

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

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

D:\>python test.py > file.txt
UnicodeEncodeError: 'ascii' codec can't encode characters [...]


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

Code: Select all
# 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"будь проклят тот день когда я сел за интерпретатор этого байткода"
krevetka
 
Posts: 5
Joined: Mon Jul 09, 2012 11:37 am


Return to Technical Support

Who is online

Users browsing this forum: No registered users and 15 guests