classification
Title: fnmatch.translate doesn't add ^ at the beginning
Type: behavior Stage: resolved
Components: Documentation, Regular Expressions Versions: Python 3.7, Python 3.6, Python 3.5, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, ezio.melotti, mrabarnett, mstol, python-dev, serhiy.storchaka
Priority: low Keywords: easy, patch

Created on 2014-11-26 14:34 by mstol, last changed 2016-10-27 19:51 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
doc_fnmatch_translate.patch serhiy.storchaka, 2016-10-16 12:52 review
Messages (8)
msg231712 - (view) Author: mstol (mstol) Date: 2014-11-26 14:34
I'm not sure if this is real bug, but the documentation of fnmatch.translate states:

fnmatch.translate(pattern)

    Return the shell-style pattern converted to a regular expression.



My intuition about shell-style pattern is that for example, pattern:t3
should match only t3, and not ost3 or xxxxxt3, but what I receive from fnmatch is:
In [2]: fnmatch.translate("t3")
Out[2]: 't3\\Z(?ms)'

so using for example re.search will match not only on t3, but also on xxxt3 (in shell-like pattern is *t3). So... I  believe it should be changed or at least the documentation should be more specific about what "shell-style pattern" mean.
msg231713 - (view) Author: mstol (mstol) Date: 2014-11-26 14:38
it can be changed easyly with changing the return statement in 
fnmatch.py function translate 

from:
    return res + '\Z(?ms)'

to:
    return '^' + res + '\Z(?ms)'
msg231715 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-26 15:34
The result of fnmatch.translate() is used with re.match(). It could be even simplified if use it with re.fullmatch(), but I afraid such change can break user code.
msg231716 - (view) Author: mstol (mstol) Date: 2014-11-26 15:49
So you suggest that this output should not be used with re.search ? Why?

I think it should be documented, or maybe it is?

I know that this is not possible to introduce such a change to the currently used versions.
msg231718 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-26 15:54
I think it should be documented.
msg231722 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2014-11-26 17:59
I notice that it puts the inline flags at the end. It would be better to put them at the start.
msg278753 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-10-16 08:52
Since issue22493 fnmatch.translate() uses scoped modifiers: r'(?s:%s)\Z' % res.

Here is a patch that updates the documentation for fnmatch.translate().
msg279566 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-10-27 19:50
New changeset a1aef5f84142 by Serhiy Storchaka in branch '3.5':
Issue #22949: Documented that fnmatch.translate() is for use with re.match().
https://hg.python.org/cpython/rev/a1aef5f84142

New changeset 8a564ab1d208 by Serhiy Storchaka in branch '2.7':
Issue #22949: Documented that fnmatch.translate() is for use with re.match().
https://hg.python.org/cpython/rev/8a564ab1d208

New changeset dfda2f33fd08 by Serhiy Storchaka in branch '3.6':
Issue #22949: Documented that fnmatch.translate() is for use with re.match().
https://hg.python.org/cpython/rev/dfda2f33fd08

New changeset d103ee917342 by Serhiy Storchaka in branch 'default':
Issue #22949: Documented that fnmatch.translate() is for use with re.match().
https://hg.python.org/cpython/rev/d103ee917342
History
Date User Action Args
2016-10-27 19:51:16serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2016-10-27 19:50:50python-devsetnosy: + python-dev
messages: + msg279566
2016-10-16 12:52:35serhiy.storchakasetfiles: + doc_fnmatch_translate.patch
keywords: + patch
2016-10-16 08:52:58serhiy.storchakasetstage: needs patch -> patch review
messages: + msg278753
versions: + Python 3.6, Python 3.7, - Python 3.4
2014-11-26 17:59:38mrabarnettsetmessages: + msg231722
2014-11-26 15:54:15serhiy.storchakasetpriority: normal -> low

assignee: docs@python
components: + Documentation
versions: + Python 3.4, Python 3.5
keywords: + easy
nosy: + docs@python

messages: + msg231718
stage: needs patch
2014-11-26 15:49:51mstolsetmessages: + msg231716
2014-11-26 15:34:22serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg231715
2014-11-26 14:38:02mstolsetmessages: + msg231713
2014-11-26 14:34:48mstolcreate