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

Glenn Linderman v+python at g.nevcal.com
Thu Mar 17 19:54:35 EDT 2016

On 3/16/2016 12:59 AM, Serhiy Storchaka wrote:
> 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.

"match" doesn't imply anchoring at the start.  "re.match()" does (and as 
a result is very confusing to newbies to Python re, that have used other 
regexp systems).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20160317/8d191be1/attachment.html>

More information about the Python-Dev mailing list