Skip to content

Commit

Permalink
Fix compatiblity with Python 3.4 – 3.5 (which do not have `os.PathLik…
Browse files Browse the repository at this point in the history
…e` yet)
  • Loading branch information
ntninja committed Jan 27, 2019
1 parent 70c259d commit e485751
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
7 changes: 4 additions & 3 deletions ipfshttpclient/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,10 @@ def auto_close_iter_fd(fd, iter):
finally:
os.close(fd)

dirname = os.path.basename(os.path.normpath(directory))
directory_str = utils.convert_path(directory)
dirname = os.path.basename(os.path.normpath(directory_str))

fd = os.open(directory, os.O_CLOEXEC | os.O_DIRECTORY)
fd = os.open(directory_str, os.O_CLOEXEC | os.O_DIRECTORY)
body, headers = stream_directory_impl(fd, dirname)
return auto_close_iter_fd(fd, body), headers
else:
Expand Down Expand Up @@ -654,7 +655,7 @@ def stream_filesystem_node(filepaths,
"""
is_dir = False
if isinstance(filepaths, utils.path_types):
is_dir = os.path.isdir(filepaths)
is_dir = os.path.isdir(utils.convert_path(filepaths))
elif isinstance(filepaths, int):
import stat
is_dir = stat.S_ISDIR(os.fstat(filepaths).st_mode)
Expand Down
28 changes: 26 additions & 2 deletions ipfshttpclient/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,31 @@ class collections:

path_types = (six.text_type, six.binary_type)
if hasattr(os, "PathLike"): #PY36+
path_types += (os.PathLike,)
path_types += (os.PathLike,)

def convert_path(path):
# Not needed since all system APIs also accept an `os.PathLike`
return path
else:
_pathlib_types = ()
try: #PY2: doesn't have `pathlib`
import pathlib
_pathlib_types += (pathlib.PurePath,)
except ImportError:
pass
# Independently maintained forward-port of `pathlib` for Py27 and others
try:
import pathlib2
_pathlib_types += (pathlib2.PurePath,)
except ImportError:
pass
path_types += _pathlib_types

def convert_path(path):
# `pathlib`'s PathLike objects need to be treated specially and
# converted to strings when interacting with system APIs
return str(path) if isinstance(path, _pathlib_types) else path



def guess_mimetype(filename):
Expand Down Expand Up @@ -59,7 +83,7 @@ def clean_file(file):
if isinstance(file, int):
return os.fdopen(file, 'rb', closefd=False), True
elif not hasattr(file, 'read'):
return open(file, 'rb'), True
return open(convert_path(file), 'rb'), True
else:
return file, False

Expand Down
2 changes: 1 addition & 1 deletion test/functional/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_mfs_file_write_stat_read_delete(client):
assert sorted(desc["Stat"].items()) == sorted(stat.items())

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

Expand Down
4 changes: 3 additions & 1 deletion test/run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ def _contextlib_suppress(*exceptions):

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

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

0 comments on commit e485751

Please sign in to comment.