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

Exception with EXTMATCH on pattern like '!(not)_@(this|that)' #218

Closed
jakedane opened this issue May 15, 2024 · 3 comments · Fixed by #219
Closed

Exception with EXTMATCH on pattern like '!(not)_@(this|that)' #218

jakedane opened this issue May 15, 2024 · 3 comments · Fixed by #219
Labels
S: triage Issue needs triage.

Comments

@jakedane
Copy link

jakedane commented May 15, 2024

  • OS: Arch Linux
  • python version: 3.12.3
  • wcmatch version: 8.5.1

When using a pattern like !(not)_@(this|that) and EXTMATCH an exception missing ), unterminated subpattern at position 5 is raised. The issue happens when a !(pattern-list) is followed by a @(pattern-list).

As a trivialized example say I have the files 1_foo, 2_foo and 3_bar and I want to match all files except the 2nd, I could use a pattern !(2)_@(foo|bar). Matching the 1st file like this:

from wcmatch import fnmatch
fnmatch.fnmatch('1_foo', '!(2)_@(foo|bar)', flags = fnmatch.EXTMATCH)

Should result True but instead the fnmatch call raises this exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.12/site-packages/wcmatch/fnmatch.py", line 86, in fnmatch
    return bool(_wcparse.compile(patterns, flags, limit, exclude=exclude).match(filename))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/wcmatch/_wcparse.py", line 787, in compile
    positive, negative = compile_pattern(patterns, flags, limit, exclude)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/wcmatch/_wcparse.py", line 739, in compile_pattern
    positive.append(_compile(expanded, flags))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/site-packages/wcmatch/_wcparse.py", line 798, in _compile
    return re.compile(WcParse(pattern, flags & FLAG_MASK).parse())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/re/__init__.py", line 228, in compile
    return _compile(pattern, flags)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/re/__init__.py", line 307, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/re/_compiler.py", line 745, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/re/_parser.py", line 979, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/re/_parser.py", line 460, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/re/_parser.py", line 862, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/re/_parser.py", line 460, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/re/_parser.py", line 864, in _parse
    raise source.error("missing ), unterminated subpattern",
re.error: missing ), unterminated subpattern at position 5

In bash it works:

$ touch 1_foo 2_foo 3_bar
$ ls !(2)_@(foo|bar)
1_foo  3_bar
@gir-bot gir-bot added the S: triage Issue needs triage. label May 15, 2024
facelessuser added a commit that referenced this issue May 15, 2024
@facelessuser
Copy link
Owner

Yeah, the negate case in EXTMATCH was always a little tricky to get right. I have a fix for this though in PR #219.

>>> fnmatch.fnmatch('1_foo', '!(2)_@(foo|bar)', flags = fnmatch.EXTMATCH)
True

facelessuser added a commit that referenced this issue May 15, 2024
facelessuser added a commit that referenced this issue May 15, 2024
@facelessuser
Copy link
Owner

I've tagged a new release. It should be available shortly on PyPI.

@jakedane
Copy link
Author

Nice, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S: triage Issue needs triage.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants