compile(unicode) & source encoding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Denis S. Otkidach

    compile(unicode) & source encoding


    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(c onst.decode('ut f-8'))
    else:
    consts.append(c onst)
    import new
    code = new.code(code.c o_argcount, code.co_nlocals ,
    code.co_stacksi ze, code.co_flags, code.co_code,
    tuple(consts), code.co_names, code.co_varname s,
    code.co_filenam e, code.co_name,
    code.co_firstli neno, 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]


  • Just

    #2
    Re: compile(unicode ) & source encoding

    In article <mailman.1180.1 070041213.702.p ython-list@python.org >,
    "Denis S. Otkidach" <ods@strana.r u> wrote:
    [color=blue]
    > 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.[/color]

    You can just pass a unicode string (in 2.3 at least), so you can do the
    decoding yourself.

    Just

    Comment

    • Martin v. Löwis

      #3
      Re: compile(unicode ) &amp; source encoding

      "Denis S. Otkidach" <ods@strana.r u> writes:

      [In the context of compile()][color=blue]
      > 1. What is the reason to encode source code to utf-8?[/color]

      I think it is because Just found that the most straight-forward thing
      to do: UTF-8 supports all Unicode characters, and you have to choose
      *some* encoding, as the resulting byte strings need to be encoded
      somehow.

      In addition, anybody desiring a different encoding can explicitly
      invoke .encode()
      [color=blue]
      > 2. Why there is now option to define encoding of source passed to
      > compile function?[/color]

      No, although this should be implemented. It is not implemented at this
      point because it is very hard to do, and I have not found a good way
      to do it, yet.

      Regards,
      Martin

      Comment

      Working...