Issue 6665: fnmatch fails on filenames containing \n character (original) (raw)
Hello, at the moment, fnmatch.fnmatch() will fail to match any string, which has \n character. This of course breaks glob as well.
Example
import fnmatch fnmatch.fnmatch("foo\nbar", "foo*") False
import glob open("foobar", "w").close() open("foo\nbar", "w").close() glob.glob("foo*") ['foobar']
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) printf("match!\n"); else printf("fail!\n"); 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.