[issue35314] fnmatch failed with leading caret (^)

Cyker Way report at bugs.python.org
Mon Nov 26 18:30:16 EST 2018


Cyker Way <cykerway at gmail.com> added the comment:

Thank you for confirmation. Knowing it is not fully POSIX-compliant helps with understanding.

I'm asking this because I had interoperability issues writing python scripts providing shell-like utilities for filename expansion and the result may surprise users. The glibc fnmatch provides a flag named `FNM_PATHNAME`, which is missing in the python fnmatch implementation. So I think there is currently no way to tell the python library if we are matching a filename or not.

All right so this is not a bug, but probably a good enhancement.

## TLDR

This is what POSIX says **for filename expansion**, in section 2.13.3:

<http://pubs.opengroup.org/onlinepubs/9699919799.2008edition/>

>   when pattern matching notation is used for filename expansion:
>
>   1.  The <slash> character in a pathname shall be explicitly matched by using one or more <slash> characters in the pattern; it shall neither be matched by the <asterisk> or <question-mark> special characters nor by a bracket expression. <slash> characters in the pattern shall be identified before bracket expressions; thus, a <slash> cannot be included in a pattern bracket expression used for filename expansion. If a <slash> character is found following an unescaped <left-square-bracket> character before a corresponding <right-square-bracket> is found, the open bracket shall be treated as an ordinary character. For example, the pattern "a[b/c]d" does not match such pathnames as abd or a/d. It only matches a pathname of literally a[b/c]d.

Currently python fnmatch.fnmatch gives:

    >>> fnmatch('abd', 'a[b/c]d')
    True
    >>> fnmatch('a/d', 'a[b/c]d')
    True
    >>> fnmatch('a[b/c]d', 'a[b/c]d')
    False

Ideally we can call `fnmatch('a/d', 'a[b/c]d', fnm_pathname=True)` to correct the behavior.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35314>
_______________________________________


More information about the Python-bugs-list mailing list