Skip to content

Unintuitive behavior with binary paths/patterns #14

@AndersBlomdell

Description

@AndersBlomdell

The following program illustrates some possible variations:

#!/usr/bin/python3

import pathspec
import os

print("A. String pattern, string path:\n    ", end="")
try:
    s = pathspec.PathSpec.from_lines('gitwildmatch', ['*.py'])
    for p in os.listdir('pathspec'):
        if s.match_file(p):
            print(p, end=" ")
    print()
except Exception as e:
    print("FAILED '%s'" % e)

print("B. String pattern, binary path:\n    ", end="")
try:
    s = pathspec.PathSpec.from_lines('gitwildmatch', ['*.py'])
    for p in os.listdir(b'pathspec'):
        if s.match_file(p):
            print(p, end=' ')
    print()
except Exception as e:
    print("FAILED '%s'" % e)

print("C. String pattern, binary path + surrogateescape:\n    ", end="")
try:
    s = pathspec.PathSpec.from_lines('gitwildmatch', ['*.py'])
    for p in os.listdir(b'pathspec'):
        if s.match_file(p.decode('utf8','surrogateescape')):
            print(p, end=' ')
    print()
except Exception as e:
    print("FAILED '%s'" % e)

print("D. Binary pattern, binary path:\n    ", end="")
s = pathspec.PathSpec.from_lines('gitwildmatch', [b'*.py'])
try:
    for p in os.listdir(b'pathspec'):
        if s.match_file(p):
            print(p, end=' ')
    print()
except Exception as e:
    print("FAILED '%s'" % e)

Gives the following result when run in the source directory:

A. String pattern, string path:
    util.py pattern.py pathspec.py __init__.py compat.py 
B. String pattern, binary path:
    FAILED 'cannot use a string pattern on a bytes-like object'
C. String pattern, binary path + surrogateescape:
    b'util.py' b'pattern.py' b'pathspec.py' b'__init__.py' b'compat.py' 
D. Binary pattern, binary path:
    

IMHO examples A-C behaves as expected, while example D does not match any files, neither does it complain on the pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions