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

windows argument expansion is failing with a regex parsing error #2195

Closed
jammer87 opened this issue Feb 17, 2022 · 1 comment · Fixed by #2201
Closed

windows argument expansion is failing with a regex parsing error #2195

jammer87 opened this issue Feb 17, 2022 · 1 comment · Fixed by #2201
Milestone

Comments

@jammer87
Copy link

I'm using pipenv to run pytest which is calling into click and attempting to expand arguments and failing.

The command I'm running in context is

pipenv run pytest tests/test_thing.py::test_something[e-a]

but it can replicated without using pipenv or pytest and produce the same exception.

Running

from click.utils import _expand_args
_expand_args(['tests/test_thing.py::test_something[e-a]'])

results in

  File "C:\Users\xxx\.virtualenvs\xxx-3g_HEZBh\lib\site-packages\click\utils.py", line 572, in _expand_args
    matches = glob(arg, recursive=glob_recursive)
  File "C:\Program Files\Python39\lib\glob.py", line 22, in glob
    return list(iglob(pathname, recursive=recursive))
  File "C:\Program Files\Python39\lib\glob.py", line 75, in _iglob
    for name in glob_in_dir(dirname, basename, dironly):
  File "C:\Program Files\Python39\lib\glob.py", line 86, in _glob1
    return fnmatch.filter(names, pattern)
  File "C:\Program Files\Python39\lib\fnmatch.py", line 58, in filter
    match = _compile_pattern(pat)
  File "C:\Program Files\Python39\lib\fnmatch.py", line 52, in _compile_pattern
    return re.compile(res).match
  File "C:\Program Files\Python39\lib\re.py", line 252, in compile
    return _compile(pattern, flags)
  File "C:\Program Files\Python39\lib\re.py", line 304, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:\Program Files\Python39\lib\sre_compile.py", line 764, in compile
    p = sre_parse.parse(p, flags)
  File "C:\Program Files\Python39\lib\sre_parse.py", line 948, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "C:\Program Files\Python39\lib\sre_parse.py", line 443, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "C:\Program Files\Python39\lib\sre_parse.py", line 834, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
  File "C:\Program Files\Python39\lib\sre_parse.py", line 443, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "C:\Program Files\Python39\lib\sre_parse.py", line 598, in _parse
    raise source.error(msg, len(this) + 1 + len(that))
re.error: bad character range e-a at position 35

Expected behavior - if expansion fails, treat it as an empty glob match. Something like this but without the bare except catch.

def _expand_args(args):
    from glob import glob

    out = []

    for arg in args:
        if user:
            arg = os.path.expanduser(arg)

        if env:
            arg = os.path.expandvars(arg)

        try:
            matches = glob(arg, recursive=glob_recursive)
        except:
            matches = []

        if not matches:
            out.append(arg)
        else:
            out.extend(matches)

    return out

Environment:

  • Python version: 3.9.7
  • Click version: 8.0.3
@davidism
Copy link
Member

davidism commented Feb 17, 2022

Related to #1901 and #2135

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants