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

Unintuitive behavior with binary paths/patterns #14

Closed
AndersBlomdell opened this issue Jun 21, 2017 · 2 comments

Comments

@AndersBlomdell
Copy link

@AndersBlomdell AndersBlomdell commented Jun 21, 2017

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.

@cpburnz

This comment has been minimized.

Copy link
Owner

@cpburnz cpburnz commented Jun 28, 2017

@AndersBlomdell I agree that cases A-C behave correctly. I would have expected case D to yield the same results as case C. I'll look into this.

@cpburnz

This comment has been minimized.

Copy link
Owner

@cpburnz cpburnz commented Jul 1, 2017

@AndersBlomdell Okay, I've fixed the issue. Your example now outputs:

A. String pattern, string path:
util.py compat.py pathspec.py __init__.py pattern.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'compat.py' b'pathspec.py' b'__init__.py' b'pattern.py' 

D. Binary pattern, binary path:
b'util.py' b'compat.py' b'pathspec.py' b'__init__.py' b'pattern.py' 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.