Skip to content

Commit e485751

Browse files
committed
Fix compatiblity with Python 3.4 – 3.5 (which do not have os.PathLike yet)
1 parent 70c259d commit e485751

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

ipfshttpclient/multipart.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,10 @@ def auto_close_iter_fd(fd, iter):
621621
finally:
622622
os.close(fd)
623623

624-
dirname = os.path.basename(os.path.normpath(directory))
624+
directory_str = utils.convert_path(directory)
625+
dirname = os.path.basename(os.path.normpath(directory_str))
625626

626-
fd = os.open(directory, os.O_CLOEXEC | os.O_DIRECTORY)
627+
fd = os.open(directory_str, os.O_CLOEXEC | os.O_DIRECTORY)
627628
body, headers = stream_directory_impl(fd, dirname)
628629
return auto_close_iter_fd(fd, body), headers
629630
else:
@@ -654,7 +655,7 @@ def stream_filesystem_node(filepaths,
654655
"""
655656
is_dir = False
656657
if isinstance(filepaths, utils.path_types):
657-
is_dir = os.path.isdir(filepaths)
658+
is_dir = os.path.isdir(utils.convert_path(filepaths))
658659
elif isinstance(filepaths, int):
659660
import stat
660661
is_dir = stat.S_ISDIR(os.fstat(filepaths).st_mode)

ipfshttpclient/utils.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,31 @@ class collections:
1818

1919
path_types = (six.text_type, six.binary_type)
2020
if hasattr(os, "PathLike"): #PY36+
21-
path_types += (os.PathLike,)
21+
path_types += (os.PathLike,)
22+
23+
def convert_path(path):
24+
# Not needed since all system APIs also accept an `os.PathLike`
25+
return path
26+
else:
27+
_pathlib_types = ()
28+
try: #PY2: doesn't have `pathlib`
29+
import pathlib
30+
_pathlib_types += (pathlib.PurePath,)
31+
except ImportError:
32+
pass
33+
# Independently maintained forward-port of `pathlib` for Py27 and others
34+
try:
35+
import pathlib2
36+
_pathlib_types += (pathlib2.PurePath,)
37+
except ImportError:
38+
pass
39+
path_types += _pathlib_types
40+
41+
def convert_path(path):
42+
# `pathlib`'s PathLike objects need to be treated specially and
43+
# converted to strings when interacting with system APIs
44+
return str(path) if isinstance(path, _pathlib_types) else path
45+
2246

2347

2448
def guess_mimetype(filename):
@@ -59,7 +83,7 @@ def clean_file(file):
5983
if isinstance(file, int):
6084
return os.fdopen(file, 'rb', closefd=False), True
6185
elif not hasattr(file, 'read'):
62-
return open(file, 'rb'), True
86+
return open(convert_path(file), 'rb'), True
6387
else:
6488
return file, False
6589

test/functional/test_files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def test_mfs_file_write_stat_read_delete(client):
240240
assert sorted(desc["Stat"].items()) == sorted(stat.items())
241241

242242
# Read back (and compare file contents)
243-
with open(desc["Name"], "rb") as file:
243+
with open(str(desc["Name"]), "rb") as file:
244244
content = client.files.read(filepath)
245245
assert content == file.read()
246246

test/run-tests.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ def _contextlib_suppress(*exceptions):
7777

7878
#PY2: Only add `encoding` parameter on Python 3 as it's not available on Unicode-hostile versions
7979
extra_args = {}
80-
if not six.PY2:
80+
if sys.version_info >= (3, 6, 0):
8181
extra_args["encoding"] = locale.getpreferredencoding()
82+
elif not six.PY2:
83+
extra_args["universal_newlines"] = True
8284

8385
# Spawn IPFS daemon in data directory
8486
DAEMON = subprocess.Popen(["ipfs", "daemon", "--enable-pubsub-experiment"],

0 commit comments

Comments
 (0)