Skip to content

Commit 0ab2ac2

Browse files
committed
Do not fail when receiving pathlib.Path objects as filepaths anywhere
1 parent 3a6ea0b commit 0ab2ac2

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

src/unasync/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ def isidentifier(s):
5252

5353
StringIO = io.StringIO
5454

55+
if hasattr(os, "fspath"): # PY3
56+
fspath = os.fspath
57+
else: # PY2
58+
fspath = str
59+
5560

5661
class Rule:
5762
"""A single set of rules for 'unasync'ing file(s)"""
5863

5964
def __init__(self, fromdir, todir, additional_replacements=None):
60-
self.fromdir = fromdir.replace("/", os.sep)
61-
self.todir = todir.replace("/", os.sep)
65+
self.fromdir = fspath(fromdir).replace("/", os.sep)
66+
self.todir = fspath(todir).replace("/", os.sep)
6267

6368
# Add any additional user-defined token replacements to our list.
6469
self.token_replacements = _ASYNC_TO_SYNC.copy()
@@ -69,6 +74,8 @@ def _match(self, filepath):
6974
"""Determines if a Rule matches a given filepath and if so
7075
returns a higher comparable value if the match is more specific.
7176
"""
77+
filepath = fspath(filepath)
78+
7279
file_segments = [x for x in filepath.split(os.sep) if x]
7380
from_segments = [x for x in self.fromdir.split(os.sep) if x]
7481
len_from_segments = len(from_segments)
@@ -83,6 +90,7 @@ def _match(self, filepath):
8390
return False
8491

8592
def _unasync_file(self, filepath):
93+
filepath = fspath(filepath)
8694
with open(filepath, "rb") as f:
8795
write_kwargs = {}
8896
if sys.version_info[0] >= 3: # PY3 # pragma: no branch

test-requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pytest>=4.3.0
2-
pytest-cov
2+
pytest-cov
3+
pathlib2 ; python_version < '3.5'

tests/test_unasync.py

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
import errno
33
import io
44
import os
5+
6+
try:
7+
import pathlib
8+
except ImportError:
9+
import pathlib2 as pathlib
510
import shutil
611
import subprocess
712
import sys
@@ -43,6 +48,12 @@ def test_rule_on_short_path():
4348
assert rule._match("/ahip/") is False
4449

4550

51+
def test_rule_with_pathlib_path():
52+
path_async_base = pathlib.Path("/ahip")
53+
path_sync_base = pathlib.Path("/hip")
54+
unasync.Rule(path_async_base / "tests", path_sync_base / "tests")
55+
56+
4657
@pytest.mark.parametrize("source_file", TEST_FILES)
4758
def test_unasync(tmpdir, source_file):
4859

0 commit comments

Comments
 (0)