This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: fnmatch fails on filenames containing \n character
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.0, Python 2.4, Python 3.1, Python 2.6, Python 2.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gregory.p.smith Nosy List: gregory.p.smith, rajcze
Priority: normal Keywords:

Created on 2009-08-07 09:49 by rajcze, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg91398 - (view) Author: Josef Skladanka (rajcze) Date: 2009-08-07 09:49
at the moment, fnmatch.fnmatch() will fail to match any string, which
has \n character. This of course breaks glob as well.


> import fnmatch
> fnmatch.fnmatch("foo\nbar", "foo*")

> import glob
> open("foobar", "w").close()
> open("foo\nbar", "w").close()
> glob.glob("foo*")

while the expected result is ['foobar', 'foo\nbar']. The standard C
fnmatch function from fnmatch.h is behaving correctly i.e. this code
will print out "match!"

#include <fnmatch.h>
#include <stdio.h>

int main()
    if (fnmatch("foo*", "foo\nbar", FNM_NOESCAPE) == 0)
    return 0;

This misbehaviour is caused by the fnmatch.translate() which adds $ to
the end of the regexp. Without the ending $ the fnmatch function works OK.
msg91638 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2009-08-16 18:31
haha wow I just read the fnmatch code... trunk r2734 | guido | 1992-01-12 

added fnmatch._cache for it to cache compiled regular expressions.  That
has -long- since become unnecessary as the re module does that itself. ;)

I'll clean this up while fixing this bug.
msg91639 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2009-08-16 18:33
aww, i guess the _cache does cache the result of the glob -> regular
expression translation.  it needs to stay for that.
msg91640 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2009-08-16 18:48
changing the '$' to \Z(?ms)' fixes the problem.
msg91642 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2009-08-16 18:58
fixed in trunk r74475, py3k r74476
msg91643 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2009-08-16 19:00
I'll backport this to 2.6 and 3.1 later (its too late for 3.1.1).
msg94804 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2009-11-01 20:37
release26-maint r76023 to appear in Python 2.6.5.
release31-maint r76024 to appear in Python 3.1.2.
Date User Action Args
2022-04-11 14:56:51adminsetgithub: 50914
2009-11-01 20:37:14gregory.p.smithsetstatus: open -> closed
resolution: fixed
messages: + msg94804
2009-08-16 19:00:52gregory.p.smithsetmessages: + msg91643
versions: - Python 2.7, Python 3.2
2009-08-16 18:59:00gregory.p.smithsetmessages: + msg91642
2009-08-16 18:48:32gregory.p.smithsetmessages: + msg91640
2009-08-16 18:33:59gregory.p.smithsetmessages: + msg91639
2009-08-16 18:31:36gregory.p.smithsetpriority: normal

messages: + msg91638
2009-08-16 18:12:16gregory.p.smithsetassignee: gregory.p.smith

nosy: + gregory.p.smith
2009-08-07 09:49:17rajczecreate