compile(unicode) & source encoding

Denis S. Otkidach ods at strana.ru
Fri Nov 28 12:40:04 EST 2003


I have 2 questions about compile

1. What is the reason to encode source code to utf-8?  I guess
"python -U" behavior is the best choice in this case.  The
following snippet seems to fix this, but I'm not sure it's
correct in all cases:

for const in code.co_consts:
    if type(const) is str:
        consts.append(const.decode('utf-8'))
    else:
        consts.append(const)
import new
code = new.code(code.co_argcount, code.co_nlocals,
                code.co_stacksize, code.co_flags, code.co_code,
                tuple(consts), code.co_names, code.co_varnames,
                code.co_filename, code.co_name,
                code.co_firstlineno, code.co_lnotab)
        return code


2. Why there is now option to define encoding of source passed to
compile function?  Yes, I can define encoding by prefixing my
code with '# -*- coding: %s -*-\n' % encoding, but it's not
handy.

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]






More information about the Python-list mailing list