PEP: Defining Python Source Code Encodings

Bengt Richter bokr at accessone.com
Tue Jul 17 15:56:16 EDT 2001


On Tue, 17 Jul 2001 12:31:24 -0600 (MDT), Bruce Sass <bsass at freenet.edmonton.ab.ca> wrote:

>On Tue, 17 Jul 2001, M.-A. Lemburg wrote:
><...>
>> - which format to use for the magic comment, e.g.
>>
>>   * Emacs style:
>>
>>       #!/usr/bin/python
>>       # -*- encoding = 'utf-8' -*-
>
>This should work for everyone, but will it confuse emacs?.
>I suppose, "# # ...", or "### ...", or almost any short sequence
>starting with "#" will work, eh.
>
>>   * Via meta-option to the interpreter:
>>
>>       #!/usr/bin/python --encoding=utf-8
>
>This will require editing if python is not in /usr/bin, and can not be
>used to pass more than one argument to the command (python, in this
>case).
>
>>   * Using a special comment format:
>>
>>       #!/usr/bin/python
>>       #!encoding = 'utf-8'
>
>This is confusing, and will only work on *nix (linux?) iff it is the
>second (or later) line; if it is the first line... it will fail
>because there is probably no executable named "encoding" available,
>and if there is, "= 'utf8'" is unlikely to exist.
>
>please,
>Avoid character sequences that have other meanings in this context.
>
A valid point.

>
>I think this should be done as a generic method for pre-processing
>Python source before the compiler/interpreter has a look at it.
>
>e.g.,
>
>	# ## encoding utf-8
>
>triggers whatever you encoding fans want,
>
>	# ## format noweb
>
>runs the source through a filter which can extract code noweb marked
>up code, and maybe even installs the weaved docs and tangled code
>(via distutils?)
>
>	# ## MySpecialMarkup
>
>runs the source through a filter named MySpecialMarkup.
>MySpecialMarkup could be anything: extensions to docstrings, a
>proprietary binary format, an entire package-in-a-file!
>
>Generally:  #<magic> <directive> [<arguments>]
>
>If Python does not know what the <directive> is it should either look
>in a set location for a program of the same name then use its output as
>the source, or look into a table that maps <directive> to a procedure
>which results in Python source.

I think you're right about #! triggering confusion, but I think a fixed keyword
as a prefix is helpful when e.g., grepping for files with directives.

I withdraw my #!pragma (or #!pythma ;-) suggestion and propose
r'^\s*#pragma\s' as the magic line prefix regular expression.




More information about the Python-list mailing list