PEP: Defining Unicode Literal Encodings (revision 1.1)

M.-A. Lemburg mal at lemburg.com
Fri Jul 13 18:21:32 EDT 2001


Here's an updated version which clarifies some issues...

--

PEP: 0263 (?)
Title: Defining Unicode Literal Encodings
Version: $Revision: 1.1 $
Author: mal at lemburg.com (Marc-André Lemburg)
Status: Draft
Type: Standards Track
Python-Version: 2.3
Created: 06-Jun-2001
Post-History: 
Requires: 244

Abstract

    This PEP proposes to use the PEP 244 statement "directive" to make
    the encoding used in Unicode string literals u"..." (and their raw
    counterparts ur"...") definable on a per source file basis.

Problem

    In Python 2.1, Unicode literals can only be written using the
    Latin-1 based encoding "unicode-escape". This makes the
    programming environment rather unfriendly to Python users who live
    and work in non-Latin-1 locales such as many of the Asian 
    countries. Programmers can write their 8-bit strings using the
    favourite encoding, but are bound to the "unicode-escape" encoding
    for Unicode literals.

Proposed Solution

    I propose to make the Unicode literal encodings (both standard and
    raw) a per-source file option which can be set using the
    "directive" statement proposed in PEP 244 in a slightly extended
    form (by adding the '=' between the directive name and it's value).

Syntax

    The syntax for the directives is as follows:

    'directive' WS+ 'unicodeencoding' WS* '=' WS* PYTHONSTRINGLITERAL
    'directive' WS+ 'rawunicodeencoding' WS* '=' WS* PYTHONSTRINGLITERAL

    with the PYTHONSTRINGLITERAL representing the encoding name to be
    used as standard Python 8-bit string literal and WS being the
    whitespace characters [ \t].

Semantics

    Whenever the Python compiler sees such an encoding directive
    during the compiling process, it updates an internal flag which
    holds the encoding name used for the specific literal form. The
    encoding name flags are initialized to "unicode-escape" for u"..." 
    literals and "raw-unicode-escape" for ur"..." respectively.

    ISSUE:
         Maybe we should restrict the directive usage to once per file
         and additionally to a placement before the first Unicode literal 
         in the source file.

         (Comments suggest that this approach suits the goal best.)

    If the Python compiler has to convert a Unicode literal to a
    Unicode object, it will pass the 8-bit string data given by the
    literal to the Python codec registry and have it decode the data
    using the current setting of the encoding name flag for the
    requested type of Unicode literal. It then checks the result of
    the decoding operation for being an Unicode object and stores it
    in the byte code stream.

    Since Python source code is defined to be ASCII, the Unicode literal
    encodings (both standard and raw) should be supersets of ASCII and 
    match the encoding used elsewhere in the program text, e.g. in 
    comments and maybe even 8-bit strings (even though their encoding 
    is only implicit and completely under the programmer's control).
    It is the responsability of the programmer to choose reasonable 
    encodings.

Scope

    This PEP only affects Python source code which makes use of the
    proposed directives. It does not affect the coercion handling of
    8-bit strings and Unicode in the given module.

Copyright

    This document has been placed in the public domain.



Local Variables:
mode: indented-text
indent-tabs-mode: nil
End:

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/




More information about the Python-list mailing list