Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fnmatch to support escape characters #58137

Closed
fruch mannequin opened this issue Feb 3, 2012 · 5 comments
Closed

fnmatch to support escape characters #58137

fruch mannequin opened this issue Feb 3, 2012 · 5 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@fruch
Copy link
Mannequin

fruch mannequin commented Feb 3, 2012

BPO 13929
Nosy @terryjreedy, @merwok, @serhiy-storchaka
Superseder
  • bpo-8402: Add a function to escape metacharacters in glob/fnmatch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2012-10-15.12:02:42.787>
    created_at = <Date 2012-02-03.06:50:23.171>
    labels = ['type-feature', 'library']
    title = 'fnmatch to support escape characters'
    updated_at = <Date 2012-10-15.12:02:42.784>
    user = 'https://bugs.python.org/fruch'

    bugs.python.org fields:

    activity = <Date 2012-10-15.12:02:42.784>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-10-15.12:02:42.787>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2012-02-03.06:50:23.171>
    creator = 'fruch'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 13929
    keywords = []
    message_count = 5.0
    messages = ['152495', '152590', '152598', '152605', '172962']
    nosy_count = 4.0
    nosy_names = ['terry.reedy', 'eric.araujo', 'fruch', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '8402'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue13929'
    versions = ['Python 3.3']

    @fruch
    Copy link
    Mannequin Author

    fruch mannequin commented Feb 3, 2012

    fnmatch to support escape characters:
    like that:

    >>> name = "Document[Ver.2].doc"
    >>> pattern = "*\[Ver.2\]*"
    >>> fnmatch.fnmatch(name, pattern)
    True
    
    that's also fix glob module:
    >>> pattern = "ipconfig /\?"
    >>> glob.glob(pattern)
    "ipconfig /?"

    @fruch fruch mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Feb 3, 2012
    @terryjreedy
    Copy link
    Member

    The doc chapters are entitled "fnmatch — Unix filename pattern matching" and "glob — Unix style pathname pattern expansion". The first explicitly disclaims the feature you request: "Be aware there is no way to quote meta-characters.", suggests using re for anything beyond fnmatch, and shows to use .translate to make a start in doing so. For your example:

    >>> re.match(r".*\[Ver\.2\].*",  "Document[Ver.2].doc")
    <_sre.SRE_Match object at 0x000000000331AF38>

    Indeed, fnmatch works by using translate() and re.match. What you are asking for in something in between the unix language and re. If one re feature is added, why not another?

    So the scope of these modules is clearly circumscribed. I suspect their intent was to make it easy to translate unix shell scripts into Python.
    What you are asking for in something in between. If you want to pursue this, post on python-list or python-ideas to garner more support. But I anticipate rejection as not needed and contrary to intent.

    Not obvious from the doc is that an unmatch '[' or ']' is escaped:
    >>> name = "Document[Ver.2.doc"
    >>> pattern = "*[Ver.2*"
    >>> fnmatch.fnmatch(name, pattern)
    True
    >>> name = "DocumentVer.2].doc"
    >>> pattern = "*Ver.2]*"
    >>> fnmatch.fnmatch(name, pattern)
    True
    I presume this matches the *nix behavior, but don't know.

    @merwok
    Copy link
    Member

    merwok commented Feb 4, 2012

    [fnmatch] explicitly disclaims the feature you request: "Be aware there is no way to quote
    meta-characters."

    This reads like a warning to me, i.e. a potential future feature, not a design choice.

    What you are asking for in something in between the unix language and re. If one re
    feature is added, why not another?

    When we use glob patterns in our shells, the shell language lets us escape what would otherwise be special characters. Python would be nicer to let us do the same.

    @terryjreedy
    Copy link
    Member

    If indeed fnmatch does not match current shells, then I would agree that it should. It looks to me so easy to add that I though it must be a deliberate decision to exclude. In translate:
    ...
    elif c == '\':
    if i < n-1:
    c2 = pat[i+1]
    <specify what to do for \c2 for all cases of c2>
    else:
    <specify what to do for pattern ending in '\'>
    else:
    res = res + re.escape(c)
    # the last two lines are current code, which is why '\' in patterns does not escape anything in the translated re.

    Changing the meaning of '\' from ordinary character to escape char will break any code that depends on its current ordinariness.
    >>> fn.fnmatch(r'\x', r'\?')
    True # for x any 'ordinary' char, but not is '\?' means "match '?'.

    This was another reason I closed, although I forgot to mention it. I suppose a new parameter 'escape = False' could be added to all 4 exposed functions to preserve back compatibility. Anyway, I have reopened for further discussion and specification.

    @terryjreedy terryjreedy reopened this Feb 4, 2012
    @serhiy-storchaka
    Copy link
    Member

    bpo-8402 has discussion and patch(es).

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants