[Python-Dev] What does a double coding cookie mean?

Serhiy Storchaka storchaka at gmail.com
Wed Mar 16 03:59:55 EDT 2016

On 16.03.16 09:46, Glenn Linderman wrote:
> On 3/16/2016 12:09 AM, Serhiy Storchaka wrote:
>> On 16.03.16 08:34, Glenn Linderman wrote:
>>>  From the PEP 263:
>>>> More precisely, the first or second line must match the regular
>>>>     expression "coding[:=]\s*([-\w.]+)". The first group of this
>>>>     expression is then interpreted as encoding name. If the encoding
>>>>     is unknown to Python, an error is raised during compilation. There
>>>>     must not be any Python statement on the line that contains the
>>>>     encoding declaration.
>>> Clearly the regular expression would only match the first of multiple
>>> cookies on the same line, so the first one should always win... but
>>> there should only be one, from the first PEP quote "a magic comment".
>> "The first group of this expression" means the first regular
>> expression group. Only the part between parenthesis "([-\w.]+)" is
>> interpreted as encoding name, not all expression.
> Sure.  But there is no mention anywhere in the PEP of more than one
> being legal: just more than one position for it, EITHER line 1 or line
> 2. So while the regular expression mentioned is not anchored, to allow
> variation in syntax between emacs and vim, "must match the regular
> expression" doesn't imply "several times", and when searching for a
> regular expression that might not be anchored, one typically expects to
> find the first.

Actually "must match the regular expression" is not correct, because 
re.match() implies anchoring at the start. I have proposed more correct 
regular expression in other branch of this thread.

More information about the Python-Dev mailing list